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;
176 = static_cast<sal_Unicode
>(ImplGetHighSurrogate(nCode
));
178 = static_cast<sal_Unicode
>(ImplGetLowSurrogate(nCode
));
179 startOfCurrentChar
= nConverted
+ 1;
185 ImplGb180302000ToUnicodeRange
const * pRange
187 sal_uInt32 nFirstNonRange
= 0;
190 if (pRange
->m_nNonRangeDataIndex
== -1)
192 else if (nCode
< pRange
->m_nFirstLinear
)
194 if (pDestBufPtr
!= pDestBufEnd
) {
197 pRange
->m_nNonRangeDataIndex
198 + (nCode
- nFirstNonRange
)];
199 startOfCurrentChar
= nConverted
+ 1;
204 else if (nCode
< pRange
->m_nPastLinear
)
206 if (pDestBufPtr
!= pDestBufEnd
) {
208 = static_cast<sal_Unicode
>(pRange
->m_nFirstUnicode
212 startOfCurrentChar
= nConverted
+ 1;
217 nFirstNonRange
= (pRange
++)->m_nPastLinear
;
220 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
232 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
233 bUndefined
, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
236 case sal::detail::textenc::BAD_INPUT_STOP
:
237 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
238 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0) {
241 nConverted
= startOfCurrentChar
;
245 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
246 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
247 startOfCurrentChar
= nConverted
+ 1;
250 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
257 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
;
261 if (eState
!= IMPL_GB_18030_TO_UNICODE_STATE_0
262 && (nInfo
& (RTL_TEXTTOUNICODE_INFO_ERROR
263 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
))
266 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0)
267 nInfo
|= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL
;
269 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
270 false, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
273 case sal::detail::textenc::BAD_INPUT_STOP
:
274 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) != 0) {
275 nConverted
= startOfCurrentChar
;
278 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
279 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
282 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
283 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
;
290 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
= eState
;
291 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
= nCode
;
296 *pSrcCvtBytes
= nConverted
;
298 return pDestBufPtr
- pDestBuf
;
301 sal_Size
ImplConvertUnicodeToGb18030(void const * pData
,
303 sal_Unicode
const * pSrcBuf
,
309 sal_Size
* pSrcCvtChars
)
311 sal_uInt32
const * pGb18030Data
312 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
313 m_pUnicodeToGb18030Data
;
314 ImplUnicodeToGb180302000Range
const * pGb18030Ranges
315 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
316 m_pUnicodeToGb18030Ranges
;
317 sal_Unicode nHighSurrogate
= 0;
318 sal_uInt32 nInfo
= 0;
319 sal_Size nConverted
= 0;
320 char * pDestBufPtr
= pDestBuf
;
321 char * pDestBufEnd
= pDestBuf
+ nDestBytes
;
325 = static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
;
327 for (; nConverted
< nSrcChars
; ++nConverted
)
329 bool bUndefined
= true;
330 sal_uInt32 nChar
= *pSrcBuf
++;
331 if (nHighSurrogate
== 0)
333 if (ImplIsHighSurrogate(nChar
))
335 nHighSurrogate
= static_cast<sal_Unicode
>(nChar
);
338 else if (ImplIsLowSurrogate(nChar
))
344 else if (ImplIsLowSurrogate(nChar
))
345 nChar
= ImplCombineSurrogates(nHighSurrogate
, nChar
);
352 assert(rtl::isUnicodeScalarValue(nChar
));
355 if (pDestBufPtr
!= pDestBufEnd
)
356 *pDestBufPtr
++ = static_cast< char >(nChar
);
359 else if (nChar
< 0x10000)
361 ImplUnicodeToGb180302000Range
const * pRange
= pGb18030Ranges
;
362 sal_Unicode nFirstNonRange
= 0x80;
365 if (nChar
< pRange
->m_nFirstUnicode
)
368 = pGb18030Data
[pRange
->m_nNonRangeDataIndex
369 + (nChar
- nFirstNonRange
)];
370 if (pDestBufEnd
- pDestBufPtr
371 >= (nCode
<= 0xFFFF ? 2 : 4))
375 *pDestBufPtr
++ = static_cast< char >(nCode
>> 24);
376 *pDestBufPtr
++ = static_cast< char >(nCode
>> 16 & 0xFF);
378 *pDestBufPtr
++ = static_cast< char >(nCode
>> 8 & 0xFF);
379 *pDestBufPtr
++ = static_cast< char >(nCode
& 0xFF);
385 if (nChar
<= pRange
->m_nLastUnicode
)
387 if (pDestBufEnd
- pDestBufPtr
>= 4)
390 = pRange
->m_nFirstLinear
391 + (nChar
- pRange
->m_nFirstUnicode
);
392 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x81);
394 = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
395 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
396 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
403 = static_cast<sal_Unicode
>((pRange
++)->m_nLastUnicode
+ 1);
407 if (pDestBufEnd
- pDestBufPtr
>= 4)
409 sal_uInt32 nCode
= nChar
- 0x10000;
410 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x90);
411 *pDestBufPtr
++ = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
412 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
413 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
421 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
422 bUndefined
, nChar
, nFlags
, &pDestBufPtr
, pDestBufEnd
,
423 &nInfo
, nullptr, 0, nullptr))
425 case sal::detail::textenc::BAD_INPUT_STOP
:
429 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
433 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
440 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
444 if (nHighSurrogate
!= 0
445 && (nInfo
& (RTL_UNICODETOTEXT_INFO_ERROR
446 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
))
449 if ((nFlags
& RTL_UNICODETOTEXT_FLAGS_FLUSH
) != 0)
450 nInfo
|= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL
;
452 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
453 false, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
, &nInfo
,
454 nullptr, 0, nullptr))
456 case sal::detail::textenc::BAD_INPUT_STOP
:
457 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
461 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
462 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
468 static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
473 *pSrcCvtChars
= nConverted
;
475 return pDestBufPtr
- pDestBuf
;
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */