forked from cmu-sei/pharos-demangle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.cpp
490 lines (445 loc) · 11 KB
/
json.cpp
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Pharos Demangler
//
// Copyright 2017-2020 Carnegie Mellon University. All Rights Reserved.
//
// NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING
// INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON
// UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR
// IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF
// FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS
// OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
// MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT,
// TRADEMARK, OR COPYRIGHT INFRINGEMENT.
//
// Released under a BSD-style license, please see license.txt or contact
// permission@sei.cmu.edu for full terms.
//
// [DISTRIBUTION STATEMENT A] This material has been approved for public
// release and unlimited distribution. Please see Copyright notice for
// non-US Government use and distribution.
//
// DM17-0949
#include "json.hpp"
#include <vector> // std::vector
#include <map> // std::map
#include <utility> // std::move
#include <cctype> // std::iscntrl
#include <ios> // std::hex
#include <iomanip> // std::setw
#include <iterator> // std::ostreambuf_iterator
#include <algorithm> // std::fill_n
namespace json {
#if __cplusplus < 201402L
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#else
using std::make_unique;
#endif
NodeRef Simple::apply(Builder const & builder)
{
switch(type) {
case INT:
return builder.simple(i);
case UINT:
return builder.simple(u);
case DOUBLE:
return builder.simple(d);
case BOOL:
return builder.simple(b);
case NULLP:
return builder.null();
case STRING:
return builder.simple(*s);
case CSTRING:
return builder.simple(c);
case STRINGRR:
return builder.simple(std::move(r));
}
return builder.null();
}
namespace {
// Return whether this is a valid utf-8 string. If it is is, return 0. If not, return -1. If
// it is valid, but truncated, return the number of bytes that need to be truncated to make the
// string valid.
int is_valid_utf8(std::string const & s) {
std::string::size_type count = 0;
int trailing_chars = 0;
int tcc = 0;
int nonzero = 0;
uint32_t val = 0;
for (unsigned char c : s) {
if (c & 0x80) {
if (trailing_chars) {
if ((c & 0x40) || (nonzero && !(c & nonzero))) {
return -1;
}
val = (val << 6) | (c & 0x3f);
nonzero = 0;
++tcc;
if (trailing_chars == tcc) {
if (val > 0x10ffff) {
return -1;
}
trailing_chars = 0; tcc = 0;
}
} else if (!(c & 0x40)) {
return -1;
} else if ((c & 0xe0) == 0xc0) {
if (!(c & 0x1e)) {
return -1;
}
val = c & 0x1f;
nonzero = 0;
trailing_chars = 1; tcc = 0; ++count;
} else if ((c & 0xf0) == 0xe0) {
val = c & 0x0f;
nonzero = val ? 0 : 0x20;
trailing_chars = 2; tcc = 0; ++count;
} else if ((c & 0xf8) == 0xf0) {
val = c & 0x07;
nonzero = val ? 0 : 0x30;
trailing_chars = 3; tcc = 0; ++count;
} else {
return -1;
}
} else if (trailing_chars) {
return -1;
} else {
val = c;
}
}
if (trailing_chars) {
return count > 1 ? tcc + 1 : -1;
}
return 0;
}
std::ostream & output_string(std::ostream & stream, std::string const & s)
{
stream << '"';
auto ut = is_valid_utf8(s);
auto e = s.size();
if (ut >= 0) {
e -= ut;
}
for (std::string::size_type i = 0; i < e; ++i) {
unsigned char c = s[i];
const char *v;
switch(c) {
case '"':
v = "\\\""; break;
case '\\':
v = "\\\\"; break;
case '\b':
v = "\\b"; break;
case '\f':
v = "\\f"; break;
case '\n':
v = "\\n"; break;
case '\r':
v = "\\r"; break;
case '\t':
v = "\\t"; break;
default:
if (std::iscntrl(c) || ((ut < 0) && c & 0x80)) {
auto flags = stream.flags();
auto fill = stream.fill('0');
stream << "\\u00" << std::setw(2) << std::hex << unsigned(c);
stream.flags(flags);
stream.fill(fill);
continue;
}
stream.put(c);
continue;
}
stream << v;
}
return stream << '"';
}
class Writer : public Visitor {
private:
std::ostream & stream;
unsigned indent;
unsigned current_indent;
bool empty;
bool kv = false;
void simple_indent() {
if (!kv) {
do_indent();
}
}
template <typename T>
bool simple_write(T v) {
simple_indent();
stream << v;
return true;
}
void inc_indent() {
current_indent += indent;
}
void dec_indent() {
current_indent -= indent;
}
void do_indent() {
std::fill_n(
std::ostreambuf_iterator<std::ostream::char_type, std::ostream::traits_type>(stream),
current_indent, ' ');
}
void newline() {
if (indent) {
stream << '\n';
}
}
void do_comma() {
if (empty) {
empty = false;
} else {
stream << ',';
}
newline();
}
public:
Writer(std::ostream & _stream, unsigned _indent = 0, unsigned _initial_indent = 0) :
stream(_stream), indent(_indent), current_indent(_initial_indent) {}
bool data_number(double n) override {
return simple_write(n);
}
bool data_number(json::Simple::integer n) override {
return simple_write(n);
}
bool data_number(json::Simple::uinteger n) override {
return simple_write(n);
}
bool data_bool(bool b) override {
return simple_write(b ? "true" : "false");
}
bool data_null() override {
return simple_write("null");
}
bool data_string(std::string const & s) override {
simple_indent();
output_string(stream, s);
return true;
}
bool begin_array() override {
simple_indent();
stream << '[';
empty = true;
kv = false;
inc_indent();
return true;
}
bool end_array() override {
dec_indent();
if (!empty) {
newline();
do_indent();
}
stream << ']';
return true;
}
bool data_value(Node const & n) override {
if (!kv) {
do_comma();
}
auto rval = n.visit(*this);
kv = false;
empty = false;
return rval;
}
bool begin_object() override {
simple_indent();
stream << '{';
empty = true;
inc_indent();
return true;
}
bool end_object() override {
dec_indent();
if (!empty) {
newline();
do_indent();
}
stream << '}';
return true;
}
bool data_key(std::string const & k) override {
do_comma();
do_indent();
kv = true;
output_string(stream, k) << ": ";
return true;
}
};
} // unnamed namespace
namespace simple {
class Node : public virtual json::Node
{
static BuilderRef builder_;
public:
json::Builder & builder() const override {
return *builder_;
}
};
template <typename T>
class Simple : public Node
{
protected:
T val;
public:
Simple(T const & v) : val(v) {}
Simple(T && v) : val(std::move(v)) {}
};
template <typename T>
class Number : public Simple<T>
{
public:
using Simple<T>::Simple;
bool visit(Visitor & v) const override {
return v.data_number(this->val);
}
};
class Bool : public Simple<bool> {
public:
using Simple<bool>::Simple;
bool visit(Visitor & v) const override {
return v.data_bool(val);
}
};
class String : public Simple<std::string> {
public:
using Simple<std::string>::Simple;
bool visit(Visitor & v) const override {
return v.data_string(val);
}
};
class Null : public Node
{
public:
bool visit(Visitor & v) const override {
return v.data_null();
}
};
class Array : public Node, public json::Array
{
std::vector<NodeRef> vec;
public:
using Node::builder;
void add(NodeRef o) override {
vec.push_back(std::move(o));
}
void add(json::Simple && o) override;
bool visit(Visitor & v) const override {
if (!v.begin_array()) {
return false;
}
for (auto & node : vec) {
if (!v.data_value(*node)) {
return false;
}
}
return v.end_array();
}
};
class Object : public Node, public json::Object {
std::map<std::string, NodeRef> map;
public:
using Node::builder;
void add(std::string const & str, NodeRef o) override {
map.emplace(str, std::move(o));
}
void add(std::string && str, NodeRef o) override {
map.emplace(std::move(str), std::move(o));
}
void add(std::string const & str, json::Simple && v) override;
void add(std::string && str, json::Simple && v) override;
bool visit(Visitor & v) const override {
if (!v.begin_object()) {
return false;
}
for (auto & pair : map) {
if (!v.data_key(pair.first)) {
return false;
}
if (!v.data_value(*pair.second)) {
return false;
}
}
return v.end_object();
}
};
class Builder : public json::Builder
{
public:
using json::Builder::simple;
NodeRef simple(json::Simple::integer i) const override {
return json::make_unique<Number<json::Simple::integer>>(i);
}
NodeRef simple(json::Simple::uinteger i) const override {
return json::make_unique<Number<json::Simple::uinteger>>(i);
}
NodeRef simple(double d) const override {
return json::make_unique<Number<double>>(d);
}
NodeRef simple(bool b) const override {
return json::make_unique<Bool>(b);
}
NodeRef null() const override {
return json::make_unique<Null>();
}
NodeRef simple(std::string && s) const override {
return json::make_unique<String>(std::move(s));
}
NodeRef simple(std::string const & s) const override {
return json::make_unique<String>(s);
}
ArrayRef array() const override {
return json::make_unique<Array>();
}
ObjectRef object() const override {
return json::make_unique<Object>();
}
};
BuilderRef Node::builder_ = json::simple_builder();
void Array::add(json::Simple && v) {
add(builder().simple(std::move(v)));
}
void Object::add(std::string const & str, json::Simple && v) {
add(str, builder().simple(std::move(v)));
}
void Object::add(std::string && str, json::Simple && v) {
add(std::move(str), builder().simple(std::move(v)));
}
} // namespace simple
namespace {
const int indent_idx = std::ios_base::xalloc();
const int initial_indent_idx = std::ios_base::xalloc();
}
std::ostream & operator<<(std::ostream & stream, Node const & n)
{
Writer w(stream, stream.iword(indent_idx), stream.iword(initial_indent_idx));
n.visit(w);
return stream;
}
std::ostream & operator<<(std::ostream & stream, NodeRef const & n)
{
return stream << *n;
}
std::ostream & operator<<(std::ostream & stream, pretty const & p)
{
stream.iword(indent_idx) = p.indent;
stream.iword(initial_indent_idx) = p.initial_indent;
return stream;
}
BuilderRef simple_builder()
{
return json::make_unique<simple::Builder>();
}
} // namespace json
/* Local Variables: */
/* mode: c++ */
/* fill-column: 95 */
/* comment-column: 0 */
/* End: */