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

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