GG
core.h
Go to the documentation of this file.
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 
31 #ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
32 #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
33 
34 #include <boost/cstdint.hpp>
35 
36 #include <iterator>
37 
38 namespace utf8
39 {
40  // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
41  // You may need to change them to match your system.
42  // These typedefs have the same names as ones from cstdint, or boost/cstdint
43  typedef boost::uint8_t uint8_t;
44  typedef boost::uint16_t uint16_t;
45  typedef boost::uint32_t uint32_t;
46 
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 = LEAD_SURROGATE_MIN - (0x10000 >> 10);
58  const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
59 
60  // Maximum valid value for a Unicode code point
61  const uint32_t CODE_POINT_MAX = 0x0010ffffu;
62 
63  template<typename octet_type>
64  inline uint8_t mask8(octet_type oc)
65  {
66  return static_cast<uint8_t>(0xff & oc);
67  }
68  template<typename u16_type>
69  inline uint16_t mask16(u16_type oc)
70  {
71  return static_cast<uint16_t>(0xffff & oc);
72  }
73  template<typename octet_type>
74  inline bool is_trail(octet_type oc)
75  {
76  return ((mask8(oc) >> 6) == 0x2);
77  }
78 
79  template <typename u16>
80  inline bool is_surrogate(u16 cp)
81  {
82  return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
83  }
84 
85  template <typename u32>
86  inline bool is_code_point_valid(u32 cp)
87  {
88  return (cp <= CODE_POINT_MAX && !is_surrogate(cp) && cp != 0xfffe && cp != 0xffff);
89  }
90 
91  template <typename octet_iterator>
92  inline typename std::iterator_traits<octet_iterator>::difference_type
93  sequence_length(octet_iterator lead_it)
94  {
95  uint8_t lead = mask8(*lead_it);
96  if (lead < 0x80)
97  return 1;
98  else if ((lead >> 5) == 0x6)
99  return 2;
100  else if ((lead >> 4) == 0xe)
101  return 3;
102  else if ((lead >> 3) == 0x1e)
103  return 4;
104  else
105  return 0;
106  }
107 
108  enum utf_error {OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
109 
110  template <typename octet_iterator>
111  utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t* code_point)
112  {
113  uint32_t cp = mask8(*it);
114  // Check the lead octet
115  typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
116  octet_difference_type length = sequence_length(it);
117 
118  // "Shortcut" for ASCII characters
119  if (length == 1) {
120  if (end - it > 0) {
121  if (code_point)
122  *code_point = cp;
123  ++it;
124  return OK;
125  }
126  else
127  return NOT_ENOUGH_ROOM;
128  }
129 
130  // Do we have enough memory?
131  if (std::distance(it, end) < length)
132  return NOT_ENOUGH_ROOM;
133 
134  // Check trail octets and calculate the code point
135  switch (length) {
136  case 0:
137  return INVALID_LEAD;
138  break;
139  case 2:
140  if (is_trail(*(++it))) {
141  cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
142  }
143  else {
144  --it;
145  return INCOMPLETE_SEQUENCE;
146  }
147  break;
148  case 3:
149  if (is_trail(*(++it))) {
150  cp = ((cp << 12) & 0xffff) + ((mask8(*it) << 6) & 0xfff);
151  if (is_trail(*(++it))) {
152  cp += (*it) & 0x3f;
153  }
154  else {
155  std::advance(it, -2);
156  return INCOMPLETE_SEQUENCE;
157  }
158  }
159  else {
160  --it;
161  return INCOMPLETE_SEQUENCE;
162  }
163  break;
164  case 4:
165  if (is_trail(*(++it))) {
166  cp = ((cp << 18) & 0x1fffff) + ((mask8(*it) << 12) & 0x3ffff);
167  if (is_trail(*(++it))) {
168  cp += (mask8(*it) << 6) & 0xfff;
169  if (is_trail(*(++it))) {
170  cp += (*it) & 0x3f;
171  }
172  else {
173  std::advance(it, -3);
174  return INCOMPLETE_SEQUENCE;
175  }
176  }
177  else {
178  std::advance(it, -2);
179  return INCOMPLETE_SEQUENCE;
180  }
181  }
182  else {
183  --it;
184  return INCOMPLETE_SEQUENCE;
185  }
186  break;
187  }
188  // Is the code point valid?
189  if (!is_code_point_valid(cp)) {
190  for (octet_difference_type i = 0; i < length - 1; ++i)
191  --it;
192  return INVALID_CODE_POINT;
193  }
194 
195  if (code_point)
196  *code_point = cp;
197 
198  if (cp < 0x80) {
199  if (length != 1) {
200  std::advance(it, -(length-1));
201  return OVERLONG_SEQUENCE;
202  }
203  }
204  else if (cp < 0x800) {
205  if (length != 2) {
206  std::advance(it, -(length-1));
207  return OVERLONG_SEQUENCE;
208  }
209  }
210  else if (cp < 0x10000) {
211  if (length != 3) {
212  std::advance(it, -(length-1));
213  return OVERLONG_SEQUENCE;
214  }
215  }
216 
217  ++it;
218  return OK;
219  }
220 
221  template <typename octet_iterator>
222  inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
223  return validate_next(it, end, 0);
224  }
225 
226 } // namespace internal
227 
229 
230  // Byte order mark
231  const uint8_t bom[] = {0xef, 0xbb, 0xbf};
232 
233  template <typename octet_iterator>
234  octet_iterator find_invalid(octet_iterator start, octet_iterator end)
235  {
236  octet_iterator result = start;
237  while (result != end) {
238  internal::utf_error err_code = internal::validate_next(result, end);
239  if (err_code != internal::OK)
240  return result;
241  }
242  return result;
243  }
244 
245  template <typename octet_iterator>
246  inline bool is_valid(octet_iterator start, octet_iterator end)
247  {
248  return (find_invalid(start, end) == end);
249  }
250 
251  template <typename octet_iterator>
252  inline bool is_bom (octet_iterator it)
253  {
254  return (
255  (internal::mask8(*it++)) == bom[0] &&
256  (internal::mask8(*it++)) == bom[1] &&
257  (internal::mask8(*it)) == bom[2]
258  );
259  }
260 } // namespace utf8
261 
262 #endif // header guard
263 
264