You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 48 kB

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