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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsIUnicodeEncoder_h___ |
michael@0 | 7 | #define nsIUnicodeEncoder_h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "nscore.h" |
michael@0 | 10 | #include "nsError.h" |
michael@0 | 11 | #include "nsISupports.h" |
michael@0 | 12 | |
michael@0 | 13 | // Interface ID for our Unicode Encoder interface |
michael@0 | 14 | // {2B2CA3D0-A4C9-11d2-8AA1-00600811A836} |
michael@0 | 15 | #define NS_IUNICODEENCODER_IID \ |
michael@0 | 16 | { 0x2b2ca3d0, 0xa4c9, 0x11d2, \ |
michael@0 | 17 | { 0x8a, 0xa1, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }} |
michael@0 | 18 | |
michael@0 | 19 | // Interface ID for our Unicode Character Encoder interface |
michael@0 | 20 | // {299BCCD0-C6DF-11d2-8AA8-00600811A836} |
michael@0 | 21 | #define NS_IUNICHARENCODER_IID \ |
michael@0 | 22 | { 0x299bccd0, 0xc6df, 0x11d2, \ |
michael@0 | 23 | {0x8a, 0xa8, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }} |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | #define NS_UNICODEENCODER_CONTRACTID_BASE "@mozilla.org/intl/unicode/encoder;1?charset=" |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * Interface which converts a single character from Unicode into a given |
michael@0 | 30 | * charset. |
michael@0 | 31 | * |
michael@0 | 32 | * @created 17/Feb/1999 |
michael@0 | 33 | * @author Catalin Rotaru [CATA] |
michael@0 | 34 | */ |
michael@0 | 35 | class nsIUnicharEncoder : public nsISupports |
michael@0 | 36 | { |
michael@0 | 37 | public: |
michael@0 | 38 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICHARENCODER_IID) |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * Converts a character from Unicode to a Charset. |
michael@0 | 42 | */ |
michael@0 | 43 | NS_IMETHOD Convert(char16_t aChar, char * aDest, int32_t * aDestLength) = 0; |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicharEncoder, NS_IUNICHARENCODER_IID) |
michael@0 | 47 | |
michael@0 | 48 | // |
michael@0 | 49 | // Malloc an Encoder (unicode -> charset) buffer if the |
michael@0 | 50 | // result won't fit in the static buffer |
michael@0 | 51 | // |
michael@0 | 52 | // p = the buffer pointer (char*) |
michael@0 | 53 | // e = encoder (nsIUnicodeEncoder*) |
michael@0 | 54 | // s = string (char16_t*) |
michael@0 | 55 | // l = string length (int32_t) |
michael@0 | 56 | // sb = static buffer (char[]) |
michael@0 | 57 | // sbl = static buffer length (uint32_t) |
michael@0 | 58 | // al = actual buffer length (int32_t) |
michael@0 | 59 | // |
michael@0 | 60 | #define ENCODER_BUFFER_ALLOC_IF_NEEDED(p,e,s,l,sb,sbl,al) \ |
michael@0 | 61 | PR_BEGIN_MACRO \ |
michael@0 | 62 | if (e \ |
michael@0 | 63 | && NS_SUCCEEDED((e)->GetMaxLength((s), (l), &(al)))\ |
michael@0 | 64 | && ((al) > (int32_t)(sbl)) \ |
michael@0 | 65 | && (nullptr!=((p)=(char*)nsMemory::Alloc((al)+1))) \ |
michael@0 | 66 | ) { \ |
michael@0 | 67 | } \ |
michael@0 | 68 | else { \ |
michael@0 | 69 | (p) = (char*)(sb); \ |
michael@0 | 70 | (al) = (sbl); \ |
michael@0 | 71 | } \ |
michael@0 | 72 | PR_END_MACRO |
michael@0 | 73 | |
michael@0 | 74 | // |
michael@0 | 75 | // Free the Encoder buffer if it was allocated |
michael@0 | 76 | // |
michael@0 | 77 | #define ENCODER_BUFFER_FREE_IF_NEEDED(p,sb) \ |
michael@0 | 78 | PR_BEGIN_MACRO \ |
michael@0 | 79 | if ((p) != (char*)(sb)) \ |
michael@0 | 80 | nsMemory::Free(p); \ |
michael@0 | 81 | PR_END_MACRO |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Interface for a Converter from Unicode into a Charset. |
michael@0 | 85 | * |
michael@0 | 86 | * @created 23/Nov/1998 |
michael@0 | 87 | * @author Catalin Rotaru [CATA] |
michael@0 | 88 | */ |
michael@0 | 89 | class nsIUnicodeEncoder : public nsISupports |
michael@0 | 90 | { |
michael@0 | 91 | public: |
michael@0 | 92 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICODEENCODER_IID) |
michael@0 | 93 | |
michael@0 | 94 | enum { |
michael@0 | 95 | kOnError_Signal, // on an error, stop and signal |
michael@0 | 96 | kOnError_CallBack, // on an error, call the error handler |
michael@0 | 97 | kOnError_Replace // on an error, replace with a different character |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | /** |
michael@0 | 101 | * Converts the data from Unicode to a Charset. |
michael@0 | 102 | * |
michael@0 | 103 | * About the byte ordering: |
michael@0 | 104 | * - The input stream is Unicode, having the byte order which is internal |
michael@0 | 105 | * for the machine on which the converter is running on. |
michael@0 | 106 | * - For output, if the converter cares (that depends of the charset, for |
michael@0 | 107 | * example a singlebyte will ignore the byte ordering) it should assume |
michael@0 | 108 | * network order. If necessary and requested, we can add a method |
michael@0 | 109 | * SetOutputByteOrder() so that the reverse order can be used, too. That |
michael@0 | 110 | * method would have as default the assumed network order. |
michael@0 | 111 | * |
michael@0 | 112 | * For the last converted char, even if there is not enough output |
michael@0 | 113 | * space, a partial output must be done until all available space will be |
michael@0 | 114 | * used. The rest of the output should be buffered until more space becomes |
michael@0 | 115 | * available. But this is not also true about the error handling method!!! |
michael@0 | 116 | * So be very, very careful... |
michael@0 | 117 | * |
michael@0 | 118 | * @param aSrc [IN] the source data buffer |
michael@0 | 119 | * @param aSrcLength [IN/OUT] the length of source data buffer; after |
michael@0 | 120 | * conversion will contain the number of Unicode |
michael@0 | 121 | * characters read |
michael@0 | 122 | * @param aDest [OUT] the destination data buffer |
michael@0 | 123 | * @param aDestLength [IN/OUT] the length of the destination data buffer; |
michael@0 | 124 | * after conversion will contain the number of bytes |
michael@0 | 125 | * written |
michael@0 | 126 | * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion |
michael@0 | 127 | * was done; more output space is needed to continue |
michael@0 | 128 | * NS_OK_UENC_MOREINPUT if only a partial conversion |
michael@0 | 129 | * was done; more input is needed to continue. This can |
michael@0 | 130 | * occur when the last UTF-16 code point in the input is |
michael@0 | 131 | * the first of a surrogate pair. |
michael@0 | 132 | * NS_ERROR_UENC_NOMAPPING if character without mapping |
michael@0 | 133 | * was encountered and the behavior was set to "signal". |
michael@0 | 134 | */ |
michael@0 | 135 | NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength, |
michael@0 | 136 | char * aDest, int32_t * aDestLength) = 0; |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Finishes the conversion. The converter has the possibility to write some |
michael@0 | 140 | * extra data and flush its final state. |
michael@0 | 141 | * |
michael@0 | 142 | * @param aDest [OUT] the destination data buffer |
michael@0 | 143 | * @param aDestLength [IN/OUT] the length of destination data buffer; after |
michael@0 | 144 | * conversion it will contain the number of bytes written |
michael@0 | 145 | * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion |
michael@0 | 146 | * was done; more output space is needed to continue |
michael@0 | 147 | */ |
michael@0 | 148 | NS_IMETHOD Finish(char * aDest, int32_t * aDestLength) = 0; |
michael@0 | 149 | |
michael@0 | 150 | /** |
michael@0 | 151 | * Returns a quick estimation of the size of the buffer needed to hold the |
michael@0 | 152 | * converted data. Remember: this estimation is >= with the actual size of |
michael@0 | 153 | * the buffer needed. It will be computed for the "worst case" |
michael@0 | 154 | * |
michael@0 | 155 | * @param aSrc [IN] the source data buffer |
michael@0 | 156 | * @param aSrcLength [IN] the length of source data buffer |
michael@0 | 157 | * @param aDestLength [OUT] the needed size of the destination buffer |
michael@0 | 158 | * @return NS_OK_UENC_EXACTLENGTH if an exact length was computed |
michael@0 | 159 | * NS_OK if all we have is an approximation |
michael@0 | 160 | */ |
michael@0 | 161 | NS_IMETHOD GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, |
michael@0 | 162 | int32_t * aDestLength) = 0; |
michael@0 | 163 | |
michael@0 | 164 | /** |
michael@0 | 165 | * Resets the charset converter so it may be recycled for a completely |
michael@0 | 166 | * different and urelated buffer of data. |
michael@0 | 167 | */ |
michael@0 | 168 | NS_IMETHOD Reset() = 0; |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * Specify what to do when a character cannot be mapped into the dest charset |
michael@0 | 172 | * |
michael@0 | 173 | * @param aOrder [IN] the behavior; taken from the enum |
michael@0 | 174 | */ |
michael@0 | 175 | NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior, |
michael@0 | 176 | nsIUnicharEncoder * aEncoder, char16_t aChar) = 0; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicodeEncoder, NS_IUNICODEENCODER_IID) |
michael@0 | 180 | |
michael@0 | 181 | #endif /* nsIUnicodeEncoder_h___ */ |