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

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