nsIUnicodeEncoder.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 nsIUnicodeEncoder_h___
  6. #define nsIUnicodeEncoder_h___
  7. #include "nscore.h"
  8. #include "nsError.h"
  9. #include "nsISupports.h"
  10. // Interface ID for our Unicode Encoder interface
  11. // {2B2CA3D0-A4C9-11d2-8AA1-00600811A836}
  12. #define NS_IUNICODEENCODER_IID \
  13. { 0x2b2ca3d0, 0xa4c9, 0x11d2, \
  14. { 0x8a, 0xa1, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }}
  15. // Interface ID for our Unicode Character Encoder interface
  16. // {299BCCD0-C6DF-11d2-8AA8-00600811A836}
  17. #define NS_IUNICHARENCODER_IID \
  18. { 0x299bccd0, 0xc6df, 0x11d2, \
  19. {0x8a, 0xa8, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }}
  20. #define NS_UNICODEENCODER_CONTRACTID_BASE "@mozilla.org/intl/unicode/encoder;1?charset="
  21. /**
  22. * Interface which converts a single character from Unicode into a given
  23. * charset.
  24. *
  25. * @created 17/Feb/1999
  26. * @author Catalin Rotaru [CATA]
  27. */
  28. class nsIUnicharEncoder : public nsISupports
  29. {
  30. public:
  31. NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICHARENCODER_IID)
  32. /**
  33. * Converts a character from Unicode to a Charset.
  34. */
  35. NS_IMETHOD Convert(char16_t aChar, char * aDest, int32_t * aDestLength) = 0;
  36. };
  37. NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicharEncoder, NS_IUNICHARENCODER_IID)
  38. /**
  39. * Interface for a Converter from Unicode into a Charset.
  40. *
  41. * @created 23/Nov/1998
  42. * @author Catalin Rotaru [CATA]
  43. */
  44. class nsIUnicodeEncoder : public nsISupports
  45. {
  46. public:
  47. NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICODEENCODER_IID)
  48. enum {
  49. kOnError_Signal, // on an error, stop and signal
  50. kOnError_CallBack, // on an error, call the error handler
  51. kOnError_Replace // on an error, replace with a different character
  52. };
  53. /**
  54. * Converts the data from Unicode to a Charset.
  55. *
  56. * About the byte ordering:
  57. * - The input stream is Unicode, having the byte order which is internal
  58. * for the machine on which the converter is running on.
  59. * - For output, if the converter cares (that depends of the charset, for
  60. * example a singlebyte will ignore the byte ordering) it should assume
  61. * network order. If necessary and requested, we can add a method
  62. * SetOutputByteOrder() so that the reverse order can be used, too. That
  63. * method would have as default the assumed network order.
  64. *
  65. * For the last converted char, even if there is not enough output
  66. * space, a partial output must be done until all available space will be
  67. * used. The rest of the output should be buffered until more space becomes
  68. * available. But this is not also true about the error handling method!!!
  69. * So be very, very careful...
  70. *
  71. * @param aSrc [IN] the source data buffer
  72. * @param aSrcLength [IN/OUT] the length of source data buffer; after
  73. * conversion will contain the number of Unicode
  74. * characters read
  75. * @param aDest [OUT] the destination data buffer
  76. * @param aDestLength [IN/OUT] the length of the destination data buffer;
  77. * after conversion will contain the number of bytes
  78. * written
  79. * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion
  80. * was done; more output space is needed to continue
  81. * NS_OK_UENC_MOREINPUT if only a partial conversion
  82. * was done; more input is needed to continue. This can
  83. * occur when the last UTF-16 code point in the input is
  84. * the first of a surrogate pair.
  85. * NS_ERROR_UENC_NOMAPPING if character without mapping
  86. * was encountered and the behavior was set to "signal".
  87. * In the case of an unmappable BMP character, aDestLength
  88. * must indicate that the unmappable character was
  89. * consumed by the encoder (unlike in the decode API!).
  90. * In the case of an unmappable astral character,
  91. * aDestLength must indicate that the high surrogate was
  92. * consumed by the encoder but the low surrogate was not.
  93. * NS_OK otherwise.
  94. */
  95. NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength,
  96. char * aDest, int32_t * aDestLength) = 0;
  97. /**
  98. * Finishes the conversion. The converter has the possibility to write some
  99. * extra data and flush its final state.
  100. *
  101. * @param aDest [OUT] the destination data buffer
  102. * @param aDestLength [IN/OUT] the length of destination data buffer; after
  103. * conversion it will contain the number of bytes written
  104. * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion
  105. * was done; more output space is needed to continue.
  106. * NS_ERROR_UENC_NOMAPPING if input ended with an unpaired
  107. * high surrogate, the behavior was "signal" and the
  108. * encoding can't represent U+FFFD.
  109. * NS_OK otherwise.
  110. */
  111. NS_IMETHOD Finish(char * aDest, int32_t * aDestLength) = 0;
  112. /**
  113. * Returns a quick estimation of the size of the buffer needed to hold the
  114. * converted data. Remember: this estimation is >= with the actual size of
  115. * the buffer needed. It will be computed for the "worst case"
  116. *
  117. * @param aSrc [IN] the source data buffer
  118. * @param aSrcLength [IN] the length of source data buffer
  119. * @param aDestLength [OUT] the needed size of the destination buffer
  120. * @return NS_OK_UENC_EXACTLENGTH if an exact length was computed
  121. * NS_ERROR_OUT_OF_MEMORY if OOM
  122. * NS_OK if all we have is an approximation
  123. */
  124. MOZ_MUST_USE NS_IMETHOD GetMaxLength(const char16_t* aSrc,
  125. int32_t aSrcLength,
  126. int32_t* aDestLength) = 0;
  127. /**
  128. * Resets the charset converter so it may be recycled for a completely
  129. * different and urelated buffer of data.
  130. */
  131. NS_IMETHOD Reset() = 0;
  132. /**
  133. * Specify what to do when a character cannot be mapped into the dest charset
  134. *
  135. * @param aOrder [IN] the behavior; taken from the enum
  136. */
  137. NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior,
  138. nsIUnicharEncoder * aEncoder, char16_t aChar) = 0;
  139. };
  140. NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicodeEncoder, NS_IUNICODEENCODER_IID)
  141. #endif /* nsIUnicodeEncoder_h___ */