-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencoding.cpp
More file actions
413 lines (356 loc) · 13.6 KB
/
Copy pathencoding.cpp
File metadata and controls
413 lines (356 loc) · 13.6 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "encoding.hpp"
#include <algorithm>
#include <cstdlib>
#include <string_view>
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
#endif
namespace acecode {
namespace {
std::string strip_utf8_bom(std::string text) {
if (text.size() >= 3 &&
static_cast<unsigned char>(text[0]) == 0xEF &&
static_cast<unsigned char>(text[1]) == 0xBB &&
static_cast<unsigned char>(text[2]) == 0xBF) {
text.erase(0, 3);
}
return text;
}
void append_utf8(std::string& out, unsigned int cp) {
if (cp <= 0x7F) {
out.push_back(static_cast<char>(cp));
} else if (cp <= 0x7FF) {
out.push_back(static_cast<char>(0xC0 | (cp >> 6)));
out.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
} else if (cp <= 0xFFFF) {
out.push_back(static_cast<char>(0xE0 | (cp >> 12)));
out.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
out.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
} else {
out.push_back(static_cast<char>(0xF0 | (cp >> 18)));
out.push_back(static_cast<char>(0x80 | ((cp >> 12) & 0x3F)));
out.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
out.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
}
}
unsigned int read_u16(std::string_view bytes, size_t pos, bool little_endian) {
unsigned char a = static_cast<unsigned char>(bytes[pos]);
unsigned char b = static_cast<unsigned char>(bytes[pos + 1]);
return little_endian
? (static_cast<unsigned int>(a) | (static_cast<unsigned int>(b) << 8))
: (static_cast<unsigned int>(b) | (static_cast<unsigned int>(a) << 8));
}
std::string utf16_to_utf8(std::string_view bytes, bool little_endian, size_t offset) {
std::string out;
out.reserve(bytes.size() / 2);
for (size_t i = offset; i + 1 < bytes.size(); i += 2) {
unsigned int u = read_u16(bytes, i, little_endian);
if (u == 0) continue;
if (u >= 0xD800 && u <= 0xDBFF && i + 3 < bytes.size()) {
unsigned int low = read_u16(bytes, i + 2, little_endian);
if (low >= 0xDC00 && low <= 0xDFFF) {
unsigned int cp = 0x10000 + (((u - 0xD800) << 10) | (low - 0xDC00));
append_utf8(out, cp);
i += 2;
continue;
}
}
if (u >= 0xD800 && u <= 0xDFFF) {
append_utf8(out, '?');
} else {
append_utf8(out, u);
}
}
return out;
}
bool looks_like_utf16le(std::string_view src) {
const size_t sample = std::min<size_t>(src.size(), 512);
if (sample < 8) return false;
size_t odd_nuls = 0;
size_t even_nuls = 0;
for (size_t i = 0; i < sample; ++i) {
if (src[i] != '\0') continue;
if ((i % 2) == 0) ++even_nuls;
else ++odd_nuls;
}
return odd_nuls > sample / 8 && even_nuls < odd_nuls / 4;
}
bool looks_like_utf16be(std::string_view src) {
const size_t sample = std::min<size_t>(src.size(), 512);
if (sample < 8) return false;
size_t odd_nuls = 0;
size_t even_nuls = 0;
for (size_t i = 0; i < sample; ++i) {
if (src[i] != '\0') continue;
if ((i % 2) == 0) ++even_nuls;
else ++odd_nuls;
}
return even_nuls > sample / 8 && odd_nuls < even_nuls / 4;
}
} // namespace
#ifdef _WIN32
std::string codepage_to_utf8(const std::string& src, unsigned int codepage) {
if (src.empty()) return src;
// First convert to wide string (UTF-16)
int wide_len = MultiByteToWideChar(codepage, 0, src.c_str(), static_cast<int>(src.size()), nullptr, 0);
if (wide_len <= 0) return src;
std::wstring wide(static_cast<size_t>(wide_len), L'\0');
MultiByteToWideChar(codepage, 0, src.c_str(), static_cast<int>(src.size()), wide.data(), wide_len);
// Then convert wide string to UTF-8
int utf8_len = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), wide_len, nullptr, 0, nullptr, nullptr);
if (utf8_len <= 0) return src;
std::string utf8(static_cast<size_t>(utf8_len), '\0');
WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), wide_len, utf8.data(), utf8_len, nullptr, nullptr);
return utf8;
}
namespace {
std::wstring multibyte_to_wide(const std::string& src,
unsigned int codepage,
unsigned long flags = 0) {
if (src.empty()) return {};
int wide_len = MultiByteToWideChar(codepage, flags, src.data(),
static_cast<int>(src.size()),
nullptr, 0);
if (wide_len <= 0) return {};
std::wstring wide(static_cast<size_t>(wide_len), L'\0');
MultiByteToWideChar(codepage, flags, src.data(), static_cast<int>(src.size()),
wide.data(), wide_len);
return wide;
}
} // namespace
std::wstring utf8_to_wide(const std::string& src) {
if (src.empty()) return {};
// Most ACECode persisted paths are UTF-8. Some older Windows paths came
// from narrow filesystem APIs and are in the active codepage, so keep an
// ACP fallback to avoid stranding existing metadata.
std::wstring wide = multibyte_to_wide(src, CP_UTF8, MB_ERR_INVALID_CHARS);
if (!wide.empty()) return wide;
return multibyte_to_wide(src, CP_ACP);
}
std::string wide_to_utf8(const std::wstring& wide) {
if (wide.empty()) return {};
int utf8_len = WideCharToMultiByte(CP_UTF8, 0, wide.data(),
static_cast<int>(wide.size()),
nullptr, 0, nullptr, nullptr);
if (utf8_len <= 0) return {};
std::string utf8(static_cast<size_t>(utf8_len), '\0');
WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()),
utf8.data(), utf8_len, nullptr, nullptr);
return utf8;
}
#endif
bool getenv_utf8(const char* name, std::string& out) {
out.clear();
if (!name || !*name) return false;
#ifdef _WIN32
std::wstring wname = utf8_to_wide(name);
if (wname.empty()) return false;
SetLastError(ERROR_SUCCESS);
DWORD needed = GetEnvironmentVariableW(wname.c_str(), nullptr, 0);
if (needed == 0) {
return GetLastError() != ERROR_ENVVAR_NOT_FOUND;
}
std::wstring value(static_cast<size_t>(needed), L'\0');
DWORD written = GetEnvironmentVariableW(wname.c_str(), value.data(), needed);
if (written == 0 && GetLastError() != ERROR_SUCCESS) return false;
value.resize(static_cast<size_t>(written));
out = wide_to_utf8(value);
return true;
#else
const char* value = std::getenv(name);
if (!value) return false;
out = value;
return true;
#endif
}
std::string getenv_utf8(const char* name) {
std::string out;
return getenv_utf8(name, out) ? out : std::string{};
}
namespace {
// Length of the UTF-8 sequence introduced by lead byte c, or 0 if c is not a
// valid lead byte (i.e. it is a continuation byte or 0xF8-0xFF).
int utf8_seq_len(unsigned char c) {
if (c <= 0x7F) return 1;
if ((c & 0xE0) == 0xC0) return 2;
if ((c & 0xF0) == 0xE0) return 3;
if ((c & 0xF8) == 0xF0) return 4;
return 0;
}
// Compute the largest prefix length of `buf` that can be emitted now without
// cutting a multibyte character in half. `buf` MUST start on a character
// boundary (the IncrementalTextDecoder maintains this invariant).
//
// Prefers a UTF-8 interpretation: if the buffer is valid UTF-8 up to a clean
// incomplete trailing sequence, that boundary is returned. Otherwise, on
// Windows with a non-UTF-8 console codepage, the buffer is treated as DBCS
// (e.g. GBK/CP936) and scanned pair-by-pair from the boundary so a split
// lead/trail pair is held back as a unit.
size_t decoder_emit_boundary(const std::string& buf, unsigned int codepage) {
const size_t n = buf.size();
if (n == 0) return 0;
const unsigned char* b = reinterpret_cast<const unsigned char*>(buf.data());
// --- UTF-8 forward scan ---
bool utf8_ok = true;
size_t i = 0;
while (i < n) {
int len = utf8_seq_len(b[i]);
if (len == 0) { utf8_ok = false; break; } // stray continuation / invalid lead
if (i + static_cast<size_t>(len) > n) {
// Valid UTF-8 so far, with a clean incomplete trailing sequence.
return i;
}
bool ok = true;
for (int j = 1; j < len; ++j) {
if ((b[i + j] & 0xC0) != 0x80) { ok = false; break; }
}
if (!ok) { utf8_ok = false; break; }
i += static_cast<size_t>(len);
}
if (utf8_ok) return n; // entire buffer is well-formed UTF-8
#ifdef _WIN32
// --- DBCS (codepage) forward scan ---
// Only meaningful for double-byte codepages; for single-byte / UTF-8
// codepages every byte is independent so the whole buffer is emittable.
if (codepage != CP_UTF8) {
size_t k = 0;
while (k < n) {
if (b[k] < 0x80) { ++k; continue; } // ASCII single byte
// DBCS lead byte: needs a trailing byte.
if (k + 1 >= n) return k; // incomplete trailing lead → hold
k += 2;
}
return n;
}
#else
(void)codepage;
#endif
// Non-Windows or UTF-8 codepage with invalid bytes: emit everything and let
// the lossy decode below replace the offending bytes. (POSIX subprocess
// output is virtually always UTF-8.)
return n;
}
// Lossily decode a byte run that starts and ends on a character boundary into
// valid UTF-8. Never throws, never returns invalid UTF-8.
std::string decoder_decode_safe(const std::string& safe, unsigned int codepage) {
if (safe.empty()) return {};
if (is_valid_utf8(safe)) return safe;
#ifdef _WIN32
if (codepage != CP_UTF8) {
std::string converted = codepage_to_utf8(safe, codepage);
if (is_valid_utf8(converted)) return converted;
}
#else
(void)codepage;
#endif
// ensure_utf8 does ACP conversion + '?' replacement as a final fallback.
return ensure_utf8(safe);
}
} // namespace
IncrementalTextDecoder::IncrementalTextDecoder() {
#ifdef _WIN32
codepage_ = GetConsoleOutputCP();
if (codepage_ == 0) codepage_ = GetACP();
#else
codepage_ = 0;
#endif
}
IncrementalTextDecoder::IncrementalTextDecoder(unsigned int codepage)
: codepage_(codepage) {}
void IncrementalTextDecoder::reset() {
pending_.clear();
bom_checked_ = false;
}
std::string IncrementalTextDecoder::push(const char* data, size_t len) {
if (data && len) pending_.append(data, len);
if (pending_.empty()) return {};
// Strip a leading UTF-8 BOM once, but only after we have ≥3 bytes so a BOM
// split across the first two chunks is not mistaken for content.
if (!bom_checked_) {
if (pending_.size() >= 3) {
if (static_cast<unsigned char>(pending_[0]) == 0xEF &&
static_cast<unsigned char>(pending_[1]) == 0xBB &&
static_cast<unsigned char>(pending_[2]) == 0xBF) {
pending_.erase(0, 3);
}
bom_checked_ = true;
} else if (!(static_cast<unsigned char>(pending_[0]) == 0xEF &&
(pending_.size() < 2 ||
static_cast<unsigned char>(pending_[1]) == 0xBB))) {
// Cannot be the start of a BOM; stop waiting.
bom_checked_ = true;
}
if (!bom_checked_) return {}; // still possibly a partial BOM
}
size_t boundary = decoder_emit_boundary(pending_, codepage_);
if (boundary == 0) return {};
std::string safe = pending_.substr(0, boundary);
pending_.erase(0, boundary);
return decoder_decode_safe(safe, codepage_);
}
std::string IncrementalTextDecoder::flush() {
if (pending_.empty()) return {};
std::string rest;
rest.swap(pending_);
bom_checked_ = false;
return decoder_decode_safe(rest, codepage_);
}
std::string ensure_utf8(const std::string& src) {
if (src.empty()) return {};
if (src.size() >= 2) {
const auto b0 = static_cast<unsigned char>(src[0]);
const auto b1 = static_cast<unsigned char>(src[1]);
if (b0 == 0xFF && b1 == 0xFE) {
return utf16_to_utf8(src, true, 2);
}
if (b0 == 0xFE && b1 == 0xFF) {
return utf16_to_utf8(src, false, 2);
}
}
if (looks_like_utf16le(src)) {
return utf16_to_utf8(src, true, 0);
}
if (looks_like_utf16be(src)) {
return utf16_to_utf8(src, false, 0);
}
if (is_valid_utf8(src)) return strip_utf8_bom(src);
#ifdef _WIN32
// Try converting from the system's active codepage (e.g., GBK/CP936)
std::string converted = codepage_to_utf8(src);
if (is_valid_utf8(converted)) return strip_utf8_bom(converted);
#endif
// Fallback: strip invalid bytes
std::string result;
result.reserve(src.size());
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(src.data());
size_t len = src.size();
for (size_t i = 0; i < len; ) {
unsigned char c = bytes[i];
int seq_len = 0;
if (c <= 0x7F) { seq_len = 1; }
else if ((c & 0xE0) == 0xC0) { seq_len = 2; }
else if ((c & 0xF0) == 0xE0) { seq_len = 3; }
else if ((c & 0xF8) == 0xF0) { seq_len = 4; }
else { result += '?'; i++; continue; }
if (i + seq_len > len) { result += '?'; i++; continue; }
bool valid = true;
for (int j = 1; j < seq_len; j++) {
if ((bytes[i + j] & 0xC0) != 0x80) { valid = false; break; }
}
if (valid) {
result.append(src, i, seq_len);
i += seq_len;
} else {
result += '?';
i++;
}
}
return strip_utf8_bom(result);
}
} // namespace acecode