CharacterEncoding.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef js_CharacterEncoding_h
  7. #define js_CharacterEncoding_h
  8. #include "mozilla/Range.h"
  9. #include "js/TypeDecls.h"
  10. #include "js/Utility.h"
  11. namespace js {
  12. class ExclusiveContext;
  13. } // namespace js
  14. class JSFlatString;
  15. namespace JS {
  16. /*
  17. * By default, all C/C++ 1-byte-per-character strings passed into the JSAPI
  18. * are treated as ISO/IEC 8859-1, also known as Latin-1. That is, each
  19. * byte is treated as a 2-byte character, and there is no way to pass in a
  20. * string containing characters beyond U+00FF.
  21. */
  22. class Latin1Chars : public mozilla::Range<Latin1Char>
  23. {
  24. typedef mozilla::Range<Latin1Char> Base;
  25. public:
  26. using CharT = Latin1Char;
  27. Latin1Chars() : Base() {}
  28. Latin1Chars(char* aBytes, size_t aLength) : Base(reinterpret_cast<Latin1Char*>(aBytes), aLength) {}
  29. Latin1Chars(const Latin1Char* aBytes, size_t aLength)
  30. : Base(const_cast<Latin1Char*>(aBytes), aLength)
  31. {}
  32. Latin1Chars(const char* aBytes, size_t aLength)
  33. : Base(reinterpret_cast<Latin1Char*>(const_cast<char*>(aBytes)), aLength)
  34. {}
  35. };
  36. /*
  37. * A Latin1Chars, but with \0 termination for C compatibility.
  38. */
  39. class Latin1CharsZ : public mozilla::RangedPtr<Latin1Char>
  40. {
  41. typedef mozilla::RangedPtr<Latin1Char> Base;
  42. public:
  43. using CharT = Latin1Char;
  44. Latin1CharsZ() : Base(nullptr, 0) {}
  45. Latin1CharsZ(char* aBytes, size_t aLength)
  46. : Base(reinterpret_cast<Latin1Char*>(aBytes), aLength)
  47. {
  48. MOZ_ASSERT(aBytes[aLength] == '\0');
  49. }
  50. Latin1CharsZ(Latin1Char* aBytes, size_t aLength)
  51. : Base(aBytes, aLength)
  52. {
  53. MOZ_ASSERT(aBytes[aLength] == '\0');
  54. }
  55. using Base::operator=;
  56. char* c_str() { return reinterpret_cast<char*>(get()); }
  57. };
  58. class UTF8Chars : public mozilla::Range<unsigned char>
  59. {
  60. typedef mozilla::Range<unsigned char> Base;
  61. public:
  62. using CharT = unsigned char;
  63. UTF8Chars() : Base() {}
  64. UTF8Chars(char* aBytes, size_t aLength)
  65. : Base(reinterpret_cast<unsigned char*>(aBytes), aLength)
  66. {}
  67. UTF8Chars(const char* aBytes, size_t aLength)
  68. : Base(reinterpret_cast<unsigned char*>(const_cast<char*>(aBytes)), aLength)
  69. {}
  70. };
  71. /*
  72. * SpiderMonkey also deals directly with UTF-8 encoded text in some places.
  73. */
  74. class UTF8CharsZ : public mozilla::RangedPtr<unsigned char>
  75. {
  76. typedef mozilla::RangedPtr<unsigned char> Base;
  77. public:
  78. using CharT = unsigned char;
  79. UTF8CharsZ() : Base(nullptr, 0) {}
  80. UTF8CharsZ(char* aBytes, size_t aLength)
  81. : Base(reinterpret_cast<unsigned char*>(aBytes), aLength)
  82. {
  83. MOZ_ASSERT(aBytes[aLength] == '\0');
  84. }
  85. UTF8CharsZ(unsigned char* aBytes, size_t aLength)
  86. : Base(aBytes, aLength)
  87. {
  88. MOZ_ASSERT(aBytes[aLength] == '\0');
  89. }
  90. using Base::operator=;
  91. char* c_str() { return reinterpret_cast<char*>(get()); }
  92. };
  93. /*
  94. * A wrapper for a "const char*" that is encoded using UTF-8.
  95. * This class does not manage ownership of the data; that is left
  96. * to others. This differs from UTF8CharsZ in that the chars are
  97. * const and it allows assignment.
  98. */
  99. class ConstUTF8CharsZ
  100. {
  101. const char* data_;
  102. public:
  103. using CharT = unsigned char;
  104. ConstUTF8CharsZ() : data_(nullptr)
  105. {}
  106. ConstUTF8CharsZ(const char* aBytes, size_t aLength)
  107. : data_(aBytes)
  108. {
  109. MOZ_ASSERT(aBytes[aLength] == '\0');
  110. #ifdef DEBUG
  111. validate(aLength);
  112. #endif
  113. }
  114. const void* get() const { return data_; }
  115. const char* c_str() const { return data_; }
  116. explicit operator bool() const { return data_ != nullptr; }
  117. private:
  118. #ifdef DEBUG
  119. void validate(size_t aLength);
  120. #endif
  121. };
  122. /*
  123. * SpiderMonkey uses a 2-byte character representation: it is a
  124. * 2-byte-at-a-time view of a UTF-16 byte stream. This is similar to UCS-2,
  125. * but unlike UCS-2, we do not strip UTF-16 extension bytes. This allows a
  126. * sufficiently dedicated JavaScript program to be fully unicode-aware by
  127. * manually interpreting UTF-16 extension characters embedded in the JS
  128. * string.
  129. */
  130. class TwoByteChars : public mozilla::Range<char16_t>
  131. {
  132. typedef mozilla::Range<char16_t> Base;
  133. public:
  134. using CharT = char16_t;
  135. TwoByteChars() : Base() {}
  136. TwoByteChars(char16_t* aChars, size_t aLength) : Base(aChars, aLength) {}
  137. TwoByteChars(const char16_t* aChars, size_t aLength) : Base(const_cast<char16_t*>(aChars), aLength) {}
  138. };
  139. /*
  140. * A TwoByteChars, but \0 terminated for compatibility with JSFlatString.
  141. */
  142. class TwoByteCharsZ : public mozilla::RangedPtr<char16_t>
  143. {
  144. typedef mozilla::RangedPtr<char16_t> Base;
  145. public:
  146. using CharT = char16_t;
  147. TwoByteCharsZ() : Base(nullptr, 0) {}
  148. TwoByteCharsZ(char16_t* chars, size_t length)
  149. : Base(chars, length)
  150. {
  151. MOZ_ASSERT(chars[length] == '\0');
  152. }
  153. using Base::operator=;
  154. };
  155. typedef mozilla::RangedPtr<const char16_t> ConstCharPtr;
  156. /*
  157. * Like TwoByteChars, but the chars are const.
  158. */
  159. class ConstTwoByteChars : public mozilla::Range<const char16_t>
  160. {
  161. typedef mozilla::Range<const char16_t> Base;
  162. public:
  163. using CharT = char16_t;
  164. ConstTwoByteChars() : Base() {}
  165. ConstTwoByteChars(const char16_t* aChars, size_t aLength) : Base(aChars, aLength) {}
  166. };
  167. /*
  168. * Convert a 2-byte character sequence to "ISO-Latin-1". This works by
  169. * truncating each 2-byte pair in the sequence to a 1-byte pair. If the source
  170. * contains any UTF-16 extension characters, then this may give invalid Latin1
  171. * output. The returned string is zero terminated. The returned string or the
  172. * returned string's |start()| must be freed with JS_free or js_free,
  173. * respectively. If allocation fails, an OOM error will be set and the method
  174. * will return a nullptr chars (which can be tested for with the ! operator).
  175. * This method cannot trigger GC.
  176. */
  177. extern Latin1CharsZ
  178. LossyTwoByteCharsToNewLatin1CharsZ(js::ExclusiveContext* cx,
  179. const mozilla::Range<const char16_t> tbchars);
  180. inline Latin1CharsZ
  181. LossyTwoByteCharsToNewLatin1CharsZ(js::ExclusiveContext* cx, const char16_t* begin, size_t length)
  182. {
  183. const mozilla::Range<const char16_t> tbchars(begin, length);
  184. return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars);
  185. }
  186. template <typename CharT>
  187. extern UTF8CharsZ
  188. CharsToNewUTF8CharsZ(js::ExclusiveContext* maybeCx, const mozilla::Range<CharT> chars);
  189. uint32_t
  190. Utf8ToOneUcs4Char(const uint8_t* utf8Buffer, int utf8Length);
  191. /*
  192. * Inflate bytes in UTF-8 encoding to char16_t.
  193. * - On error, returns an empty TwoByteCharsZ.
  194. * - On success, returns a malloc'd TwoByteCharsZ, and updates |outlen| to hold
  195. * its length; the length value excludes the trailing null.
  196. */
  197. extern TwoByteCharsZ
  198. UTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen);
  199. /*
  200. * Like UTF8CharsToNewTwoByteCharsZ, but for ConstUTF8CharsZ.
  201. */
  202. extern TwoByteCharsZ
  203. UTF8CharsToNewTwoByteCharsZ(JSContext* cx, const ConstUTF8CharsZ& utf8, size_t* outlen);
  204. /*
  205. * The same as UTF8CharsToNewTwoByteCharsZ(), except that any malformed UTF-8 characters
  206. * will be replaced by \uFFFD. No exception will be thrown for malformed UTF-8
  207. * input.
  208. */
  209. extern TwoByteCharsZ
  210. LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen);
  211. extern TwoByteCharsZ
  212. LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const ConstUTF8CharsZ& utf8, size_t* outlen);
  213. /*
  214. * Returns the length of the char buffer required to encode |s| as UTF8.
  215. * Does not include the null-terminator.
  216. */
  217. JS_PUBLIC_API(size_t)
  218. GetDeflatedUTF8StringLength(JSFlatString* s);
  219. /*
  220. * Encode |src| as UTF8. The caller must either ensure |dst| has enough space
  221. * to encode the entire string or pass the length of the buffer as |dstlenp|,
  222. * in which case the function will encode characters from the string until
  223. * the buffer is exhausted. Does not write the null terminator.
  224. *
  225. * If |dstlenp| is provided, it will be updated to hold the number of bytes
  226. * written to the buffer. If |numcharsp| is provided, it will be updated to hold
  227. * the number of Unicode characters written to the buffer (which can be less
  228. * than the length of the string, if the buffer is exhausted before the string
  229. * is fully encoded).
  230. */
  231. JS_PUBLIC_API(void)
  232. DeflateStringToUTF8Buffer(JSFlatString* src, mozilla::RangedPtr<char> dst,
  233. size_t* dstlenp = nullptr, size_t* numcharsp = nullptr);
  234. /*
  235. * The smallest character encoding capable of fully representing a particular
  236. * string.
  237. */
  238. enum class SmallestEncoding {
  239. ASCII,
  240. Latin1,
  241. UTF16
  242. };
  243. /*
  244. * Returns the smallest encoding possible for the given string: if all
  245. * codepoints are <128 then ASCII, otherwise if all codepoints are <256
  246. * Latin-1, else UTF16.
  247. */
  248. JS_PUBLIC_API(SmallestEncoding)
  249. FindSmallestEncoding(UTF8Chars utf8);
  250. /*
  251. * Return a null-terminated Latin-1 string copied from the input string,
  252. * storing its length (excluding null terminator) in |*outlen|. Fail and
  253. * report an error if the string contains non-Latin-1 codepoints. Returns
  254. * Latin1CharsZ() on failure.
  255. */
  256. extern Latin1CharsZ
  257. UTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen);
  258. /*
  259. * Return a null-terminated Latin-1 string copied from the input string,
  260. * storing its length (excluding null terminator) in |*outlen|. Non-Latin-1
  261. * codepoints are replaced by '?'. Returns Latin1CharsZ() on failure.
  262. */
  263. extern Latin1CharsZ
  264. LossyUTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen);
  265. /*
  266. * Returns true if all characters in the given null-terminated string are
  267. * ASCII, i.e. < 0x80, false otherwise.
  268. */
  269. extern bool
  270. StringIsASCII(const char* s);
  271. } // namespace JS
  272. inline void JS_free(JS::Latin1CharsZ& ptr) { js_free((void*)ptr.get()); }
  273. inline void JS_free(JS::UTF8CharsZ& ptr) { js_free((void*)ptr.get()); }
  274. #endif /* js_CharacterEncoding_h */