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