summaryrefslogtreecommitdiffstats
path: root/intl/uconv/util/nsUnicodeEncodeHelper.cpp
blob: f801e81669d50671349fd24ba38d0cbb31678f9a (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
/* -*- 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 "unicpriv.h"
#include "nsUnicodeEncodeHelper.h"
#include "nsDebug.h"

//----------------------------------------------------------------------
// Class nsUnicodeEncodeHelper [implementation]
nsresult nsUnicodeEncodeHelper::ConvertByTable(
                                     const char16_t * aSrc, 
                                     int32_t * aSrcLength, 
                                     char * aDest, 
                                     int32_t * aDestLength, 
                                     uScanClassID aScanClass,
                                     uShiftOutTable * aShiftOutTable,
                                     uMappingTable  * aMappingTable)
{
  const char16_t * src = aSrc;
  const char16_t * srcEnd = aSrc + *aSrcLength;
  char * dest = aDest;
  int32_t destLen = *aDestLength;

  char16_t med;
  int32_t bcw; // byte count for write;
  nsresult res = NS_OK;

  while (src < srcEnd) {
    if (!uMapCode((uTable*) aMappingTable, static_cast<char16_t>(*(src++)), reinterpret_cast<uint16_t*>(&med))) {
      if (aScanClass == u1ByteCharset && *(src - 1) < 0x20) {
        // some tables are missing the 0x00 - 0x20 part
        med = *(src - 1);
      } else {
        res = NS_ERROR_UENC_NOMAPPING;
        break;
      }
    }

    bool charFound;
    if (aScanClass == uMultibytesCharset) {
      NS_ASSERTION(aShiftOutTable, "shift table missing");
      charFound = uGenerateShift(aShiftOutTable, 0, med,
                                 (uint8_t *)dest, destLen, 
                                 (uint32_t *)&bcw);
    } else {
      charFound = uGenerate(aScanClass, 0, med,
                            (uint8_t *)dest, destLen, 
                            (uint32_t *)&bcw);
    }
    if (!charFound) {
      src--;
      res = NS_OK_UENC_MOREOUTPUT;
      break;
    }

    dest += bcw;
    destLen -= bcw;
  }

  *aSrcLength = src - aSrc;
  *aDestLength  = dest - aDest;
  return res;
}

nsresult nsUnicodeEncodeHelper::ConvertByMultiTable(
                                     const char16_t * aSrc, 
                                     int32_t * aSrcLength, 
                                     char * aDest, 
                                     int32_t * aDestLength, 
                                     int32_t aTableCount, 
                                     uScanClassID * aScanClassArray,
                                     uShiftOutTable ** aShiftOutTable, 
                                     uMappingTable  ** aMappingTable)
{
  const char16_t * src = aSrc;
  const char16_t * srcEnd = aSrc + *aSrcLength;
  char * dest = aDest;
  int32_t destLen = *aDestLength;

  char16_t med;
  int32_t bcw; // byte count for write;
  nsresult res = NS_OK;
  int32_t i;

  while (src < srcEnd) {
    for (i=0; i<aTableCount; i++) 
      if (uMapCode((uTable*) aMappingTable[i], static_cast<uint16_t>(*src), reinterpret_cast<uint16_t*>(&med))) break;

    src++;
    if (i == aTableCount) {
      res = NS_ERROR_UENC_NOMAPPING;
      break;
    }

    bool charFound;
    if (aScanClassArray[i] == uMultibytesCharset) {
      NS_ASSERTION(aShiftOutTable[i], "shift table missing");
      charFound = uGenerateShift(aShiftOutTable[i], 0, med,
                                 (uint8_t *)dest, destLen,
                                 (uint32_t *)&bcw);
    }
    else
      charFound = uGenerate(aScanClassArray[i], 0, med,
                            (uint8_t *)dest, destLen, 
                            (uint32_t *)&bcw);
    if (!charFound) { 
      src--;
      res = NS_OK_UENC_MOREOUTPUT;
      break;
    }

    dest += bcw;
    destLen -= bcw;
  }

  *aSrcLength = src - aSrc;
  *aDestLength  = dest - aDest;
  return res;
}