encodedstream.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef RAPIDJSON_ENCODEDSTREAM_H_
  15. #define RAPIDJSON_ENCODEDSTREAM_H_
  16. #include "rapidjson.h"
  17. #ifdef __GNUC__
  18. RAPIDJSON_DIAG_PUSH
  19. RAPIDJSON_DIAG_OFF(effc++)
  20. #endif
  21. RAPIDJSON_NAMESPACE_BEGIN
  22. //! Input byte stream wrapper with a statically bound encoding.
  23. /*!
  24. \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
  25. \tparam InputByteStream Type of input byte stream. For example, FileReadStream.
  26. */
  27. template <typename Encoding, typename InputByteStream>
  28. class EncodedInputStream {
  29. RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
  30. public:
  31. typedef typename Encoding::Ch Ch;
  32. EncodedInputStream(InputByteStream& is) : is_(is) {
  33. current_ = Encoding::TakeBOM(is_);
  34. }
  35. Ch Peek() const { return current_; }
  36. Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; }
  37. size_t Tell() const { return is_.Tell(); }
  38. // Not implemented
  39. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  40. void Flush() { RAPIDJSON_ASSERT(false); }
  41. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  42. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  43. private:
  44. EncodedInputStream(const EncodedInputStream&);
  45. EncodedInputStream& operator=(const EncodedInputStream&);
  46. InputByteStream& is_;
  47. Ch current_;
  48. };
  49. //! Output byte stream wrapper with statically bound encoding.
  50. /*!
  51. \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
  52. \tparam InputByteStream Type of input byte stream. For example, FileWriteStream.
  53. */
  54. template <typename Encoding, typename OutputByteStream>
  55. class EncodedOutputStream {
  56. RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
  57. public:
  58. typedef typename Encoding::Ch Ch;
  59. EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) {
  60. if (putBOM)
  61. Encoding::PutBOM(os_);
  62. }
  63. void Put(Ch c) { Encoding::Put(os_, c); }
  64. void Flush() { os_.Flush(); }
  65. // Not implemented
  66. Ch Peek() const { RAPIDJSON_ASSERT(false); }
  67. Ch Take() { RAPIDJSON_ASSERT(false); }
  68. size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
  69. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  70. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  71. private:
  72. EncodedOutputStream(const EncodedOutputStream&);
  73. EncodedOutputStream& operator=(const EncodedOutputStream&);
  74. OutputByteStream& os_;
  75. };
  76. #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
  77. //! Input stream wrapper with dynamically bound encoding and automatic encoding detection.
  78. /*!
  79. \tparam CharType Type of character for reading.
  80. \tparam InputByteStream type of input byte stream to be wrapped.
  81. */
  82. template <typename CharType, typename InputByteStream>
  83. class AutoUTFInputStream {
  84. RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
  85. public:
  86. typedef CharType Ch;
  87. //! Constructor.
  88. /*!
  89. \param is input stream to be wrapped.
  90. \param type UTF encoding type if it is not detected from the stream.
  91. */
  92. AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) {
  93. RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
  94. DetectType();
  95. static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
  96. takeFunc_ = f[type_];
  97. current_ = takeFunc_(*is_);
  98. }
  99. UTFType GetType() const { return type_; }
  100. bool HasBOM() const { return hasBOM_; }
  101. Ch Peek() const { return current_; }
  102. Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; }
  103. size_t Tell() const { return is_->Tell(); }
  104. // Not implemented
  105. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  106. void Flush() { RAPIDJSON_ASSERT(false); }
  107. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  108. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  109. private:
  110. AutoUTFInputStream(const AutoUTFInputStream&);
  111. AutoUTFInputStream& operator=(const AutoUTFInputStream&);
  112. // Detect encoding type with BOM or RFC 4627
  113. void DetectType() {
  114. // BOM (Byte Order Mark):
  115. // 00 00 FE FF UTF-32BE
  116. // FF FE 00 00 UTF-32LE
  117. // FE FF UTF-16BE
  118. // FF FE UTF-16LE
  119. // EF BB BF UTF-8
  120. const unsigned char* c = (const unsigned char *)is_->Peek4();
  121. if (!c)
  122. return;
  123. unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
  124. hasBOM_ = false;
  125. if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
  126. else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
  127. else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); }
  128. else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); }
  129. else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); }
  130. // RFC 4627: Section 3
  131. // "Since the first two characters of a JSON text will always be ASCII
  132. // characters [RFC0020], it is possible to determine whether an octet
  133. // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
  134. // at the pattern of nulls in the first four octets."
  135. // 00 00 00 xx UTF-32BE
  136. // 00 xx 00 xx UTF-16BE
  137. // xx 00 00 00 UTF-32LE
  138. // xx 00 xx 00 UTF-16LE
  139. // xx xx xx xx UTF-8
  140. if (!hasBOM_) {
  141. unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
  142. switch (pattern) {
  143. case 0x08: type_ = kUTF32BE; break;
  144. case 0x0A: type_ = kUTF16BE; break;
  145. case 0x01: type_ = kUTF32LE; break;
  146. case 0x05: type_ = kUTF16LE; break;
  147. case 0x0F: type_ = kUTF8; break;
  148. default: break; // Use type defined by user.
  149. }
  150. }
  151. // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
  152. if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
  153. if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
  154. }
  155. typedef Ch (*TakeFunc)(InputByteStream& is);
  156. InputByteStream* is_;
  157. UTFType type_;
  158. Ch current_;
  159. TakeFunc takeFunc_;
  160. bool hasBOM_;
  161. };
  162. //! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
  163. /*!
  164. \tparam CharType Type of character for writing.
  165. \tparam InputByteStream type of output byte stream to be wrapped.
  166. */
  167. template <typename CharType, typename OutputByteStream>
  168. class AutoUTFOutputStream {
  169. RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
  170. public:
  171. typedef CharType Ch;
  172. //! Constructor.
  173. /*!
  174. \param os output stream to be wrapped.
  175. \param type UTF encoding type.
  176. \param putBOM Whether to write BOM at the beginning of the stream.
  177. */
  178. AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) {
  179. RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
  180. // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
  181. if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
  182. if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
  183. static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
  184. putFunc_ = f[type_];
  185. if (putBOM)
  186. PutBOM();
  187. }
  188. UTFType GetType() const { return type_; }
  189. void Put(Ch c) { putFunc_(*os_, c); }
  190. void Flush() { os_->Flush(); }
  191. // Not implemented
  192. Ch Peek() const { RAPIDJSON_ASSERT(false); }
  193. Ch Take() { RAPIDJSON_ASSERT(false); }
  194. size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
  195. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  196. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  197. private:
  198. AutoUTFOutputStream(const AutoUTFOutputStream&);
  199. AutoUTFOutputStream& operator=(const AutoUTFOutputStream&);
  200. void PutBOM() {
  201. typedef void (*PutBOMFunc)(OutputByteStream&);
  202. static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
  203. f[type_](*os_);
  204. }
  205. typedef void (*PutFunc)(OutputByteStream&, Ch);
  206. OutputByteStream* os_;
  207. UTFType type_;
  208. PutFunc putFunc_;
  209. };
  210. #undef RAPIDJSON_ENCODINGS_FUNC
  211. RAPIDJSON_NAMESPACE_END
  212. #ifdef __GNUC__
  213. RAPIDJSON_DIAG_POP
  214. #endif
  215. #endif // RAPIDJSON_FILESTREAM_H_