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
|
/*************************************************************************
*
* $RCSfile: convertgb18030.c,v $
*
* $Revision: 1.3 $
*
* last change: $Author: sb $ $Date: 2001-11-19 17:46:37 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRUNTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRUNTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc..
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
#ifndef INCLUDED_RTL_TEXTENC_CONVERTGB18030_H
#include "convertgb18030.h"
#endif
#ifndef INCLUDED_RTL_TEXTENC_CONTEXT_H
#include "context.h"
#endif
#ifndef INCLUDED_RTL_TEXTENC_CONVERTER_H
#include "converter.h"
#endif
#ifndef INCLUDED_RTL_TEXTENC_TENCHELP_H
#include "tenchelp.h"
#endif
#ifndef INCLUDED_RTL_TEXTENC_UNICHARS_H
#include "unichars.h"
#endif
#ifndef _RTL_ALLOC_H_
#include "rtl/alloc.h"
#endif
#ifndef _RTL_TEXTCVT_H
#include "rtl/textcvt.h"
#endif
#ifndef _SAL_TYPES_H_
#include "sal/types.h"
#endif
typedef enum
{
IMPL_GB_18030_TO_UNICODE_STATE_0,
IMPL_GB_18030_TO_UNICODE_STATE_1,
IMPL_GB_18030_TO_UNICODE_STATE_2,
IMPL_GB_18030_TO_UNICODE_STATE_3
} ImplGb18030ToUnicodeState;
typedef struct
{
ImplGb18030ToUnicodeState m_eState;
sal_uInt32 m_nCode;
} ImplGb18030ToUnicodeContext;
void * ImplCreateGb18030ToUnicodeContext(void)
{
void * pContext
= rtl_allocateMemory(sizeof (ImplGb18030ToUnicodeContext));
((ImplGb18030ToUnicodeContext *) pContext)->m_eState
= IMPL_GB_18030_TO_UNICODE_STATE_0;
return pContext;
}
void ImplResetGb18030ToUnicodeContext(void * pContext)
{
if (pContext)
((ImplGb18030ToUnicodeContext *) pContext)->m_eState
= IMPL_GB_18030_TO_UNICODE_STATE_0;
}
sal_Size ImplConvertGb18030ToUnicode(ImplTextConverterData const * pData,
void * pContext,
sal_Char const * pSrcBuf,
sal_Size nSrcBytes,
sal_Unicode * pDestBuf,
sal_Size nDestChars,
sal_uInt32 nFlags,
sal_uInt32 * pInfo,
sal_Size * pSrcCvtBytes)
{
sal_Unicode const * pGb18030Data
= ((ImplGb18030ConverterData const *) pData)->m_pGb18030ToUnicodeData;
ImplGb180302000ToUnicodeRange const * pGb18030Ranges
= ((ImplGb18030ConverterData const *) pData)->
m_pGb18030ToUnicodeRanges;
ImplGb18030ToUnicodeState eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
sal_uInt32 nCode;
sal_uInt32 nInfo = 0;
sal_Size nConverted = 0;
sal_Unicode * pDestBufPtr = pDestBuf;
sal_Unicode * pDestBufEnd = pDestBuf + nDestChars;
if (pContext)
{
eState = ((ImplGb18030ToUnicodeContext *) pContext)->m_eState;
nCode = ((ImplGb18030ToUnicodeContext *) pContext)->m_nCode;
}
for (; nConverted < nSrcBytes; ++nConverted)
{
sal_Bool bUndefined = sal_True;
sal_uInt32 nChar = *((sal_uChar const *) pSrcBuf)++;
switch (eState)
{
case IMPL_GB_18030_TO_UNICODE_STATE_0:
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)
*pDestBufPtr++ = (sal_Unicode) nChar;
else
goto no_output;
else if (nChar == 0x80)
goto bad_input;
else if (nChar <= 0xFE)
{
nCode = nChar - 0x81;
eState = IMPL_GB_18030_TO_UNICODE_STATE_1;
}
else
{
bUndefined = sal_False;
goto bad_input;
}
break;
case IMPL_GB_18030_TO_UNICODE_STATE_1:
if (nChar >= 0x30 && nChar <= 0x39)
{
nCode = nCode * 10 + (nChar - 0x30);
eState = IMPL_GB_18030_TO_UNICODE_STATE_2;
}
else if (nChar >= 0x40 && nChar <= 0x7E
|| nChar >= 0x80 && nChar <= 0xFE)
{
nCode = nCode * 190 + (nChar <= 0x7E ? nChar - 0x40 :
nChar - 0x80 + 63);
if (pDestBufPtr != pDestBufEnd)
*pDestBufPtr++ = pGb18030Data[nCode];
else
goto no_output;
eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
}
else
{
bUndefined = sal_False;
goto bad_input;
}
break;
case IMPL_GB_18030_TO_UNICODE_STATE_2:
if (nChar >= 0x81 && nChar <= 0xFE)
{
nCode = nCode * 126 + (nChar - 0x81);
eState = IMPL_GB_18030_TO_UNICODE_STATE_3;
}
else
{
bUndefined = sal_False;
goto bad_input;
}
break;
case IMPL_GB_18030_TO_UNICODE_STATE_3:
if (nChar >= 0x30 && nChar <= 0x39)
{
nCode = nCode * 10 + (nChar - 0x30);
/* 90 30 81 30 to E3 32 9A 35 maps to U+10000 to U+10FFFF: */
if (nCode >= 189000 && nCode <= 1237575)
if (pDestBufEnd - pDestBufPtr >= 2)
{
nCode -= 189000 - 0x10000;
*pDestBufPtr++
= (sal_Unicode) ImplGetHighSurrogate(nCode);
*pDestBufPtr++
= (sal_Unicode) ImplGetLowSurrogate(nCode);
}
else
goto no_output;
else
{
ImplGb180302000ToUnicodeRange const * pRange
= pGb18030Ranges;
sal_uInt32 nFirstNonRange = 0;
for (;;)
{
if (pRange->m_nNonRangeDataIndex == -1)
goto bad_input;
else if (nCode < pRange->m_nFirstLinear)
{
if (pDestBufPtr != pDestBufEnd)
*pDestBufPtr++
= pGb18030Data[
pRange->m_nNonRangeDataIndex
+ (nCode - nFirstNonRange)];
else
goto no_output;
break;
}
else if (nCode < pRange->m_nPastLinear)
{
if (pDestBufPtr != pDestBufEnd)
*pDestBufPtr++
= (sal_Unicode)
(pRange->m_nFirstUnicode
+ (nCode
- pRange->
m_nFirstLinear));
else
goto no_output;
break;
}
nFirstNonRange = (pRange++)->m_nPastLinear;
}
}
eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
}
else
{
bUndefined = sal_False;
goto bad_input;
}
break;
}
continue;
bad_input:
switch (ImplHandleBadInputMbTextToUnicodeConversion(bUndefined,
nFlags,
&pDestBufPtr,
pDestBufEnd,
&nInfo))
{
case IMPL_BAD_INPUT_STOP:
eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
break;
case IMPL_BAD_INPUT_CONTINUE:
eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
continue;
case IMPL_BAD_INPUT_NO_OUTPUT:
goto no_output;
}
break;
no_output:
--pSrcBuf;
nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
break;
}
if (eState != IMPL_GB_18030_TO_UNICODE_STATE_0
&& (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
| RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL))
== 0)
if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOSMALL;
else
switch (ImplHandleBadInputMbTextToUnicodeConversion(sal_False,
nFlags,
&pDestBufPtr,
pDestBufEnd,
&nInfo))
{
case IMPL_BAD_INPUT_STOP:
case IMPL_BAD_INPUT_CONTINUE:
eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
break;
case IMPL_BAD_INPUT_NO_OUTPUT:
nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
break;
}
if (pContext)
{
((ImplGb18030ToUnicodeContext *) pContext)->m_eState = eState;
((ImplGb18030ToUnicodeContext *) pContext)->m_nCode = nCode;
}
if (pInfo)
*pInfo = nInfo;
if (pSrcCvtBytes)
*pSrcCvtBytes = nConverted;
return pDestBufPtr - pDestBuf;
}
sal_Size ImplConvertUnicodeToGb18030(ImplTextConverterData const * pData,
void * pContext,
sal_Unicode const * pSrcBuf,
sal_Size nSrcChars,
sal_Char * pDestBuf,
sal_Size nDestBytes,
sal_uInt32 nFlags,
sal_uInt32 * pInfo,
sal_Size * pSrcCvtChars)
{
sal_uInt32 const * pGb18030Data
= ((ImplGb18030ConverterData const *) pData)->
m_pUnicodeToGb18030Data;
ImplUnicodeToGb180302000Range const * pGb18030Ranges
= ((ImplGb18030ConverterData const *) pData)->
m_pUnicodeToGb18030Ranges;
sal_Unicode nHighSurrogate = 0;
sal_uInt32 nInfo = 0;
sal_Size nConverted = 0;
sal_Char * pDestBufPtr = pDestBuf;
sal_Char * pDestBufEnd = pDestBuf + nDestBytes;
if (pContext)
nHighSurrogate
= ((ImplUnicodeToTextContext *) pContext)->m_nHighSurrogate;
for (; nConverted < nSrcChars; ++nConverted)
{
sal_Bool bUndefined = sal_True;
sal_uInt32 nChar = *pSrcBuf++;
if (nHighSurrogate == 0)
{
if (ImplIsHighSurrogate(nChar))
{
nHighSurrogate = (sal_Unicode) nChar;
continue;
}
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
else
{
bUndefined = sal_False;
goto bad_input;
}
if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
{
bUndefined = sal_False;
goto bad_input;
}
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)
*pDestBufPtr++ = (sal_Char) nChar;
else
goto no_output;
else if (nChar < 0x10000)
{
ImplUnicodeToGb180302000Range const * pRange = pGb18030Ranges;
sal_Unicode nFirstNonRange = 0x80;
for (;;)
{
if (nChar < pRange->m_nFirstUnicode)
{
sal_uInt32 nCode
= pGb18030Data[pRange->m_nNonRangeDataIndex
+ (nChar - nFirstNonRange)];
if (pDestBufEnd - pDestBufPtr
>= (nCode <= 0xFFFF ? 2 : 4))
{
if (nCode > 0xFFFF)
{
*pDestBufPtr++ = (sal_Char) (nCode >> 24);
*pDestBufPtr++ = (sal_Char) (nCode >> 16 & 0xFF);
}
*pDestBufPtr++ = (sal_Char) (nCode >> 8 & 0xFF);
*pDestBufPtr++ = (sal_Char) (nCode & 0xFF);
}
else
goto no_output;
break;
}
else if (nChar <= pRange->m_nLastUnicode)
{
if (pDestBufEnd - pDestBufPtr >= 4)
{
sal_uInt32 nCode
= pRange->m_nFirstLinear
+ (nChar - pRange->m_nFirstUnicode);
*pDestBufPtr++ = (sal_Char) (nCode / 12600 + 0x81);
*pDestBufPtr++
= (sal_Char) (nCode / 1260 % 10 + 0x30);
*pDestBufPtr++ = (sal_Char) (nCode / 10 % 126 + 0x81);
*pDestBufPtr++ = (sal_Char) (nCode % 10 + 0x30);
}
else
goto no_output;
break;
}
nFirstNonRange
= (sal_Unicode) ((pRange++)->m_nLastUnicode + 1);
}
}
else
if (pDestBufEnd - pDestBufPtr >= 4)
{
sal_uInt32 nCode = nChar - 0x10000;
*pDestBufPtr++ = (sal_Char) (nCode / 12600 + 0x90);
*pDestBufPtr++ = (sal_Char) (nCode / 1260 % 10 + 0x30);
*pDestBufPtr++ = (sal_Char) (nCode / 10 % 126 + 0x81);
*pDestBufPtr++ = (sal_Char) (nCode % 10 + 0x30);
}
else
goto no_output;
nHighSurrogate = 0;
continue;
bad_input:
switch (ImplHandleBadInputUnicodeToTextConversion(bUndefined,
nChar,
nFlags,
&pDestBufPtr,
pDestBufEnd,
&nInfo,
NULL,
0,
NULL))
{
case IMPL_BAD_INPUT_STOP:
nHighSurrogate = 0;
break;
case IMPL_BAD_INPUT_CONTINUE:
nHighSurrogate = 0;
continue;
case IMPL_BAD_INPUT_NO_OUTPUT:
goto no_output;
}
break;
no_output:
--pSrcBuf;
nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
break;
}
if (nHighSurrogate != 0
&& (nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
| RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
== 0)
if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
else
switch (ImplHandleBadInputUnicodeToTextConversion(sal_False,
0,
nFlags,
&pDestBufPtr,
pDestBufEnd,
&nInfo,
NULL,
0,
NULL))
{
case IMPL_BAD_INPUT_STOP:
case IMPL_BAD_INPUT_CONTINUE:
nHighSurrogate = 0;
break;
case IMPL_BAD_INPUT_NO_OUTPUT:
nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
break;
}
if (pContext)
((ImplUnicodeToTextContext *) pContext)->m_nHighSurrogate
= nHighSurrogate;
if (pInfo)
*pInfo = nInfo;
if (pSrcCvtChars)
*pSrcCvtChars = nConverted;
return pDestBufPtr - pDestBuf;
}
|