1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
24 #include "rtl/textcvt.h"
25 #include "sal/types.h"
27 #include "context.hxx"
28 #include "convertbig5hkscs.hxx"
29 #include "converter.hxx"
30 #include "tenchelp.hxx"
31 #include "unichars.hxx"
35 struct ImplBig5HkscsToUnicodeContext
37 sal_Int32 m_nRow
; // 0--255; 0 means none
42 void * ImplCreateBig5HkscsToUnicodeContext()
44 ImplBig5HkscsToUnicodeContext
* pContext
=
45 new ImplBig5HkscsToUnicodeContext
;
50 void ImplResetBig5HkscsToUnicodeContext(void * pContext
)
53 static_cast< ImplBig5HkscsToUnicodeContext
* >(pContext
)->m_nRow
= 0;
56 void ImplDestroyBig5HkscsToUnicodeContext(void * pContext
)
58 delete static_cast< ImplBig5HkscsToUnicodeContext
* >(pContext
);
61 sal_Size
ImplConvertBig5HkscsToUnicode(void const * pData
,
65 sal_Unicode
* pDestBuf
,
69 sal_Size
* pSrcCvtBytes
)
71 sal_uInt16
const * pBig5Hkscs2001Data
72 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
73 m_pBig5Hkscs2001ToUnicodeData
;
74 sal_Int32
const * pBig5Hkscs2001RowOffsets
75 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
76 m_pBig5Hkscs2001ToUnicodeRowOffsets
;
77 ImplDBCSToUniLeadTab
const * pBig5Data
78 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
82 sal_Size nConverted
= 0;
83 sal_Unicode
* pDestBufPtr
= pDestBuf
;
84 sal_Unicode
* pDestBufEnd
= pDestBuf
+ nDestChars
;
87 nRow
= static_cast< ImplBig5HkscsToUnicodeContext
* >(pContext
)->m_nRow
;
89 for (; nConverted
< nSrcBytes
; ++nConverted
)
91 bool bUndefined
= true;
92 sal_uInt32 nChar
= *reinterpret_cast<unsigned char const *>(pSrcBuf
++);
95 if (pDestBufPtr
!= pDestBufEnd
)
96 *pDestBufPtr
++ = (sal_Unicode
) nChar
;
99 else if (nChar
>= 0x81 && nChar
<= 0xFE)
107 if ((nChar
>= 0x40 && nChar
<= 0x7E)
108 || (nChar
>= 0xA1 && nChar
<= 0xFE))
110 sal_uInt32 nUnicode
= 0xFFFF;
111 sal_Int32 nOffset
= pBig5Hkscs2001RowOffsets
[nRow
];
116 sal_uInt32 nFirstLast
= pBig5Hkscs2001Data
[nOffset
++];
117 nFirst
= nFirstLast
& 0xFF;
118 nLast
= nFirstLast
>> 8;
119 if (nChar
>= nFirst
&& nChar
<= nLast
)
121 = pBig5Hkscs2001Data
[nOffset
+ (nChar
- nFirst
)];
123 if (nUnicode
== 0xFFFF)
125 sal_uInt32 n
= pBig5Data
[nRow
].mnTrailStart
;
126 if (nChar
>= n
&& nChar
<= pBig5Data
[nRow
].mnTrailEnd
)
128 nUnicode
= pBig5Data
[nRow
].mpToUniTrailTab
[nChar
- n
];
131 assert(!ImplIsHighSurrogate(nUnicode
));
134 if (nUnicode
== 0xFFFF)
136 ImplDBCSEUDCData
const * p
137 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
140 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
143 for (i
= 0; i
< nCount
; ++i
)
145 if (nRow
>= p
->mnLeadStart
&& nRow
<= p
->mnLeadEnd
)
147 if (nChar
< p
->mnTrail1Start
)
149 if (nChar
<= p
->mnTrail1End
)
153 + (nRow
- p
->mnLeadStart
)
154 * p
->mnTrailRangeCount
155 + (nChar
- p
->mnTrail1Start
);
158 if (p
->mnTrailCount
< 2
159 || nChar
< p
->mnTrail2Start
)
161 if (nChar
<= p
->mnTrail2End
)
165 + (nRow
- p
->mnLeadStart
)
166 * p
->mnTrailRangeCount
167 + (nChar
- p
->mnTrail2Start
)
168 + (p
->mnTrail1End
- p
->mnTrail1Start
172 if (p
->mnTrailCount
< 3
173 || nChar
< p
->mnTrail3Start
)
175 if (nChar
<= p
->mnTrail3End
)
179 + (nRow
- p
->mnLeadStart
)
180 * p
->mnTrailRangeCount
181 + (nChar
- p
->mnTrail3Start
)
182 + (p
->mnTrail1End
- p
->mnTrail1Start
184 + (p
->mnTrail2End
- p
->mnTrail2Start
192 assert(!ImplIsHighSurrogate(nUnicode
));
194 if (nUnicode
== 0xFFFF)
196 if (ImplIsHighSurrogate(nUnicode
))
197 if (pDestBufEnd
- pDestBufPtr
>= 2)
199 nOffset
+= nLast
- nFirst
+ 1;
200 nFirst
= pBig5Hkscs2001Data
[nOffset
++];
201 *pDestBufPtr
++ = (sal_Unicode
) nUnicode
;
203 = (sal_Unicode
) pBig5Hkscs2001Data
[
204 nOffset
+ (nChar
- nFirst
)];
209 if (pDestBufPtr
!= pDestBufEnd
)
210 *pDestBufPtr
++ = (sal_Unicode
) nUnicode
;
223 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
224 bUndefined
, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
227 case sal::detail::textenc::BAD_INPUT_STOP
:
231 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
235 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
242 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
;
247 && (nInfo
& (RTL_TEXTTOUNICODE_INFO_ERROR
248 | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
))
251 if ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_FLUSH
) == 0)
252 nInfo
|= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOSMALL
;
254 switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
255 false, true, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
,
258 case sal::detail::textenc::BAD_INPUT_STOP
:
259 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
263 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
264 nInfo
|= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
;
270 static_cast< ImplBig5HkscsToUnicodeContext
* >(pContext
)->m_nRow
= nRow
;
274 *pSrcCvtBytes
= nConverted
;
276 return pDestBufPtr
- pDestBuf
;
279 sal_Size
ImplConvertUnicodeToBig5Hkscs(void const * pData
,
281 sal_Unicode
const * pSrcBuf
,
287 sal_Size
* pSrcCvtChars
)
289 sal_uInt16
const * pBig5Hkscs2001Data
290 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
291 m_pUnicodeToBig5Hkscs2001Data
;
292 sal_Int32
const * pBig5Hkscs2001PageOffsets
293 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
294 m_pUnicodeToBig5Hkscs2001PageOffsets
;
295 sal_Int32
const * pBig5Hkscs2001PlaneOffsets
296 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
297 m_pUnicodeToBig5Hkscs2001PlaneOffsets
;
298 ImplUniToDBCSHighTab
const * pBig5Data
299 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
300 m_pUnicodeToBig5Data
;
301 sal_Unicode nHighSurrogate
= 0;
302 sal_uInt32 nInfo
= 0;
303 sal_Size nConverted
= 0;
304 char * pDestBufPtr
= pDestBuf
;
305 char * pDestBufEnd
= pDestBuf
+ nDestBytes
;
309 = static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
;
311 for (; nConverted
< nSrcChars
; ++nConverted
)
313 bool bUndefined
= true;
314 sal_uInt32 nChar
= *pSrcBuf
++;
315 if (nHighSurrogate
== 0)
317 if (ImplIsHighSurrogate(nChar
))
319 nHighSurrogate
= (sal_Unicode
) nChar
;
323 else if (ImplIsLowSurrogate(nChar
))
324 nChar
= ImplCombineSurrogates(nHighSurrogate
, nChar
);
331 if (ImplIsLowSurrogate(nChar
) || ImplIsNoncharacter(nChar
))
338 if (pDestBufPtr
!= pDestBufEnd
)
339 *pDestBufPtr
++ = static_cast< char >(nChar
);
344 sal_uInt32 nBytes
= 0;
345 sal_Int32 nOffset
= pBig5Hkscs2001PlaneOffsets
[nChar
>> 16];
349 = pBig5Hkscs2001PageOffsets
[nOffset
+ ((nChar
& 0xFF00)
353 sal_uInt32 nFirstLast
= pBig5Hkscs2001Data
[nOffset
++];
354 sal_uInt32 nFirst
= nFirstLast
& 0xFF;
355 sal_uInt32 nLast
= nFirstLast
>> 8;
356 sal_uInt32 nIndex
= nChar
& 0xFF;
357 if (nIndex
>= nFirst
&& nIndex
<= nLast
)
360 = pBig5Hkscs2001Data
[nOffset
+ (nIndex
- nFirst
)];
366 sal_uInt32 nIndex1
= nChar
>> 8;
369 sal_uInt32 nIndex2
= nChar
& 0xFF;
370 sal_uInt32 nFirst
= pBig5Data
[nIndex1
].mnLowStart
;
371 if (nIndex2
>= nFirst
372 && nIndex2
<= pBig5Data
[nIndex1
].mnLowEnd
)
373 nBytes
= pBig5Data
[nIndex1
].
374 mpToUniTrailTab
[nIndex2
- nFirst
];
379 ImplDBCSEUDCData
const * p
380 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
383 = static_cast< ImplBig5HkscsConverterData
const * >(pData
)->
386 for (i
= 0; i
< nCount
; ++i
) {
387 if (nChar
>= p
->mnUniStart
&& nChar
<= p
->mnUniEnd
)
389 sal_uInt32 nIndex
= nChar
- p
->mnUniStart
;
390 sal_uInt32 nLeadOff
= nIndex
/ p
->mnTrailRangeCount
;
391 sal_uInt32 nTrailOff
= nIndex
% p
->mnTrailRangeCount
;
393 nBytes
= (p
->mnLeadStart
+ nLeadOff
) << 8;
394 nSize
= p
->mnTrail1End
- p
->mnTrail1Start
+ 1;
395 if (nTrailOff
< nSize
)
397 nBytes
|= p
->mnTrail1Start
+ nTrailOff
;
401 nSize
= p
->mnTrail2End
- p
->mnTrail2Start
+ 1;
402 if (nTrailOff
< nSize
)
404 nBytes
|= p
->mnTrail2Start
+ nTrailOff
;
408 nBytes
|= p
->mnTrail3Start
+ nTrailOff
;
416 if (pDestBufEnd
- pDestBufPtr
>= 2)
418 *pDestBufPtr
++ = static_cast< char >(nBytes
>> 8);
419 *pDestBufPtr
++ = static_cast< char >(nBytes
& 0xFF);
428 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
429 bUndefined
, nChar
, nFlags
, &pDestBufPtr
, pDestBufEnd
,
430 &nInfo
, NULL
, 0, NULL
))
432 case sal::detail::textenc::BAD_INPUT_STOP
:
436 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
440 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
447 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
451 if (nHighSurrogate
!= 0
452 && (nInfo
& (RTL_UNICODETOTEXT_INFO_ERROR
453 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
))
456 if ((nFlags
& RTL_UNICODETOTEXT_FLAGS_FLUSH
) != 0)
457 nInfo
|= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL
;
459 switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
460 false, 0, nFlags
, &pDestBufPtr
, pDestBufEnd
, &nInfo
,
463 case sal::detail::textenc::BAD_INPUT_STOP
:
464 case sal::detail::textenc::BAD_INPUT_CONTINUE
:
468 case sal::detail::textenc::BAD_INPUT_NO_OUTPUT
:
469 nInfo
|= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
475 static_cast<ImplUnicodeToTextContext
*>(pContext
)->m_nHighSurrogate
480 *pSrcCvtChars
= nConverted
;
482 return pDestBufPtr
- pDestBuf
;
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */