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

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