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