Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / textenc / convertgb18030.cxx
blobb38cc231d653865911f5d35ab2406780d27b73ca
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"
32 namespace {
34 enum ImplGb18030ToUnicodeState
36 IMPL_GB_18030_TO_UNICODE_STATE_0,
37 IMPL_GB_18030_TO_UNICODE_STATE_1,
38 IMPL_GB_18030_TO_UNICODE_STATE_2,
39 IMPL_GB_18030_TO_UNICODE_STATE_3
42 struct ImplGb18030ToUnicodeContext
44 ImplGb18030ToUnicodeState m_eState;
45 sal_uInt32 m_nCode;
50 void * ImplCreateGb18030ToUnicodeContext()
52 ImplGb18030ToUnicodeContext * pContext = new ImplGb18030ToUnicodeContext;
53 pContext->m_eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
54 return pContext;
57 void ImplResetGb18030ToUnicodeContext(void * pContext)
59 if (pContext)
60 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_eState
61 = IMPL_GB_18030_TO_UNICODE_STATE_0;
64 void ImplDestroyGb18030ToUnicodeContext(void * pContext)
66 delete static_cast< ImplGb18030ToUnicodeContext * >(pContext);
69 sal_Size ImplConvertGb18030ToUnicode(void const * pData,
70 void * pContext,
71 char const * pSrcBuf,
72 sal_Size nSrcBytes,
73 sal_Unicode * pDestBuf,
74 sal_Size nDestChars,
75 sal_uInt32 nFlags,
76 sal_uInt32 * pInfo,
77 sal_Size * pSrcCvtBytes)
79 sal_Unicode const * pGb18030Data
80 = static_cast< ImplGb18030ConverterData const * >(pData)->m_pGb18030ToUnicodeData;
81 ImplGb180302000ToUnicodeRange const * pGb18030Ranges
82 = static_cast< ImplGb18030ConverterData const * >(pData)->
83 m_pGb18030ToUnicodeRanges;
84 ImplGb18030ToUnicodeState eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
85 sal_uInt32 nCode = 0;
86 sal_uInt32 nInfo = 0;
87 sal_Size nConverted = 0;
88 sal_Unicode * pDestBufPtr = pDestBuf;
89 sal_Unicode * pDestBufEnd = pDestBuf + nDestChars;
90 sal_Size startOfCurrentChar = 0;
92 if (pContext)
94 eState = static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_eState;
95 nCode = static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_nCode;
98 for (; nConverted < nSrcBytes; ++nConverted)
100 bool bUndefined = true;
101 sal_uInt32 nChar = *reinterpret_cast<unsigned char const *>(pSrcBuf++);
102 switch (eState)
104 case IMPL_GB_18030_TO_UNICODE_STATE_0:
105 if (nChar < 0x80)
106 if (pDestBufPtr != pDestBufEnd) {
107 *pDestBufPtr++ = static_cast<sal_Unicode>(nChar);
108 startOfCurrentChar = nConverted + 1;
109 } else
110 goto no_output;
111 else if (nChar == 0x80)
112 goto bad_input;
113 else if (nChar <= 0xFE)
115 nCode = nChar - 0x81;
116 eState = IMPL_GB_18030_TO_UNICODE_STATE_1;
118 else
120 bUndefined = false;
121 goto bad_input;
123 break;
125 case IMPL_GB_18030_TO_UNICODE_STATE_1:
126 if (nChar >= 0x30 && nChar <= 0x39)
128 nCode = nCode * 10 + (nChar - 0x30);
129 eState = IMPL_GB_18030_TO_UNICODE_STATE_2;
131 else if ((nChar >= 0x40 && nChar <= 0x7E)
132 || (nChar >= 0x80 && nChar <= 0xFE))
134 nCode = nCode * 190 + (nChar <= 0x7E ? nChar - 0x40 :
135 nChar - 0x80 + 63);
136 if (pDestBufPtr != pDestBufEnd) {
137 *pDestBufPtr++ = pGb18030Data[nCode];
138 startOfCurrentChar = nConverted + 1;
139 } else
140 goto no_output;
141 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
143 else
145 bUndefined = false;
146 goto bad_input;
148 break;
150 case IMPL_GB_18030_TO_UNICODE_STATE_2:
151 if (nChar >= 0x81 && nChar <= 0xFE)
153 nCode = nCode * 126 + (nChar - 0x81);
154 eState = IMPL_GB_18030_TO_UNICODE_STATE_3;
156 else
158 bUndefined = false;
159 goto bad_input;
161 break;
163 case IMPL_GB_18030_TO_UNICODE_STATE_3:
164 if (nChar >= 0x30 && nChar <= 0x39)
166 nCode = nCode * 10 + (nChar - 0x30);
168 // 90 30 81 30 to E3 32 9A 35 maps to U+10000 to U+10FFFF:
169 if (nCode >= 189000 && nCode <= 1237575)
170 if (pDestBufEnd - pDestBufPtr >= 2)
172 nCode -= 189000 - 0x10000;
173 pDestBufPtr += rtl::splitSurrogates(nCode, pDestBufPtr);
174 startOfCurrentChar = nConverted + 1;
176 else
177 goto no_output;
178 else
180 ImplGb180302000ToUnicodeRange const * pRange
181 = pGb18030Ranges;
182 sal_uInt32 nFirstNonRange = 0;
183 for (;;)
185 if (pRange->m_nNonRangeDataIndex == -1)
186 goto bad_input;
187 else if (nCode < pRange->m_nFirstLinear)
189 if (pDestBufPtr != pDestBufEnd) {
190 *pDestBufPtr++
191 = pGb18030Data[
192 pRange->m_nNonRangeDataIndex
193 + (nCode - nFirstNonRange)];
194 startOfCurrentChar = nConverted + 1;
195 } else
196 goto no_output;
197 break;
199 else if (nCode < pRange->m_nPastLinear)
201 if (pDestBufPtr != pDestBufEnd) {
202 *pDestBufPtr++
203 = static_cast<sal_Unicode>(pRange->m_nFirstUnicode
204 + (nCode
205 - pRange->
206 m_nFirstLinear));
207 startOfCurrentChar = nConverted + 1;
208 } else
209 goto no_output;
210 break;
212 nFirstNonRange = (pRange++)->m_nPastLinear;
215 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
217 else
219 bUndefined = false;
220 goto bad_input;
222 break;
224 continue;
226 bad_input:
227 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
228 bUndefined, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
229 &nInfo))
231 case sal::detail::textenc::BAD_INPUT_STOP:
232 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
233 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0) {
234 ++nConverted;
235 } else {
236 nConverted = startOfCurrentChar;
238 break;
240 case sal::detail::textenc::BAD_INPUT_CONTINUE:
241 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
242 startOfCurrentChar = nConverted + 1;
243 continue;
245 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
246 goto no_output;
248 break;
250 no_output:
251 --pSrcBuf;
252 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
253 break;
256 if (eState != IMPL_GB_18030_TO_UNICODE_STATE_0
257 && (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
258 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
259 == 0)
261 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
262 nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL;
263 else
264 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
265 false, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
266 &nInfo))
268 case sal::detail::textenc::BAD_INPUT_STOP:
269 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) != 0) {
270 nConverted = startOfCurrentChar;
272 [[fallthrough]];
273 case sal::detail::textenc::BAD_INPUT_CONTINUE:
274 eState = IMPL_GB_18030_TO_UNICODE_STATE_0;
275 break;
277 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
278 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
279 break;
283 if (pContext)
285 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_eState = eState;
286 static_cast< ImplGb18030ToUnicodeContext * >(pContext)->m_nCode = nCode;
288 if (pInfo)
289 *pInfo = nInfo;
290 if (pSrcCvtBytes)
291 *pSrcCvtBytes = nConverted;
293 return pDestBufPtr - pDestBuf;
296 sal_Size ImplConvertUnicodeToGb18030(void const * pData,
297 void * pContext,
298 sal_Unicode const * pSrcBuf,
299 sal_Size nSrcChars,
300 char * pDestBuf,
301 sal_Size nDestBytes,
302 sal_uInt32 nFlags,
303 sal_uInt32 * pInfo,
304 sal_Size * pSrcCvtChars)
306 sal_uInt32 const * pGb18030Data
307 = static_cast< ImplGb18030ConverterData const * >(pData)->
308 m_pUnicodeToGb18030Data;
309 ImplUnicodeToGb180302000Range const * pGb18030Ranges
310 = static_cast< ImplGb18030ConverterData const * >(pData)->
311 m_pUnicodeToGb18030Ranges;
312 sal_Unicode nHighSurrogate = 0;
313 sal_uInt32 nInfo = 0;
314 sal_Size nConverted = 0;
315 char * pDestBufPtr = pDestBuf;
316 char * pDestBufEnd = pDestBuf + nDestBytes;
318 if (pContext)
319 nHighSurrogate
320 = static_cast<ImplUnicodeToTextContext *>(pContext)->m_nHighSurrogate;
322 for (; nConverted < nSrcChars; ++nConverted)
324 bool bUndefined = true;
325 sal_uInt32 nChar = *pSrcBuf++;
326 if (nHighSurrogate == 0)
328 if (rtl::isHighSurrogate(nChar))
330 nHighSurrogate = static_cast<sal_Unicode>(nChar);
331 continue;
333 else if (rtl::isLowSurrogate(nChar))
335 bUndefined = false;
336 goto bad_input;
339 else if (rtl::isLowSurrogate(nChar))
340 nChar = rtl::combineSurrogates(nHighSurrogate, nChar);
341 else
343 bUndefined = false;
344 goto bad_input;
347 assert(rtl::isUnicodeScalarValue(nChar));
349 if (nChar < 0x80)
350 if (pDestBufPtr != pDestBufEnd)
351 *pDestBufPtr++ = static_cast< char >(nChar);
352 else
353 goto no_output;
354 else if (nChar < 0x10000)
356 ImplUnicodeToGb180302000Range const * pRange = pGb18030Ranges;
357 sal_Unicode nFirstNonRange = 0x80;
358 for (;;)
360 if (nChar < pRange->m_nFirstUnicode)
362 sal_uInt32 nCode
363 = pGb18030Data[pRange->m_nNonRangeDataIndex
364 + (nChar - nFirstNonRange)];
365 if (pDestBufEnd - pDestBufPtr
366 >= (nCode <= 0xFFFF ? 2 : 4))
368 if (nCode > 0xFFFF)
370 *pDestBufPtr++ = static_cast< char >(nCode >> 24);
371 *pDestBufPtr++ = static_cast< char >(nCode >> 16 & 0xFF);
373 *pDestBufPtr++ = static_cast< char >(nCode >> 8 & 0xFF);
374 *pDestBufPtr++ = static_cast< char >(nCode & 0xFF);
376 else
377 goto no_output;
378 break;
380 if (nChar <= pRange->m_nLastUnicode)
382 if (pDestBufEnd - pDestBufPtr >= 4)
384 sal_uInt32 nCode
385 = pRange->m_nFirstLinear
386 + (nChar - pRange->m_nFirstUnicode);
387 *pDestBufPtr++ = static_cast< char >(nCode / 12600 + 0x81);
388 *pDestBufPtr++
389 = static_cast< char >(nCode / 1260 % 10 + 0x30);
390 *pDestBufPtr++ = static_cast< char >(nCode / 10 % 126 + 0x81);
391 *pDestBufPtr++ = static_cast< char >(nCode % 10 + 0x30);
393 else
394 goto no_output;
395 break;
397 nFirstNonRange
398 = static_cast<sal_Unicode>((pRange++)->m_nLastUnicode + 1);
401 else
402 if (pDestBufEnd - pDestBufPtr >= 4)
404 sal_uInt32 nCode = nChar - 0x10000;
405 *pDestBufPtr++ = static_cast< char >(nCode / 12600 + 0x90);
406 *pDestBufPtr++ = static_cast< char >(nCode / 1260 % 10 + 0x30);
407 *pDestBufPtr++ = static_cast< char >(nCode / 10 % 126 + 0x81);
408 *pDestBufPtr++ = static_cast< char >(nCode % 10 + 0x30);
410 else
411 goto no_output;
412 nHighSurrogate = 0;
413 continue;
415 bad_input:
416 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
417 bUndefined, nChar, nFlags, &pDestBufPtr, pDestBufEnd,
418 &nInfo, nullptr, 0, nullptr))
420 case sal::detail::textenc::BAD_INPUT_STOP:
421 nHighSurrogate = 0;
422 break;
424 case sal::detail::textenc::BAD_INPUT_CONTINUE:
425 nHighSurrogate = 0;
426 continue;
428 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
429 goto no_output;
431 break;
433 no_output:
434 --pSrcBuf;
435 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
436 break;
439 if (nHighSurrogate != 0
440 && (nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
441 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
442 == 0)
444 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
445 nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
446 else
447 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
448 false, 0, nFlags, &pDestBufPtr, pDestBufEnd, &nInfo,
449 nullptr, 0, nullptr))
451 case sal::detail::textenc::BAD_INPUT_STOP:
452 case sal::detail::textenc::BAD_INPUT_CONTINUE:
453 nHighSurrogate = 0;
454 break;
456 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
457 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
458 break;
462 if (pContext)
463 static_cast<ImplUnicodeToTextContext *>(pContext)->m_nHighSurrogate
464 = nHighSurrogate;
465 if (pInfo)
466 *pInfo = nInfo;
467 if (pSrcCvtChars)
468 *pSrcCvtChars = nConverted;
470 return pDestBufPtr - pDestBuf;
473 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */