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 56 kB

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