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
|
#include "yql_ast_escaping.h"
#include <util/charset/wide.h>
#include <util/stream/output.h>
#include <util/string/hex.h>
namespace NYql {
static char HexDigit(char c)
{
return (c < 10 ? '0' + c : 'A' + (c - 10));
}
static void EscapedPrintChar(ui8 c, IOutputStream* out)
{
switch (c) {
case '\\': out->Write("\\\\", 2); break;
case '"' : out->Write("\\\"", 2); break;
case '\t': out->Write("\\t", 2); break;
case '\n': out->Write("\\n", 2); break;
case '\r': out->Write("\\r", 2); break;
case '\b': out->Write("\\b", 2); break;
case '\f': out->Write("\\f", 2); break;
case '\a': out->Write("\\a", 2); break;
case '\v': out->Write("\\v", 2); break;
default: {
if (isprint(c)) out->Write(static_cast<char>(c));
else {
char buf[4] = { "\\x" };
buf[2] = HexDigit((c & 0xf0) >> 4);
buf[3] = HexDigit((c & 0x0f));
out->Write(buf, 4);
}
}
}
}
static void EscapedPrintUnicode(wchar32 rune, IOutputStream* out)
{
static const int MAX_ESCAPE_LEN = 10;
if (rune < 0x80) {
EscapedPrintChar(static_cast<ui8>(rune & 0xff), out);
} else {
int i = 0;
char buf[MAX_ESCAPE_LEN];
if (rune < 0x10000) {
buf[i++] = '\\';
buf[i++] = 'u';
} else {
buf[i++] = '\\';
buf[i++] = 'U';
buf[i++] = HexDigit((rune & 0xf0000000) >> 28);
buf[i++] = HexDigit((rune & 0x0f000000) >> 24);
buf[i++] = HexDigit((rune & 0x00f00000) >> 20);
buf[i++] = HexDigit((rune & 0x000f0000) >> 16);
}
buf[i++] = HexDigit((rune & 0xf000) >> 12);
buf[i++] = HexDigit((rune & 0x0f00) >> 8);
buf[i++] = HexDigit((rune & 0x00f0) >> 4);
buf[i++] = HexDigit((rune & 0x000f));
out->Write(buf, i);
}
}
static bool TryParseOctal(const char*& p, const char* e, int maxlen, wchar32* value)
{
while (maxlen-- && p != e) {
if (*value > 255) return false;
char ch = *p++;
if (ch >= '0' && ch <= '7') {
*value = *value * 8 + (ch - '0');
continue;
}
break;
}
return (maxlen == -1);
}
static bool TryParseHex(const char*& p, const char* e, int maxlen, wchar32* value)
{
while (maxlen-- > 0 && p != e) {
char ch = *p++;
if (ch >= '0' && ch <= '9') {
*value = *value * 16 + (ch - '0');
continue;
}
// to lower case
ch |= 0x20;
if (ch >= 'a' && ch <= 'f') {
*value = *value * 16 + (ch - 'a') + 10;
continue;
}
break;
}
return (maxlen == -1);
}
static bool IsValidUtf8Rune(wchar32 value) {
return value <= 0x10ffff && (value < 0xd800 || value > 0xdfff);
}
TStringBuf UnescapeResultToString(EUnescapeResult result)
{
switch (result) {
case EUnescapeResult::OK:
return "OK";
case EUnescapeResult::INVALID_ESCAPE_SEQUENCE:
return "Expected escape sequence";
case EUnescapeResult::INVALID_BINARY:
return "Invalid binary value";
case EUnescapeResult::INVALID_OCTAL:
return "Invalid octal value";
case EUnescapeResult::INVALID_HEXADECIMAL:
return "Invalid hexadecimal value";
case EUnescapeResult::INVALID_UNICODE:
return "Invalid unicode value";
case EUnescapeResult::INVALID_END:
return "Unexpected end of atom";
}
return "Unknown unescape error";
}
void EscapeArbitraryAtom(TStringBuf atom, char quoteChar, IOutputStream* out)
{
out->Write(quoteChar);
const ui8 *p = reinterpret_cast<const ui8*>(atom.begin()),
*e = reinterpret_cast<const ui8*>(atom.end());
while (p != e) {
wchar32 rune = 0;
size_t rune_len = 0;
if (SafeReadUTF8Char(rune, rune_len, p, e) == RECODE_RESULT::RECODE_OK && IsValidUtf8Rune(rune)) {
EscapedPrintUnicode(rune, out);
p += rune_len;
} else {
EscapedPrintChar(*p++, out);
}
}
out->Write(quoteChar);
}
EUnescapeResult UnescapeArbitraryAtom(
TStringBuf atom, char endChar, IOutputStream* out, size_t* readBytes)
{
const char *p = atom.begin(),
*e = atom.end();
while (p != e) {
char current = *p++;
// C-style escape sequences
if (current == '\\') {
if (p == e) {
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_ESCAPE_SEQUENCE;
}
char next = *p++;
switch (next) {
case 't': current = '\t'; break;
case 'n': current = '\n'; break;
case 'r': current = '\r'; break;
case 'b': current = '\b'; break;
case 'f': current = '\f'; break;
case 'a': current = '\a'; break;
case 'v': current = '\v'; break;
case '0': case '1': case '2': case '3': {
wchar32 value = (next - '0');
if (!TryParseOctal(p, e, 2, &value)) {
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_OCTAL;
}
current = value & 0xff;
break;
}
case 'x': {
wchar32 value = 0;
if (!TryParseHex(p, e, 2, &value)) {
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_HEXADECIMAL;
}
current = value & 0xff;
break;
}
case 'u':
case 'U': {
wchar32 value = 0;
int len = (next == 'u' ? 4 : 8);
if (!TryParseHex(p, e, len, &value) || !IsValidUtf8Rune(value)) {
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_UNICODE;
}
size_t written = 0;
char buf[4];
WideToUTF8(&value, 1, buf, written);
out->Write(buf, written);
continue;
}
default: {
current = next;
}
}
} else if (endChar == '`') {
if (current == '`') {
if (p == e) {
*readBytes = p - atom.begin();
return EUnescapeResult::OK;
} else {
if (*p != '`') {
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_ESCAPE_SEQUENCE;
} else {
p++;
}
}
}
} else if (current == endChar) {
*readBytes = p - atom.begin();
return EUnescapeResult::OK;
}
out->Write(current);
}
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_END;
}
void EscapeBinaryAtom(TStringBuf atom, char quoteChar, IOutputStream* out)
{
char prefix[] = { 'x', quoteChar };
out->Write(prefix, 2);
out->Write(HexEncode(atom.data(), atom.size()));
out->Write(quoteChar);
}
EUnescapeResult UnescapeBinaryAtom(
TStringBuf atom, char endChar, IOutputStream* out, size_t* readBytes)
{
const char *p = atom.begin(),
*e = atom.end();
while (p != e) {
char current = *p;
if (current == endChar) {
*readBytes = p - atom.begin();
return EUnescapeResult::OK;
}
wchar32 byte = 0;
if (!TryParseHex(p, e, 2, &byte)) {
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_BINARY;
}
out->Write(byte & 0xff);
}
*readBytes = p - atom.begin();
return EUnescapeResult::INVALID_END;
}
} // namspace NYql
|