-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtf8_16.cpp
577 lines (512 loc) · 13.8 KB
/
Utf8_16.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Utf8_16.cxx
// Copyright (C) 2002 Scott Kirkwood
//
// Permission to use, copy, modify, distribute and sell this code
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies or
// any derived copies. Scott Kirkwood makes no representations
// about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
//
// Modificated 2006 Jens Lorenz
//
// - Clean up the sources
// - Removing UCS-Bug in Utf8_Iter
// - Add convert function in Utf8_16_Write
////////////////////////////////////////////////////////////////////////////////
#include "Utf8_16.h"
const Utf8_16::utf8 Utf8_16::k_Boms[][3] = {
{0x00, 0x00, 0x00}, // Unknown
{0xEF, 0xBB, 0xBF}, // UTF8
{0xFE, 0xFF, 0x00}, // Big endian
{0xFF, 0xFE, 0x00}, // Little endian
};
// ==================================================================
Utf8_16_Read::Utf8_16_Read() {
m_eEncoding = uni8Bit;
m_nAllocatedBufSize = 0;
m_nNewBufSize = 0;
m_pNewBuf = NULL;
m_bFirstRead = true;
}
Utf8_16_Read::~Utf8_16_Read()
{
if ((m_eEncoding == uni16BE) || (m_eEncoding == uni16LE) || (m_eEncoding == uni16BE_NoBOM) || (m_eEncoding == uni16LE_NoBOM))
{
delete [] m_pNewBuf;
m_pNewBuf = NULL;
}
}
// Returned value :
// 0 : utf8
// 1 : 7bits
// 2 : 8bits
u78 Utf8_16_Read::utf8_7bits_8bits()
{
int rv = 1;
int ASCII7only = 1;
utf8 *sx = (utf8 *)m_pBuf;
utf8 *endx = sx + m_nLen;
while (sx<endx)
{
if (!*sx)
{ // For detection, we'll say that NUL means not UTF8
ASCII7only = 0;
rv = 0;
break;
}
else if (*sx < 0x80)
{ // 0nnnnnnn If the byte's first hex code begins with 0-7, it is an ASCII character.
++sx;
}
else if (*sx < (0x80 + 0x40))
{ // 10nnnnnn 8 through B cannot be first hex codes
ASCII7only=0;
rv=0;
break;
}
else if (*sx < (0x80 + 0x40 + 0x20))
{ // 110xxxvv 10nnnnnn If it begins with C or D, it is an 11 bit character
ASCII7only=0;
if (sx>=endx-1)
break;
if ((*sx & 0xC0) != 0xC0 || (sx[1]&(0x80+0x40)) != 0x80) {
rv=0; break;
}
sx+=2;
}
else if (*sx < (0x80 + 0x40 + 0x20 + 0x10))
{ // 1110qqqq 10xxxxvv 10nnnnnn If it begins with E, it is 16 bit
ASCII7only=0;
if (sx>=endx-2)
break;
if ((*sx & 0xE0) != 0xE0 || (sx[1]&(0x80+0x40)) != 0x80 || (sx[2]&(0x80+0x40)) != 0x80) {
rv=0; break;
}
sx+=3;
}
else
{ // more than 16 bits are not allowed here
ASCII7only=0;
rv=0;
break;
}
}
if (ASCII7only)
return ascii7bits;
if (rv)
return utf8NoBOM;
return ascii8bits;
}
size_t Utf8_16_Read::convert(char* buf, size_t len)
{
// bugfix by Jens Lorenz
static size_t nSkip = 0;
m_pBuf = (ubyte*)buf;
m_nLen = len;
m_nNewBufSize = 0;
if (m_bFirstRead == true)
{
determineEncoding();
nSkip = m_nSkip;
m_bFirstRead = false;
}
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie: {
// Do nothing, pass through
m_nAllocatedBufSize = 0;
m_pNewBuf = m_pBuf;
m_nNewBufSize = len;
break;
}
case uniUTF8: {
// Pass through after BOM
m_nAllocatedBufSize = 0;
m_pNewBuf = m_pBuf + nSkip;
m_nNewBufSize = len - nSkip;
break;
}
case uni16BE_NoBOM:
case uni16LE_NoBOM:
case uni16BE:
case uni16LE: {
size_t newSize = len + len / 2 + 1;
if (m_nAllocatedBufSize != newSize)
{
if (m_pNewBuf)
delete [] m_pNewBuf;
m_pNewBuf = NULL;
m_pNewBuf = new ubyte[newSize];
m_nAllocatedBufSize = newSize;
}
ubyte* pCur = m_pNewBuf;
m_Iter16.set(m_pBuf + nSkip, len - nSkip, m_eEncoding);
for (; m_Iter16; ++m_Iter16)
{
*pCur++ = m_Iter16.get();
}
m_nNewBufSize = pCur - m_pNewBuf;
break;
}
default:
break;
}
// necessary for second calls and more
nSkip = 0;
return m_nNewBufSize;
}
void Utf8_16_Read::determineEncoding()
{
INT uniTest = IS_TEXT_UNICODE_STATISTICS;
m_eEncoding = uni8Bit;
m_nSkip = 0;
// detect UTF-16 big-endian with BOM
if (m_nLen > 1 && m_pBuf[0] == k_Boms[uni16BE][0] && m_pBuf[1] == k_Boms[uni16BE][1])
{
m_eEncoding = uni16BE;
m_nSkip = 2;
}
// detect UTF-16 little-endian with BOM
else if (m_nLen > 1 && m_pBuf[0] == k_Boms[uni16LE][0] && m_pBuf[1] == k_Boms[uni16LE][1])
{
m_eEncoding = uni16LE;
m_nSkip = 2;
}
// detect UTF-8 with BOM
else if (m_nLen > 2 && m_pBuf[0] == k_Boms[uniUTF8][0] &&
m_pBuf[1] == k_Boms[uniUTF8][1] && m_pBuf[2] == k_Boms[uniUTF8][2])
{
m_eEncoding = uniUTF8;
m_nSkip = 3;
}
#if 0
// try to detect UTF-16 little-endian without BOM
else if (m_nLen > 1 && m_nLen % 2 == 0 && m_pBuf[0] != NULL && m_pBuf[1] == NULL && IsTextUnicode(m_pBuf, m_nLen, &uniTest))
{
m_eEncoding = uni16LE_NoBOM;
m_nSkip = 0;
}
//UTF-16 big-endian without BOM detection is taken away scince this detection is very week
// try to detect UTF-16 big-endian without BOM
else if (m_nLen > 1 && m_pBuf[0] == NULL && m_pBuf[1] != NULL)
{
m_eEncoding = uni16BE_NoBOM;
m_nSkip = 0;
}
#endif
else
{
u78 detectedEncoding = utf8_7bits_8bits();
if (detectedEncoding == utf8NoBOM)
m_eEncoding = uniCookie;
else if (detectedEncoding == ascii7bits)
m_eEncoding = uni7Bit;
else //(detectedEncoding == ascii8bits)
m_eEncoding = uni8Bit;
m_nSkip = 0;
}
}
UniMode Utf8_16_Read::determineEncoding(const unsigned char *buf, int bufLen)
{
// detect UTF-16 big-endian with BOM
if (bufLen > 1 && buf[0] == k_Boms[uni16BE][0] && buf[1] == k_Boms[uni16BE][1])
{
return uni16BE;
}
// detect UTF-16 little-endian with BOM
if (bufLen > 1 && buf[0] == k_Boms[uni16LE][0] && buf[1] == k_Boms[uni16LE][1])
{
return uni16LE;
}
// detect UTF-8 with BOM
if (bufLen > 2 && buf[0] == k_Boms[uniUTF8][0] &&
buf[1] == k_Boms[uniUTF8][1] && buf[2] == k_Boms[uniUTF8][2])
{
return uniUTF8;
}
return uni8Bit;
}
// ==================================================================
Utf8_16_Write::Utf8_16_Write()
{
m_eEncoding = uni8Bit;
m_pFile = NULL;
m_pNewBuf = NULL;
m_bFirstWrite = true;
m_nBufSize = 0;
}
Utf8_16_Write::~Utf8_16_Write()
{
fclose();
}
FILE * Utf8_16_Write::fopen(const TCHAR *_name, const TCHAR *_type)
{
m_pFile = ::generic_fopen(_name, _type);
m_bFirstWrite = true;
return m_pFile;
}
size_t Utf8_16_Write::fwrite(const void* p, size_t _size)
{
// no file open
if (!m_pFile)
{
return 0;
}
size_t ret = 0;
if (m_bFirstWrite)
{
switch (m_eEncoding)
{
case uniUTF8: {
::fwrite(k_Boms[m_eEncoding], 3, 1, m_pFile);
break;
}
case uni16BE:
case uni16LE:
::fwrite(k_Boms[m_eEncoding], 2, 1, m_pFile);
break;
default:
// nothing to do
break;
}
m_bFirstWrite = false;
}
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie:
case uniUTF8: {
// Normal write
ret = ::fwrite(p, _size, 1, m_pFile);
break;
}
case uni16BE_NoBOM:
case uni16LE_NoBOM:
case uni16BE:
case uni16LE: {
static const int bufSize = 64*1024;
utf16 buf[bufSize];
Utf8_Iter iter8;
iter8.set(static_cast<const ubyte*>(p), _size, m_eEncoding);
int bufIndex = 0;
while (iter8) {
if (iter8.canGet()) {
buf[bufIndex++] = iter8.get();
}
++iter8;
if(bufIndex == bufSize || !iter8) {
if(!::fwrite(buf, bufIndex*sizeof(utf16), 1, m_pFile)) return 0;
bufIndex = 0;
}
}
ret = 1;
break;
}
default:
break;
}
return ret;
}
size_t Utf8_16_Write::convert(char* p, size_t _size)
{
if (m_pNewBuf)
{
delete [] m_pNewBuf;
}
switch (m_eEncoding)
{
case uni7Bit:
case uni8Bit:
case uniCookie: {
// Normal write
m_nBufSize = _size;
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize];
memcpy(m_pNewBuf, p, _size);
break;
}
case uniUTF8: {
m_nBufSize = _size + 3;
m_pNewBuf = (ubyte*)new ubyte[m_nBufSize];
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 3);
memcpy(&m_pNewBuf[3], p, _size);
break;
}
case uni16BE_NoBOM:
case uni16LE_NoBOM:
case uni16BE:
case uni16LE:
{
utf16* pCur = NULL;
if (m_eEncoding == uni16BE || m_eEncoding == uni16LE) {
// Write the BOM
m_pNewBuf = (ubyte*)new ubyte[sizeof(utf16) * (_size + 1)];
memcpy(m_pNewBuf, k_Boms[m_eEncoding], 2);
pCur = (utf16*)&m_pNewBuf[2];
} else {
m_pNewBuf = (ubyte*)new ubyte[sizeof(utf16) * _size];
pCur = (utf16*)m_pNewBuf;
}
Utf8_Iter iter8;
iter8.set(reinterpret_cast<const ubyte*>(p), _size, m_eEncoding);
for (; iter8; ++iter8) {
if (iter8.canGet()) {
*pCur++ = iter8.get();
}
}
m_nBufSize = (const char*)pCur - (const char*)m_pNewBuf;
break;
}
default:
break;
}
return m_nBufSize;
}
void Utf8_16_Write::setEncoding(UniMode eType)
{
m_eEncoding = eType;
}
void Utf8_16_Write::fclose()
{
if (m_pNewBuf)
delete [] m_pNewBuf;
if (m_pFile)
::fclose(m_pFile);
}
//=================================================================
Utf8_Iter::Utf8_Iter()
{
reset();
}
void Utf8_Iter::reset()
{
m_pBuf = NULL;
m_pRead = NULL;
m_pEnd = NULL;
m_eState = eStart;
m_nCur = 0;
m_eEncoding = uni8Bit;
}
void Utf8_Iter::set(const ubyte* pBuf, size_t nLen, UniMode eEncoding)
{
m_pBuf = pBuf;
m_pRead = pBuf;
m_pEnd = pBuf + nLen;
m_eEncoding = eEncoding;
operator++();
// Note: m_eState, m_nCur not set
}
// Go to the next byte.
void Utf8_Iter::operator++()
{
switch (m_eState)
{
case eStart:
if (*m_pRead < 0x80) {
m_nCur = *m_pRead;
toStart();
} else if (*m_pRead < 0xE0) {
m_nCur = static_cast<utf16>((0x1F & *m_pRead) << 6);
m_eState = e2Bytes_Byte2;
} else {
m_nCur = static_cast<utf16>((0xF & *m_pRead) << 12);
m_eState = e3Bytes_Byte2;
}
break;
case e2Bytes_Byte2:
case e3Bytes_Byte3:
m_nCur |= static_cast<utf8>(0x3F & *m_pRead);
toStart();
break;
case e3Bytes_Byte2:
m_nCur |= static_cast<utf16>((0x3F & *m_pRead) << 6);
m_eState = e3Bytes_Byte3;
break;
}
++m_pRead;
}
void Utf8_Iter::toStart()
{
m_eState = eStart;
if (m_eEncoding == uni16BE || m_eEncoding == uni16BE_NoBOM)
{
swap();
}
}
void Utf8_Iter::swap()
{
utf8* p = reinterpret_cast<utf8*>(&m_nCur);
utf8 swapbyte = *p;
*p = *(p + 1);
*(p + 1) = swapbyte;
}
//==================================================
Utf16_Iter::Utf16_Iter()
{
reset();
}
void Utf16_Iter::reset()
{
m_pBuf = NULL;
m_pRead = NULL;
m_pEnd = NULL;
m_eState = eStart;
m_nCur = 0;
m_nCur16 = 0;
m_eEncoding = uni8Bit;
}
void Utf16_Iter::set(const ubyte* pBuf, size_t nLen, UniMode eEncoding)
{
m_pBuf = pBuf;
m_pRead = pBuf;
m_pEnd = pBuf + nLen;
m_eEncoding = eEncoding;
m_eState = eStart;
operator++();
// Note: m_eState, m_nCur, m_nCur16 not reinitalized.
}
// Goes to the next byte.
// Not the next symbol which you might expect.
// This way we can continue from a partial buffer that doesn't align
void Utf16_Iter::operator++()
{
switch (m_eState)
{
case eStart:
if (m_eEncoding == uni16LE || m_eEncoding == uni16LE_NoBOM)
{
m_nCur16 = *m_pRead++;
m_nCur16 |= static_cast<utf16>(*m_pRead << 8);
}
else //(m_eEncoding == uni16BE || m_eEncoding == uni16BE_NoBOM)
{
m_nCur16 = static_cast<utf16>(*m_pRead++ << 8);
m_nCur16 |= *m_pRead;
}
++m_pRead;
if (m_nCur16 < 0x80) {
m_nCur = static_cast<ubyte>(m_nCur16 & 0xFF);
m_eState = eStart;
} else if (m_nCur16 < 0x800) {
m_nCur = static_cast<ubyte>(0xC0 | m_nCur16 >> 6);
m_eState = e2Bytes2;
} else {
m_nCur = static_cast<ubyte>(0xE0 | m_nCur16 >> 12);
m_eState = e3Bytes2;
}
break;
case e2Bytes2:
case e3Bytes3:
m_nCur = static_cast<ubyte>(0x80 | m_nCur16 & 0x3F);
m_eState = eStart;
break;
case e3Bytes2:
m_nCur = static_cast<ubyte>(0x80 | ((m_nCur16 >> 6) & 0x3F));
m_eState = e3Bytes3;
break;
}
}