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