Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / textenc / convertgb18030.cxx
blob88f5a999d87da4f089e2db3f39907fd8dfab4aa0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <cassert>
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"
34 namespace {
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;
47 sal_uInt32 m_nCode;
52 void * ImplCreateGb18030ToUnicodeContext()
54 ImplGb18030ToUnicodeContext * pContext = new ImplGb18030ToUnicodeContext;
55 pContext->m_eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
56 return pContext;
59 void ImplResetGb18030ToUnicodeContext(void * pContext)
61 if (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,
72 void * pContext,
73 char const * pSrcBuf,
74 sal_Size nSrcBytes,
75 sal_Unicode * pDestBuf,
76 sal_Size nDestChars,
77 sal_uInt32 nFlags,
78 sal_uInt32 * pInfo,
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;
87 sal_uInt32 nCode = 0;
88 sal_uInt32 nInfo = 0;
89 sal_Size nConverted = 0;
90 sal_Unicode * pDestBufPtr = pDestBuf;
91 sal_Unicode * pDestBufEnd = pDestBuf + nDestChars;
92 sal_Size startOfCurrentChar = 0;
94 if (pContext)
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++);
104 switch (eState)
106 case IMPL_GB_18030_TO_UNICODE_STATE_0:
107 if (nChar < 0x80)
108 if (pDestBufPtr != pDestBufEnd) {
109 *pDestBufPtr++ = static_cast<sal_Unicode>(nChar);
110 startOfCurrentChar = nConverted + 1;
111 } else
112 goto no_output;
113 else if (nChar == 0x80)
114 goto bad_input;
115 else if (nChar <= 0xFE)
117 nCode = nChar - 0x81;
118 eState = IMPL_GB_18030_TO_UNICODE_STATE_1;
120 else
122 bUndefined = false;
123 goto bad_input;
125 break;
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 :
137 nChar - 0x80 + 63);
138 if (pDestBufPtr != pDestBufEnd) {
139 *pDestBufPtr++ = pGb18030Data[nCode];
140 startOfCurrentChar = nConverted + 1;
141 } else
142 goto no_output;
143 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
145 else
147 bUndefined = false;
148 goto bad_input;
150 break;
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;
158 else
160 bUndefined = false;
161 goto bad_input;
163 break;
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++
176 = static_cast<sal_Unicode>(ImplGetHighSurrogate(nCode));
177 *pDestBufPtr++
178 = static_cast<sal_Unicode>(ImplGetLowSurrogate(nCode));
179 startOfCurrentChar = nConverted + 1;
181 else
182 goto no_output;
183 else
185 ImplGb180302000ToUnicodeRange const * pRange
186 = pGb18030Ranges;
187 sal_uInt32 nFirstNonRange = 0;
188 for (;;)
190 if (pRange->m_nNonRangeDataIndex == -1)
191 goto bad_input;
192 else if (nCode < pRange->m_nFirstLinear)
194 if (pDestBufPtr != pDestBufEnd) {
195 *pDestBufPtr++
196 = pGb18030Data[
197 pRange->m_nNonRangeDataIndex
198 + (nCode - nFirstNonRange)];
199 startOfCurrentChar = nConverted + 1;
200 } else
201 goto no_output;
202 break;
204 else if (nCode < pRange->m_nPastLinear)
206 if (pDestBufPtr != pDestBufEnd) {
207 *pDestBufPtr++
208 = static_cast<sal_Unicode>(pRange->m_nFirstUnicode
209 + (nCode
210 - pRange->
211 m_nFirstLinear));
212 startOfCurrentChar = nConverted + 1;
213 } else
214 goto no_output;
215 break;
217 nFirstNonRange = (pRange++)->m_nPastLinear;
220 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
222 else
224 bUndefined = false;
225 goto bad_input;
227 break;
229 continue;
231 bad_input:
232 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
233 bUndefined, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
234 &nInfo))
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) {
239 ++nConverted;
240 } else {
241 nConverted = startOfCurrentChar;
243 break;
245 case sal::detail::textenc::BAD_INPUT_CONTINUE:
246 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
247 startOfCurrentChar = nConverted + 1;
248 continue;
250 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
251 goto no_output;
253 break;
255 no_output:
256 --pSrcBuf;
257 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
258 break;
261 if (eState != IMPL_GB_18030_TO_UNICODE_STATE_0
262 && (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
263 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
264 == 0)
266 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
267 nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL;
268 else
269 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
270 false, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
271 &nInfo))
273 case sal::detail::textenc::BAD_INPUT_STOP:
274 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) != 0) {
275 nConverted = startOfCurrentChar;
277 [[fallthrough]];
278 case sal::detail::textenc::BAD_INPUT_CONTINUE:
279 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
280 break;
282 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
283 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
284 break;
288 if (pContext)
290 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_eState = eState;
291 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_nCode = nCode;
293 if (pInfo)
294 *pInfo = nInfo;
295 if (pSrcCvtBytes)
296 *pSrcCvtBytes = nConverted;
298 return pDestBufPtr - pDestBuf;
301 sal_Size ImplConvertUnicodeToGb18030(void const * pData,
302 void * pContext,
303 sal_Unicode const * pSrcBuf,
304 sal_Size nSrcChars,
305 char * pDestBuf,
306 sal_Size nDestBytes,
307 sal_uInt32 nFlags,
308 sal_uInt32 * pInfo,
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;
323 if (pContext)
324 nHighSurrogate
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);
336 continue;
338 else if (ImplIsLowSurrogate(nChar))
340 bUndefined = false;
341 goto bad_input;
344 else if (ImplIsLowSurrogate(nChar))
345 nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
346 else
348 bUndefined = false;
349 goto bad_input;
352 assert(rtl::isUnicodeScalarValue(nChar));
354 if (nChar < 0x80)
355 if (pDestBufPtr != pDestBufEnd)
356 *pDestBufPtr++ = static_cast< char >(nChar);
357 else
358 goto no_output;
359 else if (nChar < 0x10000)
361 ImplUnicodeToGb180302000Range const * pRange = pGb18030Ranges;
362 sal_Unicode nFirstNonRange = 0x80;
363 for (;;)
365 if (nChar < pRange->m_nFirstUnicode)
367 sal_uInt32 nCode
368 = pGb18030Data[pRange->m_nNonRangeDataIndex
369 + (nChar - nFirstNonRange)];
370 if (pDestBufEnd - pDestBufPtr
371 >= (nCode <= 0xFFFF ? 2 : 4))
373 if (nCode > 0xFFFF)
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);
381 else
382 goto no_output;
383 break;
385 if (nChar <= pRange->m_nLastUnicode)
387 if (pDestBufEnd - pDestBufPtr >= 4)
389 sal_uInt32 nCode
390 = pRange->m_nFirstLinear
391 + (nChar - pRange->m_nFirstUnicode);
392 *pDestBufPtr++ = static_cast< char >(nCode / 12600 + 0x81);
393 *pDestBufPtr++
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);
398 else
399 goto no_output;
400 break;
402 nFirstNonRange
403 = static_cast<sal_Unicode>((pRange++)->m_nLastUnicode + 1);
406 else
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);
415 else
416 goto no_output;
417 nHighSurrogate = 0;
418 continue;
420 bad_input:
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:
426 nHighSurrogate = 0;
427 break;
429 case sal::detail::textenc::BAD_INPUT_CONTINUE:
430 nHighSurrogate = 0;
431 continue;
433 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
434 goto no_output;
436 break;
438 no_output:
439 --pSrcBuf;
440 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
441 break;
444 if (nHighSurrogate != 0
445 && (nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
446 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
447 == 0)
449 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
450 nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
451 else
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:
458 nHighSurrogate = 0;
459 break;
461 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
462 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
463 break;
467 if (pContext)
468 static_cast<ImplUnicodeToTextContext *>(pContext)->m_nHighSurrogate
469 = nHighSurrogate;
470 if (pInfo)
471 *pInfo = nInfo;
472 if (pSrcCvtChars)
473 *pSrcCvtChars = nConverted;
475 return pDestBufPtr - pDestBuf;
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */