/* vi: set sw=4 ts=4: */
/*
 * KON2 - Kanji ON Console -
 * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
 *
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

#ifndef __CCE_CONVERT_H__
#define __CCE_CONVERT_H__

#define CONVTABLE_USE_MMAP

#define	CODE_2022		0	/* 2022 のみに従う*/
#define	CODE_EUC		1	/* EUC にも従う */
#define	CODE_SJIS		2	/* SJIS にも従う */
#define CODE_BIG5   	3
#define CODE_GB     	4
#define CODE_GBK		5
#define CODE_KSC		6
#define CODE_UHC		7  /* Rui He Aug 11, 2003 added  Microsoft developed Unified Hangul Code */
#define CODE_UNICODE 	8  /* Rui He Aug 11, 2003 added */
#define CODE_NUM     	9  

#define	sjistojis(ch, cl)\
{\
    ch -= (ch > 0x9F) ? 0xB1: 0x71;\
    ch = ch * 2 + 1;\
    if (cl > 0x9E) {\
	cl = cl - 0x7E;\
	ch ++;\
    } else {\
	if (cl > 0x7E) cl --;\
	cl -= 0x1F;\
    }\
	ch |= 0x80; cl |= 0x80; \
}

#define	jistosjis(ch, cl)\
{\
	ch &= 0x7F; cl &= 0x7F; \
    if (ch & 1) cl = cl + (cl > 0x5F ? 0x20:0x1F);\
    else cl += 0x7E;\
    ch = ((ch - 0x21) >> 1) + 0x81;\
    if (ch > 0x9F) ch += 0x40;\
}

#define muletobig5(type, m1, m2)\
{\
    unsigned code = (m1 - 0x21) * 94 + (m2 - 0x21);\
\
    if (type == DF_BIG5_1) code += 0x16F0;\
    m1 = code / 157 + 0xA1;\
    m2 = code % 157;\
    m2 += m2 < 0x3F ? 64 : 98;\
}

static inline bool iskanji_byte1(u_char sysCoding, u_char c)
{
	if (c == 0xFF) return 0; /* no encoding allow 0xFF */
    switch(sysCoding) 
    {
    case CODE_SJIS:
        return (c >=0x81 && c<=0x9F) || (c >=0xE0 && c <= 0xEF /* 0xFC */);  
	case CODE_EUC:  /* EUC_JP  CHAR_SS2 0x8E */
		return (c == 0x8E || (c >= 0xA1)); /*  && c <= 0xFE)); */
    case CODE_GB:
	case CODE_KSC:  /* KSC */
        return (c >= 0xA1); /* && c <= 0xFE); */
	case CODE_UHC: /* UHC 0x81-0xFE */
		return (c >= 0x81);
	case CODE_GBK:
		return (c >= 0x81); /* && c <= 0xFE); */
    case CODE_BIG5:
       return (c >=0xa1); /*  && c<=0xfe);  //first byte */
	case CODE_UNICODE:
		return 1; /* always ? */
    default:
        return (c & 0x80);
    }
}

static inline bool iskanji_byte2(u_char sysCoding, u_char c)
{
	if (c == 0x7F || c == 0xFF) return 0; /* no encoding allow 0x7F or 0xFF */
    switch(sysCoding) 
   {
    case CODE_SJIS: /* byte2 0x40-0xFC, not 0x7F */
		return (c >= 0x40); /* && c <= 0xFE); */
	case CODE_EUC:
		return (c >= 0xA0); /* && c <= 0xFE); // A1-FE, 0x8EA0-0x8EDF */
    case CODE_GB:
	case CODE_KSC:
        return (c >= 0xA1); /* && c <= 0xFE); */
	case CODE_UHC: /* 41-5A/61-7A/81-FE */
		return ( c >= 0x81 || (c >= 0x61 && c <= 0x7A) || (c >= 0x41 && c <= 0x5A));
	case CODE_GBK:
		return (c >= 0x40); /* && c <= 0xFE); */
    case CODE_BIG5:
       return ((c >=0x40 && c<=0x7E) || (c >=0xA1)); /*  && c<=0xFE)); // second byte */
	case CODE_UNICODE:	
		return 1;  /* always 1? */
    default:
        return (c & 0x80);
    }
}

static inline bool iskanji(u_char sysCoding, u_char c1, u_char c2)
{
	return iskanji_byte1(sysCoding, c1) && iskanji_byte2(sysCoding, c2);
}
	
extern int CalculateCharIndex(int coding, unsigned int High, unsigned int Low);
extern int ConvertIndexToChar(int coding, int index, u_char buf[]);
extern int LastCodePoint[CODE_NUM];
extern int GetEncodingSpace(int coding);

#define CONV_TABLE_FILE	"cceconv.tab"

typedef struct
{
	int size; /* total file size(mmap size) */
	int ConvertTableOffset[CODE_NUM][CODE_NUM];
	int ConvertTableSize[CODE_NUM][CODE_NUM];
}ConvTableHeader;

#ifdef CONVTABLE_USE_MMAP

int MmapConvertTable(char *filename);
int MunmapConvertTable(void);

extern unsigned short  *ConvertTable[CODE_NUM][CODE_NUM];
extern unsigned int ConvertTableSize[CODE_NUM][CODE_NUM];

#endif

extern int ShiftJIS2JIS(unsigned int ch, unsigned int cl, unsigned char *OutCh, unsigned char *OutCl);
extern int JIS2ShiftJIS(unsigned int ch, unsigned int cl, unsigned char *OutCh, unsigned char *OutCl);
extern int ConvertEncoding_Char(int inCoding, int outCoding, unsigned char High, unsigned char Low, 
		unsigned char *OutHigh, unsigned char *OutLow);

#endif  /* __CCE_CONVERT_H__ */

