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