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

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