-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.cpp
More file actions
247 lines (231 loc) · 7.38 KB
/
encoding.cpp
File metadata and controls
247 lines (231 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*****
* Part of LnkDump2000
* Licence: GPL, version 3 or later (see COPYING file or https://www.gnu.org/licenses/gpl-3.0.txt)
*****/
#include "encoding.h"
//! append codepoint to an utf-8 string
void
utf8_append(std::string &s, codepoint_t c)
{
if (c <= 0x7F) {
// 7 bit
s.push_back(char(c));
} else if (c <= 0x7FF) {
// 11 bit = 5 + 6 bits
char c1 = get_bits<6, 10>(c) | 0b11000000;
char c2 = get_bits<0, 5>(c) | 0b10000000;
s.push_back(c1);
s.push_back(c2);
} else if (c <= 0xFFFF) {
// 16 bit = 4 + 6 + 6 bits
char c1 = get_bits<12, 15>(c) | 0b11100000;
char c2 = get_bits< 6, 11>(c) | 0b10000000;
char c3 = get_bits< 0, 5>(c) | 0b10000000;
s.push_back(c1);
s.push_back(c2);
s.push_back(c3);
} else if (c >= 0x10000 && c <= 0x10FFFF) {
// 21 bit = 3 + 6 + 6 + 6 bits
char c1 = get_bits<18, 20>(c) | 0b11110000;
char c2 = get_bits<12, 17>(c) | 0b10000000;
char c3 = get_bits< 6, 11>(c) | 0b10000000;
char c4 = get_bits< 0, 5>(c) | 0b10000000;
s.push_back(c1);
s.push_back(c2);
s.push_back(c3);
s.push_back(c4);
} else {
utf8_append(s, invalid_repl);
}
}
//! convert from utf16le to utf8.
std::string
utf16le_to_utf8(const std::u16string& uni)
{
std::string r;
size_t i = 0;
const auto len = uni.length();
while (i < len) {
uint16_t c1 = uni.at(i);
i++;
if (c1 <= 0xD7FF || (c1 >= 0xE000)) {
utf8_append(r, c1);
} else if (c1 >= 0xDC00 && c1 <= 0xDFFF) {
// unpaired low surrogate - replace and skip
utf8_append(r, invalid_repl);
} else if (c1 >= 0xD800 && c1 <= 0xDBFF) {
// high surrogate
if (i < len) {
uint16_t c2 = uni.at(i);
i++;
if (c2 >= 0xDC00 && c2 >= 0xDFFF) {
// paired high surrogate
codepoint_t c = ((c1 - 0xD800) << 10) + (c2 - 0xDC00) + 0x10000;
utf8_append(r, c);
} else {
// unpaired high surrogate
utf8_append(r, invalid_repl);
i--;
}
} else {
// unpaired high surrogate (end of string)
utf8_append(r, invalid_repl);
}
}
}
return r;
}
//! first value is codepoint at pos, second value is number of bytes taken.
//! second value is 0 if pos >= length of string.
std::pair<codepoint_t, size_t>
utf8_codepoint(const std::string_view& s, size_t pos)
{
const size_t slen = s.length();
if (pos >= slen) {
return std::make_pair(invalid_repl, 0);
} else {
uint8_t c = s.at(pos);
const size_t nb = (c <= 0b01111111 ? 1 :
(c <= 0b11011111 ? 2 : (c <= 0b11101111 ? 3 : (c <= 0b11110111 ? 4 : 0))));
if (nb == 0) {
// invalid first character
return std::make_pair(invalid_repl, 1);
} else if (slen - pos < nb) {
// incomplete code due to string length, eat rest of the string
return std::make_pair(invalid_repl, slen - pos);
} else {
// decode first byte, nb is 1..4
codepoint_t r = c & (nb == 1 ? 0xFF : (nb == 2 ? 0b11111 : (nb == 3 ? 0b1111 : 0b111)));
// decode bytes 2..4
pos++;
for (size_t i = 2; i <= nb; i++, pos++) {
uint8_t d = s.at(pos);
if (get_bits<6, 7>(d) == 0b10) {
// ok, bytes begin with 10
r <<= 6;
r |= get_bits<0, 5>(d);
} else {
// error, eat string up to previous character
return std::make_pair(invalid_repl, i - 1);
}
}
// ok, return the codepoint
return std::make_pair(r, nb);
}
}
}
struct DoublesDef
{
uint8_t leadingByte;
uint8_t trailingStart;
size_t length;
const uint16_t* data;
};
struct CodecDef
{
const uint16_t* singlesMap;
const DoublesDef* doublesMap;
size_t doublesLength;
};
// Codepage definitions
#include "enc_single.inc"
#include "enc_asian.inc"
constexpr std::array<std::pair<const char*, const void*>, 15> codec_defs {{
{"874 - Thai", &cp874},
{"932 - Japanese (Shift-JIS)", &cp932}, // mb
{"936 - Chinese Simplified (GBK)", &cp936}, // mb
{"949 - Korean (Hangul)", &cp949}, // mb
{"950 - Chinese (Big5)", &cp950}, // mb
{"1250 - Eastern European", &cp1250},
{"1251 - Cyrillic", &cp1251},
{"1252 - Latin 1", &cp1252},
{"1253 - Greek", &cp1253},
{"1254 - Turkish", &cp1254},
{"1255 - Hebrew", &cp1255},
{"1256 - Arabic", &cp1256},
{"1257 - Baltic", &cp1257},
{"1258 - Vietnam", &cp1258},
{"1361 - Korean (Johab)", &cp1361} // mb
}};
// Codecs
class CodecImpl
{
private:
const uint16_t* m_singles;
std::array<const DoublesDef*, 256> m_doubles;
public:
CodecImpl(size_t index)
{
const CodecDef *def = (CodecDef*)codec_defs[index].second;
m_singles = def->singlesMap;
std::fill(m_doubles.begin(), m_doubles.end(), nullptr);
for (size_t i = 0; i < def->doublesLength; i++) {
uint8_t leading = def->doublesMap[i].leadingByte;
m_doubles[leading] = &def->doublesMap[i];
}
}
std::pair<codepoint_t, size_t>
decode_char(const std::string& s, size_t pos)
{
uint8_t c1 = s.at(pos);
if (m_doubles[c1] != nullptr) {
if ((pos + 1) < s.length()) {
uint8_t c2 = s.at(pos + 1);
const DoublesDef *def = m_doubles[c1];
if (def && c2 >= def->trailingStart && c2 < def->trailingStart + def->length) {
auto d = def->data[c2 - def->trailingStart];
if (d != 0) {
return std::pair<codepoint_t, size_t>(d, 2);
} else {
return std::pair<codepoint_t, size_t>(invalid_repl, 2);
}
} else {
return std::pair<codepoint_t, size_t>(invalid_repl, 2);
}
} else {
return std::pair<codepoint_t, size_t>(invalid_repl, 1);
}
} else {
auto d = m_singles[c1];
if (d != 0) {
return std::pair<codepoint_t, size_t>(d, 1);
} else {
return std::pair<codepoint_t, size_t>(invalid_repl, 1);
}
}
}
std::string
decode_string(const std::string &s)
{
size_t pos = 0;
std::string r;
while (pos < s.length()) {
auto [cp, l] = decode_char(s, pos);
if (cp <= 0xD7FF || (cp >= 0xE000)) {
utf8_append(r, cp);
} else {
utf8_append(r, invalid_repl);
}
pos += l;
}
return r;
}
};
Codec::Codec(size_t index)
{
p = new CodecImpl(index);
m_index = index;
}
Codec::~Codec()
{
CodecImpl *i = (CodecImpl*)p;
delete i;
}
std::string
Codec::string(const std::string& s) const
{
CodecImpl *i = (CodecImpl*)p;
std::string r = i->decode_string(s);
return r;
}