Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / intl / uconv / ucvja / nsUnicodeToISO2022JP.cpp
blob92aa9eaa5f950eb4ef61dda1a16e5595a72bfdf5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsUnicodeToISO2022JP.h"
39 #include "nsIComponentManager.h"
40 #include "nsUCVJADll.h"
41 #include "nsUnicodeEncodeHelper.h"
43 //----------------------------------------------------------------------
44 // Global functions and data [declaration]
46 static const PRUint16 g_ufAsciiMapping [] = {
47 0x0001, 0x0004, 0x0005, 0x0008, 0x0000, 0x0000, 0x007F, 0x0000
50 #define SIZE_OF_TABLES 5
51 static const PRUint16 * g_ufMappingTables[SIZE_OF_TABLES] = {
52 g_ufAsciiMapping, // ASCII ISOREG 6
53 g_uf0201GLMapping, // JIS X 0201-1976 ISOREG 14
54 g_uf0208Mapping, // JIS X 0208-1983 ISOREG 87
55 g_uf0208extMapping, // JIS X 0208 - cp932 ext
56 g_uf0208Mapping, // JIS X 0208-1978 ISOREG 42
59 static const uScanClassID g_ufScanClassIDs[SIZE_OF_TABLES] = {
60 u1ByteCharset, // ASCII ISOREG 6
61 u1ByteCharset, // JIS X 0201-1976 ISOREG 14
62 u2BytesCharset, // JIS X 0208-1983 ISOREG 87
63 u2BytesCharset, // JIS X 0208- cp932 ext
64 u2BytesCharset, // JIS X 0208-1978 ISOREG 42
67 //----------------------------------------------------------------------
68 // Class nsUnicodeToISO2022JP [implementation]
70 // worst case max length:
71 // 1 2 3 4 5 6 7 8
72 // ESC $ B XX XX ESC ( B
73 nsUnicodeToISO2022JP::nsUnicodeToISO2022JP()
74 : nsEncoderSupport(8)
76 Reset();
79 nsUnicodeToISO2022JP::~nsUnicodeToISO2022JP()
83 nsresult nsUnicodeToISO2022JP::ChangeCharset(PRInt32 aCharset,
84 char * aDest,
85 PRInt32 * aDestLength)
87 // both 2 and 3 generate the same escape sequence. 2 is for
88 // the standard JISx0208 table, and 3 is for theCP932 extensions
89 // therefore, we treat them as the same one.
90 if(((2 == aCharset) && ( 3 == mCharset)) ||
91 ((3 == aCharset) && ( 2 == mCharset)) )
93 mCharset = aCharset;
96 if(aCharset == mCharset)
98 *aDestLength = 0;
99 return NS_OK;
102 if (*aDestLength < 3) {
103 *aDestLength = 0;
104 return NS_OK_UENC_MOREOUTPUT;
107 switch (aCharset) {
108 case 0: // ASCII ISOREG 6
109 aDest[0] = 0x1b;
110 aDest[1] = '(';
111 aDest[2] = 'B';
112 break;
113 case 1: // JIS X 0201-1976 ("Roman" set) ISOREG 14
114 aDest[0] = 0x1b;
115 aDest[1] = '(';
116 aDest[2] = 'J';
117 break;
118 case 2: // JIS X 0208-1983 ISOREG 87
119 case 3: // JIS X 0208-1983
120 // we currently use this for CP932 ext
121 aDest[0] = 0x1b;
122 aDest[1] = '$';
123 aDest[2] = 'B';
124 break;
125 case 4: // JIS X 0201-1978 ISOREG 87-
126 // we currently do not have a diff mapping for it.
127 aDest[0] = 0x1b;
128 aDest[1] = '$';
129 aDest[2] = '@';
130 break;
133 mCharset = aCharset;
134 *aDestLength = 3;
135 return NS_OK;
138 //----------------------------------------------------------------------
139 // Subclassing of nsTableEncoderSupport class [implementation]
141 NS_IMETHODIMP nsUnicodeToISO2022JP::FillInfo(PRUint32* aInfo)
143 return nsUnicodeEncodeHelper::FillInfo(aInfo, SIZE_OF_TABLES,
144 (uMappingTable **) g_ufMappingTables);
147 NS_IMETHODIMP nsUnicodeToISO2022JP::ConvertNoBuffNoErr(
148 const PRUnichar * aSrc,
149 PRInt32 * aSrcLength,
150 char * aDest,
151 PRInt32 * aDestLength)
153 nsresult res = NS_OK;
155 const PRUnichar * src = aSrc;
156 const PRUnichar * srcEnd = aSrc + *aSrcLength;
157 char * dest = aDest;
158 char * destEnd = aDest + *aDestLength;
159 PRInt32 bcr, bcw;
160 PRInt32 i;
162 while (src < srcEnd) {
163 for (i=0; i< SIZE_OF_TABLES ; i++) {
164 bcr = 1;
165 bcw = destEnd - dest;
166 res = nsUnicodeEncodeHelper::ConvertByTable(src, &bcr, dest, &bcw,
167 g_ufScanClassIDs[i], nsnull,
168 (uMappingTable *) g_ufMappingTables[i]);
169 if (res != NS_ERROR_UENC_NOMAPPING) break;
172 if ( i>= SIZE_OF_TABLES) {
173 res = NS_ERROR_UENC_NOMAPPING;
174 src++;
176 if (res != NS_OK) break;
178 bcw = destEnd - dest;
179 res = ChangeCharset(i, dest, &bcw);
180 dest += bcw;
181 if (res != NS_OK) break;
183 bcr = srcEnd - src;
184 bcw = destEnd - dest;
185 res = nsUnicodeEncodeHelper::ConvertByTable(src, &bcr, dest, &bcw,
186 g_ufScanClassIDs[i], nsnull,
187 (uMappingTable *) g_ufMappingTables[i]);
188 src += bcr;
189 dest += bcw;
191 if ((res != NS_OK) && (res != NS_ERROR_UENC_NOMAPPING)) break;
192 if (res == NS_ERROR_UENC_NOMAPPING) src--;
195 *aSrcLength = src - aSrc;
196 *aDestLength = dest - aDest;
197 return res;
200 NS_IMETHODIMP nsUnicodeToISO2022JP::FinishNoBuff(char * aDest,
201 PRInt32 * aDestLength)
203 ChangeCharset(0, aDest, aDestLength);
204 return NS_OK;
207 NS_IMETHODIMP nsUnicodeToISO2022JP::Reset()
209 mCharset = 0;
210 return nsEncoderSupport::Reset();