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