]> git.donarmstrong.com Git - flightcrew.git/blob - src/utf8-cpp/utf8/core.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / utf8-cpp / utf8 / 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 #include "../../BoostParts/boost/cstdint.hpp"
33
34 namespace utf8
35 {
36     // The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
37     // You may need to change them to match your system.
38     // These typedefs have the same names as ones from cstdint, or boost/cstdint
39     //typedef unsigned char   uint8_t;
40     //typedef unsigned short  uint16_t;
41     //typedef unsigned int    uint32_t;
42     
43     // Changed by Strahinja Markovic to use the boost versions 
44     // of these (for portability).
45     typedef boost::uint8_t   uint8_t;
46     typedef boost::uint16_t  uint16_t;
47     typedef boost::uint32_t  uint32_t;    
48
49 // Helper code - not intended to be directly called by the library users. May be changed at any time
50 namespace internal
51 {
52     // Unicode constants
53     // Leading (high) surrogates: 0xd800 - 0xdbff
54     // Trailing (low) surrogates: 0xdc00 - 0xdfff
55     const uint16_t LEAD_SURROGATE_MIN  = 0xd800u;
56     const uint16_t LEAD_SURROGATE_MAX  = 0xdbffu;
57     const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
58     const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
59     const uint16_t LEAD_OFFSET         = LEAD_SURROGATE_MIN - (0x10000 >> 10);
60     const uint32_t SURROGATE_OFFSET    = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
61
62     // Maximum valid value for a Unicode code point
63     const uint32_t CODE_POINT_MAX      = 0x0010ffffu;
64
65     template<typename octet_type>
66     inline uint8_t mask8(octet_type oc)
67     {
68         return static_cast<uint8_t>(0xff & oc);
69     }
70     template<typename u16_type>
71     inline uint16_t mask16(u16_type oc)
72     {
73         return static_cast<uint16_t>(0xffff & oc);
74     }
75     template<typename octet_type>
76     inline bool is_trail(octet_type oc)
77     {
78         return ((mask8(oc) >> 6) == 0x2);
79     }
80
81     template <typename u16>
82     inline bool is_lead_surrogate(u16 cp)
83     {
84         return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
85     }
86
87     template <typename u16>
88     inline bool is_trail_surrogate(u16 cp)
89     {
90         return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
91     }
92
93     template <typename u16>
94     inline bool is_surrogate(u16 cp)
95     {
96         return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
97     }
98
99     template <typename u32>
100     inline bool is_code_point_valid(u32 cp)
101     {
102         return (cp <= CODE_POINT_MAX && !is_surrogate(cp) && cp != 0xfffe && cp != 0xffff);
103     }
104
105     template <typename octet_iterator>
106     inline typename std::iterator_traits<octet_iterator>::difference_type
107     sequence_length(octet_iterator lead_it)
108     {
109         uint8_t lead = mask8(*lead_it);
110         if (lead < 0x80)
111             return 1;
112         else if ((lead >> 5) == 0x6)
113             return 2;
114         else if ((lead >> 4) == 0xe)
115             return 3;
116         else if ((lead >> 3) == 0x1e)
117             return 4;
118         else
119             return 0;
120     }
121
122     template <typename octet_difference_type>
123     inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
124     {
125         if (cp < 0x80) {
126             if (length != 1) 
127                 return true;
128         }
129         else if (cp < 0x800) {
130             if (length != 2) 
131                 return true;
132         }
133         else if (cp < 0x10000) {
134             if (length != 3) 
135                 return true;
136         }
137
138         return false;
139     }
140
141     enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
142
143     /// get_sequence_x functions decode utf-8 sequences of the length x
144
145     template <typename octet_iterator>
146     utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t* code_point)
147     {
148         if (it != end) {
149             if (code_point)
150                 *code_point = mask8(*it);
151             return UTF8_OK;
152         }
153         return NOT_ENOUGH_ROOM;
154     }
155
156     template <typename octet_iterator>
157     utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t* code_point)
158     {
159         utf_error ret_code = NOT_ENOUGH_ROOM;
160
161         if (it != end) {
162             uint32_t cp = mask8(*it);
163             if (++it != end) {
164                 if (is_trail(*it)) {
165                     cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
166
167                     if (code_point)
168                         *code_point = cp;
169                     ret_code = UTF8_OK;
170                 }
171                 else
172                     ret_code = INCOMPLETE_SEQUENCE;
173             }
174             else
175                 ret_code = NOT_ENOUGH_ROOM;
176         }
177
178         return ret_code;
179     }
180
181     template <typename octet_iterator>
182     utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t* code_point)
183     {
184         utf_error ret_code = NOT_ENOUGH_ROOM;
185
186         if (it != end) {
187             uint32_t cp = mask8(*it);
188             if (++it != end) {
189                 if (is_trail(*it)) {
190                     cp = ((cp << 12) & 0xffff) + ((mask8(*it) << 6) & 0xfff);
191                     if (++it != end) {
192                         if (is_trail(*it)) {
193                             cp += (*it) & 0x3f;
194
195                             if (code_point)
196                                 *code_point = cp;
197                             ret_code = UTF8_OK;
198                         }
199                         else 
200                             ret_code = INCOMPLETE_SEQUENCE;
201                     }
202                     else
203                         ret_code = NOT_ENOUGH_ROOM;
204                 }
205                 else
206                     ret_code = INCOMPLETE_SEQUENCE;
207             }
208             else
209                 ret_code = NOT_ENOUGH_ROOM;
210         }
211
212         return ret_code;
213     }
214
215     template <typename octet_iterator>
216     utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t* code_point)
217     {
218         utf_error ret_code = NOT_ENOUGH_ROOM;
219
220         if (it != end) {
221             uint32_t cp = mask8(*it);
222             if (++it != end) {
223                 if (is_trail(*it)) {
224                     cp = ((cp << 18) & 0x1fffff) + ((mask8(*it) << 12) & 0x3ffff);
225                     if (++it != end) {
226                         if (is_trail(*it)) {
227                             cp += (mask8(*it) << 6) & 0xfff;
228                             if (++it != end) {
229                                 if (is_trail(*it)) {
230                                     cp += (*it) & 0x3f;
231
232                                     if (code_point)
233                                         *code_point = cp;
234                                     ret_code = UTF8_OK;
235                                 }
236                                 else
237                                     ret_code = INCOMPLETE_SEQUENCE;
238                             }
239                             else
240                                 ret_code = NOT_ENOUGH_ROOM;
241                         }
242                         else
243                             ret_code = INCOMPLETE_SEQUENCE;
244                     }
245                     else
246                         ret_code = NOT_ENOUGH_ROOM;
247                 }
248                 else 
249                     ret_code = INCOMPLETE_SEQUENCE;
250             }
251             else
252                 ret_code = NOT_ENOUGH_ROOM;
253         }
254
255         return ret_code;
256     }
257
258     template <typename octet_iterator>
259     utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t* code_point)
260     {
261         // Save the original value of it so we can go back in case of failure
262         // Of course, it does not make much sense with i.e. stream iterators
263         octet_iterator original_it = it;
264
265         uint32_t cp = 0;
266         // Determine the sequence length based on the lead octet
267         typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
268         octet_difference_type length = sequence_length(it);
269         if (length == 0)
270             return INVALID_LEAD;
271
272         // Now that we have a valid sequence length, get trail octets and calculate the code point
273         utf_error err = UTF8_OK;
274         switch (length) {
275             case 1:
276                 err = get_sequence_1(it, end, &cp);
277                 break;
278             case 2:
279                 err = get_sequence_2(it, end, &cp);
280             break;
281             case 3:
282                 err = get_sequence_3(it, end, &cp);
283             break;
284             case 4:
285                 err = get_sequence_4(it, end, &cp);
286             break;
287         }
288
289         if (err == UTF8_OK) {
290             // Decoding succeeded. Now, security checks...
291             if (is_code_point_valid(cp)) {
292                 if (!is_overlong_sequence(cp, length)){
293                     // Passed! Return here.
294                     if (code_point)
295                         *code_point = cp;
296                     ++it;
297                     return UTF8_OK;
298                 }
299                 else
300                     err = OVERLONG_SEQUENCE;
301             }
302             else 
303                 err = INVALID_CODE_POINT;
304         }
305
306         // Failure branch - restore the original value of the iterator
307         it = original_it;
308         return err;
309     }
310
311     template <typename octet_iterator>
312     inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
313         return validate_next(it, end, 0);
314     }
315
316 } // namespace internal
317
318     /// The library API - functions intended to be called by the users
319
320     // Byte order mark
321     const uint8_t bom[] = {0xef, 0xbb, 0xbf};
322
323     template <typename octet_iterator>
324     octet_iterator find_invalid(octet_iterator start, octet_iterator end)
325     {
326         octet_iterator result = start;
327         while (result != end) {
328             internal::utf_error err_code = internal::validate_next(result, end);
329             if (err_code != internal::UTF8_OK)
330                 return result;
331         }
332         return result;
333     }
334
335     template <typename octet_iterator>
336     inline bool is_valid(octet_iterator start, octet_iterator end)
337     {
338         return (find_invalid(start, end) == end);
339     }
340
341     template <typename octet_iterator>
342     inline bool starts_with_bom (octet_iterator it, octet_iterator end)
343     {
344         return (
345             ((it != end) && (internal::mask8(*it++)) == bom[0]) &&
346             ((it != end) && (internal::mask8(*it++)) == bom[1]) &&
347             ((it != end) && (internal::mask8(*it))   == bom[2])
348            );
349     }
350         
351         //Deprecated in release 2.3 
352     template <typename octet_iterator>
353     inline bool is_bom (octet_iterator it)
354     {
355         return (
356             (internal::mask8(*it++)) == bom[0] &&
357             (internal::mask8(*it++)) == bom[1] &&
358             (internal::mask8(*it))   == bom[2]
359            );
360     }
361 } // namespace utf8
362
363 #endif // header guard
364
365