1
// Copyright 2006 Nemanja Trifunovic
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:
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.
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.
27
#ifndef DRIZZLED_UTF8_CORE_H
28
#define DRIZZLED_UTF8_CORE_H
37
// Helper code - not intended to be directly called by the library users. May be changed at any time
41
// Leading (high) surrogates: 0xd800 - 0xdbff
42
// Trailing (low) surrogates: 0xdc00 - 0xdfff
43
const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
44
const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
45
const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
46
const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
47
const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
48
const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
50
// Maximum valid value for a Unicode code point
51
const uint32_t CODE_POINT_MAX = 0x0010ffffu;
53
template<typename octet_type>
54
inline uint8_t mask8(octet_type oc)
56
return static_cast<uint8_t>(0xff & oc);
58
template<typename u16_type>
59
inline uint16_t mask16(u16_type oc)
61
return static_cast<uint16_t>(0xffff & oc);
63
template<typename octet_type>
64
inline bool is_trail(octet_type oc)
66
return ((mask8(oc) >> 6) == 0x2);
69
template <typename u16>
70
inline bool is_lead_surrogate(u16 cp)
72
return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
75
template <typename u16>
76
inline bool is_trail_surrogate(u16 cp)
78
return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
81
template <typename u16>
82
inline bool is_surrogate(u16 cp)
84
return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
87
template <typename u32>
88
inline bool is_code_point_valid(u32 cp)
90
return (cp <= CODE_POINT_MAX && !is_surrogate(cp) && cp != 0xfffe && cp != 0xffff);
93
template <typename octet_iterator>
94
inline typename std::iterator_traits<octet_iterator>::difference_type
95
sequence_length(octet_iterator lead_it)
97
uint8_t lead = mask8(*lead_it);
100
else if ((lead >> 5) == 0x6)
102
else if ((lead >> 4) == 0xe)
104
else if ((lead >> 3) == 0x1e)
110
template <typename octet_difference_type>
111
inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
117
else if (cp < 0x800) {
121
else if (cp < 0x10000) {
129
enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
131
/// get_sequence_x functions decode utf-8 sequences of the length x
133
template <typename octet_iterator>
134
utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t* code_point)
138
*code_point = mask8(*it);
141
return NOT_ENOUGH_ROOM;
144
template <typename octet_iterator>
145
utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t* code_point)
147
utf_error ret_code = NOT_ENOUGH_ROOM;
150
uint32_t cp = mask8(*it);
153
cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
160
ret_code = INCOMPLETE_SEQUENCE;
163
ret_code = NOT_ENOUGH_ROOM;
169
template <typename octet_iterator>
170
utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t* code_point)
172
utf_error ret_code = NOT_ENOUGH_ROOM;
175
uint32_t cp = mask8(*it);
178
cp = ((cp << 12) & 0xffff) + ((mask8(*it) << 6) & 0xfff);
188
ret_code = INCOMPLETE_SEQUENCE;
191
ret_code = NOT_ENOUGH_ROOM;
194
ret_code = INCOMPLETE_SEQUENCE;
197
ret_code = NOT_ENOUGH_ROOM;
203
template <typename octet_iterator>
204
utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t* code_point)
206
utf_error ret_code = NOT_ENOUGH_ROOM;
209
uint32_t cp = mask8(*it);
212
cp = ((cp << 18) & 0x1fffff) + ((mask8(*it) << 12) & 0x3ffff);
215
cp += (mask8(*it) << 6) & 0xfff;
225
ret_code = INCOMPLETE_SEQUENCE;
228
ret_code = NOT_ENOUGH_ROOM;
231
ret_code = INCOMPLETE_SEQUENCE;
234
ret_code = NOT_ENOUGH_ROOM;
237
ret_code = INCOMPLETE_SEQUENCE;
240
ret_code = NOT_ENOUGH_ROOM;
246
template <typename octet_iterator>
247
utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t* code_point)
249
// Save the original value of it so we can go back in case of failure
250
// Of course, it does not make much sense with i.e. stream iterators
251
octet_iterator original_it = it;
254
// Determine the sequence length based on the lead octet
255
typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
256
octet_difference_type length = sequence_length(it);
260
// Now that we have a valid sequence length, get trail octets and calculate the code point
261
utf_error err = UTF8_OK;
264
err = get_sequence_1(it, end, &cp);
267
err = get_sequence_2(it, end, &cp);
270
err = get_sequence_3(it, end, &cp);
273
err = get_sequence_4(it, end, &cp);
277
if (err == UTF8_OK) {
278
// Decoding succeeded. Now, security checks...
279
if (is_code_point_valid(cp)) {
280
if (!is_overlong_sequence(cp, length)){
281
// Passed! Return here.
288
err = OVERLONG_SEQUENCE;
291
err = INVALID_CODE_POINT;
294
// Failure branch - restore the original value of the iterator
299
template <typename octet_iterator>
300
inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
301
return validate_next(it, end, 0);
304
} // namespace internal
306
/// The library API - functions intended to be called by the users
309
const uint8_t bom[] = {0xef, 0xbb, 0xbf};
311
template <typename octet_iterator>
312
octet_iterator find_invalid(octet_iterator start, octet_iterator end)
314
octet_iterator result = start;
315
while (result != end) {
316
internal::utf_error err_code = internal::validate_next(result, end);
317
if (err_code != internal::UTF8_OK)
323
template <typename octet_iterator>
324
inline bool is_valid(octet_iterator start, octet_iterator end)
326
return (find_invalid(start, end) == end);
329
template <typename octet_iterator>
330
inline bool starts_with_bom (octet_iterator it, octet_iterator end)
333
((it != end) && (internal::mask8(*it++)) == bom[0]) &&
334
((it != end) && (internal::mask8(*it++)) == bom[1]) &&
335
((it != end) && (internal::mask8(*it)) == bom[2])
339
//Deprecated in release 2.3
340
template <typename octet_iterator>
341
inline bool is_bom (octet_iterator it)
344
(internal::mask8(*it++)) == bom[0] &&
345
(internal::mask8(*it++)) == bom[1] &&
346
(internal::mask8(*it)) == bom[2]
350
} // namespace drizzled
352
#endif /* DRIZZLED_UTF8_CORE_H */