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