Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / intl / uconv / ucvko / nsISO2022KRToUnicode.cpp
blob1168ac9d3a99f107797ab7793896fd6dca0901f3
1 /* -*- Mode: C; tab-width: 4; 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.org 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):
23 * Jungshik Shin <jshin@mailaps.org>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38 #include "nsISO2022KRToUnicode.h"
39 #include "nsUCSupport.h"
40 #include "nsICharsetConverterManager.h"
41 #include "nsIServiceManager.h"
43 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
45 NS_IMETHODIMP nsISO2022KRToUnicode::Convert(const char * aSrc, PRInt32 * aSrcLen, PRUnichar * aDest, PRInt32 * aDestLen)
47 const unsigned char* srcEnd = (unsigned char*)aSrc + *aSrcLen;
48 const unsigned char* src =(unsigned char*) aSrc;
49 PRUnichar* destEnd = aDest + *aDestLen;
50 PRUnichar* dest = aDest;
51 while((src < srcEnd))
53 switch(mState)
55 case mState_Init:
56 if(0x1b == *src) {
57 mLastLegalState = mState_ASCII;
58 mState = mState_ESC;
59 break;
61 mState = mState_ASCII;
62 // fall through
64 case mState_ASCII:
65 if(0x0e == *src) { // Shift-Out
66 mState = mState_KSX1001_1992;
67 mRunLength = 0;
69 else if(*src & 0x80) {
70 *dest++ = 0xFFFD;
71 if(dest >= destEnd)
72 goto error1;
74 else {
75 *dest++ = (PRUnichar) *src;
76 if(dest >= destEnd)
77 goto error1;
79 break;
81 case mState_ESC:
82 if('$' == *src) {
83 mState = mState_ESC_24;
85 else {
86 if((dest+2) >= destEnd)
87 goto error1;
88 *dest++ = (PRUnichar) 0x1b;
89 *dest++ = (0x80 & *src) ? 0xFFFD : (PRUnichar) *src;
90 mState = mLastLegalState;
92 break;
94 case mState_ESC_24: // ESC $
95 if(')' == *src) {
96 mState = mState_ESC_24_29;
98 else {
99 if((dest+3) >= destEnd)
100 goto error1;
101 *dest++ = (PRUnichar) 0x1b;
102 *dest++ = (PRUnichar) '$';
103 *dest++ = (0x80 & *src) ? 0xFFFD : (PRUnichar) *src;
104 mState = mLastLegalState;
106 break;
108 case mState_ESC_24_29: // ESC $ )
109 mState = mLastLegalState;
110 if('C' == *src) {
111 mState = mState_ASCII;
112 mRunLength = 0;
114 else {
115 if((dest+4) >= destEnd)
116 goto error1;
117 *dest++ = (PRUnichar) 0x1b;
118 *dest++ = (PRUnichar) '$';
119 *dest++ = (PRUnichar) ')';
120 *dest++ = (0x80 & *src) ? 0xFFFD : (PRUnichar) *src;
121 mState = mLastLegalState;
123 break;
125 case mState_KSX1001_1992:
126 if (0x20 < (PRUint8) *src && (PRUint8) *src < 0x7f) {
127 mData = (PRUint8) *src;
128 mState = mState_KSX1001_1992_2ndbyte;
130 else if (0x0f == *src) { // Shift-In (SI)
131 mState = mState_ASCII;
132 if (mRunLength == 0) {
133 if(dest+1 >= destEnd)
134 goto error1;
135 *dest++ = 0xFFFD;
137 mRunLength = 0;
139 else if ((PRUint8) *src == 0x20 || (PRUint8) *src == 0x09) {
140 // Allow space and tab between SO and SI (i.e. in Hangul segment)
141 mState = mState_KSX1001_1992;
142 *dest++ = (PRUnichar) *src;
143 ++mRunLength;
144 if(dest >= destEnd)
145 goto error1;
147 else { // Everything else is invalid.
148 *dest++ = 0xFFFD;
149 if(dest >= destEnd)
150 goto error1;
152 break;
154 case mState_KSX1001_1992_2ndbyte:
155 if ( 0x20 < (PRUint8) *src && (PRUint8) *src < 0x7f ) {
156 if (!mEUCKRDecoder) {
157 // creating a delegate converter (EUC-KR)
158 nsresult rv;
159 nsCOMPtr<nsICharsetConverterManager> ccm =
160 do_GetService(kCharsetConverterManagerCID, &rv);
161 if (NS_SUCCEEDED(rv)) {
162 rv = ccm->GetUnicodeDecoderRaw("EUC-KR", &mEUCKRDecoder);
166 if (!mEUCKRDecoder) {// failed creating a delegate converter
167 *dest++ = 0xFFFD;
169 else {
170 unsigned char ksx[2];
171 PRUnichar uni;
172 PRInt32 ksxLen = 2, uniLen = 1;
173 // mData is the original 1st byte.
174 // *src is the present 2nd byte.
175 // Put 2 bytes (one character) to ksx[] with EUC-KR encoding.
176 ksx[0] = mData | 0x80;
177 ksx[1] = *src | 0x80;
178 // Convert EUC-KR to unicode.
179 mEUCKRDecoder->Convert((const char *)ksx, &ksxLen, &uni, &uniLen);
180 *dest++ = uni;
181 ++mRunLength;
183 if(dest >= destEnd)
184 goto error1;
185 mState = mState_KSX1001_1992;
187 else { // Invalid
188 if ( 0x0f == *src ) { // Shift-In (SI)
189 mState = mState_ASCII;
191 else {
192 mState = mState_KSX1001_1992;
194 *dest++ = 0xFFFD;
195 if(dest >= destEnd)
196 goto error1;
198 break;
200 case mState_ERROR:
201 mState = mLastLegalState;
202 *dest++ = 0xFFFD;
203 if(dest >= destEnd)
204 goto error1;
205 break;
207 } // switch
208 src++;
209 if ( *src == 0x0a || *src == 0x0d ) // if LF/CR, return to US-ASCII unconditionally.
210 mState = mState_Init;
212 *aDestLen = dest - aDest;
213 return NS_OK;
215 error1:
216 *aDestLen = dest-aDest;
217 *aSrcLen = src-(unsigned char*)aSrc;
218 return NS_OK_UDEC_MOREOUTPUT;