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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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 | JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY))
  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. int compact_array_reset_flag = 0;
  402. printbuf_strappend(pb, "{" /*}*/);
  403. json_object_object_foreachC(jso, iter)
  404. {
  405. /* For objects that are not arrays, these flags are equivalent. */
  406. if (flags & JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY)
  407. {
  408. flags = JSON_C_TO_STRING_PRETTY;
  409. compact_array_reset_flag = 1;
  410. }
  411. if (had_children)
  412. {
  413. printbuf_strappend(pb, ",");
  414. }
  415. if (flags & JSON_C_TO_STRING_PRETTY)
  416. printbuf_strappend(pb, "\n");
  417. had_children = 1;
  418. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  419. printbuf_strappend(pb, " ");
  420. indent(pb, level + 1, flags);
  421. printbuf_strappend(pb, "\"");
  422. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  423. if (flags & JSON_C_TO_STRING_SPACED)
  424. printbuf_strappend(pb, "\": ");
  425. else
  426. printbuf_strappend(pb, "\":");
  427. if (iter.val == NULL)
  428. printbuf_strappend(pb, "null");
  429. else
  430. {
  431. if (compact_array_reset_flag)
  432. {
  433. flags = JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY;
  434. compact_array_reset_flag = 0;
  435. }
  436. int ret = iter.val->_to_json_string(iter.val, pb, level + 1, flags);
  437. if (ret < 0)
  438. return -1;
  439. }
  440. }
  441. if ((flags & (JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY)) && had_children)
  442. {
  443. printbuf_strappend(pb, "\n");
  444. indent(pb, level, flags);
  445. }
  446. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  447. return printbuf_strappend(pb, /*{*/ " }");
  448. else
  449. return printbuf_strappend(pb, /*{*/ "}");
  450. }
  451. static void json_object_lh_entry_free(struct lh_entry *ent)
  452. {
  453. if (!lh_entry_k_is_constant(ent))
  454. free(lh_entry_k(ent));
  455. json_object_put((struct json_object *)lh_entry_v(ent));
  456. }
  457. static void json_object_object_delete(struct json_object *jso_base)
  458. {
  459. lh_table_free(JC_OBJECT(jso_base)->c_object);
  460. json_object_generic_delete(jso_base);
  461. }
  462. struct json_object *json_object_new_object(void)
  463. {
  464. struct json_object_object *jso = JSON_OBJECT_NEW(object);
  465. if (!jso)
  466. return NULL;
  467. jso->c_object =
  468. lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
  469. if (!jso->c_object)
  470. {
  471. json_object_generic_delete(&jso->base);
  472. errno = ENOMEM;
  473. return NULL;
  474. }
  475. return &jso->base;
  476. }
  477. struct lh_table *json_object_get_object(const struct json_object *jso)
  478. {
  479. if (!jso)
  480. return NULL;
  481. switch (jso->o_type)
  482. {
  483. case json_type_object: return JC_OBJECT_C(jso)->c_object;
  484. default: return NULL;
  485. }
  486. }
  487. int json_object_object_add_ex(struct json_object *jso, const char *const key,
  488. struct json_object *const val, const unsigned opts)
  489. {
  490. struct json_object *existing_value = NULL;
  491. struct lh_entry *existing_entry;
  492. unsigned long hash;
  493. assert(json_object_get_type(jso) == json_type_object);
  494. // We lookup the entry and replace the value, rather than just deleting
  495. // and re-adding it, so the existing key remains valid.
  496. hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key);
  497. existing_entry =
  498. (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
  499. ? NULL
  500. : lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash);
  501. // The caller must avoid creating loops in the object tree, but do a
  502. // quick check anyway to make sure we're not creating a trivial loop.
  503. if (jso == val)
  504. return -1;
  505. if (!existing_entry)
  506. {
  507. const void *const k =
  508. (opts & JSON_C_OBJECT_ADD_CONSTANT_KEY) ? (const void *)key : strdup(key);
  509. if (k == NULL)
  510. return -1;
  511. return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts);
  512. }
  513. existing_value = (json_object *)lh_entry_v(existing_entry);
  514. if (existing_value)
  515. json_object_put(existing_value);
  516. lh_entry_set_val(existing_entry, val);
  517. return 0;
  518. }
  519. int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
  520. {
  521. return json_object_object_add_ex(jso, key, val, 0);
  522. }
  523. int json_object_object_length(const struct json_object *jso)
  524. {
  525. assert(json_object_get_type(jso) == json_type_object);
  526. return lh_table_length(JC_OBJECT_C(jso)->c_object);
  527. }
  528. size_t json_c_object_sizeof(void)
  529. {
  530. return sizeof(struct json_object);
  531. }
  532. struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
  533. {
  534. struct json_object *result = NULL;
  535. json_object_object_get_ex(jso, key, &result);
  536. return result;
  537. }
  538. json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
  539. struct json_object **value)
  540. {
  541. if (value != NULL)
  542. *value = NULL;
  543. if (NULL == jso)
  544. return 0;
  545. switch (jso->o_type)
  546. {
  547. case json_type_object:
  548. return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key,
  549. (void **)value);
  550. default:
  551. if (value != NULL)
  552. *value = NULL;
  553. return 0;
  554. }
  555. }
  556. void json_object_object_del(struct json_object *jso, const char *key)
  557. {
  558. assert(json_object_get_type(jso) == json_type_object);
  559. lh_table_delete(JC_OBJECT(jso)->c_object, key);
  560. }
  561. /* json_object_boolean */
  562. static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
  563. int level, int flags)
  564. {
  565. if (JC_BOOL(jso)->c_boolean)
  566. return printbuf_strappend(pb, "true");
  567. return printbuf_strappend(pb, "false");
  568. }
  569. struct json_object *json_object_new_boolean(json_bool b)
  570. {
  571. struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean);
  572. if (!jso)
  573. return NULL;
  574. jso->c_boolean = b;
  575. return &jso->base;
  576. }
  577. json_bool json_object_get_boolean(const struct json_object *jso)
  578. {
  579. if (!jso)
  580. return 0;
  581. switch (jso->o_type)
  582. {
  583. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  584. case json_type_int:
  585. switch (JC_INT_C(jso)->cint_type)
  586. {
  587. case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0);
  588. case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0);
  589. default: json_abort("invalid cint_type");
  590. }
  591. case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0);
  592. case json_type_string: return (JC_STRING_C(jso)->len != 0);
  593. default: return 0;
  594. }
  595. }
  596. int json_object_set_boolean(struct json_object *jso, json_bool new_value)
  597. {
  598. if (!jso || jso->o_type != json_type_boolean)
  599. return 0;
  600. JC_BOOL(jso)->c_boolean = new_value;
  601. return 1;
  602. }
  603. /* json_object_int */
  604. static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  605. int flags)
  606. {
  607. /* room for 19 digits, the sign char, and a null term */
  608. char sbuf[21];
  609. if (JC_INT(jso)->cint_type == json_object_int_type_int64)
  610. snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64);
  611. else
  612. snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64);
  613. return printbuf_memappend(pb, sbuf, strlen(sbuf));
  614. }
  615. struct json_object *json_object_new_int(int32_t i)
  616. {
  617. return json_object_new_int64(i);
  618. }
  619. int32_t json_object_get_int(const struct json_object *jso)
  620. {
  621. int64_t cint64 = 0;
  622. double cdouble;
  623. enum json_type o_type;
  624. if (!jso)
  625. return 0;
  626. o_type = jso->o_type;
  627. if (o_type == json_type_int)
  628. {
  629. const struct json_object_int *jsoint = JC_INT_C(jso);
  630. if (jsoint->cint_type == json_object_int_type_int64)
  631. {
  632. cint64 = jsoint->cint.c_int64;
  633. }
  634. else
  635. {
  636. if (jsoint->cint.c_uint64 >= INT64_MAX)
  637. cint64 = INT64_MAX;
  638. else
  639. cint64 = (int64_t)jsoint->cint.c_uint64;
  640. }
  641. }
  642. else if (o_type == json_type_string)
  643. {
  644. /*
  645. * Parse strings into 64-bit numbers, then use the
  646. * 64-to-32-bit number handling below.
  647. */
  648. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  649. return 0; /* whoops, it didn't work. */
  650. o_type = json_type_int;
  651. }
  652. switch (o_type)
  653. {
  654. case json_type_int:
  655. /* Make sure we return the correct values for out of range numbers. */
  656. if (cint64 <= INT32_MIN)
  657. return INT32_MIN;
  658. if (cint64 >= INT32_MAX)
  659. return INT32_MAX;
  660. return (int32_t)cint64;
  661. case json_type_double:
  662. cdouble = JC_DOUBLE_C(jso)->c_double;
  663. if (cdouble <= INT32_MIN)
  664. return INT32_MIN;
  665. if (cdouble >= INT32_MAX)
  666. return INT32_MAX;
  667. return (int32_t)cdouble;
  668. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  669. default: return 0;
  670. }
  671. }
  672. int json_object_set_int(struct json_object *jso, int new_value)
  673. {
  674. return json_object_set_int64(jso, (int64_t)new_value);
  675. }
  676. struct json_object *json_object_new_int64(int64_t i)
  677. {
  678. struct json_object_int *jso = JSON_OBJECT_NEW(int);
  679. if (!jso)
  680. return NULL;
  681. jso->cint.c_int64 = i;
  682. jso->cint_type = json_object_int_type_int64;
  683. return &jso->base;
  684. }
  685. struct json_object *json_object_new_uint64(uint64_t i)
  686. {
  687. struct json_object_int *jso = JSON_OBJECT_NEW(int);
  688. if (!jso)
  689. return NULL;
  690. jso->cint.c_uint64 = i;
  691. jso->cint_type = json_object_int_type_uint64;
  692. return &jso->base;
  693. }
  694. int64_t json_object_get_int64(const struct json_object *jso)
  695. {
  696. int64_t cint;
  697. if (!jso)
  698. return 0;
  699. switch (jso->o_type)
  700. {
  701. case json_type_int:
  702. {
  703. const struct json_object_int *jsoint = JC_INT_C(jso);
  704. switch (jsoint->cint_type)
  705. {
  706. case json_object_int_type_int64: return jsoint->cint.c_int64;
  707. case json_object_int_type_uint64:
  708. if (jsoint->cint.c_uint64 >= INT64_MAX)
  709. return INT64_MAX;
  710. return (int64_t)jsoint->cint.c_uint64;
  711. default: json_abort("invalid cint_type");
  712. }
  713. }
  714. case json_type_double:
  715. // INT64_MAX can't be exactly represented as a double
  716. // so cast to tell the compiler it's ok to round up.
  717. if (JC_DOUBLE_C(jso)->c_double >= (double)INT64_MAX)
  718. return INT64_MAX;
  719. if (JC_DOUBLE_C(jso)->c_double <= INT64_MIN)
  720. return INT64_MIN;
  721. return (int64_t)JC_DOUBLE_C(jso)->c_double;
  722. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  723. case json_type_string:
  724. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  725. return cint;
  726. /* FALLTHRU */
  727. default: return 0;
  728. }
  729. }
  730. uint64_t json_object_get_uint64(const struct json_object *jso)
  731. {
  732. uint64_t cuint;
  733. if (!jso)
  734. return 0;
  735. switch (jso->o_type)
  736. {
  737. case json_type_int:
  738. {
  739. const struct json_object_int *jsoint = JC_INT_C(jso);
  740. switch (jsoint->cint_type)
  741. {
  742. case json_object_int_type_int64:
  743. if (jsoint->cint.c_int64 < 0)
  744. return 0;
  745. return (uint64_t)jsoint->cint.c_int64;
  746. case json_object_int_type_uint64: return jsoint->cint.c_uint64;
  747. default: json_abort("invalid cint_type");
  748. }
  749. }
  750. case json_type_double:
  751. // UINT64_MAX can't be exactly represented as a double
  752. // so cast to tell the compiler it's ok to round up.
  753. if (JC_DOUBLE_C(jso)->c_double >= (double)UINT64_MAX)
  754. return UINT64_MAX;
  755. if (JC_DOUBLE_C(jso)->c_double < 0)
  756. return 0;
  757. return (uint64_t)JC_DOUBLE_C(jso)->c_double;
  758. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  759. case json_type_string:
  760. if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
  761. return cuint;
  762. /* FALLTHRU */
  763. default: return 0;
  764. }
  765. }
  766. int json_object_set_int64(struct json_object *jso, int64_t new_value)
  767. {
  768. if (!jso || jso->o_type != json_type_int)
  769. return 0;
  770. JC_INT(jso)->cint.c_int64 = new_value;
  771. JC_INT(jso)->cint_type = json_object_int_type_int64;
  772. return 1;
  773. }
  774. int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
  775. {
  776. if (!jso || jso->o_type != json_type_int)
  777. return 0;
  778. JC_INT(jso)->cint.c_uint64 = new_value;
  779. JC_INT(jso)->cint_type = json_object_int_type_uint64;
  780. return 1;
  781. }
  782. int json_object_int_inc(struct json_object *jso, int64_t val)
  783. {
  784. struct json_object_int *jsoint;
  785. if (!jso || jso->o_type != json_type_int)
  786. return 0;
  787. jsoint = JC_INT(jso);
  788. switch (jsoint->cint_type)
  789. {
  790. case json_object_int_type_int64:
  791. if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val)
  792. {
  793. jsoint->cint.c_uint64 = (uint64_t)jsoint->cint.c_int64 + (uint64_t)val;
  794. jsoint->cint_type = json_object_int_type_uint64;
  795. }
  796. else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val)
  797. {
  798. jsoint->cint.c_int64 = INT64_MIN;
  799. }
  800. else
  801. {
  802. jsoint->cint.c_int64 += val;
  803. }
  804. return 1;
  805. case json_object_int_type_uint64:
  806. if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val)
  807. {
  808. jsoint->cint.c_uint64 = UINT64_MAX;
  809. }
  810. else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val))
  811. {
  812. jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val;
  813. jsoint->cint_type = json_object_int_type_int64;
  814. }
  815. else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val))
  816. {
  817. jsoint->cint.c_uint64 -= (uint64_t)(-val);
  818. }
  819. else
  820. {
  821. jsoint->cint.c_uint64 += val;
  822. }
  823. return 1;
  824. default: json_abort("invalid cint_type");
  825. }
  826. }
  827. /* json_object_double */
  828. #if defined(HAVE___THREAD)
  829. // i.e. __thread or __declspec(thread)
  830. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  831. #endif
  832. static char *global_serialization_float_format = NULL;
  833. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  834. {
  835. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  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. #endif
  844. if (global_serialization_float_format)
  845. free(global_serialization_float_format);
  846. if (double_format)
  847. {
  848. char *p = strdup(double_format);
  849. if (p == NULL)
  850. {
  851. _json_c_set_last_err("json_c_set_serialization_double_format: "
  852. "out of memory\n");
  853. return -1;
  854. }
  855. global_serialization_float_format = p;
  856. }
  857. else
  858. {
  859. global_serialization_float_format = NULL;
  860. }
  861. }
  862. else if (global_or_thread == JSON_C_OPTION_THREAD)
  863. {
  864. #if defined(HAVE___THREAD)
  865. if (tls_serialization_float_format)
  866. {
  867. free(tls_serialization_float_format);
  868. tls_serialization_float_format = NULL;
  869. }
  870. if (double_format)
  871. {
  872. char *p = strdup(double_format);
  873. if (p == NULL)
  874. {
  875. _json_c_set_last_err("json_c_set_serialization_double_format: "
  876. "out of memory\n");
  877. return -1;
  878. }
  879. tls_serialization_float_format = p;
  880. }
  881. else
  882. {
  883. tls_serialization_float_format = NULL;
  884. }
  885. #else
  886. _json_c_set_last_err("json_c_set_serialization_double_format: not compiled "
  887. "with __thread support\n");
  888. return -1;
  889. #endif
  890. }
  891. else
  892. {
  893. _json_c_set_last_err("json_c_set_serialization_double_format: invalid "
  894. "global_or_thread value: %d\n", global_or_thread);
  895. return -1;
  896. }
  897. return 0;
  898. }
  899. static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb,
  900. int level, int flags, const char *format)
  901. {
  902. struct json_object_double *jsodbl = JC_DOUBLE(jso);
  903. char buf[128], *p, *q;
  904. int size;
  905. /* Although JSON RFC does not support
  906. * NaN or Infinity as numeric values
  907. * ECMA 262 section 9.8.1 defines
  908. * how to handle these cases as strings
  909. */
  910. if (isnan(jsodbl->c_double))
  911. {
  912. size = snprintf(buf, sizeof(buf), "NaN");
  913. }
  914. else if (isinf(jsodbl->c_double))
  915. {
  916. if (jsodbl->c_double > 0)
  917. size = snprintf(buf, sizeof(buf), "Infinity");
  918. else
  919. size = snprintf(buf, sizeof(buf), "-Infinity");
  920. }
  921. else
  922. {
  923. const char *std_format = "%.17g";
  924. int format_drops_decimals = 0;
  925. int looks_numeric = 0;
  926. if (!format)
  927. {
  928. #if defined(HAVE___THREAD)
  929. if (tls_serialization_float_format)
  930. format = tls_serialization_float_format;
  931. else
  932. #endif
  933. if (global_serialization_float_format)
  934. format = global_serialization_float_format;
  935. else
  936. format = std_format;
  937. }
  938. size = snprintf(buf, sizeof(buf), format, jsodbl->c_double);
  939. if (size < 0)
  940. return -1;
  941. p = strchr(buf, ',');
  942. if (p)
  943. *p = '.';
  944. else
  945. p = strchr(buf, '.');
  946. if (format == std_format || strstr(format, ".0f") == NULL)
  947. format_drops_decimals = 1;
  948. looks_numeric = /* Looks like *some* kind of number */
  949. is_plain_digit(buf[0]) || (size > 1 && buf[0] == '-' && is_plain_digit(buf[1]));
  950. if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */
  951. strchr(buf, 'e') == NULL && /* Not scientific notation */
  952. format_drops_decimals)
  953. {
  954. // Ensure it looks like a float, even if snprintf didn't,
  955. // unless a custom format is set to omit the decimal.
  956. strcat(buf, ".0");
  957. size += 2;
  958. }
  959. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  960. {
  961. /* last useful digit, always keep 1 zero */
  962. p++;
  963. for (q = p; *q; q++)
  964. {
  965. if (*q != '0')
  966. p = q;
  967. }
  968. /* drop trailing zeroes */
  969. if (*p != 0)
  970. *(++p) = 0;
  971. size = p - buf;
  972. }
  973. }
  974. // although unlikely, snprintf can fail
  975. if (size < 0)
  976. return -1;
  977. if (size >= (int)sizeof(buf))
  978. // The standard formats are guaranteed not to overrun the buffer,
  979. // but if a custom one happens to do so, just silently truncate.
  980. size = sizeof(buf) - 1;
  981. printbuf_memappend(pb, buf, size);
  982. return size;
  983. }
  984. static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb,
  985. int level, int flags)
  986. {
  987. return json_object_double_to_json_string_format(jso, pb, level, flags, NULL);
  988. }
  989. int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  990. int flags)
  991. {
  992. return json_object_double_to_json_string_format(jso, pb, level, flags,
  993. (const char *)jso->_userdata);
  994. }
  995. struct json_object *json_object_new_double(double d)
  996. {
  997. struct json_object_double *jso = JSON_OBJECT_NEW(double);
  998. if (!jso)
  999. return NULL;
  1000. jso->base._to_json_string = &json_object_double_to_json_string_default;
  1001. jso->c_double = d;
  1002. return &jso->base;
  1003. }
  1004. struct json_object *json_object_new_double_s(double d, const char *ds)
  1005. {
  1006. char *new_ds;
  1007. struct json_object *jso = json_object_new_double(d);
  1008. if (!jso)
  1009. return NULL;
  1010. new_ds = strdup(ds);
  1011. if (!new_ds)
  1012. {
  1013. json_object_generic_delete(jso);
  1014. errno = ENOMEM;
  1015. return NULL;
  1016. }
  1017. json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds,
  1018. json_object_free_userdata);
  1019. return jso;
  1020. }
  1021. /*
  1022. * A wrapper around json_object_userdata_to_json_string() used only
  1023. * by json_object_new_double_s() just so json_object_set_double() can
  1024. * detect when it needs to reset the serializer to the default.
  1025. */
  1026. static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb,
  1027. int level, int flags)
  1028. {
  1029. return json_object_userdata_to_json_string(jso, pb, level, flags);
  1030. }
  1031. int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1032. int flags)
  1033. {
  1034. int userdata_len = strlen((const char *)jso->_userdata);
  1035. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  1036. return userdata_len;
  1037. }
  1038. void json_object_free_userdata(struct json_object *jso, void *userdata)
  1039. {
  1040. free(userdata);
  1041. }
  1042. double json_object_get_double(const struct json_object *jso)
  1043. {
  1044. double cdouble;
  1045. char *errPtr = NULL;
  1046. if (!jso)
  1047. return 0.0;
  1048. switch (jso->o_type)
  1049. {
  1050. case json_type_double: return JC_DOUBLE_C(jso)->c_double;
  1051. case json_type_int:
  1052. switch (JC_INT_C(jso)->cint_type)
  1053. {
  1054. case json_object_int_type_int64: return JC_INT_C(jso)->cint.c_int64;
  1055. case json_object_int_type_uint64: return JC_INT_C(jso)->cint.c_uint64;
  1056. default: json_abort("invalid cint_type");
  1057. }
  1058. case json_type_boolean: return JC_BOOL_C(jso)->c_boolean;
  1059. case json_type_string:
  1060. errno = 0;
  1061. cdouble = strtod(get_string_component(jso), &errPtr);
  1062. /* if conversion stopped at the first character, return 0.0 */
  1063. if (errPtr == get_string_component(jso))
  1064. {
  1065. errno = EINVAL;
  1066. return 0.0;
  1067. }
  1068. /*
  1069. * Check that the conversion terminated on something sensible
  1070. *
  1071. * For example, { "pay" : 123AB } would parse as 123.
  1072. */
  1073. if (*errPtr != '\0')
  1074. {
  1075. errno = EINVAL;
  1076. return 0.0;
  1077. }
  1078. /*
  1079. * If strtod encounters a string which would exceed the
  1080. * capacity of a double, it returns +/- HUGE_VAL and sets
  1081. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  1082. * from a conversion, so we need to check errno.
  1083. *
  1084. * Underflow also sets errno to ERANGE, but it returns 0 in
  1085. * that case, which is what we will return anyway.
  1086. *
  1087. * See CERT guideline ERR30-C
  1088. */
  1089. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno))
  1090. cdouble = 0.0;
  1091. return cdouble;
  1092. default: errno = EINVAL; return 0.0;
  1093. }
  1094. }
  1095. int json_object_set_double(struct json_object *jso, double new_value)
  1096. {
  1097. if (!jso || jso->o_type != json_type_double)
  1098. return 0;
  1099. JC_DOUBLE(jso)->c_double = new_value;
  1100. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  1101. json_object_set_serializer(jso, NULL, NULL, NULL);
  1102. return 1;
  1103. }
  1104. /* json_object_string */
  1105. static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb,
  1106. int level, int flags)
  1107. {
  1108. ssize_t len = JC_STRING(jso)->len;
  1109. printbuf_strappend(pb, "\"");
  1110. json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags);
  1111. printbuf_strappend(pb, "\"");
  1112. return 0;
  1113. }
  1114. static void json_object_string_delete(struct json_object *jso)
  1115. {
  1116. if (JC_STRING(jso)->len < 0)
  1117. free(JC_STRING(jso)->c_string.pdata);
  1118. json_object_generic_delete(jso);
  1119. }
  1120. static struct json_object *_json_object_new_string(const char *s, const size_t len)
  1121. {
  1122. size_t objsize;
  1123. struct json_object_string *jso;
  1124. /*
  1125. * Structures Actual memory layout
  1126. * ------------------- --------------------
  1127. * [json_object_string [json_object_string
  1128. * [json_object] [json_object]
  1129. * ...other fields... ...other fields...
  1130. * c_string] len
  1131. * bytes
  1132. * of
  1133. * string
  1134. * data
  1135. * \0]
  1136. */
  1137. if (len > (SSIZE_T_MAX - (sizeof(*jso) - sizeof(jso->c_string)) - 1))
  1138. return NULL;
  1139. objsize = (sizeof(*jso) - sizeof(jso->c_string)) + len + 1;
  1140. if (len < sizeof(void *))
  1141. // We need a minimum size to support json_object_set_string() mutability
  1142. // so we can stuff a pointer into pdata :(
  1143. objsize += sizeof(void *) - len;
  1144. jso = (struct json_object_string *)json_object_new(json_type_string, objsize,
  1145. &json_object_string_to_json_string);
  1146. if (!jso)
  1147. return NULL;
  1148. jso->len = len;
  1149. memcpy(jso->c_string.idata, s, len);
  1150. // Cast below needed for Clang UB sanitizer
  1151. ((char *)jso->c_string.idata)[len] = '\0';
  1152. return &jso->base;
  1153. }
  1154. struct json_object *json_object_new_string(const char *s)
  1155. {
  1156. return _json_object_new_string(s, strlen(s));
  1157. }
  1158. struct json_object *json_object_new_string_len(const char *s, const int len)
  1159. {
  1160. return _json_object_new_string(s, len);
  1161. }
  1162. const char *json_object_get_string(struct json_object *jso)
  1163. {
  1164. if (!jso)
  1165. return NULL;
  1166. switch (jso->o_type)
  1167. {
  1168. case json_type_string: return get_string_component(jso);
  1169. default: return json_object_to_json_string(jso);
  1170. }
  1171. }
  1172. static inline ssize_t _json_object_get_string_len(const struct json_object_string *jso)
  1173. {
  1174. ssize_t len;
  1175. len = jso->len;
  1176. return (len < 0) ? -(ssize_t)len : len;
  1177. }
  1178. int json_object_get_string_len(const struct json_object *jso)
  1179. {
  1180. if (!jso)
  1181. return 0;
  1182. switch (jso->o_type)
  1183. {
  1184. case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso));
  1185. default: return 0;
  1186. }
  1187. }
  1188. static int _json_object_set_string_len(json_object *jso, const char *s, size_t len)
  1189. {
  1190. char *dstbuf;
  1191. ssize_t curlen;
  1192. ssize_t newlen;
  1193. if (jso == NULL || jso->o_type != json_type_string)
  1194. return 0;
  1195. if (len >= INT_MAX - 1)
  1196. // jso->len is a signed ssize_t, so it can't hold the
  1197. // full size_t range. json_object_get_string_len returns
  1198. // length as int, cap length at INT_MAX.
  1199. return 0;
  1200. curlen = JC_STRING(jso)->len;
  1201. if (curlen < 0) {
  1202. if (len == 0) {
  1203. free(JC_STRING(jso)->c_string.pdata);
  1204. JC_STRING(jso)->len = curlen = 0;
  1205. } else {
  1206. curlen = -curlen;
  1207. }
  1208. }
  1209. newlen = len;
  1210. dstbuf = get_string_component_mutable(jso);
  1211. if ((ssize_t)len > curlen)
  1212. {
  1213. // We have no way to return the new ptr from realloc(jso, newlen)
  1214. // and we have no way of knowing whether there's extra room available
  1215. // so we need to stuff a pointer in to pdata :(
  1216. dstbuf = (char *)malloc(len + 1);
  1217. if (dstbuf == NULL)
  1218. return 0;
  1219. if (JC_STRING(jso)->len < 0)
  1220. free(JC_STRING(jso)->c_string.pdata);
  1221. JC_STRING(jso)->c_string.pdata = dstbuf;
  1222. newlen = -(ssize_t)len;
  1223. }
  1224. else if (JC_STRING(jso)->len < 0)
  1225. {
  1226. // We've got enough room in the separate allocated buffer,
  1227. // so use it as-is and continue to indicate that pdata is used.
  1228. newlen = -(ssize_t)len;
  1229. }
  1230. memcpy(dstbuf, (const void *)s, len);
  1231. dstbuf[len] = '\0';
  1232. JC_STRING(jso)->len = newlen;
  1233. return 1;
  1234. }
  1235. int json_object_set_string(json_object *jso, const char *s)
  1236. {
  1237. return _json_object_set_string_len(jso, s, strlen(s));
  1238. }
  1239. int json_object_set_string_len(json_object *jso, const char *s, int len)
  1240. {
  1241. return _json_object_set_string_len(jso, s, len);
  1242. }
  1243. /* json_object_array */
  1244. static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
  1245. int flags)
  1246. {
  1247. int had_children = 0;
  1248. size_t ii;
  1249. int compact_array_reset_flag = 0;
  1250. int compact_array_elements_on_line = 0;
  1251. const int COMPACT_ARRAY_ELEMENTS_PER_LINE_MAX = 15;
  1252. printbuf_strappend(pb, "[");
  1253. for (ii = 0; ii < json_object_array_length(jso); ii++)
  1254. {
  1255. struct json_object *val;
  1256. val = json_object_array_get_idx(jso, ii);
  1257. json_type val_type = json_object_get_type(val);
  1258. if (had_children)
  1259. {
  1260. printbuf_strappend(pb, ",");
  1261. }
  1262. /* Do not compactly print arrays with elements that are arrays, strings or objects. */
  1263. if ((flags & JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY) || compact_array_reset_flag)
  1264. {
  1265. if (compact_array_reset_flag)
  1266. {
  1267. flags = JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY;
  1268. compact_array_reset_flag = 0;
  1269. }
  1270. if ((val_type == json_type_array) || (val_type == json_type_string) || (val_type == json_type_object))
  1271. {
  1272. flags = JSON_C_TO_STRING_PRETTY;
  1273. compact_array_reset_flag = 1;
  1274. }
  1275. }
  1276. if (flags & JSON_C_TO_STRING_PRETTY)
  1277. printbuf_strappend(pb, "\n");
  1278. if (flags & JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY)
  1279. {
  1280. if (ii == 0 || compact_array_elements_on_line >= COMPACT_ARRAY_ELEMENTS_PER_LINE_MAX)
  1281. {
  1282. printbuf_strappend(pb, "\n");
  1283. indent(pb, level + 1, flags);
  1284. compact_array_elements_on_line = 0;
  1285. }
  1286. else
  1287. {
  1288. printbuf_strappend(pb, " ");
  1289. compact_array_elements_on_line++;
  1290. }
  1291. }
  1292. had_children = 1;
  1293. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1294. printbuf_strappend(pb, " ");
  1295. if (!(flags & JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY))
  1296. indent(pb, level + 1, flags);
  1297. if (val == NULL)
  1298. printbuf_strappend(pb, "null");
  1299. else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
  1300. return -1;
  1301. }
  1302. if ((flags & (JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_PRETTY_COMPACT_ARRAY)) && had_children)
  1303. {
  1304. printbuf_strappend(pb, "\n");
  1305. indent(pb, level, flags);
  1306. }
  1307. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  1308. return printbuf_strappend(pb, " ]");
  1309. return printbuf_strappend(pb, "]");
  1310. }
  1311. static void json_object_array_entry_free(void *data)
  1312. {
  1313. json_object_put((struct json_object *)data);
  1314. }
  1315. static void json_object_array_delete(struct json_object *jso)
  1316. {
  1317. array_list_free(JC_ARRAY(jso)->c_array);
  1318. json_object_generic_delete(jso);
  1319. }
  1320. struct json_object *json_object_new_array(void)
  1321. {
  1322. return json_object_new_array_ext(ARRAY_LIST_DEFAULT_SIZE);
  1323. }
  1324. struct json_object *json_object_new_array_ext(int initial_size)
  1325. {
  1326. struct json_object_array *jso = JSON_OBJECT_NEW(array);
  1327. if (!jso)
  1328. return NULL;
  1329. jso->c_array = array_list_new2(&json_object_array_entry_free, initial_size);
  1330. if (jso->c_array == NULL)
  1331. {
  1332. free(jso);
  1333. return NULL;
  1334. }
  1335. return &jso->base;
  1336. }
  1337. struct array_list *json_object_get_array(const struct json_object *jso)
  1338. {
  1339. if (!jso)
  1340. return NULL;
  1341. switch (jso->o_type)
  1342. {
  1343. case json_type_array: return JC_ARRAY_C(jso)->c_array;
  1344. default: return NULL;
  1345. }
  1346. }
  1347. void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *))
  1348. {
  1349. assert(json_object_get_type(jso) == json_type_array);
  1350. array_list_sort(JC_ARRAY(jso)->c_array, sort_fn);
  1351. }
  1352. struct json_object *json_object_array_bsearch(const struct json_object *key,
  1353. const struct json_object *jso,
  1354. int (*sort_fn)(const void *, const void *))
  1355. {
  1356. struct json_object **result;
  1357. assert(json_object_get_type(jso) == json_type_array);
  1358. result = (struct json_object **)array_list_bsearch((const void **)(void *)&key,
  1359. JC_ARRAY_C(jso)->c_array, sort_fn);
  1360. if (!result)
  1361. return NULL;
  1362. return *result;
  1363. }
  1364. size_t json_object_array_length(const struct json_object *jso)
  1365. {
  1366. assert(json_object_get_type(jso) == json_type_array);
  1367. return array_list_length(JC_ARRAY_C(jso)->c_array);
  1368. }
  1369. int json_object_array_add(struct json_object *jso, struct json_object *val)
  1370. {
  1371. assert(json_object_get_type(jso) == json_type_array);
  1372. return array_list_add(JC_ARRAY(jso)->c_array, val);
  1373. }
  1374. int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
  1375. {
  1376. assert(json_object_get_type(jso) == json_type_array);
  1377. return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val);
  1378. }
  1379. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1380. {
  1381. assert(json_object_get_type(jso) == json_type_array);
  1382. return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count);
  1383. }
  1384. struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx)
  1385. {
  1386. assert(json_object_get_type(jso) == json_type_array);
  1387. return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx);
  1388. }
  1389. static int json_array_equal(struct json_object *jso1, struct json_object *jso2)
  1390. {
  1391. size_t len, i;
  1392. len = json_object_array_length(jso1);
  1393. if (len != json_object_array_length(jso2))
  1394. return 0;
  1395. for (i = 0; i < len; i++)
  1396. {
  1397. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1398. json_object_array_get_idx(jso2, i)))
  1399. return 0;
  1400. }
  1401. return 1;
  1402. }
  1403. int json_object_array_shrink(struct json_object *jso, int empty_slots)
  1404. {
  1405. if (empty_slots < 0)
  1406. json_abort("json_object_array_shrink called with negative empty_slots");
  1407. return array_list_shrink(JC_ARRAY(jso)->c_array, empty_slots);
  1408. }
  1409. struct json_object *json_object_new_null(void)
  1410. {
  1411. return NULL;
  1412. }
  1413. static int json_object_all_values_equal(struct json_object *jso1, struct json_object *jso2)
  1414. {
  1415. struct json_object_iter iter;
  1416. struct json_object *sub;
  1417. assert(json_object_get_type(jso1) == json_type_object);
  1418. assert(json_object_get_type(jso2) == json_type_object);
  1419. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1420. json_object_object_foreachC(jso1, iter)
  1421. {
  1422. if (!lh_table_lookup_ex(JC_OBJECT(jso2)->c_object, (void *)iter.key,
  1423. (void **)(void *)&sub))
  1424. return 0;
  1425. if (!json_object_equal(iter.val, sub))
  1426. return 0;
  1427. }
  1428. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1429. json_object_object_foreachC(jso2, iter)
  1430. {
  1431. if (!lh_table_lookup_ex(JC_OBJECT(jso1)->c_object, (void *)iter.key,
  1432. (void **)(void *)&sub))
  1433. return 0;
  1434. }
  1435. return 1;
  1436. }
  1437. int json_object_equal(struct json_object *jso1, struct json_object *jso2)
  1438. {
  1439. if (jso1 == jso2)
  1440. return 1;
  1441. if (!jso1 || !jso2)
  1442. return 0;
  1443. if (jso1->o_type != jso2->o_type)
  1444. return 0;
  1445. switch (jso1->o_type)
  1446. {
  1447. case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean);
  1448. case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double);
  1449. case json_type_int:
  1450. {
  1451. struct json_object_int *int1 = JC_INT(jso1);
  1452. struct json_object_int *int2 = JC_INT(jso2);
  1453. if (int1->cint_type == json_object_int_type_int64)
  1454. {
  1455. if (int2->cint_type == json_object_int_type_int64)
  1456. return (int1->cint.c_int64 == int2->cint.c_int64);
  1457. if (int1->cint.c_int64 < 0)
  1458. return 0;
  1459. return ((uint64_t)int1->cint.c_int64 == int2->cint.c_uint64);
  1460. }
  1461. // else jso1 is a uint64
  1462. if (int2->cint_type == json_object_int_type_uint64)
  1463. return (int1->cint.c_uint64 == int2->cint.c_uint64);
  1464. if (int2->cint.c_int64 < 0)
  1465. return 0;
  1466. return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64);
  1467. }
  1468. case json_type_string:
  1469. {
  1470. return (_json_object_get_string_len(JC_STRING(jso1)) ==
  1471. _json_object_get_string_len(JC_STRING(jso2)) &&
  1472. memcmp(get_string_component(jso1), get_string_component(jso2),
  1473. _json_object_get_string_len(JC_STRING(jso1))) == 0);
  1474. }
  1475. case json_type_object: return json_object_all_values_equal(jso1, jso2);
  1476. case json_type_array: return json_array_equal(jso1, jso2);
  1477. case json_type_null: return 1;
  1478. };
  1479. return 0;
  1480. }
  1481. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1482. {
  1483. if (!src->_userdata && !src->_user_delete)
  1484. return 0;
  1485. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1486. dst->_to_json_string == _json_object_userdata_to_json_string)
  1487. {
  1488. char *p;
  1489. assert(src->_userdata);
  1490. p = strdup(src->_userdata);
  1491. if (p == NULL)
  1492. {
  1493. _json_c_set_last_err("json_object_copy_serializer_data: out of memory\n");
  1494. return -1;
  1495. }
  1496. dst->_userdata = p;
  1497. }
  1498. // else if ... other supported serializers ...
  1499. else
  1500. {
  1501. _json_c_set_last_err(
  1502. "json_object_copy_serializer_data: unable to copy unknown serializer data: "
  1503. "%p\n", (void *)dst->_to_json_string);
  1504. return -1;
  1505. }
  1506. dst->_user_delete = src->_user_delete;
  1507. return 0;
  1508. }
  1509. /**
  1510. * The default shallow copy implementation. Simply creates a new object of the same
  1511. * type but does *not* copy over _userdata nor retain any custom serializer.
  1512. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1513. * implementation that is aware of how to copy them.
  1514. *
  1515. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1516. */
  1517. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
  1518. size_t index, json_object **dst)
  1519. {
  1520. switch (src->o_type)
  1521. {
  1522. case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break;
  1523. case json_type_double: *dst = json_object_new_double(JC_DOUBLE(src)->c_double); break;
  1524. case json_type_int:
  1525. switch (JC_INT(src)->cint_type)
  1526. {
  1527. case json_object_int_type_int64:
  1528. *dst = json_object_new_int64(JC_INT(src)->cint.c_int64);
  1529. break;
  1530. case json_object_int_type_uint64:
  1531. *dst = json_object_new_uint64(JC_INT(src)->cint.c_uint64);
  1532. break;
  1533. default: json_abort("invalid cint_type");
  1534. }
  1535. break;
  1536. case json_type_string:
  1537. *dst = json_object_new_string_len(get_string_component(src),
  1538. _json_object_get_string_len(JC_STRING(src)));
  1539. break;
  1540. case json_type_object: *dst = json_object_new_object(); break;
  1541. case json_type_array: *dst = json_object_new_array(); break;
  1542. default: errno = EINVAL; return -1;
  1543. }
  1544. if (!*dst)
  1545. {
  1546. errno = ENOMEM;
  1547. return -1;
  1548. }
  1549. (*dst)->_to_json_string = src->_to_json_string;
  1550. // _userdata and _user_delete are copied later
  1551. return 1;
  1552. }
  1553. /*
  1554. * The actual guts of json_object_deep_copy(), with a few additional args
  1555. * needed so we can keep track of where we are within the object tree.
  1556. *
  1557. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1558. */
  1559. static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent,
  1560. const char *key_in_parent, size_t index_in_parent,
  1561. struct json_object **dst,
  1562. json_c_shallow_copy_fn *shallow_copy)
  1563. {
  1564. struct json_object_iter iter;
  1565. size_t src_array_len, ii;
  1566. int shallow_copy_rc = 0;
  1567. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1568. /* -1=error, 1=object created ok, 2=userdata set */
  1569. if (shallow_copy_rc < 1)
  1570. {
  1571. errno = EINVAL;
  1572. return -1;
  1573. }
  1574. assert(*dst != NULL);
  1575. switch (src->o_type)
  1576. {
  1577. case json_type_object:
  1578. json_object_object_foreachC(src, iter)
  1579. {
  1580. struct json_object *jso = NULL;
  1581. /* This handles the `json_type_null` case */
  1582. if (!iter.val)
  1583. jso = NULL;
  1584. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, UINT_MAX,
  1585. &jso, shallow_copy) < 0)
  1586. {
  1587. json_object_put(jso);
  1588. return -1;
  1589. }
  1590. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1591. {
  1592. json_object_put(jso);
  1593. return -1;
  1594. }
  1595. }
  1596. break;
  1597. case json_type_array:
  1598. src_array_len = json_object_array_length(src);
  1599. for (ii = 0; ii < src_array_len; ii++)
  1600. {
  1601. struct json_object *jso = NULL;
  1602. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1603. /* This handles the `json_type_null` case */
  1604. if (!jso1)
  1605. jso = NULL;
  1606. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso,
  1607. shallow_copy) < 0)
  1608. {
  1609. json_object_put(jso);
  1610. return -1;
  1611. }
  1612. if (json_object_array_add(*dst, jso) < 0)
  1613. {
  1614. json_object_put(jso);
  1615. return -1;
  1616. }
  1617. }
  1618. break;
  1619. default:
  1620. break;
  1621. /* else, nothing to do, shallow_copy already did. */
  1622. }
  1623. if (shallow_copy_rc != 2)
  1624. return json_object_copy_serializer_data(src, *dst);
  1625. return 0;
  1626. }
  1627. int json_object_deep_copy(struct json_object *src, struct json_object **dst,
  1628. json_c_shallow_copy_fn *shallow_copy)
  1629. {
  1630. int rc;
  1631. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1632. if (!src || !dst || *dst)
  1633. {
  1634. errno = EINVAL;
  1635. return -1;
  1636. }
  1637. if (shallow_copy == NULL)
  1638. shallow_copy = json_c_shallow_copy_default;
  1639. rc = json_object_deep_copy_recursive(src, NULL, NULL, UINT_MAX, dst, shallow_copy);
  1640. if (rc < 0)
  1641. {
  1642. json_object_put(*dst);
  1643. *dst = NULL;
  1644. }
  1645. return rc;
  1646. }
  1647. static void json_abort(const char *message)
  1648. {
  1649. if (message != NULL)
  1650. fprintf(stderr, "json-c aborts with error: %s\n", message);
  1651. abort();
  1652. }