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>
22 #include <rtl/textcvt.h>
23 #include <sal/types.h>
25 #include "context.hxx"
26 #include "converter.hxx"
27 #include "convertgb18030.hxx"
28 #include "tenchelp.hxx"
29 #include "unichars.hxx"
33 enum ImplGb18030ToUnicodeState
35 IMPL_GB_18030_TO_UNICODE_STATE_0
,
36 IMPL_GB_18030_TO_UNICODE_STATE_1
,
37 IMPL_GB_18030_TO_UNICODE_STATE_2
,
38 IMPL_GB_18030_TO_UNICODE_STATE_3
41 struct ImplGb18030ToUnicodeContext
43 ImplGb18030ToUnicodeState m_eState
;
49 void * ImplCreateGb18030ToUnicodeContext()
51 ImplGb18030ToUnicodeContext
* pContext
= new ImplGb18030ToUnicodeContext
;
52 pContext
->m_eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
56 void ImplResetGb18030ToUnicodeContext(void * pContext
)
59 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
60 = IMPL_GB_18030_TO_UNICODE_STATE_0
;
63 void ImplDestroyGb18030ToUnicodeContext(void * pContext
)
65 delete static_cast< ImplGb18030ToUnicodeContext
* >(pContext
);
68 sal_Size
ImplConvertGb18030ToUnicode(void const * pData
,
72 sal_Unicode
* pDestBuf
,
76 sal_Size
* pSrcCvtBytes
)
78 sal_Unicode
const * pGb18030Data
79 = static_cast< ImplGb18030ConverterData
const * >(pData
)->m_pGb18030ToUnicodeData
;
80 ImplGb180302000ToUnicodeRange
const * pGb18030Ranges
81 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
82 m_pGb18030ToUnicodeRanges
;
83 ImplGb18030ToUnicodeState eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
86 sal_Size nConverted
= 0;
87 sal_Unicode
* pDestBufPtr
= pDestBuf
;
88 sal_Unicode
* pDestBufEnd
= pDestBuf
+ nDestChars
;
92 eState
= static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
;
93 nCode
= static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
;
96 for (; nConverted
< nSrcBytes
; ++nConverted
)
98 bool bUndefined
= true;
99 sal_uInt32 nChar
= *reinterpret_cast<unsigned char const *>(pSrcBuf
++);
102 case IMPL_GB_18030_TO_UNICODE_STATE_0
:
104 if (pDestBufPtr
!= pDestBufEnd
)
105 *pDestBufPtr
++ = static_cast<sal_Unicode
>(nChar
);
108 else if (nChar
== 0x80)
110 else if (nChar
<= 0xFE)
112 nCode
= nChar
- 0x81;
113 eState
= IMPL_GB_18030_TO_UNICODE_STATE_1
;
122 case IMPL_GB_18030_TO_UNICODE_STATE_1
:
123 if (nChar
>= 0x30 && nChar
<= 0x39)
125 nCode
= nCode
* 10 + (nChar
- 0x30);
126 eState
= IMPL_GB_18030_TO_UNICODE_STATE_2
;
128 else if ((nChar
>= 0x40 && nChar
<= 0x7E)
129 || (nChar
>= 0x80 && nChar
<= 0xFE))
131 nCode
= nCode
* 190 + (nChar
<= 0x7E ? nChar
- 0x40 :
133 if (pDestBufPtr
!= pDestBufEnd
)
134 *pDestBufPtr
++ = pGb18030Data
[nCode
];
137 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
146 case IMPL_GB_18030_TO_UNICODE_STATE_2
:
147 if (nChar
>= 0x81 && nChar
<= 0xFE)
149 nCode
= nCode
* 126 + (nChar
- 0x81);
150 eState
= IMPL_GB_18030_TO_UNICODE_STATE_3
;
159 case IMPL_GB_18030_TO_UNICODE_STATE_3
:
160 if (nChar
>= 0x30 && nChar
<= 0x39)
162 nCode
= nCode
* 10 + (nChar
- 0x30);
164 // 90 30 81 30 to E3 32 9A 35 maps to U+10000 to U+10FFFF:
165 if (nCode
>= 189000 && nCode
<= 1237575)
166 if (pDestBufEnd
- pDestBufPtr
>= 2)
168 nCode
-= 189000 - 0x10000;
170 = static_cast<sal_Unicode
>(ImplGetHighSurrogate(nCode
));
172 = static_cast<sal_Unicode
>(ImplGetLowSurrogate(nCode
));
178 ImplGb180302000ToUnicodeRange
const * pRange
180 sal_uInt32 nFirstNonRange
= 0;
183 if (pRange
->m_nNonRangeDataIndex
== -1)
185 else if (nCode
< pRange
->m_nFirstLinear
)
187 if (pDestBufPtr
!= pDestBufEnd
)
190 pRange
->m_nNonRangeDataIndex
191 + (nCode
- nFirstNonRange
)];
196 else if (nCode
< pRange
->m_nPastLinear
)
198 if (pDestBufPtr
!= pDestBufEnd
)
200 = static_cast<sal_Unicode
>(pRange
->m_nFirstUnicode
208 nFirstNonRange
= (pRange
++)->m_nPastLinear
;
211 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
223 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
224 bUndefined
, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
227 case sal::detail::textenc::BAD_INPUT_STOP
:
228 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
231 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
232 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
235 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
242 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
;
246 if (eState
!= IMPL_GB_18030_TO_UNICODE_STATE_0
247 && (nInfo
& (RTL_TEXTTOUNICODE_INFO_ERROR
248 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
))
251 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0)
252 nInfo
|= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL
;
254 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
255 false, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
258 case sal::detail::textenc::BAD_INPUT_STOP
:
259 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
260 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
263 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
264 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
;
271 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
= eState
;
272 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
= nCode
;
277 *pSrcCvtBytes
= nConverted
;
279 return pDestBufPtr
- pDestBuf
;
282 sal_Size
ImplConvertUnicodeToGb18030(void const * pData
,
284 sal_Unicode
const * pSrcBuf
,
290 sal_Size
* pSrcCvtChars
)
292 sal_uInt32
const * pGb18030Data
293 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
294 m_pUnicodeToGb18030Data
;
295 ImplUnicodeToGb180302000Range
const * pGb18030Ranges
296 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
297 m_pUnicodeToGb18030Ranges
;
298 sal_Unicode nHighSurrogate
= 0;
299 sal_uInt32 nInfo
= 0;
300 sal_Size nConverted
= 0;
301 char * pDestBufPtr
= pDestBuf
;
302 char * pDestBufEnd
= pDestBuf
+ nDestBytes
;
306 = static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
;
308 for (; nConverted
< nSrcChars
; ++nConverted
)
310 bool bUndefined
= true;
311 sal_uInt32 nChar
= *pSrcBuf
++;
312 if (nHighSurrogate
== 0)
314 if (ImplIsHighSurrogate(nChar
))
316 nHighSurrogate
= static_cast<sal_Unicode
>(nChar
);
320 else if (ImplIsLowSurrogate(nChar
))
321 nChar
= ImplCombineSurrogates(nHighSurrogate
, nChar
);
328 if (ImplIsLowSurrogate(nChar
) || ImplIsNoncharacter(nChar
))
335 if (pDestBufPtr
!= pDestBufEnd
)
336 *pDestBufPtr
++ = static_cast< char >(nChar
);
339 else if (nChar
< 0x10000)
341 ImplUnicodeToGb180302000Range
const * pRange
= pGb18030Ranges
;
342 sal_Unicode nFirstNonRange
= 0x80;
345 if (nChar
< pRange
->m_nFirstUnicode
)
348 = pGb18030Data
[pRange
->m_nNonRangeDataIndex
349 + (nChar
- nFirstNonRange
)];
350 if (pDestBufEnd
- pDestBufPtr
351 >= (nCode
<= 0xFFFF ? 2 : 4))
355 *pDestBufPtr
++ = static_cast< char >(nCode
>> 24);
356 *pDestBufPtr
++ = static_cast< char >(nCode
>> 16 & 0xFF);
358 *pDestBufPtr
++ = static_cast< char >(nCode
>> 8 & 0xFF);
359 *pDestBufPtr
++ = static_cast< char >(nCode
& 0xFF);
365 if (nChar
<= pRange
->m_nLastUnicode
)
367 if (pDestBufEnd
- pDestBufPtr
>= 4)
370 = pRange
->m_nFirstLinear
371 + (nChar
- pRange
->m_nFirstUnicode
);
372 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x81);
374 = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
375 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
376 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
383 = static_cast<sal_Unicode
>((pRange
++)->m_nLastUnicode
+ 1);
387 if (pDestBufEnd
- pDestBufPtr
>= 4)
389 sal_uInt32 nCode
= nChar
- 0x10000;
390 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x90);
391 *pDestBufPtr
++ = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
392 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
393 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
401 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
402 bUndefined
, nChar
, nFlags
, &pDestBufPtr
, pDestBufEnd
,
403 &nInfo
, nullptr, 0, nullptr))
405 case sal::detail::textenc::BAD_INPUT_STOP
:
409 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
413 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
420 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
424 if (nHighSurrogate
!= 0
425 && (nInfo
& (RTL_UNICODETOTEXT_INFO_ERROR
426 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
))
429 if ((nFlags
& RTL_UNICODETOTEXT_FLAGS_FLUSH
) != 0)
430 nInfo
|= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL
;
432 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
433 false, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
, &nInfo
,
434 nullptr, 0, nullptr))
436 case sal::detail::textenc::BAD_INPUT_STOP
:
437 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
441 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
442 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
448 static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
453 *pSrcCvtChars
= nConverted
;
455 return pDestBufPtr
- pDestBuf
;
458 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */