summaryrefslogtreecommitdiffstats
path: root/intl/uconv/ucvlatin/nsUTF16ToUnicode.cpp
blob: 56c88ff3edd6f7dcb3da99820cedf5658f4f0dd2 (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
/* -*- 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 "nsUTF16ToUnicode.h"
#include "nsCharTraits.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/EndianUtils.h"

enum {
  STATE_NORMAL = 0,
  STATE_HALF_CODE_POINT = 1,
  STATE_FIRST_CALL = 2,
  STATE_SECOND_BYTE = STATE_FIRST_CALL | STATE_HALF_CODE_POINT,
  STATE_ODD_SURROGATE_PAIR = 4
};

nsresult
nsUTF16ToUnicodeBase::UTF16ConvertToUnicode(const char * aSrc,
                                            int32_t * aSrcLength,
                                            char16_t * aDest,
                                            int32_t * aDestLength,
                                            bool aSwapBytes)
{
  const char* src = aSrc;
  const char* srcEnd = aSrc + *aSrcLength;
  char16_t* dest = aDest;
  char16_t* destEnd = aDest + *aDestLength;
  char16_t oddHighSurrogate;

  switch(mState) {
    case STATE_FIRST_CALL:
      NS_ASSERTION(*aSrcLength > 1, "buffer too short");
      src+=2;
      mState = STATE_NORMAL;
      break;

    case STATE_SECOND_BYTE:
      NS_ASSERTION(*aSrcLength > 0, "buffer too short");
      src++;
      mState = STATE_NORMAL;
      break;

    case STATE_ODD_SURROGATE_PAIR:
      if (*aDestLength < 2)
        goto error;
      else {
        *dest++ = mOddHighSurrogate;
        *dest++ = mOddLowSurrogate;
        mOddHighSurrogate = mOddLowSurrogate = 0;
        mState = STATE_NORMAL;
      }
      break;

    case STATE_NORMAL:
    case STATE_HALF_CODE_POINT:
    default:
      break;
  }

  oddHighSurrogate = mOddHighSurrogate;

  if (src == srcEnd) {
    *aDestLength = dest - aDest;
    return (mState != STATE_NORMAL || oddHighSurrogate) ?
           NS_OK_UDEC_MOREINPUT : NS_OK;
  }

  const char* srcEvenEnd;

  char16_t u;
  if (mState == STATE_HALF_CODE_POINT) {
    if (dest == destEnd)
      goto error;

    // the 1st byte of a 16-bit code unit was stored in |mOddByte| in the
    // previous run while the 2nd byte has to come from |*src|.
    mState = STATE_NORMAL;
#if MOZ_BIG_ENDIAN
    u = (mOddByte << 8) | uint8_t(*src++); // safe, we know we have at least one byte.
#else
    u = (*src++ << 8) | mOddByte; // safe, we know we have at least one byte.
#endif
    srcEvenEnd = src + ((srcEnd - src) & ~1); // handle even number of bytes in main loop
    goto have_codepoint;
  } else {
    srcEvenEnd = src + ((srcEnd - src) & ~1); // handle even number of bytes in main loop
  }

  while (src != srcEvenEnd) {
    if (dest == destEnd)
      goto error;

#if !defined(__sparc__) && !defined(__arm__)
    u = *(const char16_t*)src;
#else
    memcpy(&u, src, 2);
#endif
    src += 2;

have_codepoint:
    if (aSwapBytes)
      u = u << 8 | u >> 8;

    if (!IS_SURROGATE(u)) {
      if (oddHighSurrogate) {
        if (mErrBehavior == kOnError_Signal) {
          goto error2;
        }
        *dest++ = UCS2_REPLACEMENT_CHAR;
        if (dest == destEnd)
          goto error;
        oddHighSurrogate = 0;
      }
      *dest++ = u;
    } else if (NS_IS_HIGH_SURROGATE(u)) {
      if (oddHighSurrogate) {
        if (mErrBehavior == kOnError_Signal) {
          goto error2;
        }
        *dest++ = UCS2_REPLACEMENT_CHAR;
        if (dest == destEnd)
          goto error;
      }
      oddHighSurrogate = u;
    }
    else /* if (NS_IS_LOW_SURROGATE(u)) */ {
      if (oddHighSurrogate && *aDestLength > 1) {
        if (dest + 1 >= destEnd) {
          mOddLowSurrogate = u;
          mOddHighSurrogate = oddHighSurrogate;
          mState = STATE_ODD_SURROGATE_PAIR;
          goto error;
        }
        *dest++ = oddHighSurrogate;
        *dest++ = u;
      } else {
        if (mErrBehavior == kOnError_Signal) {
          goto error2;
        }
        *dest++ = UCS2_REPLACEMENT_CHAR;
      }
      oddHighSurrogate = 0;
    }
  }
  if (src != srcEnd) {
    // store the lead byte of a 16-bit unit for the next run.
    mOddByte = *src++;
    mState = STATE_HALF_CODE_POINT;
  }

  mOddHighSurrogate = oddHighSurrogate;

  *aDestLength = dest - aDest;
  *aSrcLength =  src  - aSrc; 
  return (mState != STATE_NORMAL || oddHighSurrogate) ?
         NS_OK_UDEC_MOREINPUT : NS_OK;

error:
  *aDestLength = dest - aDest;
  *aSrcLength =  src  - aSrc; 
  return  NS_OK_UDEC_MOREOUTPUT;

error2:
  *aDestLength = dest - aDest;
  *aSrcLength = --src - aSrc; 
  return  NS_ERROR_ILLEGAL_INPUT;
}

NS_IMETHODIMP
nsUTF16ToUnicodeBase::Reset()
{
  mState = STATE_FIRST_CALL;
  mOddByte = 0;
  mOddHighSurrogate = 0;
  mOddLowSurrogate = 0;
  return NS_OK;
}

NS_IMETHODIMP
nsUTF16ToUnicodeBase::GetMaxLength(const char * aSrc, int32_t aSrcLength, 
                                   int32_t * aDestLength)
{
  mozilla::CheckedInt32 length = aSrcLength;

  if (STATE_HALF_CODE_POINT & mState) {
    length += 1;
  }

  if (!length.isValid()) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  // the left-over data of the previous run have to be taken into account.
  *aDestLength = length.value() / 2;
  if (mOddHighSurrogate)
    (*aDestLength)++;
  if (mOddLowSurrogate)
    (*aDestLength)++;
  return NS_OK;
}


NS_IMETHODIMP
nsUTF16BEToUnicode::Convert(const char * aSrc, int32_t * aSrcLength,
                            char16_t * aDest, int32_t * aDestLength)
{
  switch (mState) {
    case STATE_FIRST_CALL:
      if (*aSrcLength < 2) {
        if (*aSrcLength < 1) {
          *aDestLength = 0;
          return NS_OK;
        }
        if (uint8_t(*aSrc) != 0xFE) {
          mState = STATE_NORMAL;
          break;
        }
        *aDestLength = 0;
        mState = STATE_SECOND_BYTE;
        return NS_OK_UDEC_MOREINPUT;
      }
#if MOZ_LITTLE_ENDIAN
      // on LE machines, BE BOM is 0xFFFE
      if (0xFFFE != *((char16_t*)aSrc)) {
        mState = STATE_NORMAL;
      }
#else
      if (0xFEFF != *((char16_t*)aSrc)) {
        mState = STATE_NORMAL;
      }
#endif
      break;

    case STATE_SECOND_BYTE:
      if (*aSrcLength < 1) {
        *aDestLength = 0;
        return NS_OK_UDEC_MOREINPUT;
      }
      if (uint8_t(*aSrc) != 0xFF) {
        mOddByte = 0xFE;
        mState = STATE_HALF_CODE_POINT;
      }
      break;
  }

  return UTF16ConvertToUnicode(aSrc, aSrcLength, aDest, aDestLength,
                               bool(MOZ_LITTLE_ENDIAN));
}

NS_IMETHODIMP
nsUTF16LEToUnicode::Convert(const char * aSrc, int32_t * aSrcLength,
                            char16_t * aDest, int32_t * aDestLength)
{
  switch (mState) {
    case STATE_FIRST_CALL:
      if (*aSrcLength < 2) {
        if (*aSrcLength < 1) {
          *aDestLength = 0;
          return NS_OK;
        }
        if (uint8_t(*aSrc) != 0xFF) {
          mState = STATE_NORMAL;
          break;
        }
        *aDestLength = 0;
        mState = STATE_SECOND_BYTE;
        return NS_OK_UDEC_MOREINPUT;
      }
#if MOZ_BIG_ENDIAN
      // on BE machines, LE BOM is 0xFFFE
      if (0xFFFE != *((char16_t*)aSrc)) {
        mState = STATE_NORMAL;
      }
#else
      if (0xFEFF != *((char16_t*)aSrc)) {
        mState = STATE_NORMAL;
      }
#endif
      break;

    case STATE_SECOND_BYTE:
      if (*aSrcLength < 1) {
        *aDestLength = 0;
        return NS_OK_UDEC_MOREINPUT;
      }
      if (uint8_t(*aSrc) != 0xFE) {
        mOddByte = 0xFF;
        mState = STATE_HALF_CODE_POINT;
      }
      break;
  }

  return UTF16ConvertToUnicode(aSrc, aSrcLength, aDest, aDestLength,
                               bool(MOZ_BIG_ENDIAN));
}

NS_IMETHODIMP
nsUTF16ToUnicode::Reset()
{
  mEndian = kUnknown;
  mFoundBOM = false;
  return nsUTF16ToUnicodeBase::Reset();
}

NS_IMETHODIMP
nsUTF16ToUnicode::Convert(const char * aSrc, int32_t * aSrcLength,
                          char16_t * aDest, int32_t * aDestLength)
{
    if(STATE_FIRST_CALL == mState && *aSrcLength < 2)
    {
      nsresult res = (*aSrcLength == 0) ? NS_OK : NS_ERROR_ILLEGAL_INPUT;
      *aSrcLength=0;
      *aDestLength=0;
      return res;
    }
    if(STATE_FIRST_CALL == mState) // first time called
    {
      // check if BOM (0xFEFF) is at the beginning, remove it if found, and
      // set mEndian accordingly.
      if(0xFF == uint8_t(aSrc[0]) && 0xFE == uint8_t(aSrc[1])) {
        mEndian = kLittleEndian;
        mFoundBOM = true;
      }
      else if(0xFE == uint8_t(aSrc[0]) && 0xFF == uint8_t(aSrc[1])) {
        mEndian = kBigEndian;
        mFoundBOM = true;
      }
      // BOM is not found, but we can use a simple heuristic to determine
      // the endianness. Assume the first character is [U+0001, U+00FF].
      // Not always valid, but it's very likely to hold for html/xml/css. 
      else if(!aSrc[0] && aSrc[1]) {  // 0x00 0xhh (hh != 00)
        mState = STATE_NORMAL;
        mEndian = kBigEndian;
      }
      else if(aSrc[0] && !aSrc[1]) {  // 0xhh 0x00 (hh != 00)
        mState = STATE_NORMAL;
        mEndian = kLittleEndian;
      }
      else { // Neither BOM nor 'plausible' byte patterns at the beginning.
             // Just assume it's BE (following Unicode standard)
             // and let the garbage show up in the browser. (security concern?)
             // (bug 246194)
        mState = STATE_NORMAL;
        mEndian = kBigEndian;
      }
    }
    
    nsresult rv = UTF16ConvertToUnicode(aSrc, aSrcLength, aDest, aDestLength,
#if MOZ_BIG_ENDIAN
                                        (mEndian == kLittleEndian)
#else
                                        (mEndian == kBigEndian)
#endif
                                        );

    // If BOM is not found and we're to return NS_OK, signal that BOM
    // is not found. Otherwise, return |rv| from |UTF16ConvertToUnicode|
    return (rv == NS_OK && !mFoundBOM) ? NS_OK_UDEC_NOBOMFOUND : rv;
}