Repository navigation
Expand file tree
/
Copy pathvcf_utils.cpp
More file actions
237 lines (222 loc) · 8.75 KB
/
Copy pathvcf_utils.cpp
File metadata and controls
237 lines (222 loc) · 8.75 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
#include "vcf_utils.hpp"
#include <windows.h>
std::wstring Trim(const std::wstring& s) {
size_t a = 0, b = s.size();
while (a < b && iswspace(s[a])) ++a;
while (b > a && iswspace(s[b - 1])) --b;
return s.substr(a, b - a);
}
std::wstring ToUpperASCII(const std::wstring& s) {
std::wstring t = s;
std::transform(t.begin(), t.end(), t.begin(), [](wchar_t c) {
return (c >= L'a' && c <= L'z') ? (wchar_t)(c - 32) : c;
});
return t;
}
std::vector<std::wstring> SplitLines(const std::wstring& block) {
std::vector<std::wstring> lines; lines.reserve(64);
size_t i = 0, n = block.size();
while (i < n) {
size_t j = block.find_first_of(L"\r\n", i);
if (j == std::wstring::npos) { lines.push_back(block.substr(i)); break; }
lines.push_back(block.substr(i, j - i));
// CR+CRLF ("\r\r\n") from old iOS — one separator
while (j < n && (block[j] == L'\r' || block[j] == L'\n')) ++j;
i = j;
}
return lines;
}
std::vector<std::wstring> UnfoldVCard_Folded(const std::vector<std::wstring>& lines) {
std::vector<std::wstring> out;
out.reserve(lines.size());
for (const auto& Lraw : lines) {
std::wstring L = Lraw;
while (!L.empty() && (L.back() == L'\r' || L.back() == L'\n')) L.pop_back();
if (L.empty()) continue;
if (!out.empty() && (L[0] == L' ' || L[0] == L'\t'))
out.back() += L.substr(1);
else
out.push_back(std::move(L));
}
return out;
}
std::wstring LowerInvariant(const std::wstring& s) {
if (s.empty()) return s;
int need = LCMapStringW(LOCALE_INVARIANT, LCMAP_LOWERCASE, s.c_str(), -1, nullptr, 0);
if (need <= 0) {
std::wstring t = s;
std::transform(t.begin(), t.end(), t.begin(), ::towlower);
return t;
}
std::wstring out; out.resize(need);
LCMapStringW(LOCALE_INVARIANT, LCMAP_LOWERCASE, s.c_str(), -1, &out[0], need);
if (!out.empty() && out.back() == L'\0') out.pop_back();
return out;
}
std::wstring unquote(const std::wstring& s) {
if (s.size() >= 2 && s.front() == L'"' && s.back() == L'"')
return s.substr(1, s.size() - 2);
return s;
}
std::wstring UnescapeVCard(const std::wstring& s) {
std::wstring r; r.reserve(s.size());
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] == L'\\' && i + 1 < s.size()) {
wchar_t c = s[i + 1];
if (c == L'n' || c == L'N') { r.push_back(L'\n'); ++i; continue; }
if (c == L't' || c == L'T') { r.push_back(L'\t'); ++i; continue; }
if (c == L',' || c == L';' || c == L'\\' || c == L':') { r.push_back(c); ++i; continue; }
}
r.push_back(s[i]);
}
return r;
}
std::vector<uint8_t> VcfBase64Decode(const std::wstring& wsrc) {
auto val = [](unsigned char c) -> int {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
};
// ~4/3 expansion; reject before allocating the alphabet buffer
if (wsrc.size() > (kMaxPhotoDecodedBytes / 3) * 4 + 64)
return {};
const size_t alphCap = (kMaxPhotoDecodedBytes / 3) * 4 + 64;
std::string alph;
alph.reserve(wsrc.size() < alphCap ? wsrc.size() : alphCap);
for (wchar_t wc : wsrc) {
if (wc == L'=') break;
if (wc == L'\r' || wc == L'\n' || wc == L' ' || wc == L'\t') continue;
if (wc > 127) continue;
if (val((unsigned char)wc) < 0) continue;
alph.push_back((char)wc);
}
while (alph.size() % 4 == 1 && !alph.empty()) alph.pop_back();
const size_t outCap = alph.size() / 4 * 3;
std::vector<uint8_t> out;
out.reserve(outCap < kMaxPhotoDecodedBytes ? outCap : kMaxPhotoDecodedBytes);
int v = 0, vb = -8;
for (unsigned char c : alph) {
int d = val(c);
if (d < 0) continue;
v = (v << 6) | d;
vb += 6;
if (vb >= 0) {
out.push_back((uint8_t)((v >> vb) & 0xFF));
vb -= 8;
if (out.size() > kMaxPhotoDecodedBytes) return {};
}
}
return out;
}
std::vector<uint8_t> VcfQuotedPrintableDecode(const std::wstring& wsrc, UINT codepage) {
std::string in; in.reserve(wsrc.size());
for (wchar_t wc : wsrc) {
if (wc <= 127) {
in.push_back((char)wc);
} else {
// Already-decoded text must go back to the charset's bytes, not always UTF-8.
char buf[8]{};
int n = WideCharToMultiByte(codepage, 0, &wc, 1, buf, (int)sizeof(buf), nullptr, nullptr);
for (int k = 0; k < n; ++k) in.push_back(buf[k]);
}
}
std::vector<uint8_t> out; out.reserve(in.size());
const size_t n = in.size();
for (size_t i = 0; i < n; ) {
unsigned char c = (unsigned char)in[i];
if (c == '=') {
if (i + 1 >= n) break;
if (in[i + 1] == '\r' && i + 2 < n && in[i + 2] == '\n') { i += 3; continue; }
if (in[i + 1] == '\n' || in[i + 1] == '\r') { i += 2; continue; }
if ((in[i + 1] == ' ' || in[i + 1] == '\t') && i + 2 < n && (in[i + 2] == '\r' || in[i + 2] == '\n')) {
i += (i + 3 < n && in[i + 2] == '\r' && in[i + 3] == '\n') ? 4 : 3;
continue;
}
auto hex = [](char x) -> int {
if (x >= '0' && x <= '9') return x - '0';
if (x >= 'A' && x <= 'F') return 10 + (x - 'A');
if (x >= 'a' && x <= 'f') return 10 + (x - 'a');
return -1;
};
if (i + 2 < n) {
int h1 = hex(in[i + 1]), h2 = hex(in[i + 2]);
if (h1 >= 0 && h2 >= 0) {
out.push_back((uint8_t)((h1 << 4) | h2));
i += 3;
continue;
}
}
out.push_back('=');
++i;
} else if (c == '\r' || c == '\n') {
if (out.empty() || out.back() != '\n') out.push_back('\n');
++i;
if (c == '\r' && i < n && in[i] == '\n') ++i;
} else {
out.push_back(c);
++i;
}
}
return out;
}
std::wstring VcfBytesToWide(const std::vector<uint8_t>& bytes, UINT codepage) {
if (bytes.empty()) return L"";
DWORD flags = (codepage == CP_UTF8) ? MB_ERR_INVALID_CHARS : 0;
int wlen = MultiByteToWideChar(codepage, flags, (LPCCH)bytes.data(), (int)bytes.size(), nullptr, 0);
if (wlen <= 0) {
// try strict UTF-8
wlen = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCCH)bytes.data(), (int)bytes.size(), nullptr, 0);
if (wlen > 0) {
codepage = CP_UTF8;
} else {
// try 1251
wlen = MultiByteToWideChar(1251, 0, (LPCCH)bytes.data(), (int)bytes.size(), nullptr, 0);
if (wlen > 0) {
codepage = 1251;
} else {
wlen = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)bytes.data(), (int)bytes.size(), nullptr, 0);
if (wlen <= 0) return L"";
codepage = CP_UTF8;
}
}
}
std::wstring w((size_t)wlen, L'\0');
MultiByteToWideChar(codepage, 0, (LPCCH)bytes.data(), (int)bytes.size(), &w[0], wlen);
return w;
}
UINT VcfCodepageFromCharset(const std::wstring& charset) {
std::wstring cs = ToUpperASCII(charset);
if (cs == L"UTF-8" || cs == L"UTF8") return CP_UTF8;
if (cs == L"UTF-16" || cs == L"UTF16" || cs == L"UTF-16LE") return 1200;
if (cs == L"WINDOWS-1251" || cs == L"CP1251" || cs == L"WIN-1251") return 1251;
if (cs == L"WINDOWS-1252" || cs == L"CP1252" || cs == L"WIN-1252") return 1252;
if (cs == L"WINDOWS-1250" || cs == L"CP1250" || cs == L"WIN-1250") return 1250;
if (cs == L"KOI8-R" || cs == L"KOI8R") return 20866;
if (cs == L"ISO-8859-5" || cs == L"ISO8859-5") return 28595;
if (cs == L"ISO-8859-1" || cs == L"ISO8859-1" || cs == L"LATIN1") return 28591;
if (cs == L"ISO-8859-2" || cs == L"ISO8859-2") return 28592;
return CP_UTF8;
}
static std::wstring stripQpArtifacts(std::wstring s) {
while (!s.empty()) {
wchar_t c = s.back();
if (c == L'=' || c == L' ' || c == L'\t' || c == L'\r' || c == L'\n') s.pop_back();
else break;
}
return s;
}
std::wstring VcfDecodeTextValue(const std::wstring& raw, bool isQP, const std::wstring& charset) {
// Does not unescape \, \n — call UnescapeVCard after if needed
if (!isQP) {
std::wstring t = raw;
while (!t.empty() && t.back() == L'=') t.pop_back();
return t;
}
UINT cp = VcfCodepageFromCharset(charset);
auto bytes = VcfQuotedPrintableDecode(raw, cp);
std::wstring w = VcfBytesToWide(bytes, cp);
return stripQpArtifacts(std::move(w));
}