Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / textenc / convertiso2022jp.cxx
blob93470b852a708f9cdd714f7604877b86dcbc159b
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 "convertiso2022jp.hxx"
30 #include "tenchelp.hxx"
32 namespace {
34 enum ImplIso2022JpToUnicodeState // order is important:
36 IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII,
37 IMPL_ISO_2022_JP_TO_UNICODE_STATE_JIS_ROMAN,
38 IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208,
39 IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208_2,
40 IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC,
41 IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC_LPAREN,
42 IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC_DOLLAR
45 struct ImplIso2022JpToUnicodeContext
47 ImplIso2022JpToUnicodeState m_eState;
48 sal_uInt32 m_nRow;
51 struct ImplUnicodeToIso2022JpContext
53 sal_Unicode m_nHighSurrogate;
54 bool m_b0208;
59 void * ImplCreateIso2022JpToUnicodeContext()
61 ImplIso2022JpToUnicodeContext * pContext =
62 new ImplIso2022JpToUnicodeContext;
63 pContext->m_eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
64 return pContext;
67 void ImplResetIso2022JpToUnicodeContext(void * pContext)
69 if (pContext)
70 static_cast< ImplIso2022JpToUnicodeContext * >(pContext)->m_eState
71 = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
74 void ImplDestroyIso2022JpToUnicodeContext(void * pContext)
76 delete static_cast< ImplIso2022JpToUnicodeContext * >(pContext);
79 sal_Size ImplConvertIso2022JpToUnicode(void const * pData,
80 void * pContext,
81 char const * pSrcBuf,
82 sal_Size nSrcBytes,
83 sal_Unicode * pDestBuf,
84 sal_Size nDestChars,
85 sal_uInt32 nFlags,
86 sal_uInt32 * pInfo,
87 sal_Size * pSrcCvtBytes)
89 ImplDBCSToUniLeadTab const * pJisX0208Data
90 = static_cast< ImplIso2022JpConverterData const * >(pData)->
91 m_pJisX0208ToUnicodeData;
92 ImplIso2022JpToUnicodeState eState
93 = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
94 sal_uInt32 nRow = 0;
95 sal_uInt32 nInfo = 0;
96 sal_Size nConverted = 0;
97 sal_Unicode * pDestBufPtr = pDestBuf;
98 sal_Unicode * pDestBufEnd = pDestBuf + nDestChars;
99 sal_Size startOfCurrentChar = 0;
101 if (pContext)
103 eState = static_cast< ImplIso2022JpToUnicodeContext * >(pContext)->m_eState;
104 nRow = static_cast< ImplIso2022JpToUnicodeContext * >(pContext)->m_nRow;
107 for (; nConverted < nSrcBytes; ++nConverted)
109 bool bUndefined = true;
110 sal_uInt32 nChar = *reinterpret_cast<unsigned char const *>(pSrcBuf++);
111 switch (eState)
113 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII:
114 if (nChar == 0x1B) // ESC
115 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC;
116 else if (nChar < 0x80)
117 if (pDestBufPtr != pDestBufEnd) {
118 *pDestBufPtr++ = static_cast<sal_Unicode>(nChar);
119 startOfCurrentChar = nConverted + 1;
120 } else
121 goto no_output;
122 else
124 bUndefined = false;
125 goto bad_input;
127 break;
129 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_JIS_ROMAN:
130 if (nChar == 0x1B) // ESC
131 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC;
132 else if (nChar < 0x80)
133 if (pDestBufPtr != pDestBufEnd)
135 switch (nChar)
137 case 0x5C: // REVERSE SOLIDUS (\)
138 nChar = 0xA5; // YEN SIGN
139 break;
141 case 0x7E: // ~
142 nChar = 0xAF; // MACRON
143 break;
145 *pDestBufPtr++ = static_cast<sal_Unicode>(nChar);
146 startOfCurrentChar = nConverted + 1;
148 else
149 goto no_output;
150 else
152 bUndefined = false;
153 goto bad_input;
155 break;
157 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208:
158 if (nChar == 0x1B) // ESC
159 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC;
160 else if (nChar >= 0x21 && nChar <= 0x7E)
162 nRow = nChar;
163 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208_2;
165 else
167 bUndefined = false;
168 goto bad_input;
170 break;
172 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208_2:
173 if (nChar >= 0x21 && nChar <= 0x7E)
175 sal_uInt16 nUnicode = 0;
176 sal_uInt32 nFirst = pJisX0208Data[nRow].mnTrailStart;
177 if (nChar >= nFirst
178 && nChar <= pJisX0208Data[nRow].mnTrailEnd)
179 nUnicode = pJisX0208Data[nRow].
180 mpToUniTrailTab[nChar - nFirst];
181 if (nUnicode != 0)
182 if (pDestBufPtr != pDestBufEnd)
184 *pDestBufPtr++ = static_cast<sal_Unicode>(nUnicode);
185 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208;
186 startOfCurrentChar = nConverted + 1;
188 else
189 goto no_output;
190 else
191 goto bad_input;
193 else
195 bUndefined = false;
196 goto bad_input;
198 break;
200 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC:
201 switch (nChar)
203 case 0x24: // $
204 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC_DOLLAR;
205 break;
207 case 0x28: // (
208 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC_LPAREN;
209 break;
211 default:
212 bUndefined = false;
213 goto bad_input;
215 break;
217 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC_LPAREN:
218 switch (nChar)
220 case 0x42: // B
221 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
222 break;
224 case 0x4A: // J
225 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_JIS_ROMAN;
226 break;
228 default:
229 bUndefined = false;
230 goto bad_input;
232 break;
234 case IMPL_ISO_2022_JP_TO_UNICODE_STATE_ESC_DOLLAR:
235 switch (nChar)
237 case 0x40: // @
238 case 0x42: // B
239 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208;
240 break;
242 default:
243 bUndefined = false;
244 goto bad_input;
246 break;
248 continue;
250 bad_input:
251 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
252 bUndefined, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
253 &nInfo))
255 case sal::detail::textenc::BAD_INPUT_STOP:
256 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
257 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0) {
258 ++nConverted;
259 } else {
260 nConverted = startOfCurrentChar;
262 break;
264 case sal::detail::textenc::BAD_INPUT_CONTINUE:
265 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
266 startOfCurrentChar = nConverted + 1;
267 continue;
269 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
270 goto no_output;
272 break;
274 no_output:
275 --pSrcBuf;
276 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
277 break;
280 if (eState > IMPL_ISO_2022_JP_TO_UNICODE_STATE_0208
281 && (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
282 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
283 == 0)
285 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
286 nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL;
287 else
288 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
289 false, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
290 &nInfo))
292 case sal::detail::textenc::BAD_INPUT_STOP:
293 if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) != 0) {
294 nConverted = startOfCurrentChar;
296 [[fallthrough]];
297 case sal::detail::textenc::BAD_INPUT_CONTINUE:
298 eState = IMPL_ISO_2022_JP_TO_UNICODE_STATE_ASCII;
299 break;
301 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
302 nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
303 break;
307 if (pContext)
309 static_cast< ImplIso2022JpToUnicodeContext * >(pContext)->m_eState = eState;
310 static_cast< ImplIso2022JpToUnicodeContext * >(pContext)->m_nRow = nRow;
312 if (pInfo)
313 *pInfo = nInfo;
314 if (pSrcCvtBytes)
315 *pSrcCvtBytes = nConverted;
317 return pDestBufPtr - pDestBuf;
320 void * ImplCreateUnicodeToIso2022JpContext()
322 ImplUnicodeToIso2022JpContext * pContext =
323 new ImplUnicodeToIso2022JpContext;
324 pContext->m_nHighSurrogate = 0;
325 pContext->m_b0208 = false;
326 return pContext;
329 void ImplResetUnicodeToIso2022JpContext(void * pContext)
331 if (pContext)
333 static_cast< ImplUnicodeToIso2022JpContext * >(pContext)->m_nHighSurrogate = 0;
334 static_cast< ImplUnicodeToIso2022JpContext * >(pContext)->m_b0208 = false;
338 void ImplDestroyUnicodeToIso2022JpContext(void * pContext)
340 delete static_cast< ImplUnicodeToIso2022JpContext * >(pContext);
343 sal_Size ImplConvertUnicodeToIso2022Jp(void const * pData,
344 void * pContext,
345 sal_Unicode const * pSrcBuf,
346 sal_Size nSrcChars,
347 char * pDestBuf,
348 sal_Size nDestBytes,
349 sal_uInt32 nFlags,
350 sal_uInt32 * pInfo,
351 sal_Size * pSrcCvtChars)
353 ImplUniToDBCSHighTab const * pJisX0208Data
354 = static_cast< ImplIso2022JpConverterData const * >(pData)->
355 m_pUnicodeToJisX0208Data;
356 sal_Unicode nHighSurrogate = 0;
357 bool b0208 = false;
358 sal_uInt32 nInfo = 0;
359 sal_Size nConverted = 0;
360 char * pDestBufPtr = pDestBuf;
361 char * pDestBufEnd = pDestBuf + nDestBytes;
362 bool bWritten;
364 if (pContext)
366 nHighSurrogate
367 = static_cast< ImplUnicodeToIso2022JpContext * >(pContext)->m_nHighSurrogate;
368 b0208 = static_cast< ImplUnicodeToIso2022JpContext * >(pContext)->m_b0208;
371 for (; nConverted < nSrcChars; ++nConverted)
373 bool bUndefined = true;
374 sal_uInt32 nChar = *pSrcBuf++;
375 if (nHighSurrogate == 0)
377 if (rtl::isHighSurrogate(nChar))
379 nHighSurrogate = static_cast<sal_Unicode>(nChar);
380 continue;
382 else if (rtl::isLowSurrogate(nChar))
384 bUndefined = false;
385 goto bad_input;
388 else if (rtl::isLowSurrogate(nChar))
389 nChar = rtl::combineSurrogates(nHighSurrogate, nChar);
390 else
392 bUndefined = false;
393 goto bad_input;
396 assert(rtl::isUnicodeScalarValue(nChar));
398 if (nChar == 0x0A || nChar == 0x0D) // LF, CR
400 if (b0208)
402 if (pDestBufEnd - pDestBufPtr >= 3)
404 *pDestBufPtr++ = 0x1B; // ESC
405 *pDestBufPtr++ = 0x28; // (
406 *pDestBufPtr++ = 0x42; // B
407 b0208 = false;
409 else
410 goto no_output;
412 if (pDestBufPtr != pDestBufEnd)
413 *pDestBufPtr++ = static_cast< char >(nChar);
414 else
415 goto no_output;
417 else if (nChar == 0x1B)
418 goto bad_input;
419 else if (nChar < 0x80)
421 if (b0208)
423 if (pDestBufEnd - pDestBufPtr >= 3)
425 *pDestBufPtr++ = 0x1B; // ESC
426 *pDestBufPtr++ = 0x28; // (
427 *pDestBufPtr++ = 0x42; // B
428 b0208 = false;
430 else
431 goto no_output;
433 if (pDestBufPtr != pDestBufEnd)
434 *pDestBufPtr++ = static_cast< char >(nChar);
435 else
436 goto no_output;
438 else
440 sal_uInt16 nBytes = 0;
441 sal_uInt32 nIndex1 = nChar >> 8;
442 if (nIndex1 < 0x100)
444 sal_uInt32 nIndex2 = nChar & 0xFF;
445 sal_uInt32 nFirst = pJisX0208Data[nIndex1].mnLowStart;
446 if (nIndex2 >= nFirst
447 && nIndex2 <= pJisX0208Data[nIndex1].mnLowEnd)
449 nBytes = pJisX0208Data[nIndex1].
450 mpToUniTrailTab[nIndex2 - nFirst];
451 if (nBytes == 0)
452 // For some reason, the tables in tcvtjp4.tab do not
453 // include these two conversions:
454 switch (nChar)
456 case 0xA5: // YEN SIGN
457 nBytes = 0x216F;
458 break;
460 case 0xAF: // MACRON
461 nBytes = 0x2131;
462 break;
466 if (nBytes != 0)
468 if (!b0208)
470 if (pDestBufEnd - pDestBufPtr >= 3)
472 *pDestBufPtr++ = 0x1B; // ESC
473 *pDestBufPtr++ = 0x24; // $
474 *pDestBufPtr++ = 0x42; // B
475 b0208 = true;
477 else
478 goto no_output;
480 if (pDestBufEnd - pDestBufPtr >= 2)
482 *pDestBufPtr++ = static_cast< char >(nBytes >> 8);
483 *pDestBufPtr++ = static_cast< char >(nBytes & 0xFF);
485 else
486 goto no_output;
488 else
489 goto bad_input;
491 nHighSurrogate = 0;
492 continue;
494 bad_input:
495 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
496 bUndefined, nChar, nFlags, &pDestBufPtr, pDestBufEnd,
497 &nInfo, "\x1B(B", b0208 ? 3 : 0, &bWritten))
499 case sal::detail::textenc::BAD_INPUT_STOP:
500 nHighSurrogate = 0;
501 break;
503 case sal::detail::textenc::BAD_INPUT_CONTINUE:
504 if (bWritten)
505 b0208 = false;
506 nHighSurrogate = 0;
507 continue;
509 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
510 goto no_output;
512 break;
514 no_output:
515 --pSrcBuf;
516 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
517 break;
520 if ((nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
521 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
522 == 0)
524 bool bFlush = true;
525 if (nHighSurrogate != 0)
527 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
528 nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
529 else
530 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
531 false, 0, nFlags, &pDestBufPtr, pDestBufEnd, &nInfo,
532 "\x1B(B", b0208 ? 3 : 0, &bWritten))
534 case sal::detail::textenc::BAD_INPUT_STOP:
535 nHighSurrogate = 0;
536 bFlush = false;
537 break;
539 case sal::detail::textenc::BAD_INPUT_CONTINUE:
540 if (bWritten)
541 b0208 = false;
542 nHighSurrogate = 0;
543 break;
545 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
546 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
547 break;
550 if (bFlush
551 && b0208
552 && (nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
554 if (pDestBufEnd - pDestBufPtr >= 3)
556 *pDestBufPtr++ = 0x1B; // ESC
557 *pDestBufPtr++ = 0x28; // (
558 *pDestBufPtr++ = 0x42; // B
559 b0208 = false;
561 else
562 nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
566 if (pContext)
568 static_cast< ImplUnicodeToIso2022JpContext * >(pContext)->m_nHighSurrogate
569 = nHighSurrogate;
570 static_cast< ImplUnicodeToIso2022JpContext * >(pContext)->m_b0208 = b0208;
572 if (pInfo)
573 *pInfo = nInfo;
574 if (pSrcCvtChars)
575 *pSrcCvtChars = nConverted;
577 return pDestBufPtr - pDestBuf;
580 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */