You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 48 kB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /*
  2. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  3. * Michael Clark <michael@metaparadigm.com>
  4. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the MIT license. See COPYING for details.
  8. *
  9. */
  10. #include "config.h"
  11. #include "strerror_override.h"
  12. #include <assert.h>
  13. #ifdef HAVE_LIMITS_H
  14. #include <limits.h>
  15. #endif
  16. #include <math.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "arraylist.h"
  22. #include "debug.h"
  23. #include "json_inttypes.h"
  24. #include "json_object.h"
  25. #include "json_object_private.h"
  26. #include "json_util.h"
  27. #include "linkhash.h"
  28. #include "math_compat.h"
  29. #include "printbuf.h"
  30. #include "snprintf_compat.h"
  31. #include "strdup_compat.h"
  32. /* Avoid ctype.h and locale overhead */
  33. #define is_plain_digit(c) ((c) >= '0' && (c) <= '9')
  34. #if SIZEOF_LONG_LONG != SIZEOF_INT64_T
  35. #error "The long long type isn't 64-bits"
  36. #endif
  37. #ifndef SSIZE_T_MAX
  38. #if SIZEOF_SSIZE_T == SIZEOF_INT
  39. #define SSIZE_T_MAX INT_MAX
  40. #elif SIZEOF_SSIZE_T == SIZEOF_LONG
  41. #define SSIZE_T_MAX LONG_MAX
  42. #elif SIZEOF_SSIZE_T == SIZEOF_LONG_LONG
  43. #define SSIZE_T_MAX LLONG_MAX
  44. #else
  45. #error Unable to determine size of ssize_t
  46. #endif
  47. #endif
  48. const char *json_hex_chars = "0123456789abcdefABCDEF";
  49. static void json_object_generic_delete(struct json_object *jso);
  50. #if defined(_MSC_VER) && (_MSC_VER <= 1800)
  51. /* VS2013 doesn't know about "inline" */
  52. #define inline __inline
  53. #elif defined(AIX_CC)
  54. #define inline
  55. #endif
  56. /*
  57. * Helper functions to more safely cast to a particular type of json_object
  58. */
  59. static inline struct json_object_object *JC_OBJECT(struct json_object *jso)
  60. {
  61. return (void *)jso;
  62. }
  63. static inline const struct json_object_object *JC_OBJECT_C(const struct json_object *jso)
  64. {
  65. return (const void *)jso;
  66. }
  67. static inline struct json_object_array *JC_ARRAY(struct json_object *jso)
  68. {
  69. return (void *)jso;
  70. }
  71. static inline const struct json_object_array *JC_ARRAY_C(const struct json_object *jso)
  72. {
  73. return (const void *)jso;
  74. }
  75. static inline struct json_object_boolean *JC_BOOL(struct json_object *jso)
  76. {
  77. return (void *)jso;
  78. }
  79. static inline const struct json_object_boolean *JC_BOOL_C(const struct json_object *jso)
  80. {
  81. return (const void *)jso;
  82. }
  83. static inline struct json_object_double *JC_DOUBLE(struct json_object *jso)
  84. {
  85. return (void *)jso;
  86. }
  87. static inline const struct json_object_double *JC_DOUBLE_C(const struct json_object *jso)
  88. {
  89. return (const void *)jso;
  90. }
  91. static inline struct json_object_int *JC_INT(struct json_object *jso)
  92. {
  93. return (void *)jso;
  94. }
  95. static inline const struct json_object_int *JC_INT_C(const struct json_object *jso)
  96. {
  97. return (const void *)jso;
  98. }
  99. static inline struct json_object_string *JC_STRING(struct json_object *jso)
  100. {
  101. return (void *)jso;
  102. }
  103. static inline const struct json_object_string *JC_STRING_C(const struct json_object *jso)
  104. {
  105. return (const void *)jso;
  106. }
  107. #define JC_CONCAT(a, b) a##b
  108. #define JC_CONCAT3(a, b, c) a##b##c
  109. #define JSON_OBJECT_NEW(jtype) \
  110. (struct JC_CONCAT(json_object_, jtype) *)json_object_new( \
  111. JC_CONCAT(json_type_, jtype), sizeof(struct JC_CONCAT(json_object_, jtype)), \
  112. &JC_CONCAT3(json_object_, jtype, _to_json_string))
  113. static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size,
  114. json_object_to_json_string_fn *to_json_string);
  115. static void json_object_object_delete(struct json_object *jso_base);
  116. static void json_object_string_delete(struct json_object *jso);
  117. static void json_object_array_delete(struct json_object *jso);
  118. static json_object_to_json_string_fn json_object_object_to_json_string;
  119. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  120. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  121. static json_object_to_json_string_fn json_object_int_to_json_string;
  122. static json_object_to_json_string_fn json_object_string_to_json_string;
  123. static json_object_to_json_string_fn json_object_array_to_json_string;
  124. static json_object_to_json_string_fn _json_object_userdata_to_json_string;
  125. #ifndef JSON_NORETURN
  126. #if defined(_MSC_VER)
  127. #define JSON_NORETURN __declspec(noreturn)
  128. #elif defined(__OS400__)
  129. #define JSON_NORETURN
  130. #else
  131. /* 'cold' attribute is for optimization, telling the computer this code
  132. * path is unlikely.
  133. */
  134. #define JSON_NORETURN __attribute__((noreturn, cold))
  135. #endif
  136. #endif
  137. /**
  138. * Abort and optionally print a message on standard error.
  139. * This should be used rather than assert() for unconditional abortion
  140. * (in particular for code paths which are never supposed to be run).
  141. * */
  142. JSON_NORETURN static void json_abort(const char *message);
  143. /* helper for accessing the optimized string data component in json_object
  144. */
  145. static inline char *get_string_component_mutable(struct json_object *jso)
  146. {
  147. if (JC_STRING_C(jso)->len < 0)
  148. {
  149. /* Due to json_object_set_string(), we might have a pointer */
  150. return JC_STRING(jso)->c_string.pdata;
  151. }
  152. return JC_STRING(jso)->c_string.idata;
  153. }
  154. static inline const char *get_string_component(const struct json_object *jso)
  155. {
  156. return get_string_component_mutable((void *)(uintptr_t)(const void *)jso);
  157. }
  158. /* string escaping */
  159. static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int flags)
  160. {
  161. size_t pos = 0, start_offset = 0;
  162. unsigned char c;
  163. while (len--)
  164. {
  165. c = str[pos];
  166. switch (c)
  167. {
  168. case '\b':
  169. case '\n':
  170. case '\r':
  171. case '\t':
  172. case '\f':
  173. case '"':
  174. case '\\':
  175. case '/':
  176. if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  177. {
  178. pos++;
  179. break;
  180. }
  181. if (pos > start_offset)
  182. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  183. if (c == '\b')
  184. printbuf_memappend(pb, "\\b", 2);
  185. else if (c == '\n')
  186. printbuf_memappend(pb, "\\n", 2);
  187. else if (c == '\r')
  188. printbuf_memappend(pb, "\\r", 2);
  189. else if (c == '\t')
  190. printbuf_memappend(pb, "\\t", 2);
  191. else if (c == '\f')
  192. printbuf_memappend(pb, "\\f", 2);
  193. else if (c == '"')
  194. printbuf_memappend(pb, "\\\"", 2);
  195. else if (c == '\\')
  196. printbuf_memappend(pb, "\\\\", 2);
  197. else if (c == '/')
  198. printbuf_memappend(pb, "\\/", 2);
  199. start_offset = ++pos;
  200. break;
  201. default:
  202. if (c < ' ')
  203. {
  204. char sbuf[7];
  205. if (pos > start_offset)
  206. printbuf_memappend(pb, str + start_offset,
  207. pos - start_offset);
  208. snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4],
  209. json_hex_chars[c & 0xf]);
  210. printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1);
  211. start_offset = ++pos;
  212. }
  213. else
  214. pos++;
  215. }
  216. }
  217. if (pos > start_offset)
  218. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  219. return 0;
  220. }
  221. /* reference counting */
  222. struct json_object *json_object_get(struct json_object *jso)
  223. {
  224. if (!jso)
  225. return jso;
  226. // Don't overflow the refcounter.
  227. assert(jso->_ref_count < UINT32_MAX);
  228. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  229. __sync_add_and_fetch(&jso->_ref_count, 1);
  230. #else
  231. ++jso->_ref_count;
  232. #endif
  233. return jso;
  234. }
  235. int json_object_put(struct json_object *jso)
  236. {
  237. if (!jso)
  238. return 0;
  239. /* Avoid invalid free and crash explicitly instead of (silently)
  240. * segfaulting.
  241. */
  242. assert(jso->_ref_count > 0);
  243. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  244. /* Note: this only allow the refcount to remain correct
  245. * when multiple threads are adjusting it. It is still an error
  246. * for a thread to decrement the refcount if it doesn't "own" it,
  247. * as that can result in the thread that loses the race to 0
  248. * operating on an already-freed object.
  249. */
  250. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0)
  251. return 0;
  252. #else
  253. if (--jso->_ref_count > 0)
  254. return 0;
  255. #endif
  256. if (jso->_user_delete)
  257. jso->_user_delete(jso, jso->_userdata);
  258. switch (jso->o_type)
  259. {
  260. case json_type_object: json_object_object_delete(jso); break;
  261. case json_type_array: json_object_array_delete(jso); break;
  262. case json_type_string: json_object_string_delete(jso); break;
  263. default: json_object_generic_delete(jso); break;
  264. }
  265. return 1;
  266. }
  267. /* generic object construction and destruction parts */
  268. static void json_object_generic_delete(struct json_object *jso)
  269. {
  270. printbuf_free(jso->_pb);
  271. free(jso);
  272. }
  273. static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size,
  274. json_object_to_json_string_fn *to_json_string)
  275. {
  276. struct json_object *jso;
  277. jso = (struct json_object *)malloc(alloc_size);
  278. if (!jso)
  279. return NULL;
  280. jso->o_type = o_type;
  281. jso->_ref_count = 1;
  282. jso->_to_json_string = to_json_string;
  283. jso->_pb = NULL;
  284. jso->_user_delete = NULL;
  285. jso->_userdata = NULL;
  286. //jso->... // Type-specific fields must be set by caller
  287. return jso;
  288. }
  289. /* type checking functions */
  290. int json_object_is_type(const struct json_object *jso, enum json_type type)
  291. {
  292. if (!jso)
  293. return (type == json_type_null);
  294. return (jso->o_type == type);
  295. }
  296. enum json_type json_object_get_type(const struct json_object *jso)
  297. {
  298. if (!jso)
  299. return json_type_null;
  300. return jso->o_type;
  301. }
  302. void *json_object_get_userdata(json_object *jso)
  303. {
  304. return jso ? jso->_userdata : NULL;
  305. }
  306. void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete)
  307. {
  308. // Can't return failure, so abort if we can't perform the operation.
  309. assert(jso != NULL);
  310. // First, clean up any previously existing user info
  311. if (jso->_user_delete)
  312. jso->_user_delete(jso, jso->_userdata);
  313. jso->_userdata = userdata;
  314. jso->_user_delete = user_delete;
  315. }
  316. /* set a custom conversion to string */
  317. void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func,
  318. void *userdata, json_object_delete_fn *user_delete)
  319. {
  320. json_object_set_userdata(jso, userdata, user_delete);
  321. if (to_string_func == NULL)
  322. {
  323. // Reset to the standard serialization function
  324. switch (jso->o_type)
  325. {
  326. case json_type_null: jso->_to_json_string = NULL; break;
  327. case json_type_boolean:
  328. jso->_to_json_string = &json_object_boolean_to_json_string;
  329. break;
  330. case json_type_double:
  331. jso->_to_json_string = &json_object_double_to_json_string_default;
  332. break;
  333. case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break;
  334. case json_type_object:
  335. jso->_to_json_string = &json_object_object_to_json_string;
  336. break;
  337. case json_type_array:
  338. jso->_to_json_string = &json_object_array_to_json_string;
  339. break;
  340. case json_type_string:
  341. jso->_to_json_string = &json_object_string_to_json_string;
  342. break;
  343. }
  344. return;
  345. }
  346. jso->_to_json_string = to_string_func;
  347. }
  348. /* extended conversion to string */
  349. const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  350. {
  351. const char *r = NULL;
  352. size_t s = 0;
  353. if (!jso)
  354. {
  355. s = 4;
  356. r = "null";
  357. }
  358. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  359. {
  360. printbuf_reset(jso->_pb);
  361. if (jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  362. {
  363. s = (size_t)jso->_pb->bpos;
  364. r = jso->_pb->buf;
  365. }
  366. }
  367. if (length)
  368. *length = s;
  369. return r;
  370. }
  371. const char *json_object_to_json_string_ext(struct json_object *jso, int flags)
  372. {
  373. return json_object_to_json_string_length(jso, flags, NULL);
  374. }
  375. /* backwards-compatible conversion to string */
  376. const char *json_object_to_json_string(struct json_object *jso)
  377. {
  378. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  379. }
  380. static void indent(struct printbuf *pb, int level, int flags)
  381. {
  382. if (flags & JSON_C_TO_STRING_PRETTY)
  383. {
  384. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  385. {
  386. printbuf_memset(pb, -1, '\t', level);
  387. }
  388. else
  389. {
  390. printbuf_memset(pb, -1, ' ', level * 2);
  391. }
  392. }
  393. }
  394. /* json_object_object */
  395. static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb,
  396. int level, int flags)
  397. {
  398. int had_children = 0;
  399. struct json_object_iter iter;
  400. printbuf_strappend(pb, "{" /*}*/);
  401. if (flags & JSON_C_TO_STRING_PRETTY)
  402. printbuf_strappend(pb, "\n");
  403. json_object_object_foreachC(jso, iter)
  404. {
  405. if (had_children)
  406. {
  407. printbuf_strappend(pb, ",");
  408. if (flags & JSON_C_TO_STRING_PRETTY)
  409. printbuf_strappend(pb, "\n");
  410. }
  411. had_children = 1;
  412. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  413. printbuf_strappend(pb, " ");
  414. indent(pb, level + 1, flags);
  415. printbuf_strappend(pb, "\"");
  416. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  417. if (flags & JSON_C_TO_STRING_SPACED)
  418. printbuf_strappend(pb, "\": ");
  419. else
  420. printbuf_strappend(pb, "\":");
  421. if (iter.val == NULL)
  422. printbuf_strappend(pb, "null");
  423. else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
  424. return -1;
  425. }
  426. if (flags & JSON_C_TO_STRING_PRETTY)
  427. {
  428. if (had_children)
  429. printbuf_strappend(pb, "\n");
  430. indent(pb, level, flags);
  431. }
  432. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  433. return printbuf_strappend(pb, /*{*/ " }");
  434. else
  435. return printbuf_strappend(pb, /*{*/ "}");
  436. }
  437. static void json_object_lh_entry_free(struct lh_entry *ent)
  438. {
  439. if (!lh_entry_k_is_constant(ent))
  440. free(lh_entry_k(ent));
  441. json_object_put((struct json_object *)lh_entry_v(ent));
  442. }
  443. static void json_object_object_delete(struct json_object *jso_base)
  444. {
  445. lh_table_free(JC_OBJECT(jso_base)->c_object);
  446. json_object_generic_delete(jso_base);
  447. }
  448. struct json_object *json_object_new_object(void)
  449. {
  450. struct json_object_object *jso = JSON_OBJECT_NEW(object);
  451. if (!jso)
  452. return NULL;
  453. jso->c_object =
  454. lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
  455. if (!jso->c_object)
  456. {
  457. json_object_generic_delete(&jso->base);
  458. errno = ENOMEM;
  459. return NULL;
  460. }
  461. return &jso->base;
  462. }
  463. struct lh_table *json_object_get_object(const struct json_object *jso)
  464. {
  465. if (!jso)
  466. return NULL;
  467. switch (jso->o_type)
  468. {
  469. case json_type_object: return JC_OBJECT_C(jso)->c_object;
  470. default: return NULL;
  471. }
  472. }
  473. int json_object_object_add_ex(struct json_object *jso, const char *const key,
  474. struct json_object *const val, const unsigned opts)
  475. {
  476. struct json_object *existing_value = NULL;
  477. struct lh_entry *existing_entry;
  478. unsigned long hash;
  479. assert(json_object_get_type(jso) == json_type_object);
  480. // We lookup the entry and replace the value, rather than just deleting
  481. // and re-adding it, so the existing key remains valid.
  482. hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key);
  483. existing_entry =
  484. (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
  485. ? NULL
  486. : lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash);
  487. // The caller must avoid creating loops in the object tree, but do a
  488. // quick check anyway to make sure we're not creating a trivial loop.
  489. if (jso == val)
  490. return -1;
  491. if (!existing_entry)
  492. {
  493. const void *const k =
  494. (opts & JSON_C_OBJECT_ADD_CONSTANT_KEY) ? (const void *)key : strdup(key);
  495. if (k == NULL)
  496. return -1;
  497. return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts);
  498. }
  499. existing_value = (json_object *)lh_entry_v(existing_entry);
  500. if (existing_value)
  501. json_object_put(existing_value);
  502. lh_entry_set_val(existing_entry, val);
  503. return 0;
  504. }
  505. int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
  506. {
  507. return json_object_object_add_ex(jso, key, val, 0);
  508. }
  509. int json_object_object_length(const struct json_object *jso)
  510. {
  511. assert(json_object_get_type(jso) == json_type_object);
  512. return lh_table_length(JC_OBJECT_C(jso)->c_object);
  513. }
  514. size_t json_c_object_sizeof(void)
  515. {
  516. return sizeof(struct json_object);
  517. }
  518. struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
  519. {
  520. struct json_object *result = NULL;
  521. json_object_object_get_ex(jso, key, &result);
  522. return result;
  523. }
  524. json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
  525. struct json_object **value)
  526. {
  527. if (value != NULL)
  528. *value = NULL;
  529. if (NULL == jso)
  530. return 0;
  531. switch (jso->o_type)
  532. {
  533. case json_type_object:
  534. return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key,
  535. (void **)value);
  536. default:
  537. if (value != NULL)
  538. *value = NULL;
  539. return 0;
  540. }
  541. }
  542. void json_object_object_del(struct json_object *jso, const char *key)
  543. {
  544. assert(json_object_get_type(jso) == json_type_object);
  545. lh_table_delete(JC_OBJECT(jso)->c_object, key);
  546. }
  547. /* json_object_boolean */
  548. static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
  549. int level, int flags)
  550. {
  551. if (JC_BOOL(jso)->c_boolean)
  552. return printbuf_strappend(pb, "true");
  553. return printbuf_strappend(pb, "false");
  554. }
  555. struct json_object *json_object_new_boolean(json_bool b)
  556. {
  557. struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean);
  558. if (!jso)
  559. return NULL;
  560. jso->c_boolean = b;
  561. return &jso->base;
  562. }
  563. json_bool json_object_get_boolean(const struct json_object *jso)
  564. {
  565. if (!jso)
  566. return 0;
  567. switch (jso->o_type)
  568. {
  569. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  570. case json_type_int:
  571. switch (JC_INT_C(jso)->cint_type)
  572. {
  573. case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0);
  574. case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0);
  575. default: json_abort("invalid cint_type");
  576. }
  577. case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0);
  578. case json_type_string: return (JC_STRING_C(jso)->len != 0);
  579. default: return 0;
  580. }
  581. }
  582. int json_object_set_boolean(struct json_object *jso, json_bool new_value)
  583. {
  584. if (!jso || jso->o_type != json_type_boolean)
  585. return 0;
  586. JC_BOOL(jso)->c_boolean = new_value;
  587. return 1;
  588. }
  589. /* json_object_int */
  590. static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  591. int flags)
  592. {
  593. /* room for 19 digits, the sign char, and a null term */
  594. char sbuf[21];
  595. if (JC_INT(jso)->cint_type == json_object_int_type_int64)
  596. snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64);
  597. else
  598. snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64);
  599. return printbuf_memappend(pb, sbuf, strlen(sbuf));
  600. }
  601. struct json_object *json_object_new_int(int32_t i)
  602. {
  603. return json_object_new_int64(i);
  604. }
  605. int32_t json_object_get_int(const struct json_object *jso)
  606. {
  607. int64_t cint64 = 0;
  608. double cdouble;
  609. enum json_type o_type;
  610. if (!jso)
  611. return 0;
  612. o_type = jso->o_type;
  613. if (o_type == json_type_int)
  614. {
  615. const struct json_object_int *jsoint = JC_INT_C(jso);
  616. if (jsoint->cint_type == json_object_int_type_int64)
  617. {
  618. cint64 = jsoint->cint.c_int64;
  619. }
  620. else
  621. {
  622. if (jsoint->cint.c_uint64 >= INT64_MAX)
  623. cint64 = INT64_MAX;
  624. else
  625. cint64 = (int64_t)jsoint->cint.c_uint64;
  626. }
  627. }
  628. else if (o_type == json_type_string)
  629. {
  630. /*
  631. * Parse strings into 64-bit numbers, then use the
  632. * 64-to-32-bit number handling below.
  633. */
  634. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  635. return 0; /* whoops, it didn't work. */
  636. o_type = json_type_int;
  637. }
  638. switch (o_type)
  639. {
  640. case json_type_int:
  641. /* Make sure we return the correct values for out of range numbers. */
  642. if (cint64 <= INT32_MIN)
  643. return INT32_MIN;
  644. if (cint64 >= INT32_MAX)
  645. return INT32_MAX;
  646. return (int32_t)cint64;
  647. case json_type_double:
  648. cdouble = JC_DOUBLE_C(jso)->c_double;
  649. if (cdouble <= INT32_MIN)
  650. return INT32_MIN;
  651. if (cdouble >= INT32_MAX)
  652. return INT32_MAX;
  653. return (int32_t)cdouble;
  654. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  655. default: return 0;
  656. }
  657. }
  658. int json_object_set_int(struct json_object *jso, int new_value)
  659. {
  660. return json_object_set_int64(jso, (int64_t)new_value);
  661. }
  662. struct json_object *json_object_new_int64(int64_t i)
  663. {
  664. struct json_object_int *jso = JSON_OBJECT_NEW(int);
  665. if (!jso)
  666. return NULL;
  667. jso->cint.c_int64 = i;
  668. jso->cint_type = json_object_int_type_int64;
  669. return &jso->base;
  670. }
  671. struct json_object *json_object_new_uint64(uint64_t i)
  672. {
  673. struct json_object_int *jso = JSON_OBJECT_NEW(int);
  674. if (!jso)
  675. return NULL;
  676. jso->cint.c_uint64 = i;
  677. jso->cint_type = json_object_int_type_uint64;
  678. return &jso->base;
  679. }
  680. int64_t json_object_get_int64(const struct json_object *jso)
  681. {
  682. int64_t cint;
  683. if (!jso)
  684. return 0;
  685. switch (jso->o_type)
  686. {
  687. case json_type_int:
  688. {
  689. const struct json_object_int *jsoint = JC_INT_C(jso);
  690. switch (jsoint->cint_type)
  691. {
  692. case json_object_int_type_int64: return jsoint->cint.c_int64;
  693. case json_object_int_type_uint64:
  694. if (jsoint->cint.c_uint64 >= INT64_MAX)
  695. return INT64_MAX;
  696. return (int64_t)jsoint->cint.c_uint64;
  697. default: json_abort("invalid cint_type");
  698. }
  699. }
  700. case json_type_double:
  701. // INT64_MAX can't be exactly represented as a double
  702. // so cast to tell the compiler it's ok to round up.
  703. if (JC_DOUBLE_C(jso)->c_double >= (double)INT64_MAX)
  704. return INT64_MAX;
  705. if (JC_DOUBLE_C(jso)->c_double <= INT64_MIN)
  706. return INT64_MIN;
  707. return (int64_t)JC_DOUBLE_C(jso)->c_double;
  708. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  709. case json_type_string:
  710. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  711. return cint;
  712. /* FALLTHRU */
  713. default: return 0;
  714. }
  715. }
  716. uint64_t json_object_get_uint64(const struct json_object *jso)
  717. {
  718. uint64_t cuint;
  719. if (!jso)
  720. return 0;
  721. switch (jso->o_type)
  722. {
  723. case json_type_int:
  724. {
  725. const struct json_object_int *jsoint = JC_INT_C(jso);
  726. switch (jsoint->cint_type)
  727. {
  728. case json_object_int_type_int64:
  729. if (jsoint->cint.c_int64 < 0)
  730. return 0;
  731. return (uint64_t)jsoint->cint.c_int64;
  732. case json_object_int_type_uint64: return jsoint->cint.c_uint64;
  733. default: json_abort("invalid cint_type");
  734. }
  735. }
  736. case json_type_double:
  737. // UINT64_MAX can't be exactly represented as a double
  738. // so cast to tell the compiler it's ok to round up.
  739. if (JC_DOUBLE_C(jso)->c_double >= (double)UINT64_MAX)
  740. return UINT64_MAX;
  741. if (JC_DOUBLE_C(jso)->c_double < 0)
  742. return 0;
  743. return (uint64_t)JC_DOUBLE_C(jso)->c_double;
  744. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  745. case json_type_string:
  746. if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
  747. return cuint;
  748. /* FALLTHRU */
  749. default: return 0;
  750. }
  751. }
  752. int json_object_set_int64(struct json_object *jso, int64_t new_value)
  753. {
  754. if (!jso || jso->o_type != json_type_int)
  755. return 0;
  756. JC_INT(jso)->cint.c_int64 = new_value;
  757. JC_INT(jso)->cint_type = json_object_int_type_int64;
  758. return 1;
  759. }
  760. int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
  761. {
  762. if (!jso || jso->o_type != json_type_int)
  763. return 0;
  764. JC_INT(jso)->cint.c_uint64 = new_value;
  765. JC_INT(jso)->cint_type = json_object_int_type_uint64;
  766. return 1;
  767. }
  768. int json_object_int_inc(struct json_object *jso, int64_t val)
  769. {
  770. struct json_object_int *jsoint;
  771. if (!jso || jso->o_type != json_type_int)
  772. return 0;
  773. jsoint = JC_INT(jso);
  774. switch (jsoint->cint_type)
  775. {
  776. case json_object_int_type_int64:
  777. if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val)
  778. {
  779. jsoint->cint.c_uint64 = (uint64_t)jsoint->cint.c_int64 + (uint64_t)val;
  780. jsoint->cint_type = json_object_int_type_uint64;
  781. }
  782. else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val)
  783. {
  784. jsoint->cint.c_int64 = INT64_MIN;
  785. }
  786. else
  787. {
  788. jsoint->cint.c_int64 += val;
  789. }
  790. return 1;
  791. case json_object_int_type_uint64:
  792. if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val)
  793. {
  794. jsoint->cint.c_uint64 = UINT64_MAX;
  795. }
  796. else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val))
  797. {
  798. jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val;
  799. jsoint->cint_type = json_object_int_type_int64;
  800. }
  801. else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val))
  802. {
  803. jsoint->cint.c_uint64 -= (uint64_t)(-val);
  804. }
  805. else
  806. {
  807. jsoint->cint.c_uint64 += val;
  808. }
  809. return 1;
  810. default: json_abort("invalid cint_type");
  811. }
  812. }
  813. /* json_object_double */
  814. #if defined(HAVE___THREAD)
  815. // i.e. __thread or __declspec(thread)
  816. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  817. #endif
  818. static char *global_serialization_float_format = NULL;
  819. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  820. {
  821. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  822. {
  823. #if defined(HAVE___THREAD)
  824. if (tls_serialization_float_format)
  825. {
  826. free(tls_serialization_float_format);
  827. tls_serialization_float_format = NULL;
  828. }
  829. #endif
  830. if (global_serialization_float_format)
  831. free(global_serialization_float_format);
  832. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  833. }
  834. else if (global_or_thread == JSON_C_OPTION_THREAD)
  835. {
  836. #if defined(HAVE___THREAD)
  837. if (tls_serialization_float_format)
  838. {
  839. free(tls_serialization_float_format);
  840. tls_serialization_float_format = NULL;
  841. }
  842. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  843. #else
  844. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  845. return -1;
  846. #endif
  847. }
  848. else
  849. {
  850. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n",
  851. global_or_thread);
  852. return -1;
  853. }
  854. return 0;
  855. }
  856. static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb,
  857. int level, int flags, const char *format)
  858. {
  859. struct json_object_double *jsodbl = JC_DOUBLE(jso);
  860. char buf[128], *p, *q;
  861. int size;
  862. /* Although JSON RFC does not support
  863. * NaN or Infinity as numeric values
  864. * ECMA 262 section 9.8.1 defines
  865. * how to handle these cases as strings
  866. */
  867. if (isnan(jsodbl->c_double))
  868. {
  869. size = snprintf(buf, sizeof(buf), "NaN");
  870. }
  871. else if (isinf(jsodbl->c_double))
  872. {
  873. if (jsodbl->c_double > 0)
  874. size = snprintf(buf, sizeof(buf), "Infinity");
  875. else
  876. size = snprintf(buf, sizeof(buf), "-Infinity");
  877. }
  878. else
  879. {
  880. const char *std_format = "%.17g";
  881. int format_drops_decimals = 0;
  882. int looks_numeric = 0;
  883. if (!format)
  884. {
  885. #if defined(HAVE___THREAD)
  886. if (tls_serialization_float_format)
  887. format = tls_serialization_float_format;
  888. else
  889. #endif
  890. if (global_serialization_float_format)
  891. format = global_serialization_float_format;
  892. else
  893. format = std_format;
  894. }
  895. size = snprintf(buf, sizeof(buf), format, jsodbl->c_double);
  896. if (size < 0)
  897. return -1;
  898. p = strchr(buf, ',');
  899. if (p)
  900. *p = '.';
  901. else
  902. p = strchr(buf, '.');
  903. if (format == std_format || strstr(format, ".0f") == NULL)
  904. format_drops_decimals = 1;
  905. looks_numeric = /* Looks like *some* kind of number */
  906. is_plain_digit(buf[0]) || (size > 1 && buf[0] == '-' && is_plain_digit(buf[1]));
  907. if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */
  908. strchr(buf, 'e') == NULL && /* Not scientific notation */
  909. format_drops_decimals)
  910. {
  911. // Ensure it looks like a float, even if snprintf didn't,
  912. // unless a custom format is set to omit the decimal.
  913. strcat(buf, ".0");
  914. size += 2;
  915. }
  916. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  917. {
  918. /* last useful digit, always keep 1 zero */
  919. p++;
  920. for (q = p; *q; q++)
  921. {
  922. if (*q != '0')
  923. p = q;
  924. }
  925. /* drop trailing zeroes */
  926. if (*p != 0)
  927. *(++p) = 0;
  928. size = p - buf;
  929. }
  930. }
  931. // although unlikely, snprintf can fail
  932. if (size < 0)
  933. return -1;
  934. if (size >= (int)sizeof(buf))
  935. // The standard formats are guaranteed not to overrun the buffer,
  936. // but if a custom one happens to do so, just silently truncate.
  937. size = sizeof(buf) - 1;
  938. printbuf_memappend(pb, buf, size);
  939. return size;
  940. }
  941. static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb,
  942. int level, int flags)
  943. {
  944. return json_object_double_to_json_string_format(jso, pb, level, flags, NULL);
  945. }
  946. int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  947. int flags)
  948. {
  949. return json_object_double_to_json_string_format(jso, pb, level, flags,
  950. (const char *)jso->_userdata);
  951. }
  952. struct json_object *json_object_new_double(double d)
  953. {
  954. struct json_object_double *jso = JSON_OBJECT_NEW(double);
  955. if (!jso)
  956. return NULL;
  957. jso->base._to_json_string = &json_object_double_to_json_string_default;
  958. jso->c_double = d;
  959. return &jso->base;
  960. }
  961. struct json_object *json_object_new_double_s(double d, const char *ds)
  962. {
  963. char *new_ds;
  964. struct json_object *jso = json_object_new_double(d);
  965. if (!jso)
  966. return NULL;
  967. new_ds = strdup(ds);
  968. if (!new_ds)
  969. {
  970. json_object_generic_delete(jso);
  971. errno = ENOMEM;
  972. return NULL;
  973. }
  974. json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds,
  975. json_object_free_userdata);
  976. return jso;
  977. }
  978. /*
  979. * A wrapper around json_object_userdata_to_json_string() used only
  980. * by json_object_new_double_s() just so json_object_set_double() can
  981. * detect when it needs to reset the serializer to the default.
  982. */
  983. static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb,
  984. int level, int flags)
  985. {
  986. return json_object_userdata_to_json_string(jso, pb, level, flags);
  987. }
  988. int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  989. int flags)
  990. {
  991. int userdata_len = strlen((const char *)jso->_userdata);
  992. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  993. return userdata_len;
  994. }
  995. void json_object_free_userdata(struct json_object *jso, void *userdata)
  996. {
  997. free(userdata);
  998. }
  999. double json_object_get_double(const struct json_object *jso)
  1000. {
  1001. double cdouble;
  1002. char *errPtr = NULL;
  1003. if (!jso)
  1004. return 0.0;
  1005. switch (jso->o_type)
  1006. {
  1007. case json_type_double: return JC_DOUBLE_C(jso)->c_double;
  1008. case json_type_int:
  1009. switch (JC_INT_C(jso)->cint_type)
  1010. {
  1011. case json_object_int_type_int64: return JC_INT_C(jso)->cint.c_int64;
  1012. case json_object_int_type_uint64: return JC_INT_C(jso)->cint.c_uint64;
  1013. default: json_abort("invalid cint_type");
  1014. }
  1015. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  1016. case json_type_string:
  1017. errno = 0;
  1018. cdouble = strtod(get_string_component(jso), &errPtr);
  1019. /* if conversion stopped at the first character, return 0.0 */
  1020. if (errPtr == get_string_component(jso))
  1021. {
  1022. errno = EINVAL;
  1023. return 0.0;
  1024. }
  1025. /*
  1026. * Check that the conversion terminated on something sensible
  1027. *
  1028. * For example, { "pay" : 123AB } would parse as 123.
  1029. */
  1030. if (*errPtr != '\0')
  1031. {
  1032. errno = EINVAL;
  1033. return 0.0;
  1034. }
  1035. /*
  1036. * If strtod encounters a string which would exceed the
  1037. * capacity of a double, it returns +/- HUGE_VAL and sets
  1038. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  1039. * from a conversion, so we need to check errno.
  1040. *
  1041. * Underflow also sets errno to ERANGE, but it returns 0 in
  1042. * that case, which is what we will return anyway.
  1043. *
  1044. * See CERT guideline ERR30-C
  1045. */
  1046. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno))
  1047. cdouble = 0.0;
  1048. return cdouble;
  1049. default: errno = EINVAL; return 0.0;
  1050. }
  1051. }
  1052. int json_object_set_double(struct json_object *jso, double new_value)
  1053. {
  1054. if (!jso || jso->o_type != json_type_double)
  1055. return 0;
  1056. JC_DOUBLE(jso)->c_double = new_value;
  1057. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  1058. json_object_set_serializer(jso, NULL, NULL, NULL);
  1059. return 1;
  1060. }
  1061. /* json_object_string */
  1062. static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb,
  1063. int level, int flags)
  1064. {
  1065. ssize_t len = JC_STRING(jso)->len;
  1066. printbuf_strappend(pb, "\"");
  1067. json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
  1068. printbuf_strappend(pb, "\"");
  1069. return 0;
  1070. }
  1071. static void json_object_string_delete(struct json_object *jso)
  1072. {
  1073. if (JC_STRING(jso)->len < 0)
  1074. free(JC_STRING(jso)->c_string.pdata);
  1075. json_object_generic_delete(jso);
  1076. }
  1077. static struct json_object *_json_object_new_string(const char *s, const size_t len)
  1078. {
  1079. size_t objsize;
  1080. struct json_object_string *jso;
  1081. /*
  1082. * Structures Actual memory layout
  1083. * ------------------- --------------------
  1084. * [json_object_string [json_object_string
  1085. * [json_object] [json_object]
  1086. * ...other fields... ...other fields...
  1087. * c_string] len
  1088. * bytes
  1089. * of
  1090. * string
  1091. * data
  1092. * \0]
  1093. */
  1094. if (len > (SSIZE_T_MAX - (sizeof(*jso) - sizeof(jso->c_string)) - 1))
  1095. return NULL;
  1096. objsize = (sizeof(*jso) - sizeof(jso->c_string)) + len + 1;
  1097. if (len < sizeof(void *))
  1098. // We need a minimum size to support json_object_set_string() mutability
  1099. // so we can stuff a pointer into pdata :(
  1100. objsize += sizeof(void *) - len;
  1101. jso = (struct json_object_string *)json_object_new(json_type_string, objsize,
  1102. &json_object_string_to_json_string);
  1103. if (!jso)
  1104. return NULL;
  1105. jso->len = len;
  1106. memcpy(jso->c_string.idata, s, len);
  1107. // Cast below needed for Clang UB sanitizer
  1108. ((char *)jso->c_string.idata)[len] = '\0';
  1109. return &jso->base;
  1110. }
  1111. struct json_object *json_object_new_string(const char *s)
  1112. {
  1113. return _json_object_new_string(s, strlen(s));
  1114. }
  1115. struct json_object *json_object_new_string_len(const char *s, const int len)
  1116. {
  1117. return _json_object_new_string(s, len);
  1118. }
  1119. const char *json_object_get_string(struct json_object *jso)
  1120. {
  1121. if (!jso)
  1122. return NULL;
  1123. switch (jso->o_type)
  1124. {
  1125. case json_type_string: return get_string_component(jso);
  1126. default: return json_object_to_json_string(jso);
  1127. }
  1128. }
  1129. static inline ssize_t _json_object_get_string_len(const struct json_object_string *jso)
  1130. {
  1131. ssize_t len;
  1132. len = jso->len;
  1133. return (len < 0) ? -(ssize_t)len : len;
  1134. }
  1135. int json_object_get_string_len(const struct json_object *jso)
  1136. {
  1137. if (!jso)
  1138. return 0;
  1139. switch (jso->o_type)
  1140. {
  1141. case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso));
  1142. default: return 0;
  1143. }
  1144. }
  1145. static int _json_object_set_string_len(json_object *jso, const char *s, size_t len)
  1146. {
  1147. char *dstbuf;
  1148. ssize_t curlen;
  1149. ssize_t newlen;
  1150. if (jso == NULL || jso->o_type != json_type_string)
  1151. return 0;
  1152. if (len >= INT_MAX - 1)
  1153. // jso->len is a signed ssize_t, so it can't hold the
  1154. // full size_t range. json_object_get_string_len returns
  1155. // length as int, cap length at INT_MAX.
  1156. return 0;
  1157. dstbuf = get_string_component_mutable(jso);
  1158. curlen = JC_STRING(jso)->len;
  1159. if (curlen < 0)
  1160. curlen = -curlen;
  1161. newlen = len;
  1162. if ((ssize_t)len > curlen)
  1163. {
  1164. // We have no way to return the new ptr from realloc(jso, newlen)
  1165. // and we have no way of knowing whether there's extra room available
  1166. // so we need to stuff a pointer in to pdata :(
  1167. dstbuf = (char *)malloc(len + 1);
  1168. if (dstbuf == NULL)
  1169. return 0;
  1170. if (JC_STRING(jso)->len < 0)
  1171. free(JC_STRING(jso)->c_string.pdata);
  1172. JC_STRING(jso)->c_string.pdata = dstbuf;
  1173. newlen = -(ssize_t)len;
  1174. }
  1175. else if (JC_STRING(jso)->len < 0)
  1176. {
  1177. // We've got enough room in the separate allocated buffer,
  1178. // so use it as-is and continue to indicate that pdata is used.
  1179. newlen = -(ssize_t)len;
  1180. }
  1181. memcpy(dstbuf, (const void *)s, len);
  1182. dstbuf[len] = '\0';
  1183. JC_STRING(jso)->len = newlen;
  1184. return 1;
  1185. }
  1186. int json_object_set_string(json_object *jso, const char *s)
  1187. {
  1188. return _json_object_set_string_len(jso, s, strlen(s));
  1189. }
  1190. int json_object_set_string_len(json_object *jso, const char *s, int len)
  1191. {
  1192. return _json_object_set_string_len(jso, s, len);
  1193. }
  1194. /* json_object_array */
  1195. static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1196. int flags)
  1197. {
  1198. int had_children = 0;
  1199. size_t ii;
  1200. printbuf_strappend(pb, "[");
  1201. if (flags & JSON_C_TO_STRING_PRETTY)
  1202. printbuf_strappend(pb, "\n");
  1203. for (ii = 0; ii < json_object_array_length(jso); ii++)
  1204. {
  1205. struct json_object *val;
  1206. if (had_children)
  1207. {
  1208. printbuf_strappend(pb, ",");
  1209. if (flags & JSON_C_TO_STRING_PRETTY)
  1210. printbuf_strappend(pb, "\n");
  1211. }
  1212. had_children = 1;
  1213. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1214. printbuf_strappend(pb, " ");
  1215. indent(pb, level + 1, flags);
  1216. val = json_object_array_get_idx(jso, ii);
  1217. if (val == NULL)
  1218. printbuf_strappend(pb, "null");
  1219. else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
  1220. return -1;
  1221. }
  1222. if (flags & JSON_C_TO_STRING_PRETTY)
  1223. {
  1224. if (had_children)
  1225. printbuf_strappend(pb, "\n");
  1226. indent(pb, level, flags);
  1227. }
  1228. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1229. return printbuf_strappend(pb, " ]");
  1230. return printbuf_strappend(pb, "]");
  1231. }
  1232. static void json_object_array_entry_free(void *data)
  1233. {
  1234. json_object_put((struct json_object *)data);
  1235. }
  1236. static void json_object_array_delete(struct json_object *jso)
  1237. {
  1238. array_list_free(JC_ARRAY(jso)->c_array);
  1239. json_object_generic_delete(jso);
  1240. }
  1241. struct json_object *json_object_new_array(void)
  1242. {
  1243. return json_object_new_array_ext(ARRAY_LIST_DEFAULT_SIZE);
  1244. }
  1245. struct json_object *json_object_new_array_ext(int initial_size)
  1246. {
  1247. struct json_object_array *jso = JSON_OBJECT_NEW(array);
  1248. if (!jso)
  1249. return NULL;
  1250. jso->c_array = array_list_new2(&json_object_array_entry_free, initial_size);
  1251. if (jso->c_array == NULL)
  1252. {
  1253. free(jso);
  1254. return NULL;
  1255. }
  1256. return &jso->base;
  1257. }
  1258. struct array_list *json_object_get_array(const struct json_object *jso)
  1259. {
  1260. if (!jso)
  1261. return NULL;
  1262. switch (jso->o_type)
  1263. {
  1264. case json_type_array: return JC_ARRAY_C(jso)->c_array;
  1265. default: return NULL;
  1266. }
  1267. }
  1268. void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *))
  1269. {
  1270. assert(json_object_get_type(jso) == json_type_array);
  1271. array_list_sort(JC_ARRAY(jso)->c_array, sort_fn);
  1272. }
  1273. struct json_object *json_object_array_bsearch(const struct json_object *key,
  1274. const struct json_object *jso,
  1275. int (*sort_fn)(const void *, const void *))
  1276. {
  1277. struct json_object **result;
  1278. assert(json_object_get_type(jso) == json_type_array);
  1279. result = (struct json_object **)array_list_bsearch((const void **)(void *)&key,
  1280. JC_ARRAY_C(jso)->c_array, sort_fn);
  1281. if (!result)
  1282. return NULL;
  1283. return *result;
  1284. }
  1285. size_t json_object_array_length(const struct json_object *jso)
  1286. {
  1287. assert(json_object_get_type(jso) == json_type_array);
  1288. return array_list_length(JC_ARRAY_C(jso)->c_array);
  1289. }
  1290. int json_object_array_add(struct json_object *jso, struct json_object *val)
  1291. {
  1292. assert(json_object_get_type(jso) == json_type_array);
  1293. return array_list_add(JC_ARRAY(jso)->c_array, val);
  1294. }
  1295. int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
  1296. {
  1297. assert(json_object_get_type(jso) == json_type_array);
  1298. return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val);
  1299. }
  1300. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1301. {
  1302. assert(json_object_get_type(jso) == json_type_array);
  1303. return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count);
  1304. }
  1305. struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx)
  1306. {
  1307. assert(json_object_get_type(jso) == json_type_array);
  1308. return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx);
  1309. }
  1310. static int json_array_equal(struct json_object *jso1, struct json_object *jso2)
  1311. {
  1312. size_t len, i;
  1313. len = json_object_array_length(jso1);
  1314. if (len != json_object_array_length(jso2))
  1315. return 0;
  1316. for (i = 0; i < len; i++)
  1317. {
  1318. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1319. json_object_array_get_idx(jso2, i)))
  1320. return 0;
  1321. }
  1322. return 1;
  1323. }
  1324. int json_object_array_shrink(struct json_object *jso, int empty_slots)
  1325. {
  1326. if (empty_slots < 0)
  1327. json_abort("json_object_array_shrink called with negative empty_slots");
  1328. return array_list_shrink(JC_ARRAY(jso)->c_array, empty_slots);
  1329. }
  1330. struct json_object *json_object_new_null(void)
  1331. {
  1332. return NULL;
  1333. }
  1334. static int json_object_all_values_equal(struct json_object *jso1, struct json_object *jso2)
  1335. {
  1336. struct json_object_iter iter;
  1337. struct json_object *sub;
  1338. assert(json_object_get_type(jso1) == json_type_object);
  1339. assert(json_object_get_type(jso2) == json_type_object);
  1340. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1341. json_object_object_foreachC(jso1, iter)
  1342. {
  1343. if (!lh_table_lookup_ex(JC_OBJECT(jso2)->c_object, (void *)iter.key,
  1344. (void **)(void *)&sub))
  1345. return 0;
  1346. if (!json_object_equal(iter.val, sub))
  1347. return 0;
  1348. }
  1349. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1350. json_object_object_foreachC(jso2, iter)
  1351. {
  1352. if (!lh_table_lookup_ex(JC_OBJECT(jso1)->c_object, (void *)iter.key,
  1353. (void **)(void *)&sub))
  1354. return 0;
  1355. }
  1356. return 1;
  1357. }
  1358. int json_object_equal(struct json_object *jso1, struct json_object *jso2)
  1359. {
  1360. if (jso1 == jso2)
  1361. return 1;
  1362. if (!jso1 || !jso2)
  1363. return 0;
  1364. if (jso1->o_type != jso2->o_type)
  1365. return 0;
  1366. switch (jso1->o_type)
  1367. {
  1368. case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean);
  1369. case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double);
  1370. case json_type_int:
  1371. {
  1372. struct json_object_int *int1 = JC_INT(jso1);
  1373. struct json_object_int *int2 = JC_INT(jso2);
  1374. if (int1->cint_type == json_object_int_type_int64)
  1375. {
  1376. if (int2->cint_type == json_object_int_type_int64)
  1377. return (int1->cint.c_int64 == int2->cint.c_int64);
  1378. if (int1->cint.c_int64 < 0)
  1379. return 0;
  1380. return ((uint64_t)int1->cint.c_int64 == int2->cint.c_uint64);
  1381. }
  1382. // else jso1 is a uint64
  1383. if (int2->cint_type == json_object_int_type_uint64)
  1384. return (int1->cint.c_uint64 == int2->cint.c_uint64);
  1385. if (int2->cint.c_int64 < 0)
  1386. return 0;
  1387. return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64);
  1388. }
  1389. case json_type_string:
  1390. {
  1391. return (_json_object_get_string_len(JC_STRING(jso1)) ==
  1392. _json_object_get_string_len(JC_STRING(jso2)) &&
  1393. memcmp(get_string_component(jso1), get_string_component(jso2),
  1394. _json_object_get_string_len(JC_STRING(jso1))) == 0);
  1395. }
  1396. case json_type_object: return json_object_all_values_equal(jso1, jso2);
  1397. case json_type_array: return json_array_equal(jso1, jso2);
  1398. case json_type_null: return 1;
  1399. };
  1400. return 0;
  1401. }
  1402. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1403. {
  1404. if (!src->_userdata && !src->_user_delete)
  1405. return 0;
  1406. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1407. dst->_to_json_string == _json_object_userdata_to_json_string)
  1408. {
  1409. dst->_userdata = strdup(src->_userdata);
  1410. }
  1411. // else if ... other supported serializers ...
  1412. else
  1413. {
  1414. _json_c_set_last_err(
  1415. "json_object_deep_copy: unable to copy unknown serializer data: %p\n",
  1416. (void *)dst->_to_json_string);
  1417. return -1;
  1418. }
  1419. dst->_user_delete = src->_user_delete;
  1420. return 0;
  1421. }
  1422. /**
  1423. * The default shallow copy implementation. Simply creates a new object of the same
  1424. * type but does *not* copy over _userdata nor retain any custom serializer.
  1425. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1426. * implementation that is aware of how to copy them.
  1427. *
  1428. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1429. */
  1430. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
  1431. size_t index, json_object **dst)
  1432. {
  1433. switch (src->o_type)
  1434. {
  1435. case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break;
  1436. case json_type_double: *dst = json_object_new_double(JC_DOUBLE(src)->c_double); break;
  1437. case json_type_int:
  1438. switch (JC_INT(src)->cint_type)
  1439. {
  1440. case json_object_int_type_int64:
  1441. *dst = json_object_new_int64(JC_INT(src)->cint.c_int64);
  1442. break;
  1443. case json_object_int_type_uint64:
  1444. *dst = json_object_new_uint64(JC_INT(src)->cint.c_uint64);
  1445. break;
  1446. default: json_abort("invalid cint_type");
  1447. }
  1448. break;
  1449. case json_type_string:
  1450. *dst = json_object_new_string_len(get_string_component(src),
  1451. _json_object_get_string_len(JC_STRING(src)));
  1452. break;
  1453. case json_type_object: *dst = json_object_new_object(); break;
  1454. case json_type_array: *dst = json_object_new_array(); break;
  1455. default: errno = EINVAL; return -1;
  1456. }
  1457. if (!*dst)
  1458. {
  1459. errno = ENOMEM;
  1460. return -1;
  1461. }
  1462. (*dst)->_to_json_string = src->_to_json_string;
  1463. // _userdata and _user_delete are copied later
  1464. return 1;
  1465. }
  1466. /*
  1467. * The actual guts of json_object_deep_copy(), with a few additional args
  1468. * needed so we can keep track of where we are within the object tree.
  1469. *
  1470. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1471. */
  1472. static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent,
  1473. const char *key_in_parent, size_t index_in_parent,
  1474. struct json_object **dst,
  1475. json_c_shallow_copy_fn *shallow_copy)
  1476. {
  1477. struct json_object_iter iter;
  1478. size_t src_array_len, ii;
  1479. int shallow_copy_rc = 0;
  1480. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1481. /* -1=error, 1=object created ok, 2=userdata set */
  1482. if (shallow_copy_rc < 1)
  1483. {
  1484. errno = EINVAL;
  1485. return -1;
  1486. }
  1487. assert(*dst != NULL);
  1488. switch (src->o_type)
  1489. {
  1490. case json_type_object:
  1491. json_object_object_foreachC(src, iter)
  1492. {
  1493. struct json_object *jso = NULL;
  1494. /* This handles the `json_type_null` case */
  1495. if (!iter.val)
  1496. jso = NULL;
  1497. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, UINT_MAX,
  1498. &jso, shallow_copy) < 0)
  1499. {
  1500. json_object_put(jso);
  1501. return -1;
  1502. }
  1503. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1504. {
  1505. json_object_put(jso);
  1506. return -1;
  1507. }
  1508. }
  1509. break;
  1510. case json_type_array:
  1511. src_array_len = json_object_array_length(src);
  1512. for (ii = 0; ii < src_array_len; ii++)
  1513. {
  1514. struct json_object *jso = NULL;
  1515. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1516. /* This handles the `json_type_null` case */
  1517. if (!jso1)
  1518. jso = NULL;
  1519. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso,
  1520. shallow_copy) < 0)
  1521. {
  1522. json_object_put(jso);
  1523. return -1;
  1524. }
  1525. if (json_object_array_add(*dst, jso) < 0)
  1526. {
  1527. json_object_put(jso);
  1528. return -1;
  1529. }
  1530. }
  1531. break;
  1532. default:
  1533. break;
  1534. /* else, nothing to do, shallow_copy already did. */
  1535. }
  1536. if (shallow_copy_rc != 2)
  1537. return json_object_copy_serializer_data(src, *dst);
  1538. return 0;
  1539. }
  1540. int json_object_deep_copy(struct json_object *src, struct json_object **dst,
  1541. json_c_shallow_copy_fn *shallow_copy)
  1542. {
  1543. int rc;
  1544. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1545. if (!src || !dst || *dst)
  1546. {
  1547. errno = EINVAL;
  1548. return -1;
  1549. }
  1550. if (shallow_copy == NULL)
  1551. shallow_copy = json_c_shallow_copy_default;
  1552. rc = json_object_deep_copy_recursive(src, NULL, NULL, UINT_MAX, dst, shallow_copy);
  1553. if (rc < 0)
  1554. {
  1555. json_object_put(*dst);
  1556. *dst = NULL;
  1557. }
  1558. return rc;
  1559. }
  1560. static void json_abort(const char *message)
  1561. {
  1562. if (message != NULL)
  1563. fprintf(stderr, "json-c aborts with error: %s\n", message);
  1564. abort();
  1565. }