nsUCSupport.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #ifndef nsUCvJaSupport_h___
  6. #define nsUCvJaSupport_h___
  7. #include "nsCOMPtr.h"
  8. #include "nsIUnicodeEncoder.h"
  9. #include "nsIUnicodeDecoder.h"
  10. #include "uconvutil.h"
  11. #include "mozilla/Mutex.h"
  12. #define ONE_BYTE_TABLE_SIZE 256
  13. inline bool WillOverrun(char16_t* aDest, char16_t* aDestEnd, uint32_t aLength)
  14. {
  15. NS_ASSERTION(aDest <= aDestEnd, "Pointer overrun even before check");
  16. return (uint32_t(aDestEnd - aDest) < aLength);
  17. }
  18. #define CHECK_OVERRUN(dest, destEnd, length) (WillOverrun(dest, destEnd, length))
  19. #ifdef DEBUG
  20. // {7AFC9F0A-CFE1-44ea-A755-E3B86AB1226E}
  21. #define NS_IBASICDECODER_IID \
  22. { 0x7afc9f0a, 0xcfe1, 0x44ea, { 0xa7, 0x55, 0xe3, 0xb8, 0x6a, 0xb1, 0x22, 0x6e } }
  23. // {65968A7B-6467-4c4a-B50A-3E0C97A32F07}
  24. #define NS_IBASICENCODER_IID \
  25. { 0x65968a7b, 0x6467, 0x4c4a, { 0xb5, 0xa, 0x3e, 0xc, 0x97, 0xa3, 0x2f, 0x7 } }
  26. class nsIBasicDecoder : public nsISupports {
  27. public:
  28. NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBASICDECODER_IID)
  29. };
  30. NS_DEFINE_STATIC_IID_ACCESSOR(nsIBasicDecoder, NS_IBASICDECODER_IID)
  31. class nsIBasicEncoder : public nsISupports {
  32. public:
  33. NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBASICENCODER_IID)
  34. };
  35. NS_DEFINE_STATIC_IID_ACCESSOR(nsIBasicEncoder, NS_IBASICENCODER_IID)
  36. #endif
  37. //----------------------------------------------------------------------
  38. // Class nsBasicDecoderSupport [declaration]
  39. /**
  40. * Support class for the Unicode decoders.
  41. *
  42. * The class source files for this class are in /ucvlatin/nsUCvJaSupport.
  43. * However, because these objects requires non-xpcom subclassing, local copies
  44. * will be made into the other directories using them. Just don't forget to
  45. * keep in sync with the master copy!
  46. *
  47. * This class implements:
  48. * - nsISupports
  49. * - nsIUnicodeDecoder
  50. *
  51. * @created 19/Apr/1999
  52. * @author Catalin Rotaru [CATA]
  53. */
  54. class nsBasicDecoderSupport : public nsIUnicodeDecoder
  55. #ifdef DEBUG
  56. ,public nsIBasicDecoder
  57. #endif
  58. {
  59. NS_DECL_THREADSAFE_ISUPPORTS
  60. public:
  61. /**
  62. * Class constructor.
  63. */
  64. nsBasicDecoderSupport();
  65. //--------------------------------------------------------------------
  66. // Interface nsIUnicodeDecoder [declaration]
  67. virtual void SetInputErrorBehavior(int32_t aBehavior) override;
  68. virtual char16_t GetCharacterForUnMapped() override;
  69. protected:
  70. int32_t mErrBehavior;
  71. /**
  72. * Class destructor.
  73. */
  74. virtual ~nsBasicDecoderSupport();
  75. };
  76. //----------------------------------------------------------------------
  77. // Class nsBufferDecoderSupport [declaration]
  78. /**
  79. * Support class for the Unicode decoders.
  80. *
  81. * This class implements:
  82. * - the buffer management
  83. *
  84. * @created 15/Mar/1999
  85. * @author Catalin Rotaru [CATA]
  86. */
  87. class nsBufferDecoderSupport : public nsBasicDecoderSupport
  88. {
  89. protected:
  90. /**
  91. * Internal buffer for partial conversions.
  92. */
  93. char * mBuffer;
  94. int32_t mBufferCapacity;
  95. int32_t mBufferLength;
  96. uint32_t mMaxLengthFactor;
  97. /**
  98. * Convert method but *without* the buffer management stuff.
  99. */
  100. NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
  101. char16_t * aDest, int32_t * aDestLength) = 0;
  102. void FillBuffer(const char ** aSrc, int32_t aSrcLength);
  103. public:
  104. /**
  105. * Class constructor.
  106. */
  107. explicit nsBufferDecoderSupport(uint32_t aMaxLengthFactor);
  108. /**
  109. * Class destructor.
  110. */
  111. virtual ~nsBufferDecoderSupport();
  112. //--------------------------------------------------------------------
  113. // Interface nsIUnicodeDecoder [declaration]
  114. NS_IMETHOD Convert(const char * aSrc, int32_t * aSrcLength,
  115. char16_t * aDest, int32_t * aDestLength);
  116. NS_IMETHOD Reset();
  117. NS_IMETHOD GetMaxLength(const char *aSrc,
  118. int32_t aSrcLength,
  119. int32_t* aDestLength);
  120. };
  121. //----------------------------------------------------------------------
  122. // Class nsMultiTableDecoderSupport [declaration]
  123. /**
  124. * Support class for a multi-table-driven Unicode decoder.
  125. *
  126. * @created 24/Mar/1999
  127. * @author Catalin Rotaru [CATA]
  128. */
  129. class nsMultiTableDecoderSupport : public nsBufferDecoderSupport
  130. {
  131. public:
  132. /**
  133. * Class constructor.
  134. */
  135. nsMultiTableDecoderSupport(int32_t aTableCount, const uRange * aRangeArray,
  136. uScanClassID * aScanClassArray,
  137. uMappingTable ** aMappingTable,
  138. uint32_t aMaxLengthFactor);
  139. /**
  140. * Class destructor.
  141. */
  142. virtual ~nsMultiTableDecoderSupport();
  143. protected:
  144. int32_t mTableCount;
  145. const uRange * mRangeArray;
  146. uScanClassID * mScanClassArray;
  147. uMappingTable ** mMappingTable;
  148. //--------------------------------------------------------------------
  149. // Subclassing of nsBufferDecoderSupport class [declaration]
  150. NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
  151. char16_t * aDest, int32_t * aDestLength);
  152. };
  153. //----------------------------------------------------------------------
  154. // Class nsBufferDecoderSupport [declaration]
  155. /**
  156. * Support class for a single-byte Unicode decoder.
  157. *
  158. * @created 19/Apr/1999
  159. * @author Catalin Rotaru [CATA]
  160. */
  161. class nsOneByteDecoderSupport : public nsBasicDecoderSupport
  162. {
  163. public:
  164. /**
  165. * Class constructor.
  166. */
  167. explicit nsOneByteDecoderSupport(uMappingTable * aMappingTable);
  168. /**
  169. * Class destructor.
  170. */
  171. virtual ~nsOneByteDecoderSupport();
  172. protected:
  173. uMappingTable * mMappingTable;
  174. char16_t mFastTable[ONE_BYTE_TABLE_SIZE];
  175. bool mFastTableCreated;
  176. mozilla::Mutex mFastTableMutex;
  177. //--------------------------------------------------------------------
  178. // Subclassing of nsBasicDecoderSupport class [declaration]
  179. NS_IMETHOD Convert(const char * aSrc, int32_t * aSrcLength,
  180. char16_t * aDest, int32_t * aDestLength);
  181. NS_IMETHOD GetMaxLength(const char * aSrc, int32_t aSrcLength,
  182. int32_t * aDestLength);
  183. NS_IMETHOD Reset();
  184. };
  185. //----------------------------------------------------------------------
  186. // Class nsBasicEncoder [declaration]
  187. class nsBasicEncoder : public nsIUnicodeEncoder
  188. #ifdef DEBUG
  189. ,public nsIBasicEncoder
  190. #endif
  191. {
  192. NS_DECL_ISUPPORTS
  193. public:
  194. /**
  195. * Class constructor.
  196. */
  197. nsBasicEncoder();
  198. protected:
  199. /**
  200. * Class destructor.
  201. */
  202. virtual ~nsBasicEncoder();
  203. };
  204. //----------------------------------------------------------------------
  205. // Class nsEncoderSupport [declaration]
  206. /**
  207. * Support class for the Unicode encoders.
  208. *
  209. * This class implements:
  210. * - nsISupports
  211. * - the buffer management
  212. * - error handling procedure(s)
  213. *
  214. * @created 17/Feb/1999
  215. * @author Catalin Rotaru [CATA]
  216. */
  217. class nsEncoderSupport : public nsBasicEncoder
  218. {
  219. protected:
  220. /**
  221. * Internal buffer for partial conversions.
  222. */
  223. char * mBuffer;
  224. int32_t mBufferCapacity;
  225. char * mBufferStart;
  226. char * mBufferEnd;
  227. /**
  228. * Error handling stuff
  229. */
  230. int32_t mErrBehavior;
  231. nsCOMPtr<nsIUnicharEncoder> mErrEncoder;
  232. char16_t mErrChar;
  233. uint32_t mMaxLengthFactor;
  234. /**
  235. * Convert method but *without* the buffer management stuff and *with*
  236. * error handling stuff.
  237. */
  238. NS_IMETHOD ConvertNoBuff(const char16_t * aSrc, int32_t * aSrcLength,
  239. char * aDest, int32_t * aDestLength);
  240. /**
  241. * Convert method but *without* the buffer management stuff and *without*
  242. * error handling stuff.
  243. */
  244. NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
  245. char * aDest, int32_t * aDestLength) = 0;
  246. /**
  247. * Finish method but *without* the buffer management stuff.
  248. */
  249. NS_IMETHOD FinishNoBuff(char * aDest, int32_t * aDestLength);
  250. /**
  251. * Copy as much as possible from the internal buffer to the destination.
  252. */
  253. nsresult FlushBuffer(char ** aDest, const char * aDestEnd);
  254. public:
  255. /**
  256. * Class constructor.
  257. */
  258. explicit nsEncoderSupport(uint32_t aMaxLengthFactor);
  259. /**
  260. * Class destructor.
  261. */
  262. virtual ~nsEncoderSupport();
  263. //--------------------------------------------------------------------
  264. // Interface nsIUnicodeEncoder [declaration]
  265. NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength,
  266. char * aDest, int32_t * aDestLength);
  267. NS_IMETHOD Finish(char * aDest, int32_t * aDestLength);
  268. NS_IMETHOD Reset();
  269. NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior,
  270. nsIUnicharEncoder * aEncoder, char16_t aChar);
  271. NS_IMETHOD GetMaxLength(const char16_t * aSrc,
  272. int32_t aSrcLength,
  273. int32_t * aDestLength);
  274. };
  275. //----------------------------------------------------------------------
  276. // Class nsTableEncoderSupport [declaration]
  277. /**
  278. * Support class for a single-table-driven Unicode encoder.
  279. *
  280. * @created 17/Feb/1999
  281. * @author Catalin Rotaru [CATA]
  282. */
  283. class nsTableEncoderSupport : public nsEncoderSupport
  284. {
  285. public:
  286. /**
  287. * Class constructors.
  288. */
  289. nsTableEncoderSupport(uScanClassID aScanClass,
  290. uShiftOutTable * aShiftOutTable,
  291. uMappingTable * aMappingTable,
  292. uint32_t aMaxLengthFactor);
  293. nsTableEncoderSupport(uScanClassID aScanClass,
  294. uMappingTable * aMappingTable,
  295. uint32_t aMaxLengthFactor);
  296. /**
  297. * Class destructor.
  298. */
  299. virtual ~nsTableEncoderSupport();
  300. protected:
  301. uScanClassID mScanClass;
  302. uShiftOutTable * mShiftOutTable;
  303. uMappingTable * mMappingTable;
  304. //--------------------------------------------------------------------
  305. // Subclassing of nsEncoderSupport class [declaration]
  306. NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
  307. char * aDest, int32_t * aDestLength);
  308. };
  309. //----------------------------------------------------------------------
  310. // Class nsMultiTableEncoderSupport [declaration]
  311. /**
  312. * Support class for a multi-table-driven Unicode encoder.
  313. *
  314. * @created 11/Mar/1999
  315. * @author Catalin Rotaru [CATA]
  316. */
  317. class nsMultiTableEncoderSupport : public nsEncoderSupport
  318. {
  319. public:
  320. /**
  321. * Class constructor.
  322. */
  323. nsMultiTableEncoderSupport(int32_t aTableCount,
  324. uScanClassID * aScanClassArray,
  325. uShiftOutTable ** aShiftOutTable,
  326. uMappingTable ** aMappingTable,
  327. uint32_t aMaxLengthFactor);
  328. /**
  329. * Class destructor.
  330. */
  331. virtual ~nsMultiTableEncoderSupport();
  332. protected:
  333. int32_t mTableCount;
  334. uScanClassID * mScanClassArray;
  335. uShiftOutTable ** mShiftOutTable;
  336. uMappingTable ** mMappingTable;
  337. //--------------------------------------------------------------------
  338. // Subclassing of nsEncoderSupport class [declaration]
  339. NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
  340. char * aDest, int32_t * aDestLength);
  341. };
  342. #endif /* nsUCvJaSupport_h___ */