summaryrefslogtreecommitdiffstats
path: root/intl/uconv/ucvtw/nsBIG5ToUnicode.cpp
blob: 8dbf84a1479bd855842dc8de86df19095ec3fcc8 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#include "nsBIG5ToUnicode.h"
#include "mozilla/BinarySearch.h"
#include "mozilla/ArrayUtils.h"
#include "nsBIG5Data.h"

nsBIG5ToUnicode::nsBIG5ToUnicode()
 : mPendingTrail(0)
 , mBig5Lead(0)
{
}

NS_IMETHODIMP
nsBIG5ToUnicode::Convert(const char* aSrc,
                         int32_t* aSrcLength,
                         char16_t* aDest,
                         int32_t* aDestLength)
{
  // We'll be doing comparisons as unsigned.
  const uint8_t* in = reinterpret_cast<const uint8_t*>(aSrc);
  const uint8_t* inEnd = in + *aSrcLength;
  char16_t* out = aDest;
  char16_t* outEnd = out + *aDestLength;

  if (mPendingTrail) {
    if (out == outEnd) {
      *aSrcLength = 0;
      *aDestLength = 0;
      return NS_OK_UDEC_MOREOUTPUT;
    }
    *out++ = mPendingTrail;
    mPendingTrail = 0;
  }
  for (;;) {
    if (in == inEnd) {
      *aSrcLength = in - reinterpret_cast<const uint8_t*>(aSrc);
      *aDestLength = out - aDest;
      return mBig5Lead ? NS_OK_UDEC_MOREINPUT : NS_OK;
    }
    if (out == outEnd) {
      *aSrcLength = in - reinterpret_cast<const uint8_t*>(aSrc);
      *aDestLength = out - aDest;
      return NS_OK_UDEC_MOREOUTPUT;
    }
    uint8_t b = *in++;
    if (!mBig5Lead) {
      if (b <= 0x7F) {
        *out++ = (char16_t)b;
        continue;
      }
      if (b >= 0x81 && b <= 0xFE) {
        mBig5Lead = b;
        continue;
      }
      if (mErrBehavior == kOnError_Signal) {
        --in;
        *aSrcLength = in - reinterpret_cast<const uint8_t*>(aSrc);
        *aDestLength = out - aDest;
        return NS_ERROR_ILLEGAL_INPUT;
      }
      *out++ = 0xFFFD;
      continue;
    }
    size_t lead = mBig5Lead;
    mBig5Lead = 0;
    size_t offset = (b < 0x7F) ? 0x40 : 0x62;
    if ((b >= 0x40 && b <= 0x7E) || (b >= 0xA1 && b <= 0xFE)) {
      size_t pointer = (lead - 0x81) * 157L + (b - offset);
      char16_t outTrail;
      switch (pointer) {
        case 1133:
          *out++ = 0x00CA;
          outTrail = 0x0304;
          break;
        case 1135:
          *out++ = 0x00CA;
          outTrail = 0x030C;
          break;
        case 1164:
          *out++ = 0x00EA;
          outTrail = 0x0304;
          break;
        case 1166:
          *out++ = 0x00EA;
          outTrail = 0x030C;
          break;
        default:
          char16_t lowBits = nsBIG5Data::LowBits(pointer);
          if (!lowBits) {
            if (b <= 0x7F) {
              // prepend byte to stream
              // Always legal, since we've always just read a byte
              // if we come here.
              --in;
            }
            if (mErrBehavior == kOnError_Signal) {
              --in;
              *aSrcLength = in - reinterpret_cast<const uint8_t*>(aSrc);
              *aDestLength = out - aDest;
              return NS_ERROR_ILLEGAL_INPUT;
            }
            *out++ = 0xFFFD;
            continue;
          }
          if (nsBIG5Data::IsAstral(pointer)) {
            uint32_t codePoint = uint32_t(lowBits) | 0x20000;
            *out++ = char16_t(0xD7C0 + (codePoint >> 10));
            outTrail = char16_t(0xDC00 + (codePoint & 0x3FF));
            break;
          }
          *out++ = lowBits;
          continue;
      }
      if (out == outEnd) {
        mPendingTrail = outTrail;
        *aSrcLength = in - reinterpret_cast<const uint8_t*>(aSrc);
        *aDestLength = out - aDest;
        return NS_OK_UDEC_MOREOUTPUT;
      }
      *out++ = outTrail;
      continue;
    }
    // pointer is null
    if (b <= 0x7F) {
      // prepend byte to stream
      // Always legal, since we've always just read a byte
      // if we come here.
      --in;
    }
    if (mErrBehavior == kOnError_Signal) {
      // Moving in one past the start of aSrc is actually OK per API contract,
      // since assigning -1 to aSrcLength means that we want the caller to
      // record one U+FFFD and repush the same input buffer.
      --in;
      *aSrcLength = in - reinterpret_cast<const uint8_t*>(aSrc);
      *aDestLength = out - aDest;
      return NS_ERROR_ILLEGAL_INPUT;
    }
    *out++ = 0xFFFD;
    continue;
  }
}

NS_IMETHODIMP
nsBIG5ToUnicode::GetMaxLength(const char* aSrc,
                              int32_t aSrcLength,
                              int32_t* aDestLength)
{
  // The length of the output in UTF-16 code units never exceeds the length
  // of the input in bytes.
  *aDestLength = aSrcLength + (mPendingTrail ? 1 : 0) + (mBig5Lead ? 1 : 0);
  return NS_OK;
}

NS_IMETHODIMP
nsBIG5ToUnicode::Reset()
{
  mPendingTrail = 0;
  mBig5Lead = 0;
  return NS_OK;
}