Line data Source code
1 : /*
2 : * Protobuf serialization library - utf8 support.
3 : *
4 : * Copyright (c) 2024 Antonin Kriz <antonin.kriz@gmail.com>
5 : * Reference: https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
6 : * Released under the MIT License. See LICENSE in the project root.
7 : */
8 :
9 : #pragma once
10 :
11 : #include <cstddef>
12 : #include <cstdint>
13 : #include <limits>
14 : #include <stdexcept>
15 : #include <string_view>
16 :
17 : namespace spb::detail::utf8
18 : {
19 :
20 : // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
21 : // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
22 :
23 : constexpr uint8_t ok = 0;
24 :
25 3005 : inline uint32_t decode_point(uint32_t *state, uint32_t *codep, uint8_t byte)
26 : {
27 : static const uint8_t utf8d[] = {
28 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1f
30 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3f
32 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5f
34 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7f
36 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9f
38 : 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
39 : 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // a0..bf
40 : 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
41 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0..df
42 : 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
43 : 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
44 : 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
45 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46 : 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
47 : 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1,
48 : 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
49 : 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
50 : 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
51 : 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1,
52 : 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // s7..s8
53 : };
54 :
55 3005 : uint32_t type = utf8d[byte];
56 :
57 3005 : *codep = (*state != ok) ? (byte & 0x3fu) | (*codep << 6) : (0xff >> type) & (byte);
58 :
59 3005 : *state = utf8d[256 + *state * 16 + type];
60 3005 : return *state;
61 : }
62 :
63 : /**
64 : * @brief encode codepoint to utf8
65 : *
66 : * @param unicode codepoint
67 : * @param utf8 output
68 : * @return size of output in bytes, 0 on error
69 : */
70 118 : inline uint32_t encode_point(uint32_t unicode, char utf8[4])
71 : {
72 118 : if (unicode <= 0x7F)
73 : {
74 100 : utf8[0] = (char)unicode;
75 100 : return 1;
76 : }
77 18 : if (unicode <= 0x7FF)
78 : {
79 6 : utf8[0] = (char)((unicode >> 6) | 0xC0);
80 6 : utf8[1] = (char)((unicode & 0x3F) | 0x80);
81 6 : return 2;
82 : }
83 12 : if (unicode >= 0xD800 && unicode < 0xE000)
84 0 : return 0;
85 :
86 12 : if (unicode <= 0xFFFF)
87 : {
88 6 : utf8[0] = (char)((unicode >> 12) | 0xE0);
89 6 : utf8[1] = (char)(((unicode >> 6) & 0x3F) | 0x80);
90 6 : utf8[2] = (char)((unicode & 0x3F) | 0x80);
91 6 : return 3;
92 : }
93 6 : if (unicode <= 0x10FFFF)
94 : {
95 6 : utf8[0] = (char)((unicode >> 18) | 0xF0);
96 6 : utf8[1] = (char)(((unicode >> 12) & 0x3F) | 0x80);
97 6 : utf8[2] = (char)(((unicode >> 6) & 0x3F) | 0x80);
98 6 : utf8[3] = (char)((unicode & 0x3F) | 0x80);
99 6 : return 4;
100 : }
101 0 : return 0;
102 : }
103 :
104 934 : inline bool is_valid(std::string_view str)
105 : {
106 934 : constexpr size_t mask = (std::numeric_limits<size_t>::max() / 0xFFu) * 0x80u;
107 :
108 934 : const auto *start = str.data();
109 934 : const auto *end = str.data() + str.size() - sizeof(size_t);
110 :
111 1002 : while (start <= end)
112 : {
113 80 : const size_t value = *(size_t *)start;
114 80 : if ((value & mask) == 0) [[likely]]
115 68 : start += sizeof(value);
116 : else
117 12 : break;
118 : }
119 :
120 934 : end = str.data() + str.size();
121 934 : uint32_t state = ok;
122 3659 : for (uint32_t codepoint; start < end; ++start)
123 : {
124 2725 : decode_point(&state, &codepoint, *start);
125 : }
126 934 : return state == ok;
127 : }
128 :
129 934 : inline void validate(std::string_view value)
130 : {
131 934 : if (!spb::detail::utf8::is_valid(std::string_view(value.data(), value.size()))) [[unlikely]]
132 1 : throw std::runtime_error("invalid utf8 string");
133 933 : }
134 :
135 : } // namespace spb::detail::utf8
|