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

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