1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
31 #include "rtl/textcvt.h"
32 #include "sal/types.h"
34 #include "context.hxx"
35 #include "converter.hxx"
36 #include "convertgb18030.hxx"
37 #include "tenchelp.hxx"
38 #include "unichars.hxx"
42 enum ImplGb18030ToUnicodeState
44 IMPL_GB_18030_TO_UNICODE_STATE_0
,
45 IMPL_GB_18030_TO_UNICODE_STATE_1
,
46 IMPL_GB_18030_TO_UNICODE_STATE_2
,
47 IMPL_GB_18030_TO_UNICODE_STATE_3
50 struct ImplGb18030ToUnicodeContext
52 ImplGb18030ToUnicodeState m_eState
;
58 void * ImplCreateGb18030ToUnicodeContext()
60 ImplGb18030ToUnicodeContext
* pContext
= new ImplGb18030ToUnicodeContext
;
61 pContext
->m_eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
65 void ImplResetGb18030ToUnicodeContext(void * pContext
)
68 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
69 = IMPL_GB_18030_TO_UNICODE_STATE_0
;
72 void ImplDestroyGb18030ToUnicodeContext(void * pContext
)
74 delete static_cast< ImplGb18030ToUnicodeContext
* >(pContext
);
77 sal_Size
ImplConvertGb18030ToUnicode(void const * pData
,
81 sal_Unicode
* pDestBuf
,
85 sal_Size
* pSrcCvtBytes
)
87 sal_Unicode
const * pGb18030Data
88 = static_cast< ImplGb18030ConverterData
const * >(pData
)->m_pGb18030ToUnicodeData
;
89 ImplGb180302000ToUnicodeRange
const * pGb18030Ranges
90 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
91 m_pGb18030ToUnicodeRanges
;
92 ImplGb18030ToUnicodeState eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
95 sal_Size nConverted
= 0;
96 sal_Unicode
* pDestBufPtr
= pDestBuf
;
97 sal_Unicode
* pDestBufEnd
= pDestBuf
+ nDestChars
;
101 eState
= static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
;
102 nCode
= static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
;
105 for (; nConverted
< nSrcBytes
; ++nConverted
)
107 bool bUndefined
= true;
108 sal_uInt32 nChar
= *(sal_uChar
const *) pSrcBuf
++;
111 case IMPL_GB_18030_TO_UNICODE_STATE_0
:
113 if (pDestBufPtr
!= pDestBufEnd
)
114 *pDestBufPtr
++ = (sal_Unicode
) nChar
;
117 else if (nChar
== 0x80)
119 else if (nChar
<= 0xFE)
121 nCode
= nChar
- 0x81;
122 eState
= IMPL_GB_18030_TO_UNICODE_STATE_1
;
131 case IMPL_GB_18030_TO_UNICODE_STATE_1
:
132 if (nChar
>= 0x30 && nChar
<= 0x39)
134 nCode
= nCode
* 10 + (nChar
- 0x30);
135 eState
= IMPL_GB_18030_TO_UNICODE_STATE_2
;
137 else if ((nChar
>= 0x40 && nChar
<= 0x7E)
138 || (nChar
>= 0x80 && nChar
<= 0xFE))
140 nCode
= nCode
* 190 + (nChar
<= 0x7E ? nChar
- 0x40 :
142 if (pDestBufPtr
!= pDestBufEnd
)
143 *pDestBufPtr
++ = pGb18030Data
[nCode
];
146 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
155 case IMPL_GB_18030_TO_UNICODE_STATE_2
:
156 if (nChar
>= 0x81 && nChar
<= 0xFE)
158 nCode
= nCode
* 126 + (nChar
- 0x81);
159 eState
= IMPL_GB_18030_TO_UNICODE_STATE_3
;
168 case IMPL_GB_18030_TO_UNICODE_STATE_3
:
169 if (nChar
>= 0x30 && nChar
<= 0x39)
171 nCode
= nCode
* 10 + (nChar
- 0x30);
173 // 90 30 81 30 to E3 32 9A 35 maps to U+10000 to U+10FFFF:
174 if (nCode
>= 189000 && nCode
<= 1237575)
175 if (pDestBufEnd
- pDestBufPtr
>= 2)
177 nCode
-= 189000 - 0x10000;
179 = (sal_Unicode
) ImplGetHighSurrogate(nCode
);
181 = (sal_Unicode
) ImplGetLowSurrogate(nCode
);
187 ImplGb180302000ToUnicodeRange
const * pRange
189 sal_uInt32 nFirstNonRange
= 0;
192 if (pRange
->m_nNonRangeDataIndex
== -1)
194 else if (nCode
< pRange
->m_nFirstLinear
)
196 if (pDestBufPtr
!= pDestBufEnd
)
199 pRange
->m_nNonRangeDataIndex
200 + (nCode
- nFirstNonRange
)];
205 else if (nCode
< pRange
->m_nPastLinear
)
207 if (pDestBufPtr
!= pDestBufEnd
)
210 (pRange
->m_nFirstUnicode
218 nFirstNonRange
= (pRange
++)->m_nPastLinear
;
221 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
233 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
234 bUndefined
, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
237 case sal::detail::textenc::BAD_INPUT_STOP
:
238 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
241 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
242 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
245 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
252 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
;
256 if (eState
!= IMPL_GB_18030_TO_UNICODE_STATE_0
257 && (nInfo
& (RTL_TEXTTOUNICODE_INFO_ERROR
258 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
))
261 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0)
262 nInfo
|= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOSMALL
;
264 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
265 false, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
268 case sal::detail::textenc::BAD_INPUT_STOP
:
269 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
270 eState
= IMPL_GB_18030_TO_UNICODE_STATE_0
;
273 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
274 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
;
281 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_eState
= eState
;
282 static_cast< ImplGb18030ToUnicodeContext
* >(pContext
)->m_nCode
= nCode
;
287 *pSrcCvtBytes
= nConverted
;
289 return pDestBufPtr
- pDestBuf
;
292 sal_Size
ImplConvertUnicodeToGb18030(void const * pData
,
294 sal_Unicode
const * pSrcBuf
,
300 sal_Size
* pSrcCvtChars
)
302 sal_uInt32
const * pGb18030Data
303 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
304 m_pUnicodeToGb18030Data
;
305 ImplUnicodeToGb180302000Range
const * pGb18030Ranges
306 = static_cast< ImplGb18030ConverterData
const * >(pData
)->
307 m_pUnicodeToGb18030Ranges
;
308 sal_Unicode nHighSurrogate
= 0;
309 sal_uInt32 nInfo
= 0;
310 sal_Size nConverted
= 0;
311 char * pDestBufPtr
= pDestBuf
;
312 char * pDestBufEnd
= pDestBuf
+ nDestBytes
;
316 = ((ImplUnicodeToTextContext
*) pContext
)->m_nHighSurrogate
;
318 for (; nConverted
< nSrcChars
; ++nConverted
)
320 bool bUndefined
= true;
321 sal_uInt32 nChar
= *pSrcBuf
++;
322 if (nHighSurrogate
== 0)
324 if (ImplIsHighSurrogate(nChar
))
326 nHighSurrogate
= (sal_Unicode
) nChar
;
330 else if (ImplIsLowSurrogate(nChar
))
331 nChar
= ImplCombineSurrogates(nHighSurrogate
, nChar
);
338 if (ImplIsLowSurrogate(nChar
) || ImplIsNoncharacter(nChar
))
345 if (pDestBufPtr
!= pDestBufEnd
)
346 *pDestBufPtr
++ = static_cast< char >(nChar
);
349 else if (nChar
< 0x10000)
351 ImplUnicodeToGb180302000Range
const * pRange
= pGb18030Ranges
;
352 sal_Unicode nFirstNonRange
= 0x80;
355 if (nChar
< pRange
->m_nFirstUnicode
)
358 = pGb18030Data
[pRange
->m_nNonRangeDataIndex
359 + (nChar
- nFirstNonRange
)];
360 if (pDestBufEnd
- pDestBufPtr
361 >= (nCode
<= 0xFFFF ? 2 : 4))
365 *pDestBufPtr
++ = static_cast< char >(nCode
>> 24);
366 *pDestBufPtr
++ = static_cast< char >(nCode
>> 16 & 0xFF);
368 *pDestBufPtr
++ = static_cast< char >(nCode
>> 8 & 0xFF);
369 *pDestBufPtr
++ = static_cast< char >(nCode
& 0xFF);
375 else if (nChar
<= pRange
->m_nLastUnicode
)
377 if (pDestBufEnd
- pDestBufPtr
>= 4)
380 = pRange
->m_nFirstLinear
381 + (nChar
- pRange
->m_nFirstUnicode
);
382 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x81);
384 = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
385 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
386 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
393 = (sal_Unicode
) ((pRange
++)->m_nLastUnicode
+ 1);
397 if (pDestBufEnd
- pDestBufPtr
>= 4)
399 sal_uInt32 nCode
= nChar
- 0x10000;
400 *pDestBufPtr
++ = static_cast< char >(nCode
/ 12600 + 0x90);
401 *pDestBufPtr
++ = static_cast< char >(nCode
/ 1260 % 10 + 0x30);
402 *pDestBufPtr
++ = static_cast< char >(nCode
/ 10 % 126 + 0x81);
403 *pDestBufPtr
++ = static_cast< char >(nCode
% 10 + 0x30);
411 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
412 bUndefined
, nChar
, nFlags
, &pDestBufPtr
, pDestBufEnd
,
413 &nInfo
, NULL
, 0, NULL
))
415 case sal::detail::textenc::BAD_INPUT_STOP
:
419 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
423 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
430 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
434 if (nHighSurrogate
!= 0
435 && (nInfo
& (RTL_UNICODETOTEXT_INFO_ERROR
436 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
))
439 if ((nFlags
& RTL_UNICODETOTEXT_FLAGS_FLUSH
) != 0)
440 nInfo
|= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL
;
442 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
443 false, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
, &nInfo
,
446 case sal::detail::textenc::BAD_INPUT_STOP
:
447 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
451 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
452 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
458 ((ImplUnicodeToTextContext
*) pContext
)->m_nHighSurrogate
463 *pSrcCvtChars
= nConverted
;
465 return pDestBufPtr
- pDestBuf
;
468 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */