1 // Copyright 2014 BitPay Inc.
2 // Copyright 2015 Bitcoin Core Developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <stdint.h>
7 #include <errno.h>
8 #include <iomanip>
9 #include <limits>
10 #include <sstream>
11 #include <stdexcept>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "univalue.h"
16 
17 namespace
18 {
ParsePrechecks(const std::string & str)19 static bool ParsePrechecks(const std::string& str)
20 {
21     if (str.empty()) // No empty string allowed
22         return false;
23     if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
24         return false;
25     if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
26         return false;
27     return true;
28 }
29 
ParseInt32(const std::string & str,int32_t * out)30 bool ParseInt32(const std::string& str, int32_t *out)
31 {
32     if (!ParsePrechecks(str))
33         return false;
34     char *endp = NULL;
35     errno = 0; // strtol will not set errno if valid
36     long int n = strtol(str.c_str(), &endp, 10);
37     if(out) *out = (int32_t)n;
38     // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow
39     // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
40     // platforms the size of these types may be different.
41     return endp && *endp == 0 && !errno &&
42         n >= std::numeric_limits<int32_t>::min() &&
43         n <= std::numeric_limits<int32_t>::max();
44 }
45 
ParseInt64(const std::string & str,int64_t * out)46 bool ParseInt64(const std::string& str, int64_t *out)
47 {
48     if (!ParsePrechecks(str))
49         return false;
50     char *endp = NULL;
51     errno = 0; // strtoll will not set errno if valid
52     long long int n = strtoll(str.c_str(), &endp, 10);
53     if(out) *out = (int64_t)n;
54     // Note that strtoll returns a *long long int*, so even if strtol doesn't report a over/underflow
55     // we still have to check that the returned value is within the range of an *int64_t*.
56     return endp && *endp == 0 && !errno &&
57         n >= std::numeric_limits<int64_t>::min() &&
58         n <= std::numeric_limits<int64_t>::max();
59 }
60 
ParseDouble(const std::string & str,double * out)61 bool ParseDouble(const std::string& str, double *out)
62 {
63     if (!ParsePrechecks(str))
64         return false;
65     if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
66         return false;
67     std::istringstream text(str);
68     text.imbue(std::locale::classic());
69     double result;
70     text >> result;
71     if(out) *out = result;
72     return text.eof() && !text.fail();
73 }
74 }
75 
76 using namespace std;
77 
78 const UniValue NullUniValue;
79 
clear()80 void UniValue::clear()
81 {
82     typ = VNULL;
83     val.clear();
84     keys.clear();
85     values.clear();
86 }
87 
setNull()88 bool UniValue::setNull()
89 {
90     clear();
91     return true;
92 }
93 
setBool(bool val_)94 bool UniValue::setBool(bool val_)
95 {
96     clear();
97     typ = VBOOL;
98     if (val_)
99         val = "1";
100     return true;
101 }
102 
validNumStr(const string & s)103 static bool validNumStr(const string& s)
104 {
105     string tokenVal;
106     unsigned int consumed;
107     enum jtokentype tt = getJsonToken(tokenVal, consumed, s.c_str());
108     return (tt == JTOK_NUMBER);
109 }
110 
setNumStr(const string & val_)111 bool UniValue::setNumStr(const string& val_)
112 {
113     if (!validNumStr(val_))
114         return false;
115 
116     clear();
117     typ = VNUM;
118     val = val_;
119     return true;
120 }
121 
setInt(uint64_t val)122 bool UniValue::setInt(uint64_t val)
123 {
124     string s;
125     ostringstream oss;
126 
127     oss << val;
128 
129     return setNumStr(oss.str());
130 }
131 
setInt(int64_t val)132 bool UniValue::setInt(int64_t val)
133 {
134     string s;
135     ostringstream oss;
136 
137     oss << val;
138 
139     return setNumStr(oss.str());
140 }
141 
setFloat(double val)142 bool UniValue::setFloat(double val)
143 {
144     string s;
145     ostringstream oss;
146 
147     oss << std::setprecision(16) << val;
148 
149     bool ret = setNumStr(oss.str());
150     typ = VNUM;
151     return ret;
152 }
153 
setStr(const string & val_)154 bool UniValue::setStr(const string& val_)
155 {
156     clear();
157     typ = VSTR;
158     val = val_;
159     return true;
160 }
161 
setArray()162 bool UniValue::setArray()
163 {
164     clear();
165     typ = VARR;
166     return true;
167 }
168 
setObject()169 bool UniValue::setObject()
170 {
171     clear();
172     typ = VOBJ;
173     return true;
174 }
175 
push_back(const UniValue & val)176 bool UniValue::push_back(const UniValue& val)
177 {
178     if (typ != VARR)
179         return false;
180 
181     values.push_back(val);
182     return true;
183 }
184 
push_backV(const std::vector<UniValue> & vec)185 bool UniValue::push_backV(const std::vector<UniValue>& vec)
186 {
187     if (typ != VARR)
188         return false;
189 
190     values.insert(values.end(), vec.begin(), vec.end());
191 
192     return true;
193 }
194 
pushKV(const std::string & key,const UniValue & val)195 bool UniValue::pushKV(const std::string& key, const UniValue& val)
196 {
197     if (typ != VOBJ)
198         return false;
199 
200     keys.push_back(key);
201     values.push_back(val);
202     return true;
203 }
204 
pushKVs(const UniValue & obj)205 bool UniValue::pushKVs(const UniValue& obj)
206 {
207     if (typ != VOBJ || obj.typ != VOBJ)
208         return false;
209 
210     for (unsigned int i = 0; i < obj.keys.size(); i++) {
211         keys.push_back(obj.keys[i]);
212         values.push_back(obj.values.at(i));
213     }
214 
215     return true;
216 }
217 
findKey(const std::string & key) const218 int UniValue::findKey(const std::string& key) const
219 {
220     for (unsigned int i = 0; i < keys.size(); i++) {
221         if (keys[i] == key)
222             return (int) i;
223     }
224 
225     return -1;
226 }
227 
checkObject(const std::map<std::string,UniValue::VType> & t)228 bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
229 {
230     for (std::map<std::string,UniValue::VType>::const_iterator it = t.begin();
231          it != t.end(); it++) {
232         int idx = findKey(it->first);
233         if (idx < 0)
234             return false;
235 
236         if (values.at(idx).getType() != it->second)
237             return false;
238     }
239 
240     return true;
241 }
242 
operator [](const std::string & key) const243 const UniValue& UniValue::operator[](const std::string& key) const
244 {
245     if (typ != VOBJ)
246         return NullUniValue;
247 
248     int index = findKey(key);
249     if (index < 0)
250         return NullUniValue;
251 
252     return values.at(index);
253 }
254 
operator [](unsigned int index) const255 const UniValue& UniValue::operator[](unsigned int index) const
256 {
257     if (typ != VOBJ && typ != VARR)
258         return NullUniValue;
259     if (index >= values.size())
260         return NullUniValue;
261 
262     return values.at(index);
263 }
264 
uvTypeName(UniValue::VType t)265 const char *uvTypeName(UniValue::VType t)
266 {
267     switch (t) {
268     case UniValue::VNULL: return "null";
269     case UniValue::VBOOL: return "bool";
270     case UniValue::VOBJ: return "object";
271     case UniValue::VARR: return "array";
272     case UniValue::VSTR: return "string";
273     case UniValue::VNUM: return "number";
274     }
275 
276     // not reached
277     return NULL;
278 }
279 
find_value(const UniValue & obj,const std::string & name)280 const UniValue& find_value(const UniValue& obj, const std::string& name)
281 {
282     for (unsigned int i = 0; i < obj.keys.size(); i++)
283         if (obj.keys[i] == name)
284             return obj.values.at(i);
285 
286     return NullUniValue;
287 }
288 
getKeys() const289 std::vector<std::string> UniValue::getKeys() const
290 {
291     if (typ != VOBJ)
292         throw std::runtime_error("JSON value is not an object as expected");
293     return keys;
294 }
295 
getValues() const296 std::vector<UniValue> UniValue::getValues() const
297 {
298     if (typ != VOBJ && typ != VARR)
299         throw std::runtime_error("JSON value is not an object or array as expected");
300     return values;
301 }
302 
get_bool() const303 bool UniValue::get_bool() const
304 {
305     if (typ != VBOOL)
306         throw std::runtime_error("JSON value is not a boolean as expected");
307     return getBool();
308 }
309 
get_str() const310 std::string UniValue::get_str() const
311 {
312     if (typ != VSTR)
313         throw std::runtime_error("JSON value is not a string as expected");
314     return getValStr();
315 }
316 
get_int() const317 int UniValue::get_int() const
318 {
319     if (typ != VNUM)
320         throw std::runtime_error("JSON value is not an integer as expected");
321     int32_t retval;
322     if (!ParseInt32(getValStr(), &retval))
323         throw std::runtime_error("JSON integer out of range");
324     return retval;
325 }
326 
get_int64() const327 int64_t UniValue::get_int64() const
328 {
329     if (typ != VNUM)
330         throw std::runtime_error("JSON value is not an integer as expected");
331     int64_t retval;
332     if (!ParseInt64(getValStr(), &retval))
333         throw std::runtime_error("JSON integer out of range");
334     return retval;
335 }
336 
get_real() const337 double UniValue::get_real() const
338 {
339     if (typ != VNUM)
340         throw std::runtime_error("JSON value is not a number as expected");
341     double retval;
342     if (!ParseDouble(getValStr(), &retval))
343         throw std::runtime_error("JSON double out of range");
344     return retval;
345 }
346 
get_obj() const347 const UniValue& UniValue::get_obj() const
348 {
349     if (typ != VOBJ)
350         throw std::runtime_error("JSON value is not an object as expected");
351     return *this;
352 }
353 
get_array() const354 const UniValue& UniValue::get_array() const
355 {
356     if (typ != VARR)
357         throw std::runtime_error("JSON value is not an array as expected");
358     return *this;
359 }
360 
361