ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
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.
 
 
 
 
 
 

250 lines
5.1 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_UTF_ITERATOR_H
  9. #define CPPCMS_UTF_ITERATOR_H
  10. #include <cppcms/cstdint.h>
  11. #include <string.h>
  12. namespace cppcms {
  13. namespace utf {
  14. static const uint32_t illegal = 0xFFFFFFFFu;
  15. inline bool valid(uint32_t v)
  16. {
  17. if(v>0x10FFFF)
  18. return false;
  19. if(0xD800 <=v && v<= 0xDFFF) // surragates
  20. return false;
  21. return true;
  22. }
  23. }
  24. namespace utf8 {
  25. inline bool is_trail(char ci)
  26. {
  27. unsigned char c=ci;
  28. return (c & 0xC0)==0x80;
  29. }
  30. inline int trail_length(unsigned char c)
  31. {
  32. if(c < 128)
  33. return 0;
  34. if(c < 194)
  35. return -1;
  36. if(c < 224)
  37. return 1;
  38. if(c < 240)
  39. return 2;
  40. if(c <=244)
  41. return 3;
  42. return -1;
  43. }
  44. inline int width(uint32_t value)
  45. {
  46. if(value <=0x7F) {
  47. return 1;
  48. }
  49. else if(value <=0x7FF) {
  50. return 2;
  51. }
  52. else if(value <=0xFFFF) {
  53. return 3;
  54. }
  55. else {
  56. return 4;
  57. }
  58. }
  59. // See RFC 3629
  60. // Based on: http://www.w3.org/International/questions/qa-forms-utf-8
  61. template<typename Iterator>
  62. uint32_t next(Iterator &p,Iterator e,bool html=false,bool /*decode*/=false)
  63. {
  64. using utf::illegal;
  65. if(p==e)
  66. return illegal;
  67. unsigned char lead = *p++;
  68. // First byte is fully validated here
  69. int trail_size = trail_length(lead);
  70. if(trail_size < 0)
  71. return illegal;
  72. //
  73. // Ok as only ASCII may be of size = 0
  74. // also optimize for ASCII text
  75. //
  76. if(trail_size == 0) {
  77. if(!html || (lead >= 0x20 && lead!=0x7F) || lead==0x9 || lead==0x0A || lead==0x0D)
  78. return lead;
  79. return illegal;
  80. }
  81. uint32_t c = lead & ((1<<(6-trail_size))-1);
  82. // Read the rest
  83. unsigned char tmp;
  84. switch(trail_size) {
  85. case 3:
  86. if(p==e)
  87. return illegal;
  88. tmp = *p++;
  89. if (!is_trail(tmp))
  90. return illegal;
  91. c = (c << 6) | ( tmp & 0x3F);
  92. case 2:
  93. if(p==e)
  94. return illegal;
  95. tmp = *p++;
  96. if (!is_trail(tmp))
  97. return illegal;
  98. c = (c << 6) | ( tmp & 0x3F);
  99. case 1:
  100. if(p==e)
  101. return illegal;
  102. tmp = *p++;
  103. if (!is_trail(tmp))
  104. return illegal;
  105. c = (c << 6) | ( tmp & 0x3F);
  106. }
  107. // Check code point validity: no surrogates and
  108. // valid range
  109. if(!utf::valid(c))
  110. return illegal;
  111. // make sure it is the most compact representation
  112. if(width(c)!=trail_size + 1)
  113. return illegal;
  114. if(html && c<0xA0)
  115. return illegal;
  116. return c;
  117. } // valid
  118. template<typename Iterator>
  119. bool validate(Iterator p,Iterator e,size_t &count,bool html=false)
  120. {
  121. while(p!=e) {
  122. if(next(p,e,html)==utf::illegal)
  123. return false;
  124. count++;
  125. }
  126. return true;
  127. }
  128. template<typename Iterator>
  129. bool validate(Iterator p,Iterator e,bool html=false)
  130. {
  131. while(p!=e)
  132. if(next(p,e,html)==utf::illegal)
  133. return false;
  134. return true;
  135. }
  136. struct seq {
  137. char c[4];
  138. unsigned len;
  139. };
  140. inline seq encode(uint32_t value)
  141. {
  142. seq out=seq();
  143. if(value <=0x7F) {
  144. out.c[0]=value;
  145. out.len=1;
  146. }
  147. else if(value <=0x7FF) {
  148. out.c[0]=(value >> 6) | 0xC0;
  149. out.c[1]=(value & 0x3F) | 0x80;
  150. out.len=2;
  151. }
  152. else if(value <=0xFFFF) {
  153. out.c[0]=(value >> 12) | 0xE0;
  154. out.c[1]=((value >> 6) & 0x3F) | 0x80;
  155. out.c[2]=(value & 0x3F) | 0x80;
  156. out.len=3;
  157. }
  158. else {
  159. out.c[0]=(value >> 18) | 0xF0;
  160. out.c[1]=((value >> 12) & 0x3F) | 0x80;
  161. out.c[2]=((value >> 6) & 0x3F) | 0x80;
  162. out.c[3]=(value & 0x3F) | 0x80;
  163. out.len=4;
  164. }
  165. return out;
  166. }
  167. } // namespace utf8
  168. namespace utf16 {
  169. // See RFC 2781
  170. inline bool is_first_surrogate(uint16_t x)
  171. {
  172. return 0xD800 <=x && x<= 0xDBFF;
  173. }
  174. inline bool is_second_surrogate(uint16_t x)
  175. {
  176. return 0xDC00 <=x && x<= 0xDFFF;
  177. }
  178. inline uint32_t combine_surrogate(uint16_t w1,uint16_t w2)
  179. {
  180. return ((uint32_t(w1 & 0x3FF) << 10) | (w2 & 0x3FF)) + 0x10000;
  181. }
  182. template<typename It>
  183. inline uint32_t next(It &current,It last)
  184. {
  185. uint16_t w1=*current++;
  186. if(w1 < 0xD800 || 0xDFFF < w1) {
  187. return w1;
  188. }
  189. if(w1 > 0xDBFF)
  190. return utf::illegal;
  191. if(current==last)
  192. return utf::illegal;
  193. uint16_t w2=*current++;
  194. if(w2 < 0xDC00 || 0xDFFF < w2)
  195. return utf::illegal;
  196. return combine_surrogate(w1,w2);
  197. }
  198. inline int width(uint32_t u)
  199. {
  200. return u>=0x100000 ? 2 : 1;
  201. }
  202. struct seq {
  203. uint16_t c[2];
  204. unsigned len;
  205. };
  206. inline seq encode(uint32_t u)
  207. {
  208. seq out=seq();
  209. if(u<=0xFFFF) {
  210. out.c[0]=u;
  211. out.len=1;
  212. }
  213. else {
  214. u-=0x10000;
  215. out.c[0]=0xD800 | (u>>10);
  216. out.c[1]=0xDC00 | (u & 0x3FF);
  217. out.len=2;
  218. }
  219. return out;
  220. }
  221. } // utf16;
  222. } // namespace cppcms
  223. #endif