Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / textenc / convertiso2022kr.cxx
bloba829bd0de202ed63c596c3a6b434bf448b5bd057
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 "converter.hxx"
29 #include "convertiso2022kr.hxx"
30 #include "tenchelp.hxx"
32 namespace {
34 enum ImplIso2022KrToUnicodeState // order is important:
36 IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII,
37 IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001,
38 IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001_2,
39 IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC,
40 IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC_DOLLAR,
41 IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC_DOLLAR_RPAREN
44 struct ImplIso2022KrToUnicodeContext
46 ImplIso2022KrToUnicodeState m_eState;
47 sal_uInt32 m_nRow;
50 enum ImplUnicodeToIso2022KrSet
52 IMPL_UNICODE_TO_ISO_2022_KR_SET_NONE,
53 IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII,
54 IMPL_UNICODE_TO_ISO_2022_KR_SET_1001
57 struct ImplUnicodeToIso2022KrContext
59 sal_Unicode m_nHighSurrogate;
60 ImplUnicodeToIso2022KrSet m_eSet;
65 void * ImplCreateIso2022KrToUnicodeContext()
67 ImplIso2022KrToUnicodeContext * pContext =
68 new ImplIso2022KrToUnicodeContext;
69 pContext->m_eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
70 return pContext;
73 void ImplResetIso2022KrToUnicodeContext(void * pContext)
75 if (pContext)
76 static_cast< ImplIso2022KrToUnicodeContext * >(pContext)->m_eState
77 = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
80 void ImplDestroyIso2022KrToUnicodeContext(void * pContext)
82 delete static_cast< ImplIso2022KrToUnicodeContext * >(pContext);
85 sal_Size ImplConvertIso2022KrToUnicode(void const * pData,
86 void * pContext,
87 char const * pSrcBuf,
88 sal_Size nSrcBytes,
89 sal_Unicode * pDestBuf,
90 sal_Size nDestChars,
91 sal_uInt32 nFlags,
92 sal_uInt32 * pInfo,
93 sal_Size * pSrcCvtBytes)
95 ImplDBCSToUniLeadTab const * pKsX1001Data
96 = static_cast< ImplIso2022KrConverterData const * >(pData)->
97 m_pKsX1001ToUnicodeData;
98 ImplIso2022KrToUnicodeState eState
99 = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
100 sal_uInt32 nRow = 0;
101 sal_uInt32 nInfo = 0;
102 sal_Size nConverted = 0;
103 sal_Unicode * pDestBufPtr = pDestBuf;
104 sal_Unicode * pDestBufEnd = pDestBuf + nDestChars;
105 sal_Size startOfCurrentChar = 0;
107 if (pContext)
109 eState = static_cast< ImplIso2022KrToUnicodeContext * >(pContext)->m_eState;
110 nRow = static_cast< ImplIso2022KrToUnicodeContext * >(pContext)->m_nRow;
113 for (; nConverted < nSrcBytes; ++nConverted)
115 bool bUndefined = true;
116 sal_uInt32 nChar = *reinterpret_cast<unsigned char const *>(pSrcBuf++);
117 switch (eState)
119 case IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII:
120 if (nChar == 0x0E) // SO
121 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001;
122 else if (nChar == 0x1B) // ESC
123 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC;
124 else if (nChar < 0x80)
125 if (pDestBufPtr != pDestBufEnd) {
126 *pDestBufPtr++ = static_cast<sal_Unicode>(nChar);
127 startOfCurrentChar = nConverted + 1;
128 } else
129 goto no_output;
130 else
132 bUndefined = false;
133 goto bad_input;
135 break;
137 case IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001:
138 if (nChar == 0x0F) // SI
139 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
140 else if (nChar >= 0x21 && nChar <= 0x7E)
142 nRow = nChar + 0x80;
143 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001_2;
145 else
147 bUndefined = false;
148 goto bad_input;
150 break;
152 case IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001_2:
153 if (nChar >= 0x21 && nChar <= 0x7E)
155 sal_uInt16 nUnicode = 0;
156 sal_uInt32 nFirst = pKsX1001Data[nRow].mnTrailStart;
157 nChar += 0x80;
158 if (nChar >= nFirst && nChar <= pKsX1001Data[nRow].mnTrailEnd)
159 nUnicode = pKsX1001Data[nRow].
160 mpToUniTrailTab[nChar - nFirst];
161 if (nUnicode != 0)
162 if (pDestBufPtr != pDestBufEnd)
164 *pDestBufPtr++ = static_cast<sal_Unicode>(nUnicode);
165 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001;
166 startOfCurrentChar = nConverted + 1;
168 else
169 goto no_output;
170 else
171 goto bad_input;
173 else
175 bUndefined = false;
176 goto bad_input;
178 break;
180 case IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC:
181 if (nChar == 0x24) // $
182 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC_DOLLAR;
183 else
185 bUndefined = false;
186 goto bad_input;
188 break;
190 case IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC_DOLLAR:
191 if (nChar == 0x29) // )
192 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC_DOLLAR_RPAREN;
193 else
195 bUndefined = false;
196 goto bad_input;
198 break;
200 case IMPL_ISO_2022_KR_TO_UNICODE_STATE_ESC_DOLLAR_RPAREN:
201 if (nChar == 0x43) // C
202 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
203 else
205 bUndefined = false;
206 goto bad_input;
208 break;
210 continue;
212 bad_input:
213 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
214 bUndefined, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
215 &nInfo))
217 case sal::detail::textenc::BAD_INPUT_STOP:
218 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
219 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0) {
220 ++nConverted;
221 } else {
222 nConverted = startOfCurrentChar;
224 break;
226 case sal::detail::textenc::BAD_INPUT_CONTINUE:
227 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
228 startOfCurrentChar = nConverted + 1;
229 continue;
231 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
232 goto no_output;
234 break;
236 no_output:
237 --pSrcBuf;
238 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
239 break;
242 if (eState > IMPL_ISO_2022_KR_TO_UNICODE_STATE_1001
243 && (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
244 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
245 == 0)
247 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
248 nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL;
249 else
250 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
251 false, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
252 &nInfo))
254 case sal::detail::textenc::BAD_INPUT_STOP:
255 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) != 0) {
256 nConverted = startOfCurrentChar;
258 [[fallthrough]];
259 case sal::detail::textenc::BAD_INPUT_CONTINUE:
260 eState = IMPL_ISO_2022_KR_TO_UNICODE_STATE_ASCII;
261 break;
263 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
264 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
265 break;
269 if (pContext)
271 static_cast< ImplIso2022KrToUnicodeContext * >(pContext)->m_eState = eState;
272 static_cast< ImplIso2022KrToUnicodeContext * >(pContext)->m_nRow = nRow;
274 if (pInfo)
275 *pInfo = nInfo;
276 if (pSrcCvtBytes)
277 *pSrcCvtBytes = nConverted;
279 return pDestBufPtr - pDestBuf;
282 void * ImplCreateUnicodeToIso2022KrContext()
284 ImplUnicodeToIso2022KrContext * pContext =
285 new ImplUnicodeToIso2022KrContext;
286 pContext->m_nHighSurrogate = 0;
287 pContext->m_eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_NONE;
288 return pContext;
291 void ImplResetUnicodeToIso2022KrContext(void * pContext)
293 if (pContext)
295 static_cast< ImplUnicodeToIso2022KrContext * >(pContext)->m_nHighSurrogate = 0;
296 static_cast< ImplUnicodeToIso2022KrContext * >(pContext)->m_eSet
297 = IMPL_UNICODE_TO_ISO_2022_KR_SET_NONE;
301 void ImplDestroyUnicodeToIso2022KrContext(void * pContext)
303 delete static_cast< ImplUnicodeToIso2022KrContext * >(pContext);
306 sal_Size ImplConvertUnicodeToIso2022Kr(void const * pData,
307 void * pContext,
308 sal_Unicode const * pSrcBuf,
309 sal_Size nSrcChars,
310 char * pDestBuf,
311 sal_Size nDestBytes,
312 sal_uInt32 nFlags,
313 sal_uInt32 * pInfo,
314 sal_Size * pSrcCvtChars)
316 ImplUniToDBCSHighTab const * pKsX1001Data
317 = static_cast< ImplIso2022KrConverterData const * >(pData)->
318 m_pUnicodeToKsX1001Data;
319 sal_Unicode nHighSurrogate = 0;
320 ImplUnicodeToIso2022KrSet eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_NONE;
321 sal_uInt32 nInfo = 0;
322 sal_Size nConverted = 0;
323 char * pDestBufPtr = pDestBuf;
324 char * pDestBufEnd = pDestBuf + nDestBytes;
325 bool bWritten;
327 if (pContext)
329 nHighSurrogate
330 = static_cast< ImplUnicodeToIso2022KrContext * >(pContext)->m_nHighSurrogate;
331 eSet = static_cast< ImplUnicodeToIso2022KrContext * >(pContext)->m_eSet;
334 if (eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_NONE)
336 if (pDestBufEnd - pDestBufPtr >= 4)
338 *pDestBufPtr++ = 0x1B; // ESC
339 *pDestBufPtr++ = 0x24; // $
340 *pDestBufPtr++ = 0x29; // )
341 *pDestBufPtr++ = 0x43; // C
342 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII;
344 else
345 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
348 if ((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0)
349 for (; nConverted < nSrcChars; ++nConverted)
351 bool bUndefined = true;
352 sal_uInt32 nChar = *pSrcBuf++;
353 if (nHighSurrogate == 0)
355 if (rtl::isHighSurrogate(nChar))
357 nHighSurrogate = static_cast<sal_Unicode>(nChar);
358 continue;
360 else if (rtl::isLowSurrogate(nChar))
362 bUndefined = false;
363 goto bad_input;
366 else if (rtl::isLowSurrogate(nChar))
367 nChar = rtl::combineSurrogates(nHighSurrogate, nChar);
368 else
370 bUndefined = false;
371 goto bad_input;
374 assert(rtl::isUnicodeScalarValue(nChar));
376 if (nChar == 0x0A || nChar == 0x0D) // LF, CR
378 if (eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_1001)
380 if (pDestBufPtr != pDestBufEnd)
382 *pDestBufPtr++ = 0x0F; // SI
383 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII;
385 else
386 goto no_output;
388 if (pDestBufPtr != pDestBufEnd)
389 *pDestBufPtr++ = static_cast< char >(nChar);
390 else
391 goto no_output;
393 else if (nChar == 0x0E || nChar == 0x0F || nChar == 0x1B)
394 goto bad_input;
395 else if (nChar < 0x80)
397 if (eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_1001)
399 if (pDestBufPtr != pDestBufEnd)
401 *pDestBufPtr++ = 0x0F; // SI
402 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII;
404 else
405 goto no_output;
407 if (pDestBufPtr != pDestBufEnd)
408 *pDestBufPtr++ = static_cast< char >(nChar);
409 else
410 goto no_output;
412 else
414 sal_uInt16 nBytes = 0;
415 sal_uInt32 nIndex1 = nChar >> 8;
416 if (nIndex1 < 0x100)
418 sal_uInt32 nIndex2 = nChar & 0xFF;
419 sal_uInt32 nFirst = pKsX1001Data[nIndex1].mnLowStart;
420 if (nIndex2 >= nFirst
421 && nIndex2 <= pKsX1001Data[nIndex1].mnLowEnd)
422 nBytes = pKsX1001Data[nIndex1].
423 mpToUniTrailTab[nIndex2 - nFirst];
425 if (nBytes != 0)
427 if (eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII)
429 if (pDestBufPtr != pDestBufEnd)
431 *pDestBufPtr++ = 0x0E; // SO
432 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_1001;
434 else
435 goto no_output;
437 if (pDestBufEnd - pDestBufPtr >= 2)
439 *pDestBufPtr++ = static_cast< char >((nBytes >> 8) & 0x7F);
440 *pDestBufPtr++ = static_cast< char >(nBytes & 0x7F);
442 else
443 goto no_output;
445 else
446 goto bad_input;
448 nHighSurrogate = 0;
449 continue;
451 bad_input:
452 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
453 bUndefined, nChar, nFlags, &pDestBufPtr, pDestBufEnd,
454 &nInfo, "\x0F" /* SI */,
455 eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII ? 0 : 1,
456 &bWritten))
458 case sal::detail::textenc::BAD_INPUT_STOP:
459 nHighSurrogate = 0;
460 break;
462 case sal::detail::textenc::BAD_INPUT_CONTINUE:
463 if (bWritten)
464 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII;
465 nHighSurrogate = 0;
466 continue;
468 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
469 goto no_output;
471 break;
473 no_output:
474 --pSrcBuf;
475 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
476 break;
479 if ((nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
480 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
481 == 0)
483 bool bFlush = true;
484 if (nHighSurrogate != 0)
486 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
487 nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
488 else
489 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
490 false, 0, nFlags, &pDestBufPtr, pDestBufEnd, &nInfo,
491 "\x0F" /* SI */,
492 (eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII
493 ? 0 : 1),
494 &bWritten))
496 case sal::detail::textenc::BAD_INPUT_STOP:
497 nHighSurrogate = 0;
498 bFlush = false;
499 break;
501 case sal::detail::textenc::BAD_INPUT_CONTINUE:
502 if (bWritten)
503 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII;
504 nHighSurrogate = 0;
505 break;
507 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
508 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
509 break;
512 if (bFlush
513 && eSet == IMPL_UNICODE_TO_ISO_2022_KR_SET_1001
514 && (nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
516 if (pDestBufPtr != pDestBufEnd)
518 *pDestBufPtr++ = 0x0F; // SI
519 eSet = IMPL_UNICODE_TO_ISO_2022_KR_SET_ASCII;
521 else
522 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
526 if (pContext)
528 static_cast< ImplUnicodeToIso2022KrContext * >(pContext)->m_nHighSurrogate
529 = nHighSurrogate;
530 static_cast< ImplUnicodeToIso2022KrContext * >(pContext)->m_eSet = eSet;
532 if (pInfo)
533 *pInfo = nInfo;
534 if (pSrcCvtChars)
535 *pSrcCvtChars = nConverted;
537 return pDestBufPtr - pDestBuf;
540 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */