summaryrefslogtreecommitdiffstats
path: root/intl/uconv/ucvtw/nsUnicodeToBIG5.cpp
blob: c3c9658dfb7d51b9fcd4d9f7e3d7f6325aa75ec1 (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
/* -*- 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 "nsUnicodeToBIG5.h"

NS_IMPL_ADDREF(nsUnicodeToBIG5)
NS_IMPL_RELEASE(nsUnicodeToBIG5)
NS_IMPL_QUERY_INTERFACE(nsUnicodeToBIG5,
                        nsIUnicodeEncoder)

nsUnicodeToBIG5::nsUnicodeToBIG5()
 : mUtf16Lead(0)
 , mPendingTrail(0)
 , mSignal(true) // as in nsEncoderSupport
{
}

NS_IMETHODIMP
nsUnicodeToBIG5::Convert(const char16_t* aSrc,
                         int32_t* aSrcLength,
                         char* aDest,
                         int32_t * aDestLength)
{
  const char16_t* in = aSrc;
  const char16_t* inEnd = in + *aSrcLength;
  uint8_t* out = reinterpret_cast<uint8_t*>(aDest);
  uint8_t* outEnd = out + *aDestLength;

  MOZ_ASSERT(!(mPendingTrail && mUtf16Lead),
             "Can't have both pending output and pending input.");

  if (mPendingTrail) {
    if (out == outEnd) {
      *aSrcLength = 0;
      *aDestLength = 0;
      return NS_OK_UENC_MOREOUTPUT;
    }
    *out++ = mPendingTrail;
    mPendingTrail = 0;
  }
  for (;;) {
    if (in == inEnd) {
      *aSrcLength = in - aSrc;
      *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
      return mUtf16Lead ? NS_OK_UENC_MOREINPUT : NS_OK;
    }
    if (out == outEnd) {
      *aSrcLength = in - aSrc;
      *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
      return NS_OK_UENC_MOREOUTPUT;
    }
    bool isAstral; // true means Plane 2, false means BMP
    char16_t lowBits; // The low 16 bits of the code point
    char16_t codeUnit = *in++;
    size_t highBits = (codeUnit & 0xFC00);
    if (highBits == 0xD800) {
      // high surrogate
      if (mUtf16Lead) {
        // High surrogate follows another high surrogate. The
        // *previous* code unit is in error.
        if (mSignal) {
          mUtf16Lead = 0;
          // NOTE: Encode API differs from decode API!
          --in;
          *aSrcLength = in - aSrc;
          *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
          return NS_ERROR_UENC_NOMAPPING;
        }
        *out++ = '?';
      }
      mUtf16Lead = codeUnit;
      continue;
    }
    if (highBits == 0xDC00) {
      // low surrogate
      if (!mUtf16Lead) {
        // Got low surrogate without a previous high surrogate
        if (mSignal) {
          // NOTE: Encode API differs from decode API!
          *aSrcLength = in - aSrc;
          *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
          return NS_ERROR_UENC_NOMAPPING;
        }
        *out++ = '?';
        continue;
      }
      size_t codePoint = (mUtf16Lead << 10) + codeUnit -
                         (((0xD800 << 10) - 0x10000) + 0xDC00);
      mUtf16Lead = 0;
      // Plane 2 is the only astral plane that has potentially
      // Big5-encodable characters.
      if ((0xFF0000 & codePoint) != 0x20000) {
        if (mSignal) {
          // NOTE: Encode API differs from decode API!
          // nsSaveAsCharset wants us to back up on step in the case of a
          // surrogate pair.
          --in;
          *aSrcLength = in - aSrc;
          *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
          return NS_ERROR_UENC_NOMAPPING;
        }
        *out++ = '?';
        continue;
      }
      isAstral = true;
      lowBits = (char16_t)(codePoint & 0xFFFF);
    } else {
      // not a surrogate
      if (mUtf16Lead) {
        // Non-surrogate follows a high surrogate. The *previous*
        // code unit is in error.
        mUtf16Lead = 0;
        if (mSignal) {
          // NOTE: Encode API differs from decode API!
          --in;
          *aSrcLength = in - aSrc;
          *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
          return NS_ERROR_UENC_NOMAPPING;
        }
        *out++ = '?';
        // Let's unconsume this code unit and reloop in order to
        // re-check if the output buffer still has space.
        --in;
        continue;
      }
      isAstral = false;
      lowBits = codeUnit;
    }
    // isAstral now tells us if we have a Plane 2 or a BMP character.
    // lowBits tells us the low 16 bits.
    // After all the above setup to deal with UTF-16, we are now
    // finally ready to follow the spec.
    if (!isAstral && lowBits <= 0x7F) {
      *out++ = (uint8_t)lowBits;
      continue;
    }
    size_t pointer = nsBIG5Data::FindPointer(lowBits, isAstral);
    if (!pointer) {
      if (mSignal) {
        // NOTE: Encode API differs from decode API!
        if (isAstral) {
          // nsSaveAsCharset wants us to back up on step in the case of a
          // surrogate pair.
          --in;
        }
        *aSrcLength = in - aSrc;
        *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
        return NS_ERROR_UENC_NOMAPPING;
      }
      *out++ = '?';
      continue;
    }
    uint8_t lead = (uint8_t)(pointer / 157 + 0x81);
    uint8_t trail = (uint8_t)(pointer % 157);
    if (trail < 0x3F) {
      trail += 0x40;
    } else {
      trail += 0x62;
    }
    *out++ = lead;
    if (out == outEnd) {
      mPendingTrail = trail;
      *aSrcLength = in - aSrc;
      *aDestLength = out - reinterpret_cast<uint8_t*>(aDest);
      return NS_OK_UENC_MOREOUTPUT;
    }
    *out++ = trail;
    continue;
  }
}

NS_IMETHODIMP
nsUnicodeToBIG5::Finish(char* aDest,
                        int32_t* aDestLength)
{
  MOZ_ASSERT(!(mPendingTrail && mUtf16Lead),
             "Can't have both pending output and pending input.");
  uint8_t* out = reinterpret_cast<uint8_t*>(aDest);
  if (mPendingTrail) {
    if (*aDestLength < 1) {
      *aDestLength = 0;
      return NS_OK_UENC_MOREOUTPUT;
    }
    *out = mPendingTrail;
    mPendingTrail = 0;
    *aDestLength = 1;
    return NS_OK;
  }
  if (mUtf16Lead) {
    if (*aDestLength < 1) {
      *aDestLength = 0;
      return NS_OK_UENC_MOREOUTPUT;
    }
    mUtf16Lead = 0;
    if (mSignal) {
      *aDestLength = 0;
      return NS_ERROR_UENC_NOMAPPING;
    }
    *out = '?';
    *aDestLength = 1;
    return NS_OK;
  }
  *aDestLength = 0;
  return NS_OK;
}

NS_IMETHODIMP
nsUnicodeToBIG5::GetMaxLength(const char16_t* aSrc,
                              int32_t aSrcLength,
                              int32_t* aDestLength)
{
  *aDestLength = (aSrcLength * 2) +
                 (mPendingTrail ? 1 : 0) +
                 // If the lead ends up being paired, the bytes produced
                 // are already included above.
                 // If not, it produces a single '?'.
                 (mUtf16Lead ? 1 : 0);
  return NS_OK;
}

NS_IMETHODIMP
nsUnicodeToBIG5::Reset()
{
  mUtf16Lead = 0;
  mPendingTrail = 0;
  return NS_OK;
}

NS_IMETHODIMP
nsUnicodeToBIG5::SetOutputErrorBehavior(int32_t aBehavior,
                                        nsIUnicharEncoder* aEncoder,
                                        char16_t aChar)
{
  switch (aBehavior) {
    case kOnError_Signal:
      mSignal = true;
      break;
    case kOnError_Replace:
      mSignal = false;
      MOZ_ASSERT(aChar == '?', "Unsupported replacement.");
      break;
    case kOnError_CallBack:
      MOZ_ASSERT_UNREACHABLE("kOnError_CallBack is supposed to be unused.");
      break;
    default:
      MOZ_ASSERT_UNREACHABLE("Non-existent enum item.");
      break;
    }
    return NS_OK;
}