tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / textenc / convertgb18030.cxx
blobd7963762fa2be9750677da00a9b9d5e10fc50576
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 += rtl::splitSurrogates(nCode, pDestBufPtr);
176 startOfCurrentChar = nConverted + 1;
178 else
179 goto no_output;
180 else
182 ImplGb180302000ToUnicodeRange const * pRange
183 = pGb18030Ranges;
184 sal_uInt32 nFirstNonRange = 0;
185 for (;;)
187 if (pRange->m_nNonRangeDataIndex == -1)
188 goto bad_input;
189 else if (nCode < pRange->m_nFirstLinear)
191 if (pDestBufPtr != pDestBufEnd) {
192 *pDestBufPtr++
193 = pGb18030Data[
194 pRange->m_nNonRangeDataIndex
195 + (nCode - nFirstNonRange)];
196 startOfCurrentChar = nConverted + 1;
197 } else
198 goto no_output;
199 break;
201 else if (nCode < pRange->m_nPastLinear)
203 if (pDestBufPtr != pDestBufEnd) {
204 *pDestBufPtr++
205 = static_cast<sal_Unicode>(pRange->m_nFirstUnicode
206 + (nCode
207 - pRange->
208 m_nFirstLinear));
209 startOfCurrentChar = nConverted + 1;
210 } else
211 goto no_output;
212 break;
214 nFirstNonRange = (pRange++)->m_nPastLinear;
217 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
219 else
221 bUndefined = false;
222 goto bad_input;
224 break;
226 continue;
228 bad_input:
229 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
230 bUndefined, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
231 &nInfo))
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) {
236 ++nConverted;
237 } else {
238 nConverted = startOfCurrentChar;
240 break;
242 case sal::detail::textenc::BAD_INPUT_CONTINUE:
243 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
244 startOfCurrentChar = nConverted + 1;
245 continue;
247 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
248 goto no_output;
250 break;
252 no_output:
253 --pSrcBuf;
254 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
255 break;
258 if (eState != IMPL_GB_18030_TO_UNICODE_STATE_0
259 && (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
260 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
261 == 0)
263 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
264 nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL;
265 else
266 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
267 false, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
268 &nInfo))
270 case sal::detail::textenc::BAD_INPUT_STOP:
271 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) != 0) {
272 nConverted = startOfCurrentChar;
274 [[fallthrough]];
275 case sal::detail::textenc::BAD_INPUT_CONTINUE:
276 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
277 break;
279 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
280 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
281 break;
285 if (pContext)
287 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_eState = eState;
288 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_nCode = nCode;
290 if (pInfo)
291 *pInfo = nInfo;
292 if (pSrcCvtBytes)
293 *pSrcCvtBytes = nConverted;
295 return pDestBufPtr - pDestBuf;
298 sal_Size ImplConvertUnicodeToGb18030(void const * pData,
299 void * pContext,
300 sal_Unicode const * pSrcBuf,
301 sal_Size nSrcChars,
302 char * pDestBuf,
303 sal_Size nDestBytes,
304 sal_uInt32 nFlags,
305 sal_uInt32 * pInfo,
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;
320 if (pContext)
321 nHighSurrogate
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);
333 continue;
335 else if (rtl::isLowSurrogate(nChar))
337 bUndefined = false;
338 goto bad_input;
341 else if (rtl::isLowSurrogate(nChar))
342 nChar = rtl::combineSurrogates(nHighSurrogate, nChar);
343 else
345 bUndefined = false;
346 goto bad_input;
349 assert(rtl::isUnicodeScalarValue(nChar));
351 if (nChar < 0x80)
352 if (pDestBufPtr != pDestBufEnd)
353 *pDestBufPtr++ = static_cast< char >(nChar);
354 else
355 goto no_output;
356 else if (nChar < 0x10000)
358 ImplUnicodeToGb180302000Range const * pRange = pGb18030Ranges;
359 sal_Unicode nFirstNonRange = 0x80;
360 for (;;)
362 if (nChar < pRange->m_nFirstUnicode)
364 sal_uInt32 nCode
365 = pGb18030Data[pRange->m_nNonRangeDataIndex
366 + (nChar - nFirstNonRange)];
367 if (pDestBufEnd - pDestBufPtr
368 >= (nCode <= 0xFFFF ? 2 : 4))
370 if (nCode > 0xFFFF)
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);
378 else
379 goto no_output;
380 break;
382 if (nChar <= pRange->m_nLastUnicode)
384 if (pDestBufEnd - pDestBufPtr >= 4)
386 sal_uInt32 nCode
387 = pRange->m_nFirstLinear
388 + (nChar - pRange->m_nFirstUnicode);
389 *pDestBufPtr++ = static_cast< char >(nCode / 12600 + 0x81);
390 *pDestBufPtr++
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);
395 else
396 goto no_output;
397 break;
399 nFirstNonRange
400 = static_cast<sal_Unicode>((pRange++)->m_nLastUnicode + 1);
403 else
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);
412 else
413 goto no_output;
414 nHighSurrogate = 0;
415 continue;
417 bad_input:
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:
423 nHighSurrogate = 0;
424 break;
426 case sal::detail::textenc::BAD_INPUT_CONTINUE:
427 nHighSurrogate = 0;
428 continue;
430 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
431 goto no_output;
433 break;
435 no_output:
436 --pSrcBuf;
437 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
438 break;
441 if (nHighSurrogate != 0
442 && (nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
443 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
444 == 0)
446 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
447 nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
448 else
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:
455 nHighSurrogate = 0;
456 break;
458 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
459 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
460 break;
464 if (pContext)
465 static_cast<ImplUnicodeToTextContext *>(pContext)->m_nHighSurrogate
466 = nHighSurrogate;
467 if (pInfo)
468 *pInfo = nInfo;
469 if (pSrcCvtChars)
470 *pSrcCvtChars = nConverted;
472 return pDestBufPtr - pDestBuf;
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */