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
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.
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
;
57 mLastLegalState
= mState_ASCII
;
61 mState
= mState_ASCII
;
65 if(0x0e == *src
) { // Shift-Out
66 mState
= mState_KSX1001_1992
;
69 else if(*src
& 0x80) {
75 *dest
++ = (PRUnichar
) *src
;
83 mState
= mState_ESC_24
;
86 if((dest
+2) >= destEnd
)
88 *dest
++ = (PRUnichar
) 0x1b;
89 *dest
++ = (0x80 & *src
) ? 0xFFFD : (PRUnichar
) *src
;
90 mState
= mLastLegalState
;
94 case mState_ESC_24
: // ESC $
96 mState
= mState_ESC_24_29
;
99 if((dest
+3) >= destEnd
)
101 *dest
++ = (PRUnichar
) 0x1b;
102 *dest
++ = (PRUnichar
) '$';
103 *dest
++ = (0x80 & *src
) ? 0xFFFD : (PRUnichar
) *src
;
104 mState
= mLastLegalState
;
108 case mState_ESC_24_29
: // ESC $ )
109 mState
= mLastLegalState
;
111 mState
= mState_ASCII
;
115 if((dest
+4) >= destEnd
)
117 *dest
++ = (PRUnichar
) 0x1b;
118 *dest
++ = (PRUnichar
) '$';
119 *dest
++ = (PRUnichar
) ')';
120 *dest
++ = (0x80 & *src
) ? 0xFFFD : (PRUnichar
) *src
;
121 mState
= mLastLegalState
;
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
)
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
;
147 else { // Everything else is invalid.
154 case mState_KSX1001_1992_2ndbyte
:
155 if ( 0x20 < (PRUint8
) *src
&& (PRUint8
) *src
< 0x7f ) {
156 if (!mEUCKRDecoder
) {
157 // creating a delegate converter (EUC-KR)
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
170 unsigned char ksx
[2];
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
);
185 mState
= mState_KSX1001_1992
;
188 if ( 0x0f == *src
) { // Shift-In (SI)
189 mState
= mState_ASCII
;
192 mState
= mState_KSX1001_1992
;
201 mState
= mLastLegalState
;
209 if ( *src
== 0x0a || *src
== 0x0d ) // if LF/CR, return to US-ASCII unconditionally.
210 mState
= mState_Init
;
212 *aDestLen
= dest
- aDest
;
216 *aDestLen
= dest
-aDest
;
217 *aSrcLen
= src
-(unsigned char*)aSrc
;
218 return NS_OK_UDEC_MOREOUTPUT
;