Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
6 #ifndef nsUCvJaSupport_h___
7 #define nsUCvJaSupport_h___
9 #include "nsCOMPtr.h"
10 #include "nsIUnicodeEncoder.h"
11 #include "nsIUnicodeDecoder.h"
12 #include "uconvutil.h"
13 #include "mozilla/Mutex.h"
15 #define ONE_BYTE_TABLE_SIZE 256
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))
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 } }
29 // {65968A7B-6467-4c4a-B50A-3E0C97A32F07}
30 #define NS_IBASICENCODER_IID \
31 { 0x65968a7b, 0x6467, 0x4c4a, { 0xb5, 0xa, 0x3e, 0xc, 0x97, 0xa3, 0x2f, 0x7 } }
33 class nsIBasicDecoder : public nsISupports {
34 public:
35 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBASICDECODER_IID)
36 };
38 NS_DEFINE_STATIC_IID_ACCESSOR(nsIBasicDecoder, NS_IBASICDECODER_IID)
40 class nsIBasicEncoder : public nsISupports {
41 public:
42 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBASICENCODER_IID)
43 };
45 NS_DEFINE_STATIC_IID_ACCESSOR(nsIBasicEncoder, NS_IBASICENCODER_IID)
47 #endif
49 //----------------------------------------------------------------------
50 // Class nsBasicDecoderSupport [declaration]
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
74 public:
76 /**
77 * Class constructor.
78 */
79 nsBasicDecoderSupport();
81 /**
82 * Class destructor.
83 */
84 virtual ~nsBasicDecoderSupport();
86 //--------------------------------------------------------------------
87 // Interface nsIUnicodeDecoder [declaration]
89 virtual void SetInputErrorBehavior(int32_t aBehavior);
90 virtual char16_t GetCharacterForUnMapped();
92 protected:
93 int32_t mErrBehavior;
94 };
96 //----------------------------------------------------------------------
97 // Class nsBufferDecoderSupport [declaration]
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:
112 /**
113 * Internal buffer for partial conversions.
114 */
115 char * mBuffer;
116 int32_t mBufferCapacity;
117 int32_t mBufferLength;
119 uint32_t mMaxLengthFactor;
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;
127 void FillBuffer(const char ** aSrc, int32_t aSrcLength);
129 public:
131 /**
132 * Class constructor.
133 */
134 nsBufferDecoderSupport(uint32_t aMaxLengthFactor);
136 /**
137 * Class destructor.
138 */
139 virtual ~nsBufferDecoderSupport();
141 //--------------------------------------------------------------------
142 // Interface nsIUnicodeDecoder [declaration]
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 };
152 //----------------------------------------------------------------------
153 // Class nsTableDecoderSupport [declaration]
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:
165 /**
166 * Class constructor.
167 */
168 nsTableDecoderSupport(uScanClassID aScanClass, uShiftInTable * aShiftInTable,
169 uMappingTable * aMappingTable, uint32_t aMaxLengthFactor);
171 /**
172 * Class destructor.
173 */
174 virtual ~nsTableDecoderSupport();
176 protected:
178 uScanClassID mScanClass;
179 uShiftInTable * mShiftInTable;
180 uMappingTable * mMappingTable;
182 //--------------------------------------------------------------------
183 // Subclassing of nsBufferDecoderSupport class [declaration]
185 NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
186 char16_t * aDest, int32_t * aDestLength);
187 };
189 //----------------------------------------------------------------------
190 // Class nsMultiTableDecoderSupport [declaration]
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:
202 /**
203 * Class constructor.
204 */
205 nsMultiTableDecoderSupport(int32_t aTableCount, const uRange * aRangeArray,
206 uScanClassID * aScanClassArray,
207 uMappingTable ** aMappingTable,
208 uint32_t aMaxLengthFactor);
210 /**
211 * Class destructor.
212 */
213 virtual ~nsMultiTableDecoderSupport();
215 protected:
217 int32_t mTableCount;
218 const uRange * mRangeArray;
219 uScanClassID * mScanClassArray;
220 uMappingTable ** mMappingTable;
222 //--------------------------------------------------------------------
223 // Subclassing of nsBufferDecoderSupport class [declaration]
225 NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
226 char16_t * aDest, int32_t * aDestLength);
227 };
229 //----------------------------------------------------------------------
230 // Class nsBufferDecoderSupport [declaration]
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:
242 /**
243 * Class constructor.
244 */
245 nsOneByteDecoderSupport(uMappingTable * aMappingTable);
247 /**
248 * Class destructor.
249 */
250 virtual ~nsOneByteDecoderSupport();
252 protected:
254 uMappingTable * mMappingTable;
255 char16_t mFastTable[ONE_BYTE_TABLE_SIZE];
256 bool mFastTableCreated;
257 mozilla::Mutex mFastTableMutex;
259 //--------------------------------------------------------------------
260 // Subclassing of nsBasicDecoderSupport class [declaration]
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 };
269 //----------------------------------------------------------------------
270 // Class nsBasicEncoder [declaration]
272 class nsBasicEncoder : public nsIUnicodeEncoder
273 #ifdef DEBUG
274 ,public nsIBasicEncoder
275 #endif
276 {
277 NS_DECL_ISUPPORTS
279 public:
280 /**
281 * Class constructor.
282 */
283 nsBasicEncoder();
285 /**
286 * Class destructor.
287 */
288 virtual ~nsBasicEncoder();
290 };
291 //----------------------------------------------------------------------
292 // Class nsEncoderSupport [declaration]
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 {
308 protected:
310 /**
311 * Internal buffer for partial conversions.
312 */
313 char * mBuffer;
314 int32_t mBufferCapacity;
315 char * mBufferStart;
316 char * mBufferEnd;
318 /**
319 * Error handling stuff
320 */
321 int32_t mErrBehavior;
322 nsCOMPtr<nsIUnicharEncoder> mErrEncoder;
323 char16_t mErrChar;
324 uint32_t mMaxLengthFactor;
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);
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;
340 /**
341 * Finish method but *without* the buffer management stuff.
342 */
343 NS_IMETHOD FinishNoBuff(char * aDest, int32_t * aDestLength);
345 /**
346 * Copy as much as possible from the internal buffer to the destination.
347 */
348 nsresult FlushBuffer(char ** aDest, const char * aDestEnd);
350 public:
352 /**
353 * Class constructor.
354 */
355 nsEncoderSupport(uint32_t aMaxLengthFactor);
357 /**
358 * Class destructor.
359 */
360 virtual ~nsEncoderSupport();
362 //--------------------------------------------------------------------
363 // Interface nsIUnicodeEncoder [declaration]
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 };
376 //----------------------------------------------------------------------
377 // Class nsTableEncoderSupport [declaration]
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:
389 /**
390 * Class constructors.
391 */
392 nsTableEncoderSupport(uScanClassID aScanClass,
393 uShiftOutTable * aShiftOutTable,
394 uMappingTable * aMappingTable,
395 uint32_t aMaxLengthFactor);
397 nsTableEncoderSupport(uScanClassID aScanClass,
398 uMappingTable * aMappingTable,
399 uint32_t aMaxLengthFactor);
401 /**
402 * Class destructor.
403 */
404 virtual ~nsTableEncoderSupport();
406 protected:
408 uScanClassID mScanClass;
409 uShiftOutTable * mShiftOutTable;
410 uMappingTable * mMappingTable;
412 //--------------------------------------------------------------------
413 // Subclassing of nsEncoderSupport class [declaration]
415 NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
416 char * aDest, int32_t * aDestLength);
417 };
419 //----------------------------------------------------------------------
420 // Class nsMultiTableEncoderSupport [declaration]
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:
432 /**
433 * Class constructor.
434 */
435 nsMultiTableEncoderSupport(int32_t aTableCount,
436 uScanClassID * aScanClassArray,
437 uShiftOutTable ** aShiftOutTable,
438 uMappingTable ** aMappingTable,
439 uint32_t aMaxLengthFactor);
441 /**
442 * Class destructor.
443 */
444 virtual ~nsMultiTableEncoderSupport();
446 protected:
448 int32_t mTableCount;
449 uScanClassID * mScanClassArray;
450 uShiftOutTable ** mShiftOutTable;
451 uMappingTable ** mMappingTable;
453 //--------------------------------------------------------------------
454 // Subclassing of nsEncoderSupport class [declaration]
456 NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
457 char * aDest, int32_t * aDestLength);
458 };
461 #endif /* nsUCvJaSupport_h___ */