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