1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
24 #include <rtl/character.hxx>
25 #include <rtl/textcvt.h>
26 #include <sal/types.h>
28 #include "context.hxx"
29 #include "converter.hxx"
30 #include "convertgb18030.hxx"
31 #include "tenchelp.hxx"
32 #include "unichars.hxx"
36 enum ImplGb18030ToUnicodeState
38 IMPL_GB_18030_TO_UNICODE_STATE_0
,
39 IMPL_GB_18030_TO_UNICODE_STATE_1
,
40 IMPL_GB_18030_TO_UNICODE_STATE_2
,
41 IMPL_GB_18030_TO_UNICODE_STATE_3
44 struct ImplGb18030ToUnicodeContext
46 ImplGb18030ToUnicodeState m_eState
;
52 void * ImplCreateGb18030ToUnicodeContext()
54 ImplGb18030ToUnicodeContext
* pContext
= new ImplGb18030ToUnicodeContext
;
55 pContext
->m_eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
59 void ImplResetGb18030ToUnicodeContext(void * pContext
)
62 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
63 = IMPL_GB_18030_TO_UNICODE_STATE_0
;
66 void ImplDestroyGb18030ToUnicodeContext(void * pContext
)
68 delete static_cast< ImplGb18030ToUnicodeContext
* >(pContext
);
71 sal_Size
ImplConvertGb18030ToUnicode(void const * pData
,
75 sal_Unicode
* pDestBuf
,
79 sal_Size
* pSrcCvtBytes
)
81 sal_Unicode
const * pGb18030Data
82 = static_cast< ImplGb18030ConverterData
const * >(pData
)->m_pGb18030ToUnicodeData
;
83 ImplGb180302000ToUnicodeRange
const * pGb18030Ranges
84 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
85 m_pGb18030ToUnicodeRanges
;
86 ImplGb18030ToUnicodeState eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
89 sal_Size nConverted
= 0;
90 sal_Unicode
* pDestBufPtr
= pDestBuf
;
91 sal_Unicode
* pDestBufEnd
= pDestBuf
+ nDestChars
;
92 sal_Size startOfCurrentChar
= 0;
96 eState
= static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
;
97 nCode
= static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
;
100 for (; nConverted
< nSrcBytes
; ++nConverted
)
102 bool bUndefined
= true;
103 sal_uInt32 nChar
= *reinterpret_cast<unsigned char const *>(pSrcBuf
++);
106 case IMPL_GB_18030_TO_UNICODE_STATE_0
:
108 if (pDestBufPtr
!= pDestBufEnd
) {
109 *pDestBufPtr
++ = static_cast<sal_Unicode
>(nChar
);
110 startOfCurrentChar
= nConverted
+ 1;
113 else if (nChar
== 0x80)
115 else if (nChar
<= 0xFE)
117 nCode
= nChar
- 0x81;
118 eState
= IMPL_GB_18030_TO_UNICODE_STATE_1
;
127 case IMPL_GB_18030_TO_UNICODE_STATE_1
:
128 if (nChar
>= 0x30 && nChar
<= 0x39)
130 nCode
= nCode
* 10 + (nChar
- 0x30);
131 eState
= IMPL_GB_18030_TO_UNICODE_STATE_2
;
133 else if ((nChar
>= 0x40 && nChar
<= 0x7E)
134 || (nChar
>= 0x80 && nChar
<= 0xFE))
136 nCode
= nCode
* 190 + (nChar
<= 0x7E ? nChar
- 0x40 :
138 if (pDestBufPtr
!= pDestBufEnd
) {
139 *pDestBufPtr
++ = pGb18030Data
[nCode
];
140 startOfCurrentChar
= nConverted
+ 1;
143 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
152 case IMPL_GB_18030_TO_UNICODE_STATE_2
:
153 if (nChar
>= 0x81 && nChar
<= 0xFE)
155 nCode
= nCode
* 126 + (nChar
- 0x81);
156 eState
= IMPL_GB_18030_TO_UNICODE_STATE_3
;
165 case IMPL_GB_18030_TO_UNICODE_STATE_3
:
166 if (nChar
>= 0x30 && nChar
<= 0x39)
168 nCode
= nCode
* 10 + (nChar
- 0x30);
170 // 90 30 81 30 to E3 32 9A 35 maps to U+10000 to U+10FFFF:
171 if (nCode
>= 189000 && nCode
<= 1237575)
172 if (pDestBufEnd
- pDestBufPtr
>= 2)
174 nCode
-= 189000 - 0x10000;
175 pDestBufPtr
+= rtl::splitSurrogates(nCode
, pDestBufPtr
);
176 startOfCurrentChar
= nConverted
+ 1;
182 ImplGb180302000ToUnicodeRange
const * pRange
184 sal_uInt32 nFirstNonRange
= 0;
187 if (pRange
->m_nNonRangeDataIndex
== -1)
189 else if (nCode
< pRange
->m_nFirstLinear
)
191 if (pDestBufPtr
!= pDestBufEnd
) {
194 pRange
->m_nNonRangeDataIndex
195 + (nCode
- nFirstNonRange
)];
196 startOfCurrentChar
= nConverted
+ 1;
201 else if (nCode
< pRange
->m_nPastLinear
)
203 if (pDestBufPtr
!= pDestBufEnd
) {
205 = static_cast<sal_Unicode
>(pRange
->m_nFirstUnicode
209 startOfCurrentChar
= nConverted
+ 1;
214 nFirstNonRange
= (pRange
++)->m_nPastLinear
;
217 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
229 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
230 bUndefined
, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
233 case sal::detail::textenc::BAD_INPUT_STOP
:
234 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
235 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0) {
238 nConverted
= startOfCurrentChar
;
242 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
243 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
244 startOfCurrentChar
= nConverted
+ 1;
247 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
254 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
;
258 if (eState
!= IMPL_GB_18030_TO_UNICODE_STATE_0
259 && (nInfo
& (RTL_TEXTTOUNICODE_INFO_ERROR
260 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
))
263 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0)
264 nInfo
|= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL
;
266 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
267 false, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
270 case sal::detail::textenc::BAD_INPUT_STOP
:
271 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) != 0) {
272 nConverted
= startOfCurrentChar
;
275 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
276 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
279 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
280 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
;
287 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
= eState
;
288 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
= nCode
;
293 *pSrcCvtBytes
= nConverted
;
295 return pDestBufPtr
- pDestBuf
;
298 sal_Size
ImplConvertUnicodeToGb18030(void const * pData
,
300 sal_Unicode
const * pSrcBuf
,
306 sal_Size
* pSrcCvtChars
)
308 sal_uInt32
const * pGb18030Data
309 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
310 m_pUnicodeToGb18030Data
;
311 ImplUnicodeToGb180302000Range
const * pGb18030Ranges
312 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
313 m_pUnicodeToGb18030Ranges
;
314 sal_Unicode nHighSurrogate
= 0;
315 sal_uInt32 nInfo
= 0;
316 sal_Size nConverted
= 0;
317 char * pDestBufPtr
= pDestBuf
;
318 char * pDestBufEnd
= pDestBuf
+ nDestBytes
;
322 = static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
;
324 for (; nConverted
< nSrcChars
; ++nConverted
)
326 bool bUndefined
= true;
327 sal_uInt32 nChar
= *pSrcBuf
++;
328 if (nHighSurrogate
== 0)
330 if (rtl::isHighSurrogate(nChar
))
332 nHighSurrogate
= static_cast<sal_Unicode
>(nChar
);
335 else if (rtl::isLowSurrogate(nChar
))
341 else if (rtl::isLowSurrogate(nChar
))
342 nChar
= rtl::combineSurrogates(nHighSurrogate
, nChar
);
349 assert(rtl::isUnicodeScalarValue(nChar
));
352 if (pDestBufPtr
!= pDestBufEnd
)
353 *pDestBufPtr
++ = static_cast< char >(nChar
);
356 else if (nChar
< 0x10000)
358 ImplUnicodeToGb180302000Range
const * pRange
= pGb18030Ranges
;
359 sal_Unicode nFirstNonRange
= 0x80;
362 if (nChar
< pRange
->m_nFirstUnicode
)
365 = pGb18030Data
[pRange
->m_nNonRangeDataIndex
366 + (nChar
- nFirstNonRange
)];
367 if (pDestBufEnd
- pDestBufPtr
368 >= (nCode
<= 0xFFFF ? 2 : 4))
372 *pDestBufPtr
++ = static_cast< char >(nCode
>> 24);
373 *pDestBufPtr
++ = static_cast< char >(nCode
>> 16 & 0xFF);
375 *pDestBufPtr
++ = static_cast< char >(nCode
>> 8 & 0xFF);
376 *pDestBufPtr
++ = static_cast< char >(nCode
& 0xFF);
382 if (nChar
<= pRange
->m_nLastUnicode
)
384 if (pDestBufEnd
- pDestBufPtr
>= 4)
387 = pRange
->m_nFirstLinear
388 + (nChar
- pRange
->m_nFirstUnicode
);
389 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x81);
391 = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
392 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
393 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
400 = static_cast<sal_Unicode
>((pRange
++)->m_nLastUnicode
+ 1);
404 if (pDestBufEnd
- pDestBufPtr
>= 4)
406 sal_uInt32 nCode
= nChar
- 0x10000;
407 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x90);
408 *pDestBufPtr
++ = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
409 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
410 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
418 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
419 bUndefined
, nChar
, nFlags
, &pDestBufPtr
, pDestBufEnd
,
420 &nInfo
, nullptr, 0, nullptr))
422 case sal::detail::textenc::BAD_INPUT_STOP
:
426 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
430 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
437 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
441 if (nHighSurrogate
!= 0
442 && (nInfo
& (RTL_UNICODETOTEXT_INFO_ERROR
443 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
))
446 if ((nFlags
& RTL_UNICODETOTEXT_FLAGS_FLUSH
) != 0)
447 nInfo
|= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL
;
449 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
450 false, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
, &nInfo
,
451 nullptr, 0, nullptr))
453 case sal::detail::textenc::BAD_INPUT_STOP
:
454 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
458 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
459 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
465 static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
470 *pSrcCvtChars
= nConverted
;
472 return pDestBufPtr
- pDestBuf
;
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */