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
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 Communicator client 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 : #include "pratom.h"
39 : #include "unicpriv.h"
40 : #include "nsIUnicodeDecoder.h"
41 : #include "nsUnicodeDecodeHelper.h"
42 :
43 : //----------------------------------------------------------------------
44 : // Class nsUnicodeDecodeHelper [implementation]
45 1170 : nsresult nsUnicodeDecodeHelper::ConvertByTable(
46 : const char * aSrc,
47 : PRInt32 * aSrcLength,
48 : PRUnichar * aDest,
49 : PRInt32 * aDestLength,
50 : uScanClassID aScanClass,
51 : uShiftInTable * aShiftInTable,
52 : uMappingTable * aMappingTable,
53 : bool aErrorSignal)
54 : {
55 1170 : const char * src = aSrc;
56 1170 : PRInt32 srcLen = *aSrcLength;
57 1170 : PRUnichar * dest = aDest;
58 1170 : PRUnichar * destEnd = aDest + *aDestLength;
59 :
60 : PRUnichar med;
61 : PRInt32 bcr; // byte count for read
62 1170 : nsresult res = NS_OK;
63 :
64 234795 : while ((srcLen > 0) && (dest < destEnd)) {
65 : bool charFound;
66 232455 : if (aScanClass == uMultibytesCharset) {
67 0 : NS_ASSERTION(aShiftInTable, "shift table missing");
68 : charFound = uScanShift(aShiftInTable, NULL, (PRUint8 *)src,
69 : reinterpret_cast<PRUint16*>(&med), srcLen,
70 0 : (PRUint32 *)&bcr);
71 : } else {
72 : charFound = uScan(aScanClass, NULL, (PRUint8 *)src,
73 : reinterpret_cast<PRUint16*>(&med),
74 232455 : srcLen, (PRUint32 *)&bcr);
75 : }
76 232455 : if (!charFound) {
77 0 : res = NS_OK_UDEC_MOREINPUT;
78 0 : break;
79 : }
80 :
81 232455 : if (!uMapCode((uTable*) aMappingTable, static_cast<PRUint16>(med), reinterpret_cast<PRUint16*>(dest))) {
82 2731 : if (med < 0x20) {
83 : // somehow some table miss the 0x00 - 0x20 part
84 640 : *dest = med;
85 : } else {
86 2091 : if (aErrorSignal) {
87 0 : res = NS_ERROR_ILLEGAL_INPUT;
88 0 : break;
89 : }
90 : // Unicode replacement value for unmappable chars
91 2091 : *dest = 0xfffd;
92 : }
93 : }
94 :
95 232455 : src += bcr;
96 232455 : srcLen -= bcr;
97 232455 : dest++;
98 : }
99 :
100 1170 : if ((srcLen > 0) && (res == NS_OK)) res = NS_OK_UDEC_MOREOUTPUT;
101 :
102 1170 : *aSrcLength = src - aSrc;
103 1170 : *aDestLength = dest - aDest;
104 1170 : return res;
105 : }
106 :
107 270081 : nsresult nsUnicodeDecodeHelper::ConvertByMultiTable(
108 : const char * aSrc,
109 : PRInt32 * aSrcLength,
110 : PRUnichar * aDest,
111 : PRInt32 * aDestLength,
112 : PRInt32 aTableCount,
113 : const uRange * aRangeArray,
114 : uScanClassID * aScanClassArray,
115 : uMappingTable ** aMappingTable,
116 : bool aErrorSignal)
117 : {
118 270081 : PRUint8 * src = (PRUint8 *)aSrc;
119 270081 : PRInt32 srcLen = *aSrcLength;
120 270081 : PRUnichar * dest = aDest;
121 270081 : PRUnichar * destEnd = aDest + *aDestLength;
122 :
123 : PRUnichar med;
124 : PRInt32 bcr; // byte count for read
125 270081 : nsresult res = NS_OK;
126 : PRInt32 i;
127 :
128 2344944 : while ((srcLen > 0) && (dest < destEnd))
129 : {
130 1805096 : bool done= false;
131 1805096 : bool passRangeCheck = false;
132 1805096 : bool passScan = false;
133 5550803 : for (i=0; (!done) && (i<aTableCount); i++)
134 : {
135 3745707 : if ((aRangeArray[i].min <= *src) && (*src <= aRangeArray[i].max))
136 : {
137 2467818 : passRangeCheck = true;
138 4935636 : if (uScan(aScanClassArray[i], NULL, src,
139 : reinterpret_cast<PRUint16*>(&med), srcLen,
140 4935636 : (PRUint32 *)&bcr))
141 : {
142 1824955 : passScan = true;
143 1824955 : done = uMapCode((uTable*) aMappingTable[i],
144 : static_cast<PRUint16>(med),
145 3649910 : reinterpret_cast<PRUint16*>(dest));
146 : } // if (uScan ... )
147 : } // if Range
148 : } // for loop
149 :
150 1805096 : if(passRangeCheck && (! passScan))
151 : {
152 314 : if (res != NS_ERROR_ILLEGAL_INPUT)
153 314 : res = NS_OK_UDEC_MOREINPUT;
154 314 : break;
155 : }
156 1804782 : if(! done)
157 : {
158 68836 : bcr = 1;
159 68836 : if ((PRUint8)*src < 0x20) {
160 : // somehow some table miss the 0x00 - 0x20 part
161 0 : *dest = *src;
162 68836 : } else if(*src == (PRUint8) 0xa0) {
163 : // handle nbsp
164 387 : *dest = 0x00a0;
165 : } else {
166 : // we need to decide how many byte we skip. We can use uScan to do this
167 237477 : for (i=0; i<aTableCount; i++)
168 : {
169 227553 : if ((aRangeArray[i].min <= *src) && (*src <= aRangeArray[i].max))
170 : {
171 135622 : if (uScan(aScanClassArray[i], NULL, src,
172 : reinterpret_cast<PRUint16*>(&med), srcLen,
173 135622 : (PRUint32*)&bcr))
174 : {
175 : // match the patten
176 :
177 : PRInt32 k;
178 73036 : for(k = 1; k < bcr; k++)
179 : {
180 58525 : if(0 == (src[k] & 0x80))
181 : { // only skip if all bytes > 0x80
182 : // if we hit bytes <= 0x80, skip only one byte
183 44014 : bcr = 1;
184 44014 : break;
185 : }
186 : }
187 58525 : break;
188 : }
189 : }
190 : }
191 : // treat it as NSBR if bcr == 1 and it is 0xa0
192 68449 : if ((1==bcr)&&(*src == (PRUint8)0xa0 )) {
193 0 : *dest = 0x00a0;
194 : } else {
195 68449 : if (aErrorSignal) {
196 0 : res = NS_ERROR_ILLEGAL_INPUT;
197 0 : break;
198 : }
199 68449 : *dest = 0xfffd;
200 : }
201 : }
202 : }
203 :
204 1804782 : src += bcr;
205 1804782 : srcLen -= bcr;
206 1804782 : dest++;
207 : } // while
208 :
209 270081 : if ((srcLen > 0) && (res == NS_OK)) res = NS_OK_UDEC_MOREOUTPUT;
210 :
211 270081 : *aSrcLength = src - (PRUint8 *)aSrc;
212 270081 : *aDestLength = dest - aDest;
213 270081 : return res;
214 : }
215 :
216 98705 : nsresult nsUnicodeDecodeHelper::ConvertByFastTable(
217 : const char * aSrc,
218 : PRInt32 * aSrcLength,
219 : PRUnichar * aDest,
220 : PRInt32 * aDestLength,
221 : const PRUnichar * aFastTable,
222 : PRInt32 aTableSize,
223 : bool aErrorSignal)
224 : {
225 98705 : PRUint8 * src = (PRUint8 *)aSrc;
226 98705 : PRUint8 * srcEnd = src;
227 98705 : PRUnichar * dest = aDest;
228 :
229 : nsresult res;
230 98705 : if (*aSrcLength > *aDestLength) {
231 0 : srcEnd += (*aDestLength);
232 0 : res = NS_PARTIAL_MORE_OUTPUT;
233 : } else {
234 98705 : srcEnd += (*aSrcLength);
235 98705 : res = NS_OK;
236 : }
237 :
238 477402 : for (; src<srcEnd;) {
239 279992 : *dest = aFastTable[*src];
240 279992 : if (*dest == 0xfffd && aErrorSignal) {
241 0 : res = NS_ERROR_ILLEGAL_INPUT;
242 0 : break;
243 : }
244 279992 : src++;
245 279992 : dest++;
246 : }
247 :
248 98705 : *aSrcLength = src - (PRUint8 *)aSrc;
249 98705 : *aDestLength = dest - aDest;
250 98705 : return res;
251 : }
252 :
253 907 : nsresult nsUnicodeDecodeHelper::CreateFastTable(
254 : uMappingTable * aMappingTable,
255 : PRUnichar * aFastTable,
256 : PRInt32 aTableSize)
257 : {
258 907 : PRInt32 tableSize = aTableSize;
259 907 : PRInt32 buffSize = aTableSize;
260 1814 : char * buff = new char [buffSize];
261 907 : if (buff == NULL) return NS_ERROR_OUT_OF_MEMORY;
262 :
263 907 : char * p = buff;
264 907 : for (PRInt32 i=0; i<aTableSize; i++) *(p++) = i;
265 : nsresult res = ConvertByTable(buff, &buffSize, aFastTable, &tableSize,
266 907 : u1ByteCharset, nsnull, aMappingTable);
267 :
268 907 : delete [] buff;
269 907 : return res;
270 : }
271 :
|