|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsUCvJaSupport_h___ |
|
7 #define nsUCvJaSupport_h___ |
|
8 |
|
9 #include "nsCOMPtr.h" |
|
10 #include "nsIUnicodeEncoder.h" |
|
11 #include "nsIUnicodeDecoder.h" |
|
12 #include "uconvutil.h" |
|
13 #include "mozilla/Mutex.h" |
|
14 |
|
15 #define ONE_BYTE_TABLE_SIZE 256 |
|
16 |
|
17 inline bool WillOverrun(char16_t* aDest, char16_t* aDestEnd, uint32_t aLength) |
|
18 { |
|
19 NS_ASSERTION(aDest <= aDestEnd, "Pointer overrun even before check"); |
|
20 return (uint32_t(aDestEnd - aDest) < aLength); |
|
21 } |
|
22 #define CHECK_OVERRUN(dest, destEnd, length) (WillOverrun(dest, destEnd, length)) |
|
23 |
|
24 #ifdef DEBUG |
|
25 // {7AFC9F0A-CFE1-44ea-A755-E3B86AB1226E} |
|
26 #define NS_IBASICDECODER_IID \ |
|
27 { 0x7afc9f0a, 0xcfe1, 0x44ea, { 0xa7, 0x55, 0xe3, 0xb8, 0x6a, 0xb1, 0x22, 0x6e } } |
|
28 |
|
29 // {65968A7B-6467-4c4a-B50A-3E0C97A32F07} |
|
30 #define NS_IBASICENCODER_IID \ |
|
31 { 0x65968a7b, 0x6467, 0x4c4a, { 0xb5, 0xa, 0x3e, 0xc, 0x97, 0xa3, 0x2f, 0x7 } } |
|
32 |
|
33 class nsIBasicDecoder : public nsISupports { |
|
34 public: |
|
35 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBASICDECODER_IID) |
|
36 }; |
|
37 |
|
38 NS_DEFINE_STATIC_IID_ACCESSOR(nsIBasicDecoder, NS_IBASICDECODER_IID) |
|
39 |
|
40 class nsIBasicEncoder : public nsISupports { |
|
41 public: |
|
42 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBASICENCODER_IID) |
|
43 }; |
|
44 |
|
45 NS_DEFINE_STATIC_IID_ACCESSOR(nsIBasicEncoder, NS_IBASICENCODER_IID) |
|
46 |
|
47 #endif |
|
48 |
|
49 //---------------------------------------------------------------------- |
|
50 // Class nsBasicDecoderSupport [declaration] |
|
51 |
|
52 /** |
|
53 * Support class for the Unicode decoders. |
|
54 * |
|
55 * The class source files for this class are in /ucvlatin/nsUCvJaSupport. |
|
56 * However, because these objects requires non-xpcom subclassing, local copies |
|
57 * will be made into the other directories using them. Just don't forget to |
|
58 * keep in sync with the master copy! |
|
59 * |
|
60 * This class implements: |
|
61 * - nsISupports |
|
62 * - nsIUnicodeDecoder |
|
63 * |
|
64 * @created 19/Apr/1999 |
|
65 * @author Catalin Rotaru [CATA] |
|
66 */ |
|
67 class nsBasicDecoderSupport : public nsIUnicodeDecoder |
|
68 #ifdef DEBUG |
|
69 ,public nsIBasicDecoder |
|
70 #endif |
|
71 { |
|
72 NS_DECL_THREADSAFE_ISUPPORTS |
|
73 |
|
74 public: |
|
75 |
|
76 /** |
|
77 * Class constructor. |
|
78 */ |
|
79 nsBasicDecoderSupport(); |
|
80 |
|
81 /** |
|
82 * Class destructor. |
|
83 */ |
|
84 virtual ~nsBasicDecoderSupport(); |
|
85 |
|
86 //-------------------------------------------------------------------- |
|
87 // Interface nsIUnicodeDecoder [declaration] |
|
88 |
|
89 virtual void SetInputErrorBehavior(int32_t aBehavior); |
|
90 virtual char16_t GetCharacterForUnMapped(); |
|
91 |
|
92 protected: |
|
93 int32_t mErrBehavior; |
|
94 }; |
|
95 |
|
96 //---------------------------------------------------------------------- |
|
97 // Class nsBufferDecoderSupport [declaration] |
|
98 |
|
99 /** |
|
100 * Support class for the Unicode decoders. |
|
101 * |
|
102 * This class implements: |
|
103 * - the buffer management |
|
104 * |
|
105 * @created 15/Mar/1999 |
|
106 * @author Catalin Rotaru [CATA] |
|
107 */ |
|
108 class nsBufferDecoderSupport : public nsBasicDecoderSupport |
|
109 { |
|
110 protected: |
|
111 |
|
112 /** |
|
113 * Internal buffer for partial conversions. |
|
114 */ |
|
115 char * mBuffer; |
|
116 int32_t mBufferCapacity; |
|
117 int32_t mBufferLength; |
|
118 |
|
119 uint32_t mMaxLengthFactor; |
|
120 |
|
121 /** |
|
122 * Convert method but *without* the buffer management stuff. |
|
123 */ |
|
124 NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength, |
|
125 char16_t * aDest, int32_t * aDestLength) = 0; |
|
126 |
|
127 void FillBuffer(const char ** aSrc, int32_t aSrcLength); |
|
128 |
|
129 public: |
|
130 |
|
131 /** |
|
132 * Class constructor. |
|
133 */ |
|
134 nsBufferDecoderSupport(uint32_t aMaxLengthFactor); |
|
135 |
|
136 /** |
|
137 * Class destructor. |
|
138 */ |
|
139 virtual ~nsBufferDecoderSupport(); |
|
140 |
|
141 //-------------------------------------------------------------------- |
|
142 // Interface nsIUnicodeDecoder [declaration] |
|
143 |
|
144 NS_IMETHOD Convert(const char * aSrc, int32_t * aSrcLength, |
|
145 char16_t * aDest, int32_t * aDestLength); |
|
146 NS_IMETHOD Reset(); |
|
147 NS_IMETHOD GetMaxLength(const char *aSrc, |
|
148 int32_t aSrcLength, |
|
149 int32_t* aDestLength); |
|
150 }; |
|
151 |
|
152 //---------------------------------------------------------------------- |
|
153 // Class nsTableDecoderSupport [declaration] |
|
154 |
|
155 /** |
|
156 * Support class for a single-table-driven Unicode decoder. |
|
157 * |
|
158 * @created 15/Mar/1999 |
|
159 * @author Catalin Rotaru [CATA] |
|
160 */ |
|
161 class nsTableDecoderSupport : public nsBufferDecoderSupport |
|
162 { |
|
163 public: |
|
164 |
|
165 /** |
|
166 * Class constructor. |
|
167 */ |
|
168 nsTableDecoderSupport(uScanClassID aScanClass, uShiftInTable * aShiftInTable, |
|
169 uMappingTable * aMappingTable, uint32_t aMaxLengthFactor); |
|
170 |
|
171 /** |
|
172 * Class destructor. |
|
173 */ |
|
174 virtual ~nsTableDecoderSupport(); |
|
175 |
|
176 protected: |
|
177 |
|
178 uScanClassID mScanClass; |
|
179 uShiftInTable * mShiftInTable; |
|
180 uMappingTable * mMappingTable; |
|
181 |
|
182 //-------------------------------------------------------------------- |
|
183 // Subclassing of nsBufferDecoderSupport class [declaration] |
|
184 |
|
185 NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength, |
|
186 char16_t * aDest, int32_t * aDestLength); |
|
187 }; |
|
188 |
|
189 //---------------------------------------------------------------------- |
|
190 // Class nsMultiTableDecoderSupport [declaration] |
|
191 |
|
192 /** |
|
193 * Support class for a multi-table-driven Unicode decoder. |
|
194 * |
|
195 * @created 24/Mar/1999 |
|
196 * @author Catalin Rotaru [CATA] |
|
197 */ |
|
198 class nsMultiTableDecoderSupport : public nsBufferDecoderSupport |
|
199 { |
|
200 public: |
|
201 |
|
202 /** |
|
203 * Class constructor. |
|
204 */ |
|
205 nsMultiTableDecoderSupport(int32_t aTableCount, const uRange * aRangeArray, |
|
206 uScanClassID * aScanClassArray, |
|
207 uMappingTable ** aMappingTable, |
|
208 uint32_t aMaxLengthFactor); |
|
209 |
|
210 /** |
|
211 * Class destructor. |
|
212 */ |
|
213 virtual ~nsMultiTableDecoderSupport(); |
|
214 |
|
215 protected: |
|
216 |
|
217 int32_t mTableCount; |
|
218 const uRange * mRangeArray; |
|
219 uScanClassID * mScanClassArray; |
|
220 uMappingTable ** mMappingTable; |
|
221 |
|
222 //-------------------------------------------------------------------- |
|
223 // Subclassing of nsBufferDecoderSupport class [declaration] |
|
224 |
|
225 NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength, |
|
226 char16_t * aDest, int32_t * aDestLength); |
|
227 }; |
|
228 |
|
229 //---------------------------------------------------------------------- |
|
230 // Class nsBufferDecoderSupport [declaration] |
|
231 |
|
232 /** |
|
233 * Support class for a single-byte Unicode decoder. |
|
234 * |
|
235 * @created 19/Apr/1999 |
|
236 * @author Catalin Rotaru [CATA] |
|
237 */ |
|
238 class nsOneByteDecoderSupport : public nsBasicDecoderSupport |
|
239 { |
|
240 public: |
|
241 |
|
242 /** |
|
243 * Class constructor. |
|
244 */ |
|
245 nsOneByteDecoderSupport(uMappingTable * aMappingTable); |
|
246 |
|
247 /** |
|
248 * Class destructor. |
|
249 */ |
|
250 virtual ~nsOneByteDecoderSupport(); |
|
251 |
|
252 protected: |
|
253 |
|
254 uMappingTable * mMappingTable; |
|
255 char16_t mFastTable[ONE_BYTE_TABLE_SIZE]; |
|
256 bool mFastTableCreated; |
|
257 mozilla::Mutex mFastTableMutex; |
|
258 |
|
259 //-------------------------------------------------------------------- |
|
260 // Subclassing of nsBasicDecoderSupport class [declaration] |
|
261 |
|
262 NS_IMETHOD Convert(const char * aSrc, int32_t * aSrcLength, |
|
263 char16_t * aDest, int32_t * aDestLength); |
|
264 NS_IMETHOD GetMaxLength(const char * aSrc, int32_t aSrcLength, |
|
265 int32_t * aDestLength); |
|
266 NS_IMETHOD Reset(); |
|
267 }; |
|
268 |
|
269 //---------------------------------------------------------------------- |
|
270 // Class nsBasicEncoder [declaration] |
|
271 |
|
272 class nsBasicEncoder : public nsIUnicodeEncoder |
|
273 #ifdef DEBUG |
|
274 ,public nsIBasicEncoder |
|
275 #endif |
|
276 { |
|
277 NS_DECL_ISUPPORTS |
|
278 |
|
279 public: |
|
280 /** |
|
281 * Class constructor. |
|
282 */ |
|
283 nsBasicEncoder(); |
|
284 |
|
285 /** |
|
286 * Class destructor. |
|
287 */ |
|
288 virtual ~nsBasicEncoder(); |
|
289 |
|
290 }; |
|
291 //---------------------------------------------------------------------- |
|
292 // Class nsEncoderSupport [declaration] |
|
293 |
|
294 /** |
|
295 * Support class for the Unicode encoders. |
|
296 * |
|
297 * This class implements: |
|
298 * - nsISupports |
|
299 * - the buffer management |
|
300 * - error handling procedure(s) |
|
301 * |
|
302 * @created 17/Feb/1999 |
|
303 * @author Catalin Rotaru [CATA] |
|
304 */ |
|
305 class nsEncoderSupport : public nsBasicEncoder |
|
306 { |
|
307 |
|
308 protected: |
|
309 |
|
310 /** |
|
311 * Internal buffer for partial conversions. |
|
312 */ |
|
313 char * mBuffer; |
|
314 int32_t mBufferCapacity; |
|
315 char * mBufferStart; |
|
316 char * mBufferEnd; |
|
317 |
|
318 /** |
|
319 * Error handling stuff |
|
320 */ |
|
321 int32_t mErrBehavior; |
|
322 nsCOMPtr<nsIUnicharEncoder> mErrEncoder; |
|
323 char16_t mErrChar; |
|
324 uint32_t mMaxLengthFactor; |
|
325 |
|
326 /** |
|
327 * Convert method but *without* the buffer management stuff and *with* |
|
328 * error handling stuff. |
|
329 */ |
|
330 NS_IMETHOD ConvertNoBuff(const char16_t * aSrc, int32_t * aSrcLength, |
|
331 char * aDest, int32_t * aDestLength); |
|
332 |
|
333 /** |
|
334 * Convert method but *without* the buffer management stuff and *without* |
|
335 * error handling stuff. |
|
336 */ |
|
337 NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength, |
|
338 char * aDest, int32_t * aDestLength) = 0; |
|
339 |
|
340 /** |
|
341 * Finish method but *without* the buffer management stuff. |
|
342 */ |
|
343 NS_IMETHOD FinishNoBuff(char * aDest, int32_t * aDestLength); |
|
344 |
|
345 /** |
|
346 * Copy as much as possible from the internal buffer to the destination. |
|
347 */ |
|
348 nsresult FlushBuffer(char ** aDest, const char * aDestEnd); |
|
349 |
|
350 public: |
|
351 |
|
352 /** |
|
353 * Class constructor. |
|
354 */ |
|
355 nsEncoderSupport(uint32_t aMaxLengthFactor); |
|
356 |
|
357 /** |
|
358 * Class destructor. |
|
359 */ |
|
360 virtual ~nsEncoderSupport(); |
|
361 |
|
362 //-------------------------------------------------------------------- |
|
363 // Interface nsIUnicodeEncoder [declaration] |
|
364 |
|
365 NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength, |
|
366 char * aDest, int32_t * aDestLength); |
|
367 NS_IMETHOD Finish(char * aDest, int32_t * aDestLength); |
|
368 NS_IMETHOD Reset(); |
|
369 NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior, |
|
370 nsIUnicharEncoder * aEncoder, char16_t aChar); |
|
371 NS_IMETHOD GetMaxLength(const char16_t * aSrc, |
|
372 int32_t aSrcLength, |
|
373 int32_t * aDestLength); |
|
374 }; |
|
375 |
|
376 //---------------------------------------------------------------------- |
|
377 // Class nsTableEncoderSupport [declaration] |
|
378 |
|
379 /** |
|
380 * Support class for a single-table-driven Unicode encoder. |
|
381 * |
|
382 * @created 17/Feb/1999 |
|
383 * @author Catalin Rotaru [CATA] |
|
384 */ |
|
385 class nsTableEncoderSupport : public nsEncoderSupport |
|
386 { |
|
387 public: |
|
388 |
|
389 /** |
|
390 * Class constructors. |
|
391 */ |
|
392 nsTableEncoderSupport(uScanClassID aScanClass, |
|
393 uShiftOutTable * aShiftOutTable, |
|
394 uMappingTable * aMappingTable, |
|
395 uint32_t aMaxLengthFactor); |
|
396 |
|
397 nsTableEncoderSupport(uScanClassID aScanClass, |
|
398 uMappingTable * aMappingTable, |
|
399 uint32_t aMaxLengthFactor); |
|
400 |
|
401 /** |
|
402 * Class destructor. |
|
403 */ |
|
404 virtual ~nsTableEncoderSupport(); |
|
405 |
|
406 protected: |
|
407 |
|
408 uScanClassID mScanClass; |
|
409 uShiftOutTable * mShiftOutTable; |
|
410 uMappingTable * mMappingTable; |
|
411 |
|
412 //-------------------------------------------------------------------- |
|
413 // Subclassing of nsEncoderSupport class [declaration] |
|
414 |
|
415 NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength, |
|
416 char * aDest, int32_t * aDestLength); |
|
417 }; |
|
418 |
|
419 //---------------------------------------------------------------------- |
|
420 // Class nsMultiTableEncoderSupport [declaration] |
|
421 |
|
422 /** |
|
423 * Support class for a multi-table-driven Unicode encoder. |
|
424 * |
|
425 * @created 11/Mar/1999 |
|
426 * @author Catalin Rotaru [CATA] |
|
427 */ |
|
428 class nsMultiTableEncoderSupport : public nsEncoderSupport |
|
429 { |
|
430 public: |
|
431 |
|
432 /** |
|
433 * Class constructor. |
|
434 */ |
|
435 nsMultiTableEncoderSupport(int32_t aTableCount, |
|
436 uScanClassID * aScanClassArray, |
|
437 uShiftOutTable ** aShiftOutTable, |
|
438 uMappingTable ** aMappingTable, |
|
439 uint32_t aMaxLengthFactor); |
|
440 |
|
441 /** |
|
442 * Class destructor. |
|
443 */ |
|
444 virtual ~nsMultiTableEncoderSupport(); |
|
445 |
|
446 protected: |
|
447 |
|
448 int32_t mTableCount; |
|
449 uScanClassID * mScanClassArray; |
|
450 uShiftOutTable ** mShiftOutTable; |
|
451 uMappingTable ** mMappingTable; |
|
452 |
|
453 //-------------------------------------------------------------------- |
|
454 // Subclassing of nsEncoderSupport class [declaration] |
|
455 |
|
456 NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength, |
|
457 char * aDest, int32_t * aDestLength); |
|
458 }; |
|
459 |
|
460 |
|
461 #endif /* nsUCvJaSupport_h___ */ |