| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 1998-2002 Pham Kim Long <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef __CHARSET_CONVERT_H |
| 8 |
#define __CHARSET_CONVERT_H |
| 9 |
|
| 10 |
#if !defined(_WIN32) |
| 11 |
#include <stdint.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#if defined(_WIN32) |
| 15 |
#if defined(UNIKEYHOOK) |
| 16 |
#define DllInterface __declspec(dllexport) |
| 17 |
#else |
| 18 |
#define DllInterface __declspec(dllimport) |
| 19 |
#endif |
| 20 |
#else |
| 21 |
#define DllInterface // not used |
| 22 |
#define DllExport |
| 23 |
#define DllImport |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "byteio.h" |
| 27 |
#include "pattern.h" |
| 28 |
#include "vnconv.h" |
| 29 |
|
| 30 |
#define TOTAL_VNCHARS 213 |
| 31 |
#define TOTAL_ALPHA_VNCHARS 186 |
| 32 |
|
| 33 |
#if defined(_WIN32) |
| 34 |
typedef unsigned __int32 StdVnChar; |
| 35 |
typedef unsigned __int16 UnicodeChar; |
| 36 |
typedef unsigned __int16 UKWORD; |
| 37 |
typedef unsigned __int32 UKDWORD; |
| 38 |
#else |
| 39 |
// typedef unsigned int StdVnChar; //the size should be more specific |
| 40 |
typedef uint32_t StdVnChar; |
| 41 |
typedef uint16_t UnicodeChar; |
| 42 |
typedef uint16_t UKWORD; |
| 43 |
typedef uint32_t UKDWORD; |
| 44 |
#endif |
| 45 |
|
| 46 |
// typedef unsigned short UnicodeChar; |
| 47 |
// typedef unsigned short UKWORD; |
| 48 |
|
| 49 |
// typedef unsigned int UKDWORD; //the size should be more specific |
| 50 |
|
| 51 |
#ifndef LOWORD |
| 52 |
#define LOWORD(l) ((UKWORD)(l)) |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef HIWORD |
| 56 |
#define HIWORD(l) ((UKWORD)(((UKDWORD)(l) >> 16) & 0xFFFF)) |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef MAKEWORD |
| 60 |
#define MAKEWORD(a, b) ((UKWORD)(((UKBYTE)(a)) | ((UKWORD)((UKBYTE)(b))) << 8)) |
| 61 |
#endif |
| 62 |
|
| 63 |
const StdVnChar VnStdCharOffset = 0x10000; |
| 64 |
const StdVnChar INVALID_STD_CHAR = 0xFFFFFFFF; |
| 65 |
// const unsigned char PadChar = '?'; //? is used for VIQR charset |
| 66 |
const unsigned char PadChar = '#'; |
| 67 |
const unsigned char PadStartQuote = '\"'; |
| 68 |
const unsigned char PadEndQuote = '\"'; |
| 69 |
const unsigned char PadEllipsis = '.'; |
| 70 |
|
| 71 |
class DllInterface VnCharset { |
| 72 |
public: |
| 73 |
virtual void startInput() {} |
| 74 |
virtual void startOutput() {} |
| 75 |
// virtual UKBYTE *nextInput(UKBYTE *input, int inLen, StdVnChar & stdChar, |
| 76 |
// int & bytesRead) = 0; |
| 77 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, |
| 78 |
int &bytesRead) = 0; |
| 79 |
|
| 80 |
//------------------------------------------------------------------------ |
| 81 |
// put a character to the output after converting it |
| 82 |
// Arguments: |
| 83 |
// output[in]: output buffer |
| 84 |
// stdChar[in]: character in standard charset |
| 85 |
// outLen[out]: length of converted sequence |
| 86 |
// maxAvail[in]: max length available. |
| 87 |
// Returns: next position in output |
| 88 |
//------------------------------------------------------------------------ |
| 89 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen) = 0; |
| 90 |
virtual int elementSize(); |
| 91 |
virtual ~VnCharset() {} |
| 92 |
}; |
| 93 |
|
| 94 |
//-------------------------------------------------- |
| 95 |
class SingleByteCharset : public VnCharset { |
| 96 |
protected: |
| 97 |
UKWORD m_stdMap[256]; |
| 98 |
unsigned char *m_vnChars; |
| 99 |
|
| 100 |
public: |
| 101 |
SingleByteCharset(unsigned char *vnChars); |
| 102 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 103 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 104 |
}; |
| 105 |
|
| 106 |
//-------------------------------------------------- |
| 107 |
class VnInternalCharset : public VnCharset { |
| 108 |
public: |
| 109 |
VnInternalCharset() {} |
| 110 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 111 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 112 |
virtual int elementSize(); |
| 113 |
}; |
| 114 |
|
| 115 |
//-------------------------------------------------- |
| 116 |
class UnicodeCharset : public VnCharset { |
| 117 |
protected: |
| 118 |
UKDWORD m_vnChars[TOTAL_VNCHARS]; |
| 119 |
UnicodeChar *m_toUnicode; |
| 120 |
|
| 121 |
public: |
| 122 |
UnicodeCharset(UnicodeChar *vnChars); |
| 123 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 124 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 125 |
virtual int elementSize(); |
| 126 |
}; |
| 127 |
|
| 128 |
//-------------------------------------------------- |
| 129 |
class DoubleByteCharset : public VnCharset { |
| 130 |
protected: |
| 131 |
UKWORD m_stdMap[256]; |
| 132 |
UKDWORD m_vnChars[TOTAL_VNCHARS]; |
| 133 |
UKWORD *m_toDoubleChar; |
| 134 |
|
| 135 |
public: |
| 136 |
DoubleByteCharset(UKWORD *vnChars); |
| 137 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 138 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 139 |
}; |
| 140 |
|
| 141 |
//-------------------------------------------------- |
| 142 |
class UnicodeUTF8Charset : public UnicodeCharset { |
| 143 |
public: |
| 144 |
UnicodeUTF8Charset(UnicodeChar *vnChars) : UnicodeCharset(vnChars) {} |
| 145 |
|
| 146 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 147 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 148 |
}; |
| 149 |
|
| 150 |
//-------------------------------------------------- |
| 151 |
class UnicodeRefCharset : public UnicodeCharset { |
| 152 |
public: |
| 153 |
UnicodeRefCharset(UnicodeChar *vnChars) : UnicodeCharset(vnChars) {} |
| 154 |
|
| 155 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 156 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 157 |
}; |
| 158 |
|
| 159 |
//-------------------------------------------------- |
| 160 |
class UnicodeHexCharset : public UnicodeRefCharset { |
| 161 |
public: |
| 162 |
UnicodeHexCharset(UnicodeChar *vnChars) : UnicodeRefCharset(vnChars) {} |
| 163 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 164 |
}; |
| 165 |
|
| 166 |
//-------------------------------------------------- |
| 167 |
class UnicodeCStringCharset : public UnicodeCharset { |
| 168 |
protected: |
| 169 |
int m_prevIsHex; |
| 170 |
|
| 171 |
public: |
| 172 |
UnicodeCStringCharset(UnicodeChar *vnChars) : UnicodeCharset(vnChars) {} |
| 173 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 174 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 175 |
virtual void startInput(); |
| 176 |
}; |
| 177 |
|
| 178 |
//-------------------------------------------------- |
| 179 |
class WinCP1258Charset : public VnCharset { |
| 180 |
protected: |
| 181 |
UKWORD m_stdMap[256]; |
| 182 |
UKDWORD m_vnChars[TOTAL_VNCHARS * 2]; |
| 183 |
UKWORD *m_toDoubleChar; |
| 184 |
int m_totalChars; |
| 185 |
|
| 186 |
public: |
| 187 |
WinCP1258Charset(UKWORD *compositeChars, UKWORD *precomposedChars); |
| 188 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 189 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 190 |
}; |
| 191 |
|
| 192 |
//-------------------------------------------------- |
| 193 |
struct UniCompCharInfo { |
| 194 |
UKDWORD compChar; |
| 195 |
int stdIndex; |
| 196 |
}; |
| 197 |
|
| 198 |
class UnicodeCompCharset : public VnCharset { |
| 199 |
protected: |
| 200 |
UniCompCharInfo m_info[TOTAL_VNCHARS * 2]; |
| 201 |
UKDWORD *m_uniCompChars; |
| 202 |
int m_totalChars; |
| 203 |
|
| 204 |
public: |
| 205 |
UnicodeCompCharset(UnicodeChar *uniChars, UKDWORD *uniCompChars); |
| 206 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 207 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 208 |
virtual int elementSize(); |
| 209 |
}; |
| 210 |
|
| 211 |
//-------------------------------------------------- |
| 212 |
class VIQRCharset : public VnCharset { |
| 213 |
protected: |
| 214 |
UKDWORD *m_vnChars; |
| 215 |
UKWORD m_stdMap[256]; |
| 216 |
int m_atWordBeginning; |
| 217 |
int m_escapeBowl; |
| 218 |
int m_escapeRoof; |
| 219 |
int m_escapeHook; |
| 220 |
int m_escapeTone; |
| 221 |
int m_gotTone; |
| 222 |
int m_escAll; |
| 223 |
int m_noOutEsc; |
| 224 |
|
| 225 |
public: |
| 226 |
int m_suspicious; |
| 227 |
VIQRCharset(UKDWORD *vnChars); |
| 228 |
virtual void startInput(); |
| 229 |
virtual void startOutput(); |
| 230 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 231 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 232 |
}; |
| 233 |
|
| 234 |
//-------------------------------------------------- |
| 235 |
class UTF8VIQRCharset : public VnCharset { |
| 236 |
|
| 237 |
protected: |
| 238 |
VIQRCharset *m_pViqr; |
| 239 |
UnicodeUTF8Charset *m_pUtf; |
| 240 |
|
| 241 |
public: |
| 242 |
UTF8VIQRCharset(UnicodeUTF8Charset *pUtf, VIQRCharset *pViqr); |
| 243 |
virtual void startInput(); |
| 244 |
virtual void startOutput(); |
| 245 |
virtual int nextInput(ByteInStream &is, StdVnChar &stdChar, int &bytesRead); |
| 246 |
virtual int putChar(ByteOutStream &os, StdVnChar stdChar, int &outLen); |
| 247 |
}; |
| 248 |
|
| 249 |
//-------------------------------------------------- |
| 250 |
class DllInterface CVnCharsetLib { |
| 251 |
protected: |
| 252 |
SingleByteCharset *m_sgCharsets[CONV_TOTAL_SINGLE_CHARSETS]; |
| 253 |
DoubleByteCharset *m_dbCharsets[CONV_TOTAL_DOUBLE_CHARSETS]; |
| 254 |
UnicodeCharset *m_pUniCharset; |
| 255 |
UnicodeCompCharset *m_pUniCompCharset; |
| 256 |
UnicodeUTF8Charset *m_pUniUTF8; |
| 257 |
UnicodeRefCharset *m_pUniRef; |
| 258 |
UnicodeHexCharset *m_pUniHex; |
| 259 |
VIQRCharset *m_pVIQRCharObj; |
| 260 |
UTF8VIQRCharset *m_pUVIQRCharObj; |
| 261 |
WinCP1258Charset *m_pWinCP1258; |
| 262 |
UnicodeCStringCharset *m_pUniCString; |
| 263 |
VnInternalCharset *m_pVnIntCharset; |
| 264 |
|
| 265 |
public: |
| 266 |
PatternList m_VIQREscPatterns, m_VIQROutEscPatterns; |
| 267 |
VnConvOptions m_options; |
| 268 |
CVnCharsetLib(); |
| 269 |
~CVnCharsetLib(); |
| 270 |
VnCharset *getVnCharset(int charsetIdx); |
| 271 |
}; |
| 272 |
|
| 273 |
extern unsigned char SingleByteTables[][TOTAL_VNCHARS]; |
| 274 |
extern UKWORD DoubleByteTables[][TOTAL_VNCHARS]; |
| 275 |
extern UnicodeChar UnicodeTable[TOTAL_VNCHARS]; |
| 276 |
extern UKDWORD VIQRTable[TOTAL_VNCHARS]; |
| 277 |
extern UKDWORD UnicodeComposite[TOTAL_VNCHARS]; |
| 278 |
extern UKWORD WinCP1258[TOTAL_VNCHARS]; |
| 279 |
extern UKWORD WinCP1258Pre[TOTAL_VNCHARS]; |
| 280 |
|
| 281 |
extern DllInterface CVnCharsetLib VnCharsetLibObj; |
| 282 |
extern VnConvOptions VnConvGlobalOptions; |
| 283 |
extern int StdVnNoTone[TOTAL_VNCHARS]; |
| 284 |
extern int StdVnRootChar[TOTAL_VNCHARS]; |
| 285 |
|
| 286 |
DllInterface int genConvert(VnCharset &incs, VnCharset &outcs, |
| 287 |
ByteInStream &input, ByteOutStream &output); |
| 288 |
|
| 289 |
StdVnChar StdVnToUpper(StdVnChar ch); |
| 290 |
StdVnChar StdVnToLower(StdVnChar ch); |
| 291 |
StdVnChar StdVnGetRoot(StdVnChar ch); |
| 292 |
|
| 293 |
#endif |