You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 48 kB

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