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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 : /**
38 : * A character set converter from Unicode to HZ.
39 : *
40 : *
41 : * @created 08/Sept/1999
42 : * @author Yueheng Xu, Yueheng.Xu@intel.com
43 : * Revision History
44 : * 04/Oct/1999. Yueheng Xu: Fixed line continuation problem when line
45 : * ended by '~';
46 : * Used table UnicodeToGBK[] to speed up the mapping.
47 : */
48 : #include "nsUnicodeToHZ.h"
49 : #include "nsUCvCnDll.h"
50 : #include "gbku.h"
51 : //----------------------------------------------------------------------
52 : // Class nsUnicodeToGBK [implementation]
53 : #define HZ_STATE_GB 1
54 : #define HZ_STATE_ASCII 2
55 : #define HZ_STATE_TILD 3
56 : #define HZLEAD1 '~'
57 : #define HZLEAD2 '{'
58 : #define HZLEAD3 '}'
59 : #define UNICODE_TILD 0x007E
60 6 : nsUnicodeToHZ::nsUnicodeToHZ() : nsEncoderSupport(6)
61 : {
62 6 : mUtil.InitToGBKTable();
63 6 : mHZState = HZ_STATE_ASCII; // per HZ spec, default to HZ mode
64 6 : }
65 4 : NS_IMETHODIMP nsUnicodeToHZ::ConvertNoBuff(
66 : const PRUnichar * aSrc,
67 : PRInt32 * aSrcLength,
68 : char * aDest,
69 : PRInt32 * aDestLength)
70 : {
71 4 : PRInt32 i=0;
72 4 : PRInt32 iSrcLength = *aSrcLength;
73 4 : PRInt32 iDestLength = 0;
74 :
75 40 : for (i=0;i< iSrcLength;i++)
76 : {
77 36 : if(! IS_ASCII(*aSrc))
78 : {
79 : // hi byte has something, it is not ASCII, process as a GB
80 2 : if ( mHZState != HZ_STATE_GB )
81 : {
82 : // we are adding a '~{' ESC sequence to star a HZ string
83 2 : mHZState = HZ_STATE_GB;
84 2 : aDest[0] = '~';
85 2 : aDest[1] = '{';
86 2 : aDest += 2; // increment 2 bytes
87 2 : iDestLength +=2;
88 : }
89 2 : if(mUtil.UnicodeToGBKChar(*aSrc, true, &aDest[0], &aDest[1])) {
90 2 : aDest += 2; // increment 2 bytes
91 2 : iDestLength +=2;
92 : } else {
93 : // some thing that we cannot convert
94 : // xxx fix me ftang
95 : // error handling here
96 : }
97 : } else {
98 : // this is an ASCII
99 :
100 : // if we are in HZ mode, end it by adding a '~}' ESC sequence
101 34 : if ( mHZState == HZ_STATE_GB )
102 : {
103 1 : mHZState = HZ_STATE_ASCII;
104 1 : aDest[0] = '~';
105 1 : aDest[1] = '}';
106 1 : aDest += 2; // increment 2 bytes
107 1 : iDestLength +=2;
108 : }
109 :
110 : // if this is a regular char '~' , convert it to two '~'
111 34 : if ( *aSrc == UNICODE_TILD )
112 : {
113 0 : aDest[0] = '~';
114 0 : aDest[1] = '~';
115 0 : aDest += 2; // increment 2 bytes
116 0 : iDestLength +=2;
117 : } else {
118 : // other regular ASCII chars convert by normal ways
119 :
120 : // Is this works for both little endian and big endian machines ?
121 34 : *aDest = (char) ( (PRUnichar)(*aSrc) );
122 34 : aDest++; // increment 1 byte
123 34 : iDestLength +=1;
124 : }
125 : }
126 36 : aSrc++; // increment 2 bytes
127 36 : if ( iDestLength >= (*aDestLength) )
128 : {
129 0 : break;
130 : }
131 : }
132 4 : *aDestLength = iDestLength;
133 4 : *aSrcLength = i;
134 4 : return NS_OK;
135 : }
136 :
137 65154 : NS_IMETHODIMP nsUnicodeToHZ::FinishNoBuff(char * aDest, PRInt32 * aDestLength)
138 : {
139 65154 : if ( mHZState == HZ_STATE_GB )
140 : {
141 : // if we are in HZ mode, end it by adding a '~}' ESC sequence
142 1 : mHZState = HZ_STATE_ASCII;
143 1 : aDest[0] = '~';
144 1 : aDest[1] = '}';
145 1 : *aDestLength = 2;
146 : } else {
147 65153 : *aDestLength = 0;
148 : }
149 65154 : return NS_OK;
150 : }
|