1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46:
47: 48: 49: 50:
51:
52: #ifndef __FIMP_FAT_ENC_H__
53: #define __FIMP_FAT_ENC_H__
54:
55: #include <t2ex/fs.h>
56: #include <tk/tkernel.h>
57: #include <string.h>
58: #include <ctype.h>
59:
60: 61: 62:
63: #define fat_strncpy(dst,src,n) (UB*)strncpy((B*)(dst),(const B*)(src),(size_t)(n))
64: IMPORT W fat_strncasecmp(const UB *str1, const UB *str2, UW n);
65: IMPORT W fat_strlen16(const UH *utf16str);
66:
67: 68: 69:
70: #if BIGENDIAN
71: #define CEH(x) swapH(x)
72: #define CEW(x) swapW(x)
73: #else
74: #define CEH(x) (x)
75: #define CEW(x) (x)
76: #endif
77:
78: Inline UH swapH(UH x)
79: {
80: return (x << 8) | (x >> 8);
81: }
82: Inline UW swapW(UW x)
83: {
84: return (UW)swapH((UH)x) << 16 | swapH((UH)(x >> 16));
85: }
86:
87: 88: 89:
90: Inline UH GetMisalignH(UB d[2])
91: {
92: #if ALLOW_MISALIGN
93: return *(UH *)d;
94: #else
95: #if BIGENDIAN
96: return ((UH)d[0] << 8) | (UH)d[1];
97: #else
98: return ((UH)d[1] << 8) | (UH)d[0];
99: #endif
100: #endif
101: }
102:
103: Inline void SetMisalignH(UB d[2], UH x)
104: {
105: #if ALLOW_MISALIGN
106: *(UH *)d = x;
107: #else
108: #if BIGENDIAN
109: d[0] = x >> 8;
110: d[1] = x;
111: #else
112: d[1] = x >> 8;
113: d[0] = x;
114: #endif
115: #endif
116: }
117:
118: Inline UW GetMisalignW(UB d[4])
119: {
120: #if ALLOW_MISALIGN
121: return *(UW *)d;
122: #else
123: #if BIGENDIAN
124: return ((UW)d[0] << 24) | ((UW)d[1] << 16) | \
125: ((UW)d[2] << 8) | (UW) d[3];
126: #else
127: return ((UW)d[3] << 24) | ((UW)d[2] << 16) | \
128: ((UW)d[1] << 8) | (UW) d[0];
129: #endif
130: #endif
131: }
132:
133: Inline void SetMisalignW(UB d[4], UW x)
134: {
135: #if ALLOW_MISALIGN
136: *(UW *)d = x;
137: #else
138: #if BIGENDIAN
139: d[0] = x >> 24;
140: d[1] = x >> 16;
141: d[2] = x >> 8;
142: d[3] = x;
143: #else
144: d[3] = x >> 24;
145: d[2] = x >> 16;
146: d[1] = x >> 8;
147: d[0] = x;
148: #endif
149: #endif
150: }
151:
152: 153: 154:
155: #define UTF8_MAX (6)
156: #define ENC_CHAR_MAX (4)
157:
158: #define ASCII_MAX (0x7f)
159:
160:
161: IMPORT const UB fatEncAsciiKindTable[ASCII_MAX + 1];
162:
163: #define AsciiToUP(ascii) ((UB)toupper((ascii) & ASCII_MAX))
164: #define AsciiToLOW(ascii) ((UB)tolower((ascii) & ASCII_MAX))
165:
166: #define getAsciiKind(ascii) (fatEncAsciiKindTable[(ascii)] & 0xf)
167: #define ASCII_LOW (0)
168: #define ASCII_UP (1)
169: #define ASCII_NUM (2)
170: #define ASCII_SYM (3)
171: #define ASCII_CON (4)
172: #define ASCII_KIND_MAX (5)
173:
174: #define AsciiIsLOW(ascii) (getAsciiKind(ascii) == ASCII_LOW)
175: #define AsciiIsUP(ascii) (getAsciiKind(ascii) == ASCII_UP)
176: #define AsciiIsNUM(ascii) (getAsciiKind(ascii) == ASCII_NUM)
177: #define AsciiIsSYM(ascii) (getAsciiKind(ascii) == ASCII_SYM)
178:
179: #define AsciiIsSFN(ascii) ((fatEncAsciiKindTable[(ascii)] & 0x10) == 0x10)
180: #define AsciiIsLFN(ascii) ((fatEncAsciiKindTable[(ascii)] & 0x20) == 0x20)
181:
182:
183: #define CharIsAscii(c) ((0 < (c)) && ((c) <= ASCII_MAX))
184:
185: #define Utf8IsAscii(utf8) ((0 < (utf8)) && ((utf8) <= ASCII_MAX))
186:
187: #define Utf16IsUpSurrogate(utf16) \
188: ((0xD800 <= (utf16)) && ((utf16) <= 0xDBFF))
189: #define Utf16IsLowSurrogate(utf16) \
190: ((0xDC00 <= (utf16)) && ((utf16) <= 0xDFFF))
191: #define Utf16ToUnicode(up, low) \
192: (UW)((((up) - 0xd800) * 0x400) + ((low) - 0xdc00))
193:
194: #define UnicodeIsAscii(unicode) \
195: ((0 < (unicode)) && ((unicode) <= ASCII_MAX))
196: #define UnicodeIsNormal(unicode) \
197: ((0 < (unicode)) && ((unicode) < 0x10000))
198: #define UnicodeIsSurrogate(unicode) \
199: ((0x10000 <= (unicode)) && ((unicode) < 0x20000))
200: #define UnicodeIsInvalid(unicode) \
201: (((unicode) <= 0) || (0x20000 <= (unicode)))
202:
203: #define UnicodeToUtf16UpSurrogate(unicode) \
204: (UH)(((unicode - 0x10000) / 0x400) + 0xd800)
205: #define UnicodeToUtf16LowSurrogate(unicode) \
206: (UH)(((unicode - 0x10000) % 0x400) + 0xdc00)
207:
208: 209: 210:
211: IMPORT W fatEncUtf8ToUnicode(const UB utf8str[], UW *_unicode);
212:
213: IMPORT W fatEncUnicodeToUtf8(UW unicode, UB utf8a[UTF8_MAX]);
214:
215: IMPORT W fatEncUtf8strToUtf16str(const UB *utf8str,
216: W utf8len, UH *utf16str, W utf16buflen, BOOL toUpper);
217:
218: IMPORT W fatEncUtf16strToUtf8str(const UH *utf16str,
219: W utf16len, UB *utf8str, W utf8buflen);
220:
221: #ifndef FAT_ASCII_FN_ONLY
222:
223: IMPORT W fatEncCP932ToUnicode(const UB cp932[], UW *unicode);
224: IMPORT W fatEncUnicodeToCP932(UW unicode, UB cp932[ENC_CHAR_MAX]);
225:
226: Inline W fatEncUnicodeToLocal(UW unicode, UB local[ENC_CHAR_MAX])
227: {
228: return fatEncUnicodeToCP932(unicode, local);
229: }
230:
231: Inline W fatEncLocalToUnicode(const UB local[], UW *unicode)
232: {
233: return fatEncCP932ToUnicode(local, unicode);
234: }
235: #endif
236:
237: #endif
238: