summaryrefslogtreecommitdiffstats
path: root/sal/textenc/convertbig5hkscs.cxx
blob: 275e91d3d3b482695032310986933a15cbbc774c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <cassert>

#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>

#include "context.hxx"
#include "convertbig5hkscs.hxx"
#include "converter.hxx"
#include "tenchelp.hxx"

namespace {

struct ImplBig5HkscsToUnicodeContext
{
    sal_Int32 m_nRow; // 0--255; 0 means none
};

}

void * ImplCreateBig5HkscsToUnicodeContext()
{
    ImplBig5HkscsToUnicodeContext * pContext =
        new ImplBig5HkscsToUnicodeContext;
    pContext->m_nRow = 0;
    return pContext;
}

void ImplResetBig5HkscsToUnicodeContext(void * pContext)
{
    if (pContext)
        static_cast< ImplBig5HkscsToUnicodeContext * >(pContext)->m_nRow = 0;
}

void ImplDestroyBig5HkscsToUnicodeContext(void * pContext)
{
    delete static_cast< ImplBig5HkscsToUnicodeContext * >(pContext);
}

sal_Size ImplConvertBig5HkscsToUnicode(void const * pData,
                                       void * pContext,
                                       char const * pSrcBuf,
                                       sal_Size nSrcBytes,
                                       sal_Unicode * pDestBuf,
                                       sal_Size nDestChars,
                                       sal_uInt32 nFlags,
                                       sal_uInt32 * pInfo,
                                       sal_Size * pSrcCvtBytes)
{
    sal_uInt16 const * pBig5Hkscs2001Data
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pBig5Hkscs2001ToUnicodeData;
    sal_Int32 const * pBig5Hkscs2001RowOffsets
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pBig5Hkscs2001ToUnicodeRowOffsets;
    ImplDBCSToUniLeadTab const * pBig5Data
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pBig5ToUnicodeData;
    sal_Int32 nRow = 0;
    sal_uInt32 nInfo = 0;
    sal_Size nConverted = 0;
    sal_Unicode * pDestBufPtr = pDestBuf;
    sal_Unicode * pDestBufEnd = pDestBuf + nDestChars;
    sal_Size startOfCurrentChar = 0;

    if (pContext)
        nRow = static_cast< ImplBig5HkscsToUnicodeContext * >(pContext)->m_nRow;

    for (; nConverted < nSrcBytes; ++nConverted)
    {
        bool bUndefined = true;
        sal_uInt32 nChar = *reinterpret_cast<unsigned char const *>(pSrcBuf++);
        if (nRow == 0)
            if (nChar < 0x80)
                if (pDestBufPtr != pDestBufEnd) {
                    *pDestBufPtr++ = static_cast<sal_Unicode>(nChar);
                    startOfCurrentChar = nConverted + 1;
                } else
                    goto no_output;
            else if (nChar >= 0x81 && nChar <= 0xFE)
                nRow = nChar;
            else
            {
                bUndefined = false;
                goto bad_input;
            }
        else
            if ((nChar >= 0x40 && nChar <= 0x7E)
                || (nChar >= 0xA1 && nChar <= 0xFE))
            {
                sal_uInt32 nUnicode = 0xFFFF;
                sal_Int32 nOffset = pBig5Hkscs2001RowOffsets[nRow];
                sal_uInt32 nFirst=0;
                sal_uInt32 nLast=0;
                if (nOffset != -1)
                {
                    sal_uInt32 nFirstLast = pBig5Hkscs2001Data[nOffset++];
                    nFirst = nFirstLast & 0xFF;
                    nLast = nFirstLast >> 8;
                    if (nChar >= nFirst && nChar <= nLast)
                        nUnicode
                            = pBig5Hkscs2001Data[nOffset + (nChar - nFirst)];
                }
                if (nUnicode == 0xFFFF)
                {
                    sal_uInt32 n = pBig5Data[nRow].mnTrailStart;
                    if (nChar >= n && nChar <= pBig5Data[nRow].mnTrailEnd)
                    {
                        nUnicode = pBig5Data[nRow].mpToUniTrailTab[nChar - n];
                        if (nUnicode == 0)
                            nUnicode = 0xFFFF;
                        assert(!rtl::isHighSurrogate(nUnicode));
                    }
                }
                if (nUnicode == 0xFFFF)
                {
                    ImplDBCSEUDCData const * p
                        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
                              m_pEudcData;
                    sal_uInt32 nCount
                        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
                              m_nEudcCount;
                    sal_uInt32 i;
                    for (i = 0; i < nCount; ++i)
                    {
                        if (nRow >= p->mnLeadStart && nRow <= p->mnLeadEnd)
                        {
                            if (nChar < p->mnTrail1Start)
                                break;
                            if (nChar <= p->mnTrail1End)
                            {
                                nUnicode
                                    = p->mnUniStart
                                          + (nRow - p->mnLeadStart)
                                                * p->mnTrailRangeCount
                                          + (nChar - p->mnTrail1Start);
                                break;
                            }
                            if (p->mnTrailCount < 2
                                || nChar < p->mnTrail2Start)
                                break;
                            if (nChar <= p->mnTrail2End)
                            {
                                nUnicode
                                    = p->mnUniStart
                                          + (nRow - p->mnLeadStart)
                                                * p->mnTrailRangeCount
                                          + (nChar - p->mnTrail2Start)
                                          + (p->mnTrail1End - p->mnTrail1Start
                                                 + 1);
                                break;
                            }
                            if (p->mnTrailCount < 3
                                || nChar < p->mnTrail3Start)
                                break;
                            if (nChar <= p->mnTrail3End)
                            {
                                nUnicode
                                    = p->mnUniStart
                                          + (nRow - p->mnLeadStart)
                                                * p->mnTrailRangeCount
                                          + (nChar - p->mnTrail3Start)
                                          + (p->mnTrail1End - p->mnTrail1Start
                                                 + 1)
                                          + (p->mnTrail2End - p->mnTrail2Start
                                                 + 1);
                                break;
                            }
                            break;
                        }
                        ++p;
                    }
                    assert(!rtl::isHighSurrogate(nUnicode));
                }
                if (nUnicode == 0xFFFF)
                    goto bad_input;
                if (rtl::isHighSurrogate(nUnicode))
                    if (pDestBufEnd - pDestBufPtr >= 2)
                    {
                        nOffset += nLast - nFirst + 1;
                        nFirst = pBig5Hkscs2001Data[nOffset++];
                        *pDestBufPtr++ = static_cast<sal_Unicode>(nUnicode);
                        *pDestBufPtr++
                            = static_cast<sal_Unicode>(pBig5Hkscs2001Data[
                                                nOffset + (nChar - nFirst)]);
                        startOfCurrentChar = nConverted + 1;
                    }
                    else
                        goto no_output;
                else
                    if (pDestBufPtr != pDestBufEnd) {
                        *pDestBufPtr++ = static_cast<sal_Unicode>(nUnicode);
                        startOfCurrentChar = nConverted + 1;
                    } else
                        goto no_output;
                nRow = 0;
            }
            else
            {
                bUndefined = false;
                goto bad_input;
            }
        continue;

    bad_input:
        switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
                    bUndefined, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
                    &nInfo))
        {
        case sal::detail::textenc::BAD_INPUT_STOP:
            nRow = 0;
            if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0) {
                ++nConverted;
            } else {
                nConverted = startOfCurrentChar;
            }
            break;

        case sal::detail::textenc::BAD_INPUT_CONTINUE:
            nRow = 0;
            startOfCurrentChar = nConverted + 1;
            continue;

        case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
            goto no_output;
        }
        break;

    no_output:
        --pSrcBuf;
        nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
        break;
    }

    if (nRow != 0
        && (nInfo & (RTL_TEXTTOUNICODE_INFO_ERROR
                         | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
               == 0)
    {
        if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) == 0)
            nInfo |= RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL;
        else
            switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
                        false, true, 0, nFlags, &pDestBufPtr, pDestBufEnd,
                        &nInfo))
            {
            case sal::detail::textenc::BAD_INPUT_STOP:
                if ((nFlags & RTL_TEXTTOUNICODE_FLAGS_FLUSH) != 0) {
                    nConverted = startOfCurrentChar;
                }
                [[fallthrough]];
            case sal::detail::textenc::BAD_INPUT_CONTINUE:
                nRow = 0;
                break;

            case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
                nInfo |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
                break;
            }
    }

    if (pContext)
        static_cast< ImplBig5HkscsToUnicodeContext * >(pContext)->m_nRow = nRow;
    if (pInfo)
        *pInfo = nInfo;
    if (pSrcCvtBytes)
        *pSrcCvtBytes = nConverted;

    return pDestBufPtr - pDestBuf;
}

sal_Size ImplConvertUnicodeToBig5Hkscs(void const * pData,
                                       void * pContext,
                                       sal_Unicode const * pSrcBuf,
                                       sal_Size nSrcChars,
                                       char * pDestBuf,
                                       sal_Size nDestBytes,
                                       sal_uInt32 nFlags,
                                       sal_uInt32 * pInfo,
                                       sal_Size * pSrcCvtChars)
{
    sal_uInt16 const * pBig5Hkscs2001Data
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pUnicodeToBig5Hkscs2001Data;
    sal_Int32 const * pBig5Hkscs2001PageOffsets
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pUnicodeToBig5Hkscs2001PageOffsets;
    sal_Int32 const * pBig5Hkscs2001PlaneOffsets
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pUnicodeToBig5Hkscs2001PlaneOffsets;
    ImplUniToDBCSHighTab const * pBig5Data
        = static_cast< ImplBig5HkscsConverterData const * >(pData)->
              m_pUnicodeToBig5Data;
    sal_Unicode nHighSurrogate = 0;
    sal_uInt32 nInfo = 0;
    sal_Size nConverted = 0;
    char * pDestBufPtr = pDestBuf;
    char * pDestBufEnd = pDestBuf + nDestBytes;

    if (pContext)
        nHighSurrogate
            = static_cast<ImplUnicodeToTextContext *>(pContext)->m_nHighSurrogate;

    for (; nConverted < nSrcChars; ++nConverted)
    {
        bool bUndefined = true;
        sal_uInt32 nChar = *pSrcBuf++;
        if (nHighSurrogate == 0)
        {
            if (rtl::isHighSurrogate(nChar))
            {
                nHighSurrogate = static_cast<sal_Unicode>(nChar);
                continue;
            }
            else if (rtl::isLowSurrogate(nChar))
            {
                bUndefined = false;
                goto bad_input;
            }
        }
        else if (rtl::isLowSurrogate(nChar))
            nChar = rtl::combineSurrogates(nHighSurrogate, nChar);
        else
        {
            bUndefined = false;
            goto bad_input;
        }

        assert(rtl::isUnicodeScalarValue(nChar));

        if (nChar < 0x80)
            if (pDestBufPtr != pDestBufEnd)
                *pDestBufPtr++ = static_cast< char >(nChar);
            else
                goto no_output;
        else
        {
            sal_uInt32 nBytes = 0;
            sal_Int32 nOffset = pBig5Hkscs2001PlaneOffsets[nChar >> 16];
            if (nOffset != -1)
            {
                nOffset
                    = pBig5Hkscs2001PageOffsets[nOffset + ((nChar & 0xFF00)
                                                               >> 8)];
                if (nOffset != -1)
                {
                    sal_uInt32 nFirstLast = pBig5Hkscs2001Data[nOffset++];
                    sal_uInt32 nFirst = nFirstLast & 0xFF;
                    sal_uInt32 nLast = nFirstLast >> 8;
                    sal_uInt32 nIndex = nChar & 0xFF;
                    if (nIndex >= nFirst && nIndex <= nLast)
                    {
                        nBytes
                            = pBig5Hkscs2001Data[nOffset + (nIndex - nFirst)];
                    }
                }
            }
            if (nBytes == 0)
            {
                sal_uInt32 nIndex1 = nChar >> 8;
                if (nIndex1 < 0x100)
                {
                    sal_uInt32 nIndex2 = nChar & 0xFF;
                    sal_uInt32 nFirst = pBig5Data[nIndex1].mnLowStart;
                    if (nIndex2 >= nFirst
                        && nIndex2 <= pBig5Data[nIndex1].mnLowEnd)
                        nBytes = pBig5Data[nIndex1].
                                     mpToUniTrailTab[nIndex2 - nFirst];
                }
            }
            if (nBytes == 0)
            {
                ImplDBCSEUDCData const * p
                    = static_cast< ImplBig5HkscsConverterData const * >(pData)->
                          m_pEudcData;
                sal_uInt32 nCount
                    = static_cast< ImplBig5HkscsConverterData const * >(pData)->
                          m_nEudcCount;
                sal_uInt32 i;
                for (i = 0; i < nCount; ++i) {
                    if (nChar >= p->mnUniStart && nChar <= p->mnUniEnd)
                    {
                        sal_uInt32 nIndex = nChar - p->mnUniStart;
                        sal_uInt32 nLeadOff = nIndex / p->mnTrailRangeCount;
                        sal_uInt32 nTrailOff = nIndex % p->mnTrailRangeCount;
                        sal_uInt32 nSize;
                        nBytes = (p->mnLeadStart + nLeadOff) << 8;
                        nSize = p->mnTrail1End - p->mnTrail1Start + 1;
                        if (nTrailOff < nSize)
                        {
                            nBytes |= p->mnTrail1Start + nTrailOff;
                            break;
                        }
                        nTrailOff -= nSize;
                        nSize = p->mnTrail2End - p->mnTrail2Start + 1;
                        if (nTrailOff < nSize)
                        {
                            nBytes |= p->mnTrail2Start + nTrailOff;
                            break;
                        }
                        nTrailOff -= nSize;
                        nBytes |= p->mnTrail3Start + nTrailOff;
                        break;
                    }
                    ++p;
                }
            }
            if (nBytes == 0)
                goto bad_input;
            if (pDestBufEnd - pDestBufPtr >= 2)
            {
                *pDestBufPtr++ = static_cast< char >(nBytes >> 8);
                *pDestBufPtr++ = static_cast< char >(nBytes & 0xFF);
            }
            else
                goto no_output;
        }
        nHighSurrogate = 0;
        continue;

    bad_input:
        switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
                    bUndefined, nChar, nFlags, &pDestBufPtr, pDestBufEnd,
                    &nInfo, nullptr, 0, nullptr))
        {
        case sal::detail::textenc::BAD_INPUT_STOP:
            nHighSurrogate = 0;
            break;

        case sal::detail::textenc::BAD_INPUT_CONTINUE:
            nHighSurrogate = 0;
            continue;

        case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
            goto no_output;
        }
        break;

    no_output:
        --pSrcBuf;
        nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
        break;
    }

    if (nHighSurrogate != 0
        && (nInfo & (RTL_UNICODETOTEXT_INFO_ERROR
                         | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
               == 0)
    {
        if ((nFlags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0)
            nInfo |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
        else
            switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
                        false, 0, nFlags, &pDestBufPtr, pDestBufEnd, &nInfo,
                        nullptr, 0, nullptr))
            {
            case sal::detail::textenc::BAD_INPUT_STOP:
            case sal::detail::textenc::BAD_INPUT_CONTINUE:
                nHighSurrogate = 0;
                break;

            case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
                nInfo |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
                break;
            }
    }

    if (pContext)
        static_cast<ImplUnicodeToTextContext *>(pContext)->m_nHighSurrogate
            = nHighSurrogate;
    if (pInfo)
        *pInfo = nInfo;
    if (pSrcCvtChars)
        *pSrcCvtChars = nConverted;

    return pDestBufPtr - pDestBuf;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */