core.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. // Copyright 2006 Nemanja Trifunovic
  19. /*
  20. Permission is hereby granted, free of charge, to any person or organization
  21. obtaining a copy of the software and accompanying documentation covered by
  22. this license (the "Software") to use, reproduce, display, distribute,
  23. execute, and transmit the Software, and to prepare derivative works of the
  24. Software, and to permit third-parties to whom the Software is furnished to
  25. do so, all subject to the following:
  26. The copyright notices in the Software and this entire statement, including
  27. the above license grant, this restriction and the following disclaimer,
  28. must be included in all copies of the Software, in whole or in part, and
  29. all derivative works of the Software, unless such copies or derivative
  30. works are solely in the form of machine-executable object code generated by
  31. a source language processor.
  32. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  35. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  36. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  37. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  38. DEALINGS IN THE SOFTWARE.
  39. */
  40. #ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  41. #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
  42. #include <iterator>
  43. namespace utf8 {
  44. // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
  45. // You may need to change them to match your system.
  46. // These typedefs have the same names as ones from cstdint, or boost/cstdint
  47. typedef unsigned char uint8_t;
  48. typedef unsigned short uint16_t;
  49. typedef unsigned int uint32_t;
  50. // Helper code - not intended to be directly called by the library users. May be
  51. // changed at any time
  52. namespace internal {
  53. // Unicode constants
  54. // Leading (high) surrogates: 0xd800 - 0xdbff
  55. // Trailing (low) surrogates: 0xdc00 - 0xdfff
  56. const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
  57. const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
  58. const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
  59. const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
  60. const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
  61. const uint32_t SURROGATE_OFFSET =
  62. 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
  63. // Maximum valid value for a Unicode code point
  64. const uint32_t CODE_POINT_MAX = 0x0010ffffu;
  65. template <typename octet_type>
  66. inline uint8_t mask8(octet_type oc) {
  67. return static_cast<uint8_t>(0xff & oc);
  68. }
  69. template <typename u16_type>
  70. inline uint16_t mask16(u16_type oc) {
  71. return static_cast<uint16_t>(0xffff & oc);
  72. }
  73. template <typename octet_type>
  74. inline bool is_trail(octet_type oc) {
  75. return ((utf8::internal::mask8(oc) >> 6) == 0x2);
  76. }
  77. template <typename u16>
  78. inline bool is_lead_surrogate(u16 cp) {
  79. return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
  80. }
  81. template <typename u16>
  82. inline bool is_trail_surrogate(u16 cp) {
  83. return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
  84. }
  85. template <typename u16>
  86. inline bool is_surrogate(u16 cp) {
  87. return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
  88. }
  89. template <typename u32>
  90. inline bool is_code_point_valid(u32 cp) {
  91. return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
  92. }
  93. template <typename octet_iterator>
  94. inline typename std::iterator_traits<octet_iterator>::difference_type
  95. sequence_length(octet_iterator lead_it) {
  96. uint8_t lead = utf8::internal::mask8(*lead_it);
  97. if (lead < 0x80)
  98. return 1;
  99. else if ((lead >> 5) == 0x6)
  100. return 2;
  101. else if ((lead >> 4) == 0xe)
  102. return 3;
  103. else if ((lead >> 3) == 0x1e)
  104. return 4;
  105. else
  106. return 0;
  107. }
  108. template <typename octet_difference_type>
  109. inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length) {
  110. if (cp < 0x80) {
  111. if (length != 1) return true;
  112. } else if (cp < 0x800) {
  113. if (length != 2) return true;
  114. } else if (cp < 0x10000) {
  115. if (length != 3) return true;
  116. }
  117. return false;
  118. }
  119. enum utf_error {
  120. UTF8_OK,
  121. NOT_ENOUGH_ROOM,
  122. INVALID_LEAD,
  123. INCOMPLETE_SEQUENCE,
  124. OVERLONG_SEQUENCE,
  125. INVALID_CODE_POINT
  126. };
  127. /// Helper for get_sequence_x
  128. template <typename octet_iterator>
  129. utf_error increase_safely(octet_iterator& it, octet_iterator end) {
  130. if (++it == end) return NOT_ENOUGH_ROOM;
  131. if (!utf8::internal::is_trail(*it)) return INCOMPLETE_SEQUENCE;
  132. return UTF8_OK;
  133. }
  134. #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) \
  135. { \
  136. utf_error ret = increase_safely(IT, END); \
  137. if (ret != UTF8_OK) return ret; \
  138. }
  139. /// get_sequence_x functions decode utf-8 sequences of the length x
  140. template <typename octet_iterator>
  141. utf_error get_sequence_1(octet_iterator& it, octet_iterator end,
  142. uint32_t& code_point) {
  143. if (it == end) return NOT_ENOUGH_ROOM;
  144. code_point = utf8::internal::mask8(*it);
  145. return UTF8_OK;
  146. }
  147. template <typename octet_iterator>
  148. utf_error get_sequence_2(octet_iterator& it, octet_iterator end,
  149. uint32_t& code_point) {
  150. if (it == end) return NOT_ENOUGH_ROOM;
  151. code_point = utf8::internal::mask8(*it);
  152. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  153. code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
  154. return UTF8_OK;
  155. }
  156. template <typename octet_iterator>
  157. utf_error get_sequence_3(octet_iterator& it, octet_iterator end,
  158. uint32_t& code_point) {
  159. if (it == end) return NOT_ENOUGH_ROOM;
  160. code_point = utf8::internal::mask8(*it);
  161. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  162. code_point = ((code_point << 12) & 0xffff) +
  163. ((utf8::internal::mask8(*it) << 6) & 0xfff);
  164. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  165. code_point += (*it) & 0x3f;
  166. return UTF8_OK;
  167. }
  168. template <typename octet_iterator>
  169. utf_error get_sequence_4(octet_iterator& it, octet_iterator end,
  170. uint32_t& code_point) {
  171. if (it == end) return NOT_ENOUGH_ROOM;
  172. code_point = utf8::internal::mask8(*it);
  173. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  174. code_point = ((code_point << 18) & 0x1fffff) +
  175. ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
  176. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  177. code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
  178. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  179. code_point += (*it) & 0x3f;
  180. return UTF8_OK;
  181. }
  182. #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
  183. template <typename octet_iterator>
  184. utf_error validate_next(octet_iterator& it, octet_iterator end,
  185. uint32_t& code_point) {
  186. // Save the original value of it so we can go back in case of failure
  187. // Of course, it does not make much sense with i.e. stream iterators
  188. octet_iterator original_it = it;
  189. uint32_t cp = 0;
  190. // Determine the sequence length based on the lead octet
  191. typedef typename std::iterator_traits<octet_iterator>::difference_type
  192. octet_difference_type;
  193. const octet_difference_type length = utf8::internal::sequence_length(it);
  194. // Get trail octets and calculate the code point
  195. utf_error err = UTF8_OK;
  196. switch (length) {
  197. case 0:
  198. return INVALID_LEAD;
  199. case 1:
  200. err = utf8::internal::get_sequence_1(it, end, cp);
  201. break;
  202. case 2:
  203. err = utf8::internal::get_sequence_2(it, end, cp);
  204. break;
  205. case 3:
  206. err = utf8::internal::get_sequence_3(it, end, cp);
  207. break;
  208. case 4:
  209. err = utf8::internal::get_sequence_4(it, end, cp);
  210. break;
  211. }
  212. if (err == UTF8_OK) {
  213. // Decoding succeeded. Now, security checks...
  214. if (utf8::internal::is_code_point_valid(cp)) {
  215. if (!utf8::internal::is_overlong_sequence(cp, length)) {
  216. // Passed! Return here.
  217. code_point = cp;
  218. ++it;
  219. return UTF8_OK;
  220. } else
  221. err = OVERLONG_SEQUENCE;
  222. } else
  223. err = INVALID_CODE_POINT;
  224. }
  225. // Failure branch - restore the original value of the iterator
  226. it = original_it;
  227. return err;
  228. }
  229. template <typename octet_iterator>
  230. inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
  231. uint32_t ignored;
  232. return utf8::internal::validate_next(it, end, ignored);
  233. }
  234. } // namespace internal
  235. /// The library API - functions intended to be called by the users
  236. // Byte order mark
  237. const uint8_t bom[] = {0xef, 0xbb, 0xbf};
  238. template <typename octet_iterator>
  239. octet_iterator find_invalid(octet_iterator start, octet_iterator end) {
  240. octet_iterator result = start;
  241. while (result != end) {
  242. utf8::internal::utf_error err_code =
  243. utf8::internal::validate_next(result, end);
  244. if (err_code != internal::UTF8_OK) return result;
  245. }
  246. return result;
  247. }
  248. template <typename octet_iterator>
  249. inline bool is_valid(octet_iterator start, octet_iterator end) {
  250. return (utf8::find_invalid(start, end) == end);
  251. }
  252. template <typename octet_iterator>
  253. inline bool starts_with_bom(octet_iterator it, octet_iterator end) {
  254. return (((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
  255. ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
  256. ((it != end) && (utf8::internal::mask8(*it)) == bom[2]));
  257. }
  258. // Deprecated in release 2.3
  259. template <typename octet_iterator>
  260. inline bool is_bom(octet_iterator it) {
  261. return ((utf8::internal::mask8(*it++)) == bom[0] &&
  262. (utf8::internal::mask8(*it++)) == bom[1] &&
  263. (utf8::internal::mask8(*it)) == bom[2]);
  264. }
  265. } // namespace utf8
  266. #endif // header guard