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
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.
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:
72 // ESC $ B XX XX ESC ( B
73 nsUnicodeToISO2022JP::nsUnicodeToISO2022JP()
79 nsUnicodeToISO2022JP::~nsUnicodeToISO2022JP()
83 nsresult
nsUnicodeToISO2022JP::ChangeCharset(PRInt32 aCharset
,
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
)) )
96 if(aCharset
== mCharset
)
102 if (*aDestLength
< 3) {
104 return NS_OK_UENC_MOREOUTPUT
;
108 case 0: // ASCII ISOREG 6
113 case 1: // JIS X 0201-1976 ("Roman" set) ISOREG 14
118 case 2: // JIS X 0208-1983 ISOREG 87
119 case 3: // JIS X 0208-1983
120 // we currently use this for CP932 ext
125 case 4: // JIS X 0201-1978 ISOREG 87-
126 // we currently do not have a diff mapping for it.
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
,
151 PRInt32
* aDestLength
)
153 nsresult res
= NS_OK
;
155 const PRUnichar
* src
= aSrc
;
156 const PRUnichar
* srcEnd
= aSrc
+ *aSrcLength
;
158 char * destEnd
= aDest
+ *aDestLength
;
162 while (src
< srcEnd
) {
163 for (i
=0; i
< SIZE_OF_TABLES
; i
++) {
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
;
176 if (res
!= NS_OK
) break;
178 bcw
= destEnd
- dest
;
179 res
= ChangeCharset(i
, dest
, &bcw
);
181 if (res
!= NS_OK
) break;
184 bcw
= destEnd
- dest
;
185 res
= nsUnicodeEncodeHelper::ConvertByTable(src
, &bcr
, dest
, &bcw
,
186 g_ufScanClassIDs
[i
], nsnull
,
187 (uMappingTable
*) g_ufMappingTables
[i
]);
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
;
200 NS_IMETHODIMP
nsUnicodeToISO2022JP::FinishNoBuff(char * aDest
,
201 PRInt32
* aDestLength
)
203 ChangeCharset(0, aDest
, aDestLength
);
207 NS_IMETHODIMP
nsUnicodeToISO2022JP::Reset()
210 return nsEncoderSupport::Reset();