core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2006 Nemanja Trifunovic
  2. /*
  3. Permission is hereby granted, free of charge, to any person or organization
  4. obtaining a copy of the software and accompanying documentation covered by
  5. this license (the "Software") to use, reproduce, display, distribute,
  6. execute, and transmit the Software, and to prepare derivative works of the
  7. Software, and to permit third-parties to whom the Software is furnished to
  8. do so, all subject to the following:
  9. The copyright notices in the Software and this entire statement, including
  10. the above license grant, this restriction and the following disclaimer,
  11. must be included in all copies of the Software, in whole or in part, and
  12. all derivative works of the Software, unless such copies or derivative
  13. works are solely in the form of machine-executable object code generated by
  14. a source language processor.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  18. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  19. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  24. #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  25. #include <iterator>
  26. // Determine the C++ standard version.
  27. // If the user defines UTF_CPP_CPLUSPLUS, use that.
  28. // Otherwise, trust the unreliable predefined macro __cplusplus
  29. #if !defined UTF_CPP_CPLUSPLUS
  30. #define UTF_CPP_CPLUSPLUS __cplusplus
  31. #endif
  32. #if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later
  33. #define UTF_CPP_OVERRIDE override
  34. #define UTF_CPP_NOEXCEPT noexcept
  35. #else // C++ 98/03
  36. #define UTF_CPP_OVERRIDE
  37. #define UTF_CPP_NOEXCEPT throw()
  38. #endif // C++ 11 or later
  39. namespace utf8
  40. {
  41. // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
  42. // You may need to change them to match your system.
  43. // These typedefs have the same names as ones from cstdint, or boost/cstdint
  44. typedef unsigned char uint8_t;
  45. typedef unsigned short uint16_t;
  46. typedef unsigned int uint32_t;
  47. // Helper code - not intended to be directly called by the library users. May be changed at any time
  48. namespace internal
  49. {
  50. // Unicode constants
  51. // Leading (high) surrogates: 0xd800 - 0xdbff
  52. // Trailing (low) surrogates: 0xdc00 - 0xdfff
  53. const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
  54. const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
  55. const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
  56. const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
  57. const uint16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10)
  58. const uint32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN
  59. // Maximum valid value for a Unicode code point
  60. const uint32_t CODE_POINT_MAX = 0x0010ffffu;
  61. template<typename octet_type>
  62. inline uint8_t mask8(octet_type oc)
  63. {
  64. return static_cast<uint8_t>(0xff & oc);
  65. }
  66. template<typename u16_type>
  67. inline uint16_t mask16(u16_type oc)
  68. {
  69. return static_cast<uint16_t>(0xffff & oc);
  70. }
  71. template<typename octet_type>
  72. inline bool is_trail(octet_type oc)
  73. {
  74. return ((utf8::internal::mask8(oc) >> 6) == 0x2);
  75. }
  76. template <typename u16>
  77. inline bool is_lead_surrogate(u16 cp)
  78. {
  79. return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
  80. }
  81. template <typename u16>
  82. inline bool is_trail_surrogate(u16 cp)
  83. {
  84. return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
  85. }
  86. template <typename u16>
  87. inline bool is_surrogate(u16 cp)
  88. {
  89. return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
  90. }
  91. template <typename u32>
  92. inline bool is_code_point_valid(u32 cp)
  93. {
  94. return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
  95. }
  96. template <typename octet_iterator>
  97. inline typename std::iterator_traits<octet_iterator>::difference_type
  98. sequence_length(octet_iterator lead_it)
  99. {
  100. uint8_t lead = utf8::internal::mask8(*lead_it);
  101. if (lead < 0x80)
  102. return 1;
  103. else if ((lead >> 5) == 0x6)
  104. return 2;
  105. else if ((lead >> 4) == 0xe)
  106. return 3;
  107. else if ((lead >> 3) == 0x1e)
  108. return 4;
  109. else
  110. return 0;
  111. }
  112. template <typename octet_difference_type>
  113. inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
  114. {
  115. if (cp < 0x80) {
  116. if (length != 1)
  117. return true;
  118. }
  119. else if (cp < 0x800) {
  120. if (length != 2)
  121. return true;
  122. }
  123. else if (cp < 0x10000) {
  124. if (length != 3)
  125. return true;
  126. }
  127. return false;
  128. }
  129. enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
  130. /// Helper for get_sequence_x
  131. template <typename octet_iterator>
  132. utf_error increase_safely(octet_iterator& it, octet_iterator end)
  133. {
  134. if (++it == end)
  135. return NOT_ENOUGH_ROOM;
  136. if (!utf8::internal::is_trail(*it))
  137. return INCOMPLETE_SEQUENCE;
  138. return UTF8_OK;
  139. }
  140. #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;}
  141. /// get_sequence_x functions decode utf-8 sequences of the length x
  142. template <typename octet_iterator>
  143. utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  144. {
  145. if (it == end)
  146. return NOT_ENOUGH_ROOM;
  147. code_point = utf8::internal::mask8(*it);
  148. return UTF8_OK;
  149. }
  150. template <typename octet_iterator>
  151. utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  152. {
  153. if (it == end)
  154. return NOT_ENOUGH_ROOM;
  155. code_point = utf8::internal::mask8(*it);
  156. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  157. code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
  158. return UTF8_OK;
  159. }
  160. template <typename octet_iterator>
  161. utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  162. {
  163. if (it == end)
  164. return NOT_ENOUGH_ROOM;
  165. code_point = utf8::internal::mask8(*it);
  166. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  167. code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
  168. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  169. code_point += (*it) & 0x3f;
  170. return UTF8_OK;
  171. }
  172. template <typename octet_iterator>
  173. utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  174. {
  175. if (it == end)
  176. return NOT_ENOUGH_ROOM;
  177. code_point = utf8::internal::mask8(*it);
  178. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  179. code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
  180. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  181. code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
  182. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  183. code_point += (*it) & 0x3f;
  184. return UTF8_OK;
  185. }
  186. #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
  187. template <typename octet_iterator>
  188. utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  189. {
  190. if (it == end)
  191. return NOT_ENOUGH_ROOM;
  192. // Save the original value of it so we can go back in case of failure
  193. // Of course, it does not make much sense with i.e. stream iterators
  194. octet_iterator original_it = it;
  195. uint32_t cp = 0;
  196. // Determine the sequence length based on the lead octet
  197. typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
  198. const octet_difference_type length = utf8::internal::sequence_length(it);
  199. // Get trail octets and calculate the code point
  200. utf_error err = UTF8_OK;
  201. switch (length) {
  202. case 0:
  203. return INVALID_LEAD;
  204. case 1:
  205. err = utf8::internal::get_sequence_1(it, end, cp);
  206. break;
  207. case 2:
  208. err = utf8::internal::get_sequence_2(it, end, cp);
  209. break;
  210. case 3:
  211. err = utf8::internal::get_sequence_3(it, end, cp);
  212. break;
  213. case 4:
  214. err = utf8::internal::get_sequence_4(it, end, cp);
  215. break;
  216. }
  217. if (err == UTF8_OK) {
  218. // Decoding succeeded. Now, security checks...
  219. if (utf8::internal::is_code_point_valid(cp)) {
  220. if (!utf8::internal::is_overlong_sequence(cp, length)){
  221. // Passed! Return here.
  222. code_point = cp;
  223. ++it;
  224. return UTF8_OK;
  225. }
  226. else
  227. err = OVERLONG_SEQUENCE;
  228. }
  229. else
  230. err = INVALID_CODE_POINT;
  231. }
  232. // Failure branch - restore the original value of the iterator
  233. it = original_it;
  234. return err;
  235. }
  236. template <typename octet_iterator>
  237. inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
  238. uint32_t ignored;
  239. return utf8::internal::validate_next(it, end, ignored);
  240. }
  241. // Internal implementation of both checked and unchecked append() function
  242. // This function will be invoked by the overloads below, as they will know
  243. // the octet_type.
  244. template <typename octet_iterator, typename octet_type>
  245. octet_iterator append(uint32_t cp, octet_iterator result) {
  246. if (cp < 0x80) // one octet
  247. *(result++) = static_cast<octet_type>(cp);
  248. else if (cp < 0x800) { // two octets
  249. *(result++) = static_cast<octet_type>((cp >> 6) | 0xc0);
  250. *(result++) = static_cast<octet_type>((cp & 0x3f) | 0x80);
  251. }
  252. else if (cp < 0x10000) { // three octets
  253. *(result++) = static_cast<octet_type>((cp >> 12) | 0xe0);
  254. *(result++) = static_cast<octet_type>(((cp >> 6) & 0x3f) | 0x80);
  255. *(result++) = static_cast<octet_type>((cp & 0x3f) | 0x80);
  256. }
  257. else { // four octets
  258. *(result++) = static_cast<octet_type>((cp >> 18) | 0xf0);
  259. *(result++) = static_cast<octet_type>(((cp >> 12) & 0x3f)| 0x80);
  260. *(result++) = static_cast<octet_type>(((cp >> 6) & 0x3f) | 0x80);
  261. *(result++) = static_cast<octet_type>((cp & 0x3f) | 0x80);
  262. }
  263. return result;
  264. }
  265. // One of the following overloads will be invoked from the API calls
  266. // A simple (but dangerous) case: the caller appends byte(s) to a char array
  267. inline char* append(uint32_t cp, char* result) {
  268. return append<char*, char>(cp, result);
  269. }
  270. // Hopefully, most common case: the caller uses back_inserter
  271. // i.e. append(cp, std::back_inserter(str));
  272. template<typename container_type>
  273. std::back_insert_iterator<container_type> append
  274. (uint32_t cp, std::back_insert_iterator<container_type> result) {
  275. return append<std::back_insert_iterator<container_type>,
  276. typename container_type::value_type>(cp, result);
  277. }
  278. // The caller uses some other kind of output operator - not covered above
  279. // Note that in this case we are not able to determine octet_type
  280. // so we assume it's uint_8; that can cause a conversion warning if we are wrong.
  281. template <typename octet_iterator>
  282. octet_iterator append(uint32_t cp, octet_iterator result) {
  283. return append<octet_iterator, uint8_t>(cp, result);
  284. }
  285. } // namespace internal
  286. /// The library API - functions intended to be called by the users
  287. // Byte order mark
  288. const uint8_t bom[] = {0xef, 0xbb, 0xbf};
  289. template <typename octet_iterator>
  290. octet_iterator find_invalid(octet_iterator start, octet_iterator end)
  291. {
  292. octet_iterator result = start;
  293. while (result != end) {
  294. utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
  295. if (err_code != internal::UTF8_OK)
  296. return result;
  297. }
  298. return result;
  299. }
  300. template <typename octet_iterator>
  301. inline bool is_valid(octet_iterator start, octet_iterator end)
  302. {
  303. return (utf8::find_invalid(start, end) == end);
  304. }
  305. template <typename octet_iterator>
  306. inline bool starts_with_bom (octet_iterator it, octet_iterator end)
  307. {
  308. return (
  309. ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
  310. ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
  311. ((it != end) && (utf8::internal::mask8(*it)) == bom[2])
  312. );
  313. }
  314. } // namespace utf8
  315. #endif // header guard