Fabcoin Core  0.16.2
P2P Digital Currency
univalue_read.cpp
Go to the documentation of this file.
1 // Copyright 2014 BitPay Inc.
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <string.h>
6 #include <vector>
7 #include <stdio.h>
8 #include "univalue.h"
9 #include "univalue_utffilter.h"
10 
11 using namespace std;
12 
13 static bool json_isdigit(int ch)
14 {
15  return ((ch >= '0') && (ch <= '9'));
16 }
17 
18 // convert hexadecimal string to unsigned integer
19 static const char *hatoui(const char *first, const char *last,
20  unsigned int& out)
21 {
22  unsigned int result = 0;
23  for (; first != last; ++first)
24  {
25  int digit;
26  if (json_isdigit(*first))
27  digit = *first - '0';
28 
29  else if (*first >= 'a' && *first <= 'f')
30  digit = *first - 'a' + 10;
31 
32  else if (*first >= 'A' && *first <= 'F')
33  digit = *first - 'A' + 10;
34 
35  else
36  break;
37 
38  result = 16 * result + digit;
39  }
40  out = result;
41 
42  return first;
43 }
44 
45 enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
46  const char *raw, const char *end)
47 {
48  tokenVal.clear();
49  consumed = 0;
50 
51  const char *rawStart = raw;
52 
53  while (raw < end && (json_isspace(*raw))) // skip whitespace
54  raw++;
55 
56  if (raw >= end)
57  return JTOK_NONE;
58 
59  switch (*raw) {
60 
61  case '{':
62  raw++;
63  consumed = (raw - rawStart);
64  return JTOK_OBJ_OPEN;
65  case '}':
66  raw++;
67  consumed = (raw - rawStart);
68  return JTOK_OBJ_CLOSE;
69  case '[':
70  raw++;
71  consumed = (raw - rawStart);
72  return JTOK_ARR_OPEN;
73  case ']':
74  raw++;
75  consumed = (raw - rawStart);
76  return JTOK_ARR_CLOSE;
77 
78  case ':':
79  raw++;
80  consumed = (raw - rawStart);
81  return JTOK_COLON;
82  case ',':
83  raw++;
84  consumed = (raw - rawStart);
85  return JTOK_COMMA;
86 
87  case 'n':
88  case 't':
89  case 'f':
90  if (!strncmp(raw, "null", 4)) {
91  raw += 4;
92  consumed = (raw - rawStart);
93  return JTOK_KW_NULL;
94  } else if (!strncmp(raw, "true", 4)) {
95  raw += 4;
96  consumed = (raw - rawStart);
97  return JTOK_KW_TRUE;
98  } else if (!strncmp(raw, "false", 5)) {
99  raw += 5;
100  consumed = (raw - rawStart);
101  return JTOK_KW_FALSE;
102  } else
103  return JTOK_ERR;
104 
105  case '-':
106  case '0':
107  case '1':
108  case '2':
109  case '3':
110  case '4':
111  case '5':
112  case '6':
113  case '7':
114  case '8':
115  case '9': {
116  // part 1: int
117  string numStr;
118 
119  const char *first = raw;
120 
121  const char *firstDigit = first;
122  if (!json_isdigit(*firstDigit))
123  firstDigit++;
124  if ((*firstDigit == '0') && json_isdigit(firstDigit[1]))
125  return JTOK_ERR;
126 
127  numStr += *raw; // copy first char
128  raw++;
129 
130  if ((*first == '-') && (raw < end) && (!json_isdigit(*raw)))
131  return JTOK_ERR;
132 
133  while (raw < end && json_isdigit(*raw)) { // copy digits
134  numStr += *raw;
135  raw++;
136  }
137 
138  // part 2: frac
139  if (raw < end && *raw == '.') {
140  numStr += *raw; // copy .
141  raw++;
142 
143  if (raw >= end || !json_isdigit(*raw))
144  return JTOK_ERR;
145  while (raw < end && json_isdigit(*raw)) { // copy digits
146  numStr += *raw;
147  raw++;
148  }
149  }
150 
151  // part 3: exp
152  if (raw < end && (*raw == 'e' || *raw == 'E')) {
153  numStr += *raw; // copy E
154  raw++;
155 
156  if (raw < end && (*raw == '-' || *raw == '+')) { // copy +/-
157  numStr += *raw;
158  raw++;
159  }
160 
161  if (raw >= end || !json_isdigit(*raw))
162  return JTOK_ERR;
163  while (raw < end && json_isdigit(*raw)) { // copy digits
164  numStr += *raw;
165  raw++;
166  }
167  }
168 
169  tokenVal = numStr;
170  consumed = (raw - rawStart);
171  return JTOK_NUMBER;
172  }
173 
174  case '"': {
175  raw++; // skip "
176 
177  string valStr;
178  JSONUTF8StringFilter writer(valStr);
179 
180  while (true) {
181  if (raw >= end || (unsigned char)*raw < 0x20)
182  return JTOK_ERR;
183 
184  else if (*raw == '\\') {
185  raw++; // skip backslash
186 
187  if (raw >= end)
188  return JTOK_ERR;
189 
190  switch (*raw) {
191  case '"': writer.push_back('\"'); break;
192  case '\\': writer.push_back('\\'); break;
193  case '/': writer.push_back('/'); break;
194  case 'b': writer.push_back('\b'); break;
195  case 'f': writer.push_back('\f'); break;
196  case 'n': writer.push_back('\n'); break;
197  case 'r': writer.push_back('\r'); break;
198  case 't': writer.push_back('\t'); break;
199 
200  case 'u': {
201  unsigned int codepoint;
202  if (raw + 1 + 4 >= end ||
203  hatoui(raw + 1, raw + 1 + 4, codepoint) !=
204  raw + 1 + 4)
205  return JTOK_ERR;
206  writer.push_back_u(codepoint);
207  raw += 4;
208  break;
209  }
210  default:
211  return JTOK_ERR;
212 
213  }
214 
215  raw++; // skip esc'd char
216  }
217 
218  else if (*raw == '"') {
219  raw++; // skip "
220  break; // stop scanning
221  }
222 
223  else {
224  writer.push_back(*raw);
225  raw++;
226  }
227  }
228 
229  if (!writer.finalize())
230  return JTOK_ERR;
231  tokenVal = valStr;
232  consumed = (raw - rawStart);
233  return JTOK_STRING;
234  }
235 
236  default:
237  return JTOK_ERR;
238  }
239 }
240 
242  EXP_OBJ_NAME = (1U << 0),
243  EXP_COLON = (1U << 1),
244  EXP_ARR_VALUE = (1U << 2),
245  EXP_VALUE = (1U << 3),
246  EXP_NOT_VALUE = (1U << 4),
247 };
248 
249 #define expect(bit) (expectMask & (EXP_##bit))
250 #define setExpect(bit) (expectMask |= EXP_##bit)
251 #define clearExpect(bit) (expectMask &= ~EXP_##bit)
252 
253 bool UniValue::read(const char *raw, size_t size)
254 {
255  clear();
256 
257  uint32_t expectMask = 0;
258  vector<UniValue*> stack;
259 
260  string tokenVal;
261  unsigned int consumed;
262  enum jtokentype tok = JTOK_NONE;
263  enum jtokentype last_tok = JTOK_NONE;
264  const char* end = raw + size;
265  do {
266  last_tok = tok;
267 
268  tok = getJsonToken(tokenVal, consumed, raw, end);
269  if (tok == JTOK_NONE || tok == JTOK_ERR)
270  return false;
271  raw += consumed;
272 
273  bool isValueOpen = jsonTokenIsValue(tok) ||
274  tok == JTOK_OBJ_OPEN || tok == JTOK_ARR_OPEN;
275 
276  if (expect(VALUE)) {
277  if (!isValueOpen)
278  return false;
279  clearExpect(VALUE);
280 
281  } else if (expect(ARR_VALUE)) {
282  bool isArrValue = isValueOpen || (tok == JTOK_ARR_CLOSE);
283  if (!isArrValue)
284  return false;
285 
286  clearExpect(ARR_VALUE);
287 
288  } else if (expect(OBJ_NAME)) {
289  bool isObjName = (tok == JTOK_OBJ_CLOSE || tok == JTOK_STRING);
290  if (!isObjName)
291  return false;
292 
293  } else if (expect(COLON)) {
294  if (tok != JTOK_COLON)
295  return false;
296  clearExpect(COLON);
297 
298  } else if (!expect(COLON) && (tok == JTOK_COLON)) {
299  return false;
300  }
301 
302  if (expect(NOT_VALUE)) {
303  if (isValueOpen)
304  return false;
305  clearExpect(NOT_VALUE);
306  }
307 
308  switch (tok) {
309 
310  case JTOK_OBJ_OPEN:
311  case JTOK_ARR_OPEN: {
312  VType utyp = (tok == JTOK_OBJ_OPEN ? VOBJ : VARR);
313  if (!stack.size()) {
314  if (utyp == VOBJ)
315  setObject();
316  else
317  setArray();
318  stack.push_back(this);
319  } else {
320  UniValue tmpVal(utyp);
321  UniValue *top = stack.back();
322  top->values.push_back(tmpVal);
323 
324  UniValue *newTop = &(top->values.back());
325  stack.push_back(newTop);
326  }
327 
328  if (utyp == VOBJ)
329  setExpect(OBJ_NAME);
330  else
331  setExpect(ARR_VALUE);
332  break;
333  }
334 
335  case JTOK_OBJ_CLOSE:
336  case JTOK_ARR_CLOSE: {
337  if (!stack.size() || (last_tok == JTOK_COMMA))
338  return false;
339 
340  VType utyp = (tok == JTOK_OBJ_CLOSE ? VOBJ : VARR);
341  UniValue *top = stack.back();
342  if (utyp != top->getType())
343  return false;
344 
345  stack.pop_back();
346  clearExpect(OBJ_NAME);
347  setExpect(NOT_VALUE);
348  break;
349  }
350 
351  case JTOK_COLON: {
352  if (!stack.size())
353  return false;
354 
355  UniValue *top = stack.back();
356  if (top->getType() != VOBJ)
357  return false;
358 
359  setExpect(VALUE);
360  break;
361  }
362 
363  case JTOK_COMMA: {
364  if (!stack.size() ||
365  (last_tok == JTOK_COMMA) || (last_tok == JTOK_ARR_OPEN))
366  return false;
367 
368  UniValue *top = stack.back();
369  if (top->getType() == VOBJ)
370  setExpect(OBJ_NAME);
371  else
372  setExpect(ARR_VALUE);
373  break;
374  }
375 
376  case JTOK_KW_NULL:
377  case JTOK_KW_TRUE:
378  case JTOK_KW_FALSE: {
379  UniValue tmpVal;
380  switch (tok) {
381  case JTOK_KW_NULL:
382  // do nothing more
383  break;
384  case JTOK_KW_TRUE:
385  tmpVal.setBool(true);
386  break;
387  case JTOK_KW_FALSE:
388  tmpVal.setBool(false);
389  break;
390  default: /* impossible */ break;
391  }
392 
393  if (!stack.size()) {
394  *this = tmpVal;
395  break;
396  }
397 
398  UniValue *top = stack.back();
399  top->values.push_back(tmpVal);
400 
401  setExpect(NOT_VALUE);
402  break;
403  }
404 
405  case JTOK_NUMBER: {
406  UniValue tmpVal(VNUM, tokenVal);
407  if (!stack.size()) {
408  *this = tmpVal;
409  break;
410  }
411 
412  UniValue *top = stack.back();
413  top->values.push_back(tmpVal);
414 
415  setExpect(NOT_VALUE);
416  break;
417  }
418 
419  case JTOK_STRING: {
420  if (expect(OBJ_NAME)) {
421  UniValue *top = stack.back();
422  top->keys.push_back(tokenVal);
423  clearExpect(OBJ_NAME);
424  setExpect(COLON);
425  } else {
426  UniValue tmpVal(VSTR, tokenVal);
427  if (!stack.size()) {
428  *this = tmpVal;
429  break;
430  }
431  UniValue *top = stack.back();
432  top->values.push_back(tmpVal);
433  }
434 
435  setExpect(NOT_VALUE);
436  break;
437  }
438 
439  default:
440  return false;
441  }
442  } while (!stack.empty ());
443 
444  /* Check that nothing follows the initial construct (parsed above). */
445  tok = getJsonToken(tokenVal, consumed, raw, end);
446  if (tok != JTOK_NONE)
447  return false;
448 
449  return true;
450 }
451 
expect_bits
bool read(const char *raw, size_t len)
#define setExpect(bit)
#define expect(bit)
std::hash for asio::adress
Definition: Common.h:323
std::vector< UniValue > values
Definition: univalue.h:156
void push_back(unsigned char ch)
enum jtokentype getJsonToken(string &tokenVal, unsigned int &consumed, const char *raw, const char *end)
enum VType getType() const
Definition: univalue.h:66
Filter that generates and validates UTF-8, as well as collates UTF-16 surrogate pairs as specified in...
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
jtokentype
Definition: univalue.h:246
uint8_t const size_t const size
Definition: sha3.h:20
bool setBool(bool val)
Definition: univalue.cpp:31
std::vector< std::string > keys
Definition: univalue.h:155
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
Definition: picosha2.h:73
void push_back_u(unsigned int codepoint_)
#define clearExpect(bit)