2015-10-01 10:37:19 +02:00
|
|
|
// Copyright 2014 BitPay Inc.
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "univalue.h"
|
2016-06-10 15:19:51 +02:00
|
|
|
#include "univalue_utffilter.h"
|
2015-10-01 10:37:19 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
static bool json_isdigit(int ch)
|
|
|
|
{
|
|
|
|
return ((ch >= '0') && (ch <= '9'));
|
|
|
|
}
|
|
|
|
|
2015-10-01 10:37:19 +02:00
|
|
|
// convert hexadecimal string to unsigned integer
|
|
|
|
static const char *hatoui(const char *first, const char *last,
|
|
|
|
unsigned int& out)
|
|
|
|
{
|
|
|
|
unsigned int result = 0;
|
|
|
|
for (; first != last; ++first)
|
|
|
|
{
|
|
|
|
int digit;
|
2015-12-02 12:26:24 +01:00
|
|
|
if (json_isdigit(*first))
|
2015-10-01 10:37:19 +02:00
|
|
|
digit = *first - '0';
|
|
|
|
|
|
|
|
else if (*first >= 'a' && *first <= 'f')
|
|
|
|
digit = *first - 'a' + 10;
|
|
|
|
|
|
|
|
else if (*first >= 'A' && *first <= 'F')
|
|
|
|
digit = *first - 'A' + 10;
|
|
|
|
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
result = 16 * result + digit;
|
|
|
|
}
|
|
|
|
out = result;
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
|
|
|
|
const char *raw)
|
|
|
|
{
|
|
|
|
tokenVal.clear();
|
|
|
|
consumed = 0;
|
|
|
|
|
|
|
|
const char *rawStart = raw;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
while ((*raw) && (json_isspace(*raw))) // skip whitespace
|
2015-10-01 10:37:19 +02:00
|
|
|
raw++;
|
|
|
|
|
|
|
|
switch (*raw) {
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
return JTOK_NONE;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
raw++;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_OBJ_OPEN;
|
|
|
|
case '}':
|
|
|
|
raw++;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_OBJ_CLOSE;
|
|
|
|
case '[':
|
|
|
|
raw++;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_ARR_OPEN;
|
|
|
|
case ']':
|
|
|
|
raw++;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_ARR_CLOSE;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
raw++;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_COLON;
|
|
|
|
case ',':
|
|
|
|
raw++;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_COMMA;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
case 't':
|
|
|
|
case 'f':
|
|
|
|
if (!strncmp(raw, "null", 4)) {
|
|
|
|
raw += 4;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_KW_NULL;
|
|
|
|
} else if (!strncmp(raw, "true", 4)) {
|
|
|
|
raw += 4;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_KW_TRUE;
|
|
|
|
} else if (!strncmp(raw, "false", 5)) {
|
|
|
|
raw += 5;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_KW_FALSE;
|
|
|
|
} else
|
|
|
|
return JTOK_ERR;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9': {
|
|
|
|
// part 1: int
|
|
|
|
string numStr;
|
|
|
|
|
|
|
|
const char *first = raw;
|
|
|
|
|
|
|
|
const char *firstDigit = first;
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!json_isdigit(*firstDigit))
|
2015-10-01 10:37:19 +02:00
|
|
|
firstDigit++;
|
2015-12-02 12:26:24 +01:00
|
|
|
if ((*firstDigit == '0') && json_isdigit(firstDigit[1]))
|
2015-10-01 10:37:19 +02:00
|
|
|
return JTOK_ERR;
|
|
|
|
|
|
|
|
numStr += *raw; // copy first char
|
|
|
|
raw++;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
if ((*first == '-') && (!json_isdigit(*raw)))
|
2015-10-01 10:37:19 +02:00
|
|
|
return JTOK_ERR;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
while ((*raw) && json_isdigit(*raw)) { // copy digits
|
2015-10-01 10:37:19 +02:00
|
|
|
numStr += *raw;
|
|
|
|
raw++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// part 2: frac
|
|
|
|
if (*raw == '.') {
|
|
|
|
numStr += *raw; // copy .
|
|
|
|
raw++;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!json_isdigit(*raw))
|
2015-10-01 10:37:19 +02:00
|
|
|
return JTOK_ERR;
|
2015-12-02 12:26:24 +01:00
|
|
|
while ((*raw) && json_isdigit(*raw)) { // copy digits
|
2015-10-01 10:37:19 +02:00
|
|
|
numStr += *raw;
|
|
|
|
raw++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// part 3: exp
|
|
|
|
if (*raw == 'e' || *raw == 'E') {
|
|
|
|
numStr += *raw; // copy E
|
|
|
|
raw++;
|
|
|
|
|
|
|
|
if (*raw == '-' || *raw == '+') { // copy +/-
|
|
|
|
numStr += *raw;
|
|
|
|
raw++;
|
|
|
|
}
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!json_isdigit(*raw))
|
2015-10-01 10:37:19 +02:00
|
|
|
return JTOK_ERR;
|
2015-12-02 12:26:24 +01:00
|
|
|
while ((*raw) && json_isdigit(*raw)) { // copy digits
|
2015-10-01 10:37:19 +02:00
|
|
|
numStr += *raw;
|
|
|
|
raw++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenVal = numStr;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_NUMBER;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '"': {
|
|
|
|
raw++; // skip "
|
|
|
|
|
|
|
|
string valStr;
|
2016-06-10 15:19:51 +02:00
|
|
|
JSONUTF8StringFilter writer(valStr);
|
2015-10-01 10:37:19 +02:00
|
|
|
|
|
|
|
while (*raw) {
|
2016-06-10 15:19:51 +02:00
|
|
|
if ((unsigned char)*raw < 0x20)
|
2015-10-01 10:37:19 +02:00
|
|
|
return JTOK_ERR;
|
|
|
|
|
|
|
|
else if (*raw == '\\') {
|
|
|
|
raw++; // skip backslash
|
|
|
|
|
|
|
|
switch (*raw) {
|
2016-06-10 15:19:51 +02:00
|
|
|
case '"': writer.push_back('\"'); break;
|
|
|
|
case '\\': writer.push_back('\\'); break;
|
|
|
|
case '/': writer.push_back('/'); break;
|
|
|
|
case 'b': writer.push_back('\b'); break;
|
|
|
|
case 'f': writer.push_back('\f'); break;
|
|
|
|
case 'n': writer.push_back('\n'); break;
|
|
|
|
case 'r': writer.push_back('\r'); break;
|
|
|
|
case 't': writer.push_back('\t'); break;
|
2015-10-01 10:37:19 +02:00
|
|
|
|
|
|
|
case 'u': {
|
|
|
|
unsigned int codepoint;
|
|
|
|
if (hatoui(raw + 1, raw + 1 + 4, codepoint) !=
|
|
|
|
raw + 1 + 4)
|
|
|
|
return JTOK_ERR;
|
2016-06-10 15:19:51 +02:00
|
|
|
writer.push_back_u(codepoint);
|
2015-10-01 10:37:19 +02:00
|
|
|
raw += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return JTOK_ERR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
raw++; // skip esc'd char
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (*raw == '"') {
|
|
|
|
raw++; // skip "
|
|
|
|
break; // stop scanning
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2016-06-10 15:19:51 +02:00
|
|
|
writer.push_back(*raw);
|
2015-10-01 10:37:19 +02:00
|
|
|
raw++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 15:19:51 +02:00
|
|
|
if (!writer.finalize())
|
|
|
|
return JTOK_ERR;
|
2015-10-01 10:37:19 +02:00
|
|
|
tokenVal = valStr;
|
|
|
|
consumed = (raw - rawStart);
|
|
|
|
return JTOK_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return JTOK_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
enum expect_bits {
|
|
|
|
EXP_OBJ_NAME = (1U << 0),
|
|
|
|
EXP_COLON = (1U << 1),
|
|
|
|
EXP_ARR_VALUE = (1U << 2),
|
|
|
|
EXP_VALUE = (1U << 3),
|
|
|
|
EXP_NOT_VALUE = (1U << 4),
|
|
|
|
};
|
|
|
|
|
|
|
|
#define expect(bit) (expectMask & (EXP_##bit))
|
|
|
|
#define setExpect(bit) (expectMask |= EXP_##bit)
|
|
|
|
#define clearExpect(bit) (expectMask &= ~EXP_##bit)
|
|
|
|
|
2015-10-01 10:37:19 +02:00
|
|
|
bool UniValue::read(const char *raw)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
uint32_t expectMask = 0;
|
2015-10-01 10:37:19 +02:00
|
|
|
vector<UniValue*> stack;
|
|
|
|
|
|
|
|
string tokenVal;
|
|
|
|
unsigned int consumed;
|
|
|
|
enum jtokentype tok = JTOK_NONE;
|
|
|
|
enum jtokentype last_tok = JTOK_NONE;
|
|
|
|
do {
|
|
|
|
last_tok = tok;
|
|
|
|
|
|
|
|
tok = getJsonToken(tokenVal, consumed, raw);
|
|
|
|
if (tok == JTOK_NONE || tok == JTOK_ERR)
|
|
|
|
return false;
|
|
|
|
raw += consumed;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
bool isValueOpen = jsonTokenIsValue(tok) ||
|
|
|
|
tok == JTOK_OBJ_OPEN || tok == JTOK_ARR_OPEN;
|
|
|
|
|
|
|
|
if (expect(VALUE)) {
|
|
|
|
if (!isValueOpen)
|
|
|
|
return false;
|
|
|
|
clearExpect(VALUE);
|
|
|
|
|
|
|
|
} else if (expect(ARR_VALUE)) {
|
|
|
|
bool isArrValue = isValueOpen || (tok == JTOK_ARR_CLOSE);
|
|
|
|
if (!isArrValue)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
clearExpect(ARR_VALUE);
|
|
|
|
|
|
|
|
} else if (expect(OBJ_NAME)) {
|
|
|
|
bool isObjName = (tok == JTOK_OBJ_CLOSE || tok == JTOK_STRING);
|
|
|
|
if (!isObjName)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (expect(COLON)) {
|
|
|
|
if (tok != JTOK_COLON)
|
|
|
|
return false;
|
|
|
|
clearExpect(COLON);
|
|
|
|
|
|
|
|
} else if (!expect(COLON) && (tok == JTOK_COLON)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expect(NOT_VALUE)) {
|
|
|
|
if (isValueOpen)
|
|
|
|
return false;
|
|
|
|
clearExpect(NOT_VALUE);
|
|
|
|
}
|
|
|
|
|
2015-10-01 10:37:19 +02:00
|
|
|
switch (tok) {
|
|
|
|
|
|
|
|
case JTOK_OBJ_OPEN:
|
|
|
|
case JTOK_ARR_OPEN: {
|
|
|
|
VType utyp = (tok == JTOK_OBJ_OPEN ? VOBJ : VARR);
|
|
|
|
if (!stack.size()) {
|
|
|
|
if (utyp == VOBJ)
|
|
|
|
setObject();
|
|
|
|
else
|
|
|
|
setArray();
|
|
|
|
stack.push_back(this);
|
|
|
|
} else {
|
|
|
|
UniValue tmpVal(utyp);
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
top->values.push_back(tmpVal);
|
|
|
|
|
|
|
|
UniValue *newTop = &(top->values.back());
|
|
|
|
stack.push_back(newTop);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (utyp == VOBJ)
|
2015-12-02 12:26:24 +01:00
|
|
|
setExpect(OBJ_NAME);
|
|
|
|
else
|
|
|
|
setExpect(ARR_VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JTOK_OBJ_CLOSE:
|
|
|
|
case JTOK_ARR_CLOSE: {
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!stack.size() || (last_tok == JTOK_COMMA))
|
2015-10-01 10:37:19 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
VType utyp = (tok == JTOK_OBJ_CLOSE ? VOBJ : VARR);
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
if (utyp != top->getType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
stack.pop_back();
|
2015-12-02 12:26:24 +01:00
|
|
|
clearExpect(OBJ_NAME);
|
|
|
|
setExpect(NOT_VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JTOK_COLON: {
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!stack.size())
|
2015-10-01 10:37:19 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
if (top->getType() != VOBJ)
|
|
|
|
return false;
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
setExpect(VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JTOK_COMMA: {
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!stack.size() ||
|
2015-10-01 10:37:19 +02:00
|
|
|
(last_tok == JTOK_COMMA) || (last_tok == JTOK_ARR_OPEN))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
if (top->getType() == VOBJ)
|
2015-12-02 12:26:24 +01:00
|
|
|
setExpect(OBJ_NAME);
|
|
|
|
else
|
|
|
|
setExpect(ARR_VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JTOK_KW_NULL:
|
|
|
|
case JTOK_KW_TRUE:
|
|
|
|
case JTOK_KW_FALSE: {
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!stack.size())
|
2015-10-01 10:37:19 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
UniValue tmpVal;
|
|
|
|
switch (tok) {
|
|
|
|
case JTOK_KW_NULL:
|
|
|
|
// do nothing more
|
|
|
|
break;
|
|
|
|
case JTOK_KW_TRUE:
|
|
|
|
tmpVal.setBool(true);
|
|
|
|
break;
|
|
|
|
case JTOK_KW_FALSE:
|
|
|
|
tmpVal.setBool(false);
|
|
|
|
break;
|
|
|
|
default: /* impossible */ break;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
top->values.push_back(tmpVal);
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
setExpect(NOT_VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JTOK_NUMBER: {
|
2015-12-02 12:26:24 +01:00
|
|
|
if (!stack.size())
|
2015-10-01 10:37:19 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
UniValue tmpVal(VNUM, tokenVal);
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
top->values.push_back(tmpVal);
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
setExpect(NOT_VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JTOK_STRING: {
|
|
|
|
if (!stack.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UniValue *top = stack.back();
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
if (expect(OBJ_NAME)) {
|
2015-10-01 10:37:19 +02:00
|
|
|
top->keys.push_back(tokenVal);
|
2015-12-02 12:26:24 +01:00
|
|
|
clearExpect(OBJ_NAME);
|
|
|
|
setExpect(COLON);
|
2015-10-01 10:37:19 +02:00
|
|
|
} else {
|
|
|
|
UniValue tmpVal(VSTR, tokenVal);
|
|
|
|
top->values.push_back(tmpVal);
|
|
|
|
}
|
|
|
|
|
2015-12-02 12:26:24 +01:00
|
|
|
setExpect(NOT_VALUE);
|
2015-10-01 10:37:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (!stack.empty ());
|
|
|
|
|
|
|
|
/* Check that nothing follows the initial construct (parsed above). */
|
|
|
|
tok = getJsonToken(tokenVal, consumed, raw);
|
|
|
|
if (tok != JTOK_NONE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|