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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "InspectorValues.h"
#if ENABLE(INSPECTOR)
namespace WebCore {
namespace {
static const int stackLimit = 1000;
enum Token {
OBJECT_BEGIN,
OBJECT_END,
ARRAY_BEGIN,
ARRAY_END,
STRING,
NUMBER,
BOOL_TRUE,
BOOL_FALSE,
NULL_TOKEN,
LIST_SEPARATOR,
OBJECT_PAIR_SEPARATOR,
INVALID_TOKEN,
};
const char* const nullString = "null";
const char* const trueString = "true";
const char* const falseString = "false";
bool parseConstToken(const UChar* start, const UChar* end, const UChar** tokenEnd, const char* token)
{
while (start < end && *token != '\0' && *start++ == *token++) { }
if (*token != '\0')
return false;
*tokenEnd = start;
return true;
}
bool readInt(const UChar* start, const UChar* end, const UChar** tokenEnd, bool canHaveLeadingZeros)
{
if (start == end)
return false;
bool haveLeadingZero = '0' == *start;
int length = 0;
while (start < end && '0' <= *start && *start <= '9') {
++start;
++length;
}
if (!length)
return false;
if (!canHaveLeadingZeros && length > 1 && haveLeadingZero)
return false;
*tokenEnd = start;
return true;
}
bool parseNumberToken(const UChar* start, const UChar* end, const UChar** tokenEnd)
{
// We just grab the number here. We validate the size in DecodeNumber.
// According to RFC4627, a valid number is: [minus] int [frac] [exp]
if (start == end)
return false;
UChar c = *start;
if ('-' == c)
++start;
if (!readInt(start, end, &start, false))
return false;
if (start == end) {
*tokenEnd = start;
return true;
}
// Optional fraction part
c = *start;
if ('.' == c) {
++start;
if (!readInt(start, end, &start, true))
return false;
if (start == end) {
*tokenEnd = start;
return true;
}
c = *start;
}
// Optional exponent part
if ('e' == c || 'E' == c) {
++start;
if (start == end)
return false;
c = *start;
if ('-' == c || '+' == c) {
++start;
if (start == end)
return false;
}
if (!readInt(start, end, &start, true))
return false;
}
*tokenEnd = start;
return true;
}
bool readHexDigits(const UChar* start, const UChar* end, const UChar** tokenEnd, int digits)
{
if (end - start < digits)
return false;
for (int i = 0; i < digits; ++i) {
UChar c = *start++;
if (!(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')))
return false;
}
*tokenEnd = start;
return true;
}
bool parseStringToken(const UChar* start, const UChar* end, const UChar** tokenEnd)
{
while (start < end) {
UChar c = *start++;
if ('\\' == c) {
c = *start++;
// Make sure the escaped char is valid.
switch (c) {
case 'x':
if (!readHexDigits(start, end, &start, 2))
return false;
break;
case 'u':
if (!readHexDigits(start, end, &start, 4))
return false;
break;
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'v':
case '"':
break;
default:
return false;
}
} else if ('"' == c) {
*tokenEnd = start;
return true;
}
}
return false;
}
Token parseToken(const UChar* start, const UChar* end, const UChar** tokenEnd)
{
if (start == end)
return INVALID_TOKEN;
switch (*start) {
case 'n':
if (parseConstToken(start, end, tokenEnd, nullString))
return NULL_TOKEN;
break;
case 't':
if (parseConstToken(start, end, tokenEnd, trueString))
return BOOL_TRUE;
break;
case 'f':
if (parseConstToken(start, end, tokenEnd, falseString))
return BOOL_FALSE;
break;
case '[':
*tokenEnd = start + 1;
return ARRAY_BEGIN;
case ']':
*tokenEnd = start + 1;
return ARRAY_END;
case ',':
*tokenEnd = start + 1;
return LIST_SEPARATOR;
case '{':
*tokenEnd = start + 1;
return OBJECT_BEGIN;
case '}':
*tokenEnd = start + 1;
return OBJECT_END;
case ':':
*tokenEnd = start + 1;
return OBJECT_PAIR_SEPARATOR;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
if (parseNumberToken(start, end, tokenEnd))
return NUMBER;
break;
case '"':
if (parseStringToken(start + 1, end, tokenEnd))
return STRING;
break;
}
return INVALID_TOKEN;
}
inline int hexToInt(UChar c)
{
if ('0' <= c && c <= '9')
return c - '0';
if ('A' <= c && c <= 'F')
return c - 'A' + 10;
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
ASSERT_NOT_REACHED();
return 0;
}
bool decodeString(const UChar* start, const UChar* end, Vector<UChar>* output)
{
while (start < end) {
UChar c = *start++;
if ('\\' != c) {
output->append(c);
continue;
}
c = *start++;
switch (c) {
case '"':
case '/':
case '\\':
break;
case 'b':
c = '\b';
break;
case 'f':
c = '\f';
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
case 'x':
c = (hexToInt(*start) << 4) +
hexToInt(*(start + 1));
start += 2;
break;
case 'u':
c = (hexToInt(*start) << 12) +
(hexToInt(*(start + 1)) << 8) +
(hexToInt(*(start + 2)) << 4) +
hexToInt(*(start + 3));
start += 4;
break;
default:
return false;
}
output->append(c);
}
return true;
}
bool decodeString(const UChar* start, const UChar* end, String* output)
{
if (start == end) {
*output = "";
return true;
}
if (start > end)
return false;
Vector<UChar> buffer;
buffer.reserveCapacity(end - start);
if (!decodeString(start, end, &buffer))
return false;
*output = String(buffer.data(), buffer.size());
return true;
}
PassRefPtr<InspectorValue> buildValue(const UChar* start, const UChar* end, const UChar** valueTokenEnd, int depth)
{
if (depth > stackLimit)
return 0;
RefPtr<InspectorValue> result;
const UChar* tokenEnd;
Token token = parseToken(start, end, &tokenEnd);
switch (token) {
case INVALID_TOKEN:
return 0;
case NULL_TOKEN:
result = InspectorValue::null();
break;
case BOOL_TRUE:
result = InspectorBasicValue::create(true);
break;
case BOOL_FALSE:
result = InspectorBasicValue::create(false);
break;
case NUMBER: {
bool ok;
double value = charactersToDouble(start, tokenEnd - start, &ok);
if (!ok)
return 0;
result = InspectorBasicValue::create(value);
break;
}
case STRING: {
String value;
bool ok = decodeString(start + 1, tokenEnd - 1, &value);
if (!ok)
return 0;
result = InspectorString::create(value);
break;
}
case ARRAY_BEGIN: {
RefPtr<InspectorArray> array = InspectorArray::create();
start = tokenEnd;
token = parseToken(start, end, &tokenEnd);
while (token != ARRAY_END) {
RefPtr<InspectorValue> arrayNode = buildValue(start, end, &tokenEnd, depth + 1);
if (!arrayNode)
return 0;
array->push(arrayNode);
// After a list value, we expect a comma or the end of the list.
start = tokenEnd;
token = parseToken(start, end, &tokenEnd);
if (token == LIST_SEPARATOR) {
start = tokenEnd;
token = parseToken(start, end, &tokenEnd);
if (token == ARRAY_END)
return 0;
} else if (token != ARRAY_END) {
// Unexpected value after list value. Bail out.
return 0;
}
}
if (token != ARRAY_END)
return 0;
result = array.release();
break;
}
case OBJECT_BEGIN: {
RefPtr<InspectorObject> object = InspectorObject::create();
start = tokenEnd;
token = parseToken(start, end, &tokenEnd);
while (token != OBJECT_END) {
if (token != STRING)
return 0;
String key;
if (!decodeString(start + 1, tokenEnd - 1, &key))
return 0;
start = tokenEnd;
token = parseToken(start, end, &tokenEnd);
if (token != OBJECT_PAIR_SEPARATOR)
return 0;
start = tokenEnd;
RefPtr<InspectorValue> value = buildValue(start, end, &tokenEnd, depth + 1);
if (!value)
return 0;
object->set(key, value);
start = tokenEnd;
// After a key/value pair, we expect a comma or the end of the
// object.
token = parseToken(start, end, &tokenEnd);
if (token == LIST_SEPARATOR) {
start = tokenEnd;
token = parseToken(start, end, &tokenEnd);
if (token == OBJECT_END)
return 0;
} else if (token != OBJECT_END) {
// Unexpected value after last object value. Bail out.
return 0;
}
}
if (token != OBJECT_END)
return 0;
result = object.release();
break;
}
default:
// We got a token that's not a value.
return 0;
}
*valueTokenEnd = tokenEnd;
return result.release();
}
inline bool escapeChar(UChar c, Vector<UChar>* dst)
{
switch (c) {
case '\b': dst->append("\\b", 2); break;
case '\f': dst->append("\\f", 2); break;
case '\n': dst->append("\\n", 2); break;
case '\r': dst->append("\\r", 2); break;
case '\t': dst->append("\\t", 2); break;
case '\\': dst->append("\\\\", 2); break;
case '"': dst->append("\\\"", 2); break;
default:
return false;
}
return true;
}
inline void doubleQuoteString(const String& str, Vector<UChar>* dst)
{
dst->append('"');
for (unsigned i = 0; i < str.length(); ++i) {
UChar c = str[i];
if (!escapeChar(c, dst)) {
if (c < 32 || c > 126 || c == '<' || c == '>') {
// 1. Escaping <, > to prevent script execution.
// 2. Technically, we could also pass through c > 126 as UTF8, but this
// is also optional. It would also be a pain to implement here.
unsigned int symbol = static_cast<unsigned int>(c);
String symbolCode = String::format("\\u%04X", symbol);
dst->append(symbolCode.characters(), symbolCode.length());
} else
dst->append(c);
}
}
dst->append('"');
}
} // anonymous namespace
bool InspectorValue::asBool(bool*) const
{
return false;
}
bool InspectorValue::asNumber(double*) const
{
return false;
}
bool InspectorValue::asNumber(long*) const
{
return false;
}
bool InspectorValue::asNumber(unsigned long*) const
{
return false;
}
bool InspectorValue::asString(String*) const
{
return false;
}
PassRefPtr<InspectorObject> InspectorValue::asObject()
{
return 0;
}
PassRefPtr<InspectorArray> InspectorValue::asArray()
{
return 0;
}
PassRefPtr<InspectorValue> InspectorValue::parseJSON(const String& json)
{
const UChar* start = json.characters();
const UChar* end = json.characters() + json.length();
const UChar *tokenEnd;
RefPtr<InspectorValue> value = buildValue(start, end, &tokenEnd, 0);
if (!value || tokenEnd != end)
return 0;
return value.release();
}
String InspectorValue::toJSONString() const
{
Vector<UChar> result;
result.reserveCapacity(512);
writeJSON(&result);
return String(result.data(), result.size());
}
void InspectorValue::writeJSON(Vector<UChar>* output) const
{
ASSERT(m_type == TypeNull);
output->append(nullString, 4);
}
bool InspectorBasicValue::asBool(bool* output) const
{
if (type() != TypeBoolean)
return false;
*output = m_boolValue;
return true;
}
bool InspectorBasicValue::asNumber(double* output) const
{
if (type() != TypeDouble)
return false;
*output = m_doubleValue;
return true;
}
bool InspectorBasicValue::asNumber(long* output) const
{
if (type() != TypeDouble)
return false;
*output = static_cast<long>(m_doubleValue);
return true;
}
bool InspectorBasicValue::asNumber(unsigned long* output) const
{
if (type() != TypeDouble)
return false;
*output = static_cast<unsigned long>(m_doubleValue);
return true;
}
void InspectorBasicValue::writeJSON(Vector<UChar>* output) const
{
ASSERT(type() == TypeBoolean || type() == TypeDouble);
if (type() == TypeBoolean) {
if (m_boolValue)
output->append(trueString, 4);
else
output->append(falseString, 5);
} else if (type() == TypeDouble) {
String value = String::format("%f", m_doubleValue);
value.replace(',', '.');
output->append(value.characters(), value.length());
}
}
bool InspectorString::asString(String* output) const
{
*output = m_stringValue;
return true;
}
void InspectorString::writeJSON(Vector<UChar>* output) const
{
ASSERT(type() == TypeString);
doubleQuoteString(m_stringValue, output);
}
PassRefPtr<InspectorObject> InspectorObject::asObject()
{
return this;
}
bool InspectorObject::getBool(const String& name, bool* output) const
{
RefPtr<InspectorValue> value = get(name);
if (!value)
return false;
return value->asBool(output);
}
bool InspectorObject::getNumber(const String& name, double* output) const
{
RefPtr<InspectorValue> value = get(name);
if (!value)
return false;
return value->asNumber(output);
}
bool InspectorObject::getString(const String& name, String* output) const
{
RefPtr<InspectorValue> value = get(name);
if (!value)
return false;
return value->asString(output);
}
PassRefPtr<InspectorObject> InspectorObject::getObject(const String& name) const
{
PassRefPtr<InspectorValue> value = get(name);
if (!value)
return 0;
return value->asObject();
}
PassRefPtr<InspectorArray> InspectorObject::getArray(const String& name) const
{
PassRefPtr<InspectorValue> value = get(name);
if (!value)
return 0;
return value->asArray();
}
PassRefPtr<InspectorValue> InspectorObject::get(const String& name) const
{
Dictionary::const_iterator it = m_data.find(name);
if (it == m_data.end())
return 0;
return it->second;
}
void InspectorObject::writeJSON(Vector<UChar>* output) const
{
output->append('{');
for (size_t i = 0; i < m_order.size(); ++i) {
Dictionary::const_iterator it = m_data.find(m_order[i]);
ASSERT(it != m_data.end());
if (i)
output->append(',');
doubleQuoteString(it->first, output);
output->append(':');
it->second->writeJSON(output);
}
output->append('}');
}
PassRefPtr<InspectorArray> InspectorArray::asArray()
{
return this;
}
void InspectorArray::writeJSON(Vector<UChar>* output) const
{
output->append('[');
for (Vector<RefPtr<InspectorValue> >::const_iterator it = m_data.begin(); it != m_data.end(); ++it) {
if (it != m_data.begin())
output->append(',');
(*it)->writeJSON(output);
}
output->append(']');
}
PassRefPtr<InspectorValue> InspectorArray::get(size_t index)
{
ASSERT(index < m_data.size());
return m_data[index];
}
} // namespace WebCore
#endif // ENABLE(INSPECTOR)
|