Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. namespace utf8
  27. {
  28. // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
  29. // You may need to change them to match your system.
  30. // These typedefs have the same names as ones from cstdint, or boost/cstdint
  31. typedef unsigned char uint8_t;
  32. typedef unsigned short uint16_t;
  33. typedef unsigned int uint32_t;
  34. // Helper code - not intended to be directly called by the library users. May be changed at any time
  35. namespace internal
  36. {
  37. // Unicode constants
  38. // Leading (high) surrogates: 0xd800 - 0xdbff
  39. // Trailing (low) surrogates: 0xdc00 - 0xdfff
  40. const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
  41. const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
  42. const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
  43. const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
  44. const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
  45. const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
  46. // Maximum valid value for a Unicode code point
  47. const uint32_t CODE_POINT_MAX = 0x0010ffffu;
  48. template<typename octet_type>
  49. inline uint8_t mask8(octet_type oc)
  50. {
  51. return static_cast<uint8_t>(0xff & oc);
  52. }
  53. template<typename u16_type>
  54. inline uint16_t mask16(u16_type oc)
  55. {
  56. return static_cast<uint16_t>(0xffff & oc);
  57. }
  58. template<typename octet_type>
  59. inline bool is_trail(octet_type oc)
  60. {
  61. return ((utf8::internal::mask8(oc) >> 6) == 0x2);
  62. }
  63. template <typename u16>
  64. inline bool is_lead_surrogate(u16 cp)
  65. {
  66. return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
  67. }
  68. template <typename u16>
  69. inline bool is_trail_surrogate(u16 cp)
  70. {
  71. return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
  72. }
  73. template <typename u16>
  74. inline bool is_surrogate(u16 cp)
  75. {
  76. return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
  77. }
  78. template <typename u32>
  79. inline bool is_code_point_valid(u32 cp)
  80. {
  81. return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
  82. }
  83. template <typename octet_iterator>
  84. inline typename std::iterator_traits<octet_iterator>::difference_type
  85. sequence_length(octet_iterator lead_it)
  86. {
  87. uint8_t lead = utf8::internal::mask8(*lead_it);
  88. if (lead < 0x80)
  89. return 1;
  90. else if ((lead >> 5) == 0x6)
  91. return 2;
  92. else if ((lead >> 4) == 0xe)
  93. return 3;
  94. else if ((lead >> 3) == 0x1e)
  95. return 4;
  96. else
  97. return 0;
  98. }
  99. template <typename octet_difference_type>
  100. inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
  101. {
  102. if (cp < 0x80) {
  103. if (length != 1)
  104. return true;
  105. }
  106. else if (cp < 0x800) {
  107. if (length != 2)
  108. return true;
  109. }
  110. else if (cp < 0x10000) {
  111. if (length != 3)
  112. return true;
  113. }
  114. return false;
  115. }
  116. enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
  117. /// Helper for get_sequence_x
  118. template <typename octet_iterator>
  119. utf_error increase_safely(octet_iterator& it, octet_iterator end)
  120. {
  121. if (++it == end)
  122. return NOT_ENOUGH_ROOM;
  123. if (!utf8::internal::is_trail(*it))
  124. return INCOMPLETE_SEQUENCE;
  125. return UTF8_OK;
  126. }
  127. #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;}
  128. /// get_sequence_x functions decode utf-8 sequences of the length x
  129. template <typename octet_iterator>
  130. utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  131. {
  132. if (it == end)
  133. return NOT_ENOUGH_ROOM;
  134. code_point = utf8::internal::mask8(*it);
  135. return UTF8_OK;
  136. }
  137. template <typename octet_iterator>
  138. utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  139. {
  140. if (it == end)
  141. return NOT_ENOUGH_ROOM;
  142. code_point = utf8::internal::mask8(*it);
  143. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  144. code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
  145. return UTF8_OK;
  146. }
  147. template <typename octet_iterator>
  148. utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  149. {
  150. if (it == end)
  151. return NOT_ENOUGH_ROOM;
  152. code_point = utf8::internal::mask8(*it);
  153. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  154. code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
  155. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  156. code_point += (*it) & 0x3f;
  157. return UTF8_OK;
  158. }
  159. template <typename octet_iterator>
  160. utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  161. {
  162. if (it == end)
  163. return NOT_ENOUGH_ROOM;
  164. code_point = utf8::internal::mask8(*it);
  165. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  166. code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
  167. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  168. code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
  169. UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
  170. code_point += (*it) & 0x3f;
  171. return UTF8_OK;
  172. }
  173. #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
  174. template <typename octet_iterator>
  175. utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point)
  176. {
  177. // Save the original value of it so we can go back in case of failure
  178. // Of course, it does not make much sense with i.e. stream iterators
  179. octet_iterator original_it = it;
  180. uint32_t cp = 0;
  181. // Determine the sequence length based on the lead octet
  182. typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
  183. const octet_difference_type length = utf8::internal::sequence_length(it);
  184. // Get trail octets and calculate the code point
  185. utf_error err = UTF8_OK;
  186. switch (length) {
  187. case 0:
  188. return INVALID_LEAD;
  189. case 1:
  190. err = utf8::internal::get_sequence_1(it, end, cp);
  191. break;
  192. case 2:
  193. err = utf8::internal::get_sequence_2(it, end, cp);
  194. break;
  195. case 3:
  196. err = utf8::internal::get_sequence_3(it, end, cp);
  197. break;
  198. case 4:
  199. err = utf8::internal::get_sequence_4(it, end, cp);
  200. break;
  201. }
  202. if (err == UTF8_OK) {
  203. // Decoding succeeded. Now, security checks...
  204. if (utf8::internal::is_code_point_valid(cp)) {
  205. if (!utf8::internal::is_overlong_sequence(cp, length)){
  206. // Passed! Return here.
  207. code_point = cp;
  208. ++it;
  209. return UTF8_OK;
  210. }
  211. else
  212. err = OVERLONG_SEQUENCE;
  213. }
  214. else
  215. err = INVALID_CODE_POINT;
  216. }
  217. // Failure branch - restore the original value of the iterator
  218. it = original_it;
  219. return err;
  220. }
  221. template <typename octet_iterator>
  222. inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
  223. uint32_t ignored;
  224. return utf8::internal::validate_next(it, end, ignored);
  225. }
  226. } // namespace internal
  227. /// The library API - functions intended to be called by the users
  228. // Byte order mark
  229. const uint8_t bom[] = {0xef, 0xbb, 0xbf};
  230. template <typename octet_iterator>
  231. octet_iterator find_invalid(octet_iterator start, octet_iterator end)
  232. {
  233. octet_iterator result = start;
  234. while (result != end) {
  235. utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
  236. if (err_code != internal::UTF8_OK)
  237. return result;
  238. }
  239. return result;
  240. }
  241. template <typename octet_iterator>
  242. inline bool is_valid(octet_iterator start, octet_iterator end)
  243. {
  244. return (utf8::find_invalid(start, end) == end);
  245. }
  246. template <typename octet_iterator>
  247. inline bool starts_with_bom (octet_iterator it, octet_iterator end)
  248. {
  249. return (
  250. ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
  251. ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
  252. ((it != end) && (utf8::internal::mask8(*it)) == bom[2])
  253. );
  254. }
  255. //Deprecated in release 2.3
  256. template <typename octet_iterator>
  257. inline bool is_bom (octet_iterator it)
  258. {
  259. return (
  260. (utf8::internal::mask8(*it++)) == bom[0] &&
  261. (utf8::internal::mask8(*it++)) == bom[1] &&
  262. (utf8::internal::mask8(*it)) == bom[2]
  263. );
  264. }
  265. } // namespace utf8
  266. #endif // header guard