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

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