cJSON.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. /* cJSON */
  20. /* JSON parser in C. */
  21. /* disable warnings about old C89 functions in MSVC */
  22. #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
  23. #define _CRT_SECURE_NO_DEPRECATE
  24. #endif
  25. #ifdef __GNUC__
  26. #pragma GCC visibility push(default)
  27. #endif
  28. #if defined(_MSC_VER)
  29. #pragma warning (push)
  30. /* disable warning about single line comments in system headers */
  31. #pragma warning (disable : 4001)
  32. #endif
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <math.h>
  36. #include <stdlib.h>
  37. #include <limits.h>
  38. #include <ctype.h>
  39. #include <float.h>
  40. #ifdef ENABLE_LOCALES
  41. #include <locale.h>
  42. #endif
  43. #if defined(_MSC_VER)
  44. #pragma warning (pop)
  45. #endif
  46. #ifdef __GNUC__
  47. #pragma GCC visibility pop
  48. #endif
  49. #include <aws/common/external/cJSON.h>
  50. /* define our own boolean type */
  51. #ifdef true
  52. #undef true
  53. #endif
  54. #define true ((cJSON_bool)1)
  55. #ifdef false
  56. #undef false
  57. #endif
  58. #define false ((cJSON_bool)0)
  59. /* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */
  60. #ifndef isinf
  61. #define isinf(d) (isnan((d - d)) && !isnan(d))
  62. #endif
  63. #ifndef isnan
  64. #define isnan(d) (d != d)
  65. #endif
  66. #ifndef NAN
  67. #ifdef _WIN32
  68. #define NAN sqrt(-1.0)
  69. #else
  70. #define NAN 0.0/0.0
  71. #endif
  72. #endif
  73. typedef struct {
  74. const unsigned char *json;
  75. size_t position;
  76. } error;
  77. CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item)
  78. {
  79. if (!cJSON_IsString(item))
  80. {
  81. return NULL;
  82. }
  83. return item->valuestring;
  84. }
  85. CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
  86. {
  87. if (!cJSON_IsNumber(item))
  88. {
  89. return (double) NAN;
  90. }
  91. return item->valuedouble;
  92. }
  93. /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
  94. #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 15)
  95. #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
  96. #endif
  97. CJSON_PUBLIC(const char*) cJSON_Version(void)
  98. {
  99. static char version[15];
  100. snprintf(version, sizeof(version) / sizeof(char), "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
  101. return version;
  102. }
  103. /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
  104. static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
  105. {
  106. if ((string1 == NULL) || (string2 == NULL))
  107. {
  108. return 1;
  109. }
  110. if (string1 == string2)
  111. {
  112. return 0;
  113. }
  114. for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
  115. {
  116. if (*string1 == '\0')
  117. {
  118. return 0;
  119. }
  120. }
  121. return tolower(*string1) - tolower(*string2);
  122. }
  123. typedef struct internal_hooks
  124. {
  125. void *(CJSON_CDECL *allocate)(size_t size);
  126. void (CJSON_CDECL *deallocate)(void *pointer);
  127. void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
  128. } internal_hooks;
  129. #if defined(_MSC_VER)
  130. /* work around MSVC error C2322: '...' address of dllimport '...' is not static */
  131. static void * CJSON_CDECL internal_malloc(size_t size)
  132. {
  133. return malloc(size);
  134. }
  135. static void CJSON_CDECL internal_free(void *pointer)
  136. {
  137. free(pointer);
  138. }
  139. static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
  140. {
  141. return realloc(pointer, size);
  142. }
  143. #else
  144. #define internal_malloc malloc
  145. #define internal_free free
  146. #define internal_realloc realloc
  147. #endif
  148. /* strlen of character literals resolved at compile time */
  149. #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
  150. static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; // NOLINT
  151. static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) // NOLINT
  152. {
  153. size_t length = 0;
  154. unsigned char *copy = NULL;
  155. if (string == NULL)
  156. {
  157. return NULL;
  158. }
  159. length = strlen((const char*)string) + sizeof("");
  160. copy = (unsigned char*)hooks->allocate(length);
  161. if (copy == NULL)
  162. {
  163. return NULL;
  164. }
  165. memcpy(copy, string, length);
  166. return copy;
  167. }
  168. CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) // NOLINT
  169. {
  170. if (hooks == NULL)
  171. {
  172. /* Reset hooks */
  173. global_hooks.allocate = malloc;
  174. global_hooks.deallocate = free;
  175. global_hooks.reallocate = realloc;
  176. return;
  177. }
  178. global_hooks.allocate = malloc;
  179. if (hooks->malloc_fn != NULL)
  180. {
  181. global_hooks.allocate = hooks->malloc_fn;
  182. }
  183. global_hooks.deallocate = free;
  184. if (hooks->free_fn != NULL)
  185. {
  186. global_hooks.deallocate = hooks->free_fn;
  187. }
  188. /* use realloc only if both free and malloc are used */
  189. global_hooks.reallocate = NULL;
  190. if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
  191. {
  192. global_hooks.reallocate = realloc;
  193. }
  194. }
  195. /* Internal constructor. */
  196. static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
  197. {
  198. cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
  199. if (node)
  200. {
  201. memset(node, '\0', sizeof(cJSON));
  202. }
  203. return node;
  204. }
  205. /* Delete a cJSON structure. */
  206. CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
  207. {
  208. cJSON *next = NULL;
  209. while (item != NULL)
  210. {
  211. next = item->next;
  212. if (!(item->type & cJSON_IsReference) && (item->child != NULL))
  213. {
  214. cJSON_Delete(item->child);
  215. }
  216. if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
  217. {
  218. global_hooks.deallocate(item->valuestring);
  219. }
  220. if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
  221. {
  222. global_hooks.deallocate(item->string);
  223. }
  224. global_hooks.deallocate(item);
  225. item = next;
  226. }
  227. }
  228. /* get the decimal point character of the current locale */
  229. static unsigned char get_decimal_point(void)
  230. {
  231. #ifdef ENABLE_LOCALES
  232. struct lconv *lconv = localeconv();
  233. return (unsigned char) lconv->decimal_point[0];
  234. #else
  235. return '.';
  236. #endif
  237. }
  238. typedef struct
  239. {
  240. const unsigned char *content;
  241. size_t length;
  242. size_t offset;
  243. size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
  244. internal_hooks hooks;
  245. } parse_buffer;
  246. /* check if the given size is left to read in a given parse buffer (starting with 1) */
  247. #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) // NOLINT
  248. /* check if the buffer can be accessed at the given index (starting with 0) */
  249. #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) // NOLINT
  250. #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
  251. /* get a pointer to the buffer at the position */
  252. #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
  253. /* Parse the input text to generate a number, and populate the result into item. */
  254. static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) // NOLINT
  255. {
  256. double number = 0;
  257. unsigned char *after_end = NULL;
  258. unsigned char number_c_string[64];
  259. unsigned char decimal_point = get_decimal_point(); // NOLINT
  260. size_t i = 0;
  261. if ((input_buffer == NULL) || (input_buffer->content == NULL))
  262. {
  263. return false; // NOLINT
  264. }
  265. /* copy the number into a temporary buffer and replace '.' with the decimal point
  266. * of the current locale (for strtod)
  267. * This also takes care of '\0' not necessarily being available for marking the end of the input */
  268. for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
  269. {
  270. switch (buffer_at_offset(input_buffer)[i])
  271. {
  272. case '0':
  273. case '1':
  274. case '2':
  275. case '3':
  276. case '4':
  277. case '5':
  278. case '6':
  279. case '7':
  280. case '8':
  281. case '9':
  282. case '+':
  283. case '-':
  284. case 'e':
  285. case 'E':
  286. number_c_string[i] = buffer_at_offset(input_buffer)[i];
  287. break;
  288. case '.':
  289. number_c_string[i] = decimal_point;
  290. break;
  291. default:
  292. goto loop_end;
  293. }
  294. }
  295. loop_end:
  296. number_c_string[i] = '\0';
  297. number = strtod((const char*)number_c_string, (char**)&after_end);
  298. if (number_c_string == after_end)
  299. {
  300. return false; /* parse_error */ // NOLINT
  301. }
  302. item->valuedouble = number;
  303. /* use saturation in case of overflow */
  304. if (number >= INT_MAX)
  305. { // NOLINT
  306. item->valueint = INT_MAX;
  307. }
  308. else if (number <= (double)INT_MIN)
  309. {
  310. item->valueint = INT_MIN;
  311. }
  312. else
  313. {
  314. item->valueint = (int)number;
  315. }
  316. item->type = cJSON_Number; // NOLINT
  317. input_buffer->offset += (size_t)(after_end - number_c_string);
  318. return true; // NOLINT
  319. }
  320. /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
  321. CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) // NOLINT
  322. {
  323. if (number >= INT_MAX)
  324. {
  325. object->valueint = INT_MAX;
  326. }
  327. else if (number <= (double)INT_MIN)
  328. {
  329. object->valueint = INT_MIN;
  330. }
  331. else
  332. {
  333. object->valueint = (int)number;
  334. }
  335. return object->valuedouble = number;
  336. }
  337. CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
  338. {
  339. char *copy = NULL;
  340. /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
  341. if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference))
  342. {
  343. return NULL;
  344. }
  345. if (strlen(valuestring) <= strlen(object->valuestring))
  346. {
  347. size_t value_length = strlen(valuestring) + sizeof("");
  348. memcpy(object->valuestring, valuestring, value_length);
  349. return object->valuestring;
  350. }
  351. copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
  352. if (copy == NULL)
  353. {
  354. return NULL;
  355. }
  356. if (object->valuestring != NULL)
  357. {
  358. cJSON_free(object->valuestring);
  359. }
  360. object->valuestring = copy;
  361. return copy;
  362. }
  363. typedef struct
  364. {
  365. unsigned char *buffer;
  366. size_t length;
  367. size_t offset;
  368. size_t depth; /* current nesting depth (for formatted printing) */
  369. cJSON_bool noalloc;
  370. cJSON_bool format; /* is this print a formatted print */
  371. internal_hooks hooks;
  372. } printbuffer;
  373. /* realloc printbuffer if necessary to have at least "needed" bytes more */
  374. static unsigned char* ensure(printbuffer * const p, size_t needed) // NOLINT
  375. {
  376. unsigned char *newbuffer = NULL;
  377. size_t newsize = 0;
  378. if ((p == NULL) || (p->buffer == NULL))
  379. {
  380. return NULL;
  381. }
  382. if ((p->length > 0) && (p->offset >= p->length))
  383. {
  384. /* make sure that offset is valid */
  385. return NULL;
  386. }
  387. if (needed > INT_MAX)
  388. {
  389. /* sizes bigger than INT_MAX are currently not supported */
  390. return NULL;
  391. }
  392. needed += p->offset + 1;
  393. if (needed <= p->length)
  394. {
  395. return p->buffer + p->offset;
  396. }
  397. if (p->noalloc) {
  398. return NULL;
  399. }
  400. /* calculate new buffer size */
  401. if (needed > (INT_MAX / 2))
  402. {
  403. /* overflow of int, use INT_MAX if possible */
  404. if (needed <= INT_MAX)
  405. {
  406. newsize = INT_MAX;
  407. }
  408. else
  409. {
  410. return NULL;
  411. }
  412. }
  413. else
  414. {
  415. newsize = needed * 2;
  416. }
  417. if (p->hooks.reallocate != NULL)
  418. {
  419. /* reallocate with realloc if available */
  420. newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
  421. if (newbuffer == NULL)
  422. {
  423. p->hooks.deallocate(p->buffer);
  424. p->length = 0;
  425. p->buffer = NULL;
  426. return NULL;
  427. }
  428. }
  429. else
  430. {
  431. /* otherwise reallocate manually */
  432. newbuffer = (unsigned char*)p->hooks.allocate(newsize);
  433. if (!newbuffer)
  434. {
  435. p->hooks.deallocate(p->buffer);
  436. p->length = 0;
  437. p->buffer = NULL;
  438. return NULL;
  439. }
  440. memcpy(newbuffer, p->buffer, p->offset + 1);
  441. p->hooks.deallocate(p->buffer);
  442. }
  443. p->length = newsize;
  444. p->buffer = newbuffer;
  445. return newbuffer + p->offset;
  446. }
  447. /* calculate the new length of the string in a printbuffer and update the offset */
  448. static void update_offset(printbuffer * const buffer) // NOLINT
  449. {
  450. const unsigned char *buffer_pointer = NULL;
  451. if ((buffer == NULL) || (buffer->buffer == NULL))
  452. {
  453. return;
  454. }
  455. buffer_pointer = buffer->buffer + buffer->offset;
  456. buffer->offset += strlen((const char*)buffer_pointer);
  457. }
  458. /* securely comparison of floating-point variables */
  459. static cJSON_bool compare_double(double a, double b) // NOLINT
  460. {
  461. double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
  462. return (fabs(a - b) <= maxVal * DBL_EPSILON);
  463. }
  464. /* Render the number nicely from the given item into a string. */
  465. static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) // NOLINT
  466. {
  467. unsigned char *output_pointer = NULL;
  468. double d = item->valuedouble;
  469. int length = 0;
  470. size_t i = 0;
  471. unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */
  472. unsigned char decimal_point = get_decimal_point(); // NOLINT
  473. double test = 0.0;
  474. if (output_buffer == NULL)
  475. {
  476. return false;
  477. }
  478. /* This checks for NaN and Infinity */
  479. if (isnan(d) || isinf(d))
  480. {
  481. length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "null");
  482. }
  483. else
  484. {
  485. /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
  486. length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%1.15g", d);
  487. /* Check whether the original double can be recovered */
  488. if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
  489. {
  490. /* If not, print with 17 decimal places of precision */
  491. length = snprintf((char*)number_buffer, sizeof(number_buffer) / sizeof(char), "%1.17g", d);
  492. }
  493. }
  494. /* sprintf failed or buffer overrun occurred */
  495. if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
  496. {
  497. return false;
  498. }
  499. /* reserve appropriate space in the output */
  500. output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
  501. if (output_pointer == NULL)
  502. {
  503. return false;
  504. }
  505. /* copy the printed number to the output and replace locale
  506. * dependent decimal point with '.' */
  507. for (i = 0; i < ((size_t)length); i++)
  508. {
  509. if (number_buffer[i] == decimal_point)
  510. {
  511. output_pointer[i] = '.';
  512. continue;
  513. }
  514. output_pointer[i] = number_buffer[i];
  515. }
  516. output_pointer[i] = '\0';
  517. output_buffer->offset += (size_t)length;
  518. return true;
  519. }
  520. /* parse 4 digit hexadecimal number */
  521. static unsigned parse_hex4(const unsigned char * const input)
  522. {
  523. unsigned int h = 0;
  524. size_t i = 0;
  525. for (i = 0; i < 4; i++)
  526. {
  527. /* parse digit */
  528. if ((input[i] >= '0') && (input[i] <= '9'))
  529. {
  530. h += (unsigned int) input[i] - '0';
  531. }
  532. else if ((input[i] >= 'A') && (input[i] <= 'F'))
  533. {
  534. h += (unsigned int) 10 + input[i] - 'A';
  535. }
  536. else if ((input[i] >= 'a') && (input[i] <= 'f'))
  537. {
  538. h += (unsigned int) 10 + input[i] - 'a';
  539. }
  540. else /* invalid */
  541. {
  542. return 0;
  543. }
  544. if (i < 3)
  545. {
  546. /* shift left to make place for the next nibble */
  547. h = h << 4;
  548. }
  549. }
  550. return h;
  551. }
  552. /* converts a UTF-16 literal to UTF-8
  553. * A literal can be one or two sequences of the form \uXXXX */
  554. static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
  555. {
  556. long unsigned int codepoint = 0;
  557. unsigned int first_code = 0;
  558. const unsigned char *first_sequence = input_pointer;
  559. unsigned char utf8_length = 0;
  560. unsigned char utf8_position = 0;
  561. unsigned char sequence_length = 0;
  562. unsigned char first_byte_mark = 0;
  563. if ((input_end - first_sequence) < 6)
  564. {
  565. /* input ends unexpectedly */
  566. goto fail;
  567. }
  568. /* get the first utf16 sequence */
  569. first_code = parse_hex4(first_sequence + 2);
  570. /* check that the code is valid */
  571. if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
  572. {
  573. goto fail;
  574. }
  575. /* UTF16 surrogate pair */
  576. if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
  577. {
  578. const unsigned char *second_sequence = first_sequence + 6;
  579. unsigned int second_code = 0;
  580. sequence_length = 12; /* \uXXXX\uXXXX */
  581. if ((input_end - second_sequence) < 6)
  582. {
  583. /* input ends unexpectedly */
  584. goto fail;
  585. }
  586. if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
  587. {
  588. /* missing second half of the surrogate pair */
  589. goto fail;
  590. }
  591. /* get the second utf16 sequence */
  592. second_code = parse_hex4(second_sequence + 2);
  593. /* check that the code is valid */
  594. if ((second_code < 0xDC00) || (second_code > 0xDFFF))
  595. {
  596. /* invalid second half of the surrogate pair */
  597. goto fail;
  598. }
  599. /* calculate the unicode codepoint from the surrogate pair */
  600. codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
  601. }
  602. else
  603. {
  604. sequence_length = 6; /* \uXXXX */
  605. codepoint = first_code;
  606. }
  607. /* encode as UTF-8
  608. * takes at maximum 4 bytes to encode:
  609. * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
  610. if (codepoint < 0x80)
  611. {
  612. /* normal ascii, encoding 0xxxxxxx */
  613. utf8_length = 1;
  614. }
  615. else if (codepoint < 0x800)
  616. {
  617. /* two bytes, encoding 110xxxxx 10xxxxxx */
  618. utf8_length = 2;
  619. first_byte_mark = 0xC0; /* 11000000 */
  620. }
  621. else if (codepoint < 0x10000)
  622. {
  623. /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
  624. utf8_length = 3;
  625. first_byte_mark = 0xE0; /* 11100000 */
  626. }
  627. else if (codepoint <= 0x10FFFF)
  628. {
  629. /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
  630. utf8_length = 4;
  631. first_byte_mark = 0xF0; /* 11110000 */
  632. }
  633. else
  634. {
  635. /* invalid unicode codepoint */
  636. goto fail;
  637. }
  638. /* encode as utf8 */
  639. for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
  640. {
  641. /* 10xxxxxx */
  642. (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
  643. codepoint >>= 6;
  644. }
  645. /* encode first byte */
  646. if (utf8_length > 1)
  647. {
  648. (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
  649. }
  650. else
  651. {
  652. (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
  653. }
  654. *output_pointer += utf8_length;
  655. return sequence_length;
  656. fail:
  657. return 0;
  658. }
  659. /* Parse the input text into an unescaped cinput, and populate item. */
  660. static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) // NOLINT
  661. {
  662. const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
  663. const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
  664. unsigned char *output_pointer = NULL;
  665. unsigned char *output = NULL;
  666. /* not a string */
  667. if (buffer_at_offset(input_buffer)[0] != '\"')
  668. {
  669. goto fail;
  670. }
  671. {
  672. /* calculate approximate size of the output (overestimate) */
  673. size_t allocation_length = 0;
  674. size_t skipped_bytes = 0;
  675. while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
  676. {
  677. /* is escape sequence */
  678. if (input_end[0] == '\\')
  679. {
  680. if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
  681. {
  682. /* prevent buffer overflow when last input character is a backslash */
  683. goto fail;
  684. }
  685. skipped_bytes++;
  686. input_end++;
  687. }
  688. input_end++;
  689. }
  690. if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
  691. {
  692. goto fail; /* string ended unexpectedly */
  693. }
  694. /* This is at most how much we need for the output */
  695. allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
  696. output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
  697. if (output == NULL)
  698. {
  699. goto fail; /* allocation failure */
  700. }
  701. }
  702. output_pointer = output;
  703. /* loop through the string literal */
  704. while (input_pointer < input_end)
  705. {
  706. if (*input_pointer != '\\')
  707. {
  708. *output_pointer++ = *input_pointer++;
  709. }
  710. /* escape sequence */
  711. else
  712. {
  713. unsigned char sequence_length = 2;
  714. if ((input_end - input_pointer) < 1)
  715. {
  716. goto fail;
  717. }
  718. switch (input_pointer[1])
  719. {
  720. case 'b':
  721. *output_pointer++ = '\b';
  722. break;
  723. case 'f':
  724. *output_pointer++ = '\f';
  725. break;
  726. case 'n':
  727. *output_pointer++ = '\n';
  728. break;
  729. case 'r':
  730. *output_pointer++ = '\r';
  731. break;
  732. case 't':
  733. *output_pointer++ = '\t';
  734. break;
  735. case '\"':
  736. case '\\':
  737. case '/':
  738. *output_pointer++ = input_pointer[1];
  739. break;
  740. /* UTF-16 literal */
  741. case 'u':
  742. sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
  743. if (sequence_length == 0)
  744. {
  745. /* failed to convert UTF16-literal to UTF-8 */
  746. goto fail;
  747. }
  748. break;
  749. default:
  750. goto fail;
  751. }
  752. input_pointer += sequence_length;
  753. }
  754. }
  755. /* zero terminate the output */
  756. *output_pointer = '\0';
  757. item->type = cJSON_String;
  758. item->valuestring = (char*)output;
  759. input_buffer->offset = (size_t) (input_end - input_buffer->content);
  760. input_buffer->offset++;
  761. return true;
  762. fail:
  763. if (output != NULL)
  764. {
  765. input_buffer->hooks.deallocate(output);
  766. }
  767. if (input_pointer != NULL)
  768. {
  769. input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
  770. }
  771. return false;
  772. }
  773. /* Render the cstring provided to an escaped version that can be printed. */
  774. static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) // NOLINT
  775. {
  776. const unsigned char *input_pointer = NULL;
  777. unsigned char *output = NULL;
  778. unsigned char *output_pointer = NULL;
  779. size_t output_length = 0;
  780. /* numbers of additional characters needed for escaping */
  781. size_t escape_characters = 0;
  782. if (output_buffer == NULL)
  783. {
  784. return false;
  785. }
  786. /* empty string */
  787. if (input == NULL)
  788. {
  789. output = ensure(output_buffer, sizeof("\"\""));
  790. if (output == NULL)
  791. {
  792. return false;
  793. }
  794. memcpy(output, "\"\"", 3); /* NOLINT */
  795. return true;
  796. }
  797. /* set "flag" to 1 if something needs to be escaped */
  798. for (input_pointer = input; *input_pointer; input_pointer++)
  799. {
  800. switch (*input_pointer)
  801. {
  802. case '\"':
  803. case '\\':
  804. case '\b':
  805. case '\f':
  806. case '\n':
  807. case '\r':
  808. case '\t':
  809. /* one character escape sequence */
  810. escape_characters++;
  811. break;
  812. default:
  813. if (*input_pointer < 32)
  814. {
  815. /* UTF-16 escape sequence uXXXX */
  816. escape_characters += 5;
  817. }
  818. break;
  819. }
  820. }
  821. output_length = (size_t)(input_pointer - input) + escape_characters;
  822. output = ensure(output_buffer, output_length + sizeof("\"\""));
  823. if (output == NULL)
  824. {
  825. return false;
  826. }
  827. /* no characters have to be escaped */
  828. if (escape_characters == 0)
  829. {
  830. output[0] = '\"';
  831. memcpy(output + 1, input, output_length);
  832. output[output_length + 1] = '\"';
  833. output[output_length + 2] = '\0';
  834. return true;
  835. }
  836. output[0] = '\"';
  837. output_pointer = output + 1;
  838. /* copy the string */
  839. for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
  840. {
  841. if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
  842. {
  843. /* normal character, copy */
  844. *output_pointer = *input_pointer;
  845. }
  846. else
  847. {
  848. /* character needs to be escaped */
  849. *output_pointer++ = '\\';
  850. switch (*input_pointer)
  851. {
  852. case '\\':
  853. *output_pointer = '\\';
  854. break;
  855. case '\"':
  856. *output_pointer = '\"';
  857. break;
  858. case '\b':
  859. *output_pointer = 'b';
  860. break;
  861. case '\f':
  862. *output_pointer = 'f';
  863. break;
  864. case '\n':
  865. *output_pointer = 'n';
  866. break;
  867. case '\r':
  868. *output_pointer = 'r';
  869. break;
  870. case '\t':
  871. *output_pointer = 't';
  872. break;
  873. default:
  874. /* escape and print as unicode codepoint */
  875. snprintf((char*)output_pointer, 6 * sizeof(char), "u%04x", *input_pointer);
  876. output_pointer += 4;
  877. break;
  878. }
  879. }
  880. }
  881. output[output_length + 1] = '\"';
  882. output[output_length + 2] = '\0';
  883. return true;
  884. }
  885. /* Invoke print_string_ptr (which is useful) on an item. */
  886. static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
  887. {
  888. return print_string_ptr((unsigned char*)item->valuestring, p);
  889. }
  890. /* Predeclare these prototypes. */
  891. static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
  892. static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
  893. static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
  894. static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
  895. static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
  896. static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
  897. /* Utility to jump whitespace and cr/lf */
  898. static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) // NOLINT
  899. {
  900. if ((buffer == NULL) || (buffer->content == NULL))
  901. {
  902. return NULL;
  903. }
  904. if (cannot_access_at_index(buffer, 0))
  905. {
  906. return buffer;
  907. }
  908. while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
  909. {
  910. buffer->offset++;
  911. }
  912. if (buffer->offset == buffer->length)
  913. {
  914. buffer->offset--;
  915. }
  916. return buffer;
  917. }
  918. /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
  919. static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) // NOLINT
  920. {
  921. if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
  922. {
  923. return NULL;
  924. }
  925. if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
  926. {
  927. buffer->offset += 3;
  928. }
  929. return buffer;
  930. }
  931. CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
  932. {
  933. size_t buffer_length;
  934. if (NULL == value)
  935. {
  936. return NULL;
  937. }
  938. /* Adding null character size due to require_null_terminated. */
  939. buffer_length = strlen(value) + sizeof("");
  940. return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated);
  941. }
  942. /* Parse an object - create a new root, and populate. */
  943. CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated)
  944. {
  945. parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  946. cJSON *item = NULL;
  947. if (value == NULL || 0 == buffer_length)
  948. {
  949. goto fail;
  950. }
  951. buffer.content = (const unsigned char*)value;
  952. buffer.length = buffer_length;
  953. buffer.offset = 0;
  954. buffer.hooks = global_hooks;
  955. item = cJSON_New_Item(&global_hooks);
  956. if (item == NULL) /* memory fail */
  957. {
  958. goto fail;
  959. }
  960. if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
  961. {
  962. /* parse failure. ep is set. */
  963. goto fail;
  964. }
  965. /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
  966. if (require_null_terminated)
  967. {
  968. buffer_skip_whitespace(&buffer);
  969. if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
  970. {
  971. goto fail;
  972. }
  973. }
  974. if (return_parse_end)
  975. {
  976. *return_parse_end = (const char*)buffer_at_offset(&buffer);
  977. }
  978. return item;
  979. fail:
  980. if (item != NULL)
  981. {
  982. cJSON_Delete(item);
  983. }
  984. if (value != NULL)
  985. {
  986. error local_error;
  987. local_error.json = (const unsigned char*)value;
  988. local_error.position = 0;
  989. if (buffer.offset < buffer.length)
  990. {
  991. local_error.position = buffer.offset;
  992. }
  993. else if (buffer.length > 0)
  994. {
  995. local_error.position = buffer.length - 1;
  996. }
  997. if (return_parse_end != NULL)
  998. {
  999. *return_parse_end = (const char*)local_error.json + local_error.position;
  1000. }
  1001. }
  1002. return NULL;
  1003. }
  1004. /* Default options for cJSON_Parse */
  1005. CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
  1006. {
  1007. return cJSON_ParseWithOpts(value, 0, 0);
  1008. }
  1009. CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length)
  1010. {
  1011. return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0);
  1012. }
  1013. #define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
  1014. static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
  1015. {
  1016. static const size_t default_buffer_size = 256;
  1017. printbuffer buffer[1];
  1018. unsigned char *printed = NULL;
  1019. memset(buffer, 0, sizeof(buffer));
  1020. /* create buffer */
  1021. buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
  1022. buffer->length = default_buffer_size;
  1023. buffer->format = format;
  1024. buffer->hooks = *hooks;
  1025. if (buffer->buffer == NULL)
  1026. {
  1027. goto fail;
  1028. }
  1029. /* print the value */
  1030. if (!print_value(item, buffer))
  1031. {
  1032. goto fail;
  1033. }
  1034. update_offset(buffer);
  1035. /* check if reallocate is available */
  1036. if (hooks->reallocate != NULL)
  1037. {
  1038. printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
  1039. if (printed == NULL) {
  1040. goto fail;
  1041. }
  1042. buffer->buffer = NULL;
  1043. }
  1044. else /* otherwise copy the JSON over to a new buffer */
  1045. {
  1046. printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
  1047. if (printed == NULL)
  1048. {
  1049. goto fail;
  1050. }
  1051. memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
  1052. printed[buffer->offset] = '\0'; /* just to be sure */
  1053. /* free the buffer */
  1054. hooks->deallocate(buffer->buffer);
  1055. }
  1056. return printed;
  1057. fail:
  1058. if (buffer->buffer != NULL)
  1059. {
  1060. hooks->deallocate(buffer->buffer);
  1061. }
  1062. if (printed != NULL)
  1063. {
  1064. hooks->deallocate(printed);
  1065. }
  1066. return NULL;
  1067. }
  1068. /* Render a cJSON item/entity/structure to text. */
  1069. CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
  1070. {
  1071. return (char*)print(item, true, &global_hooks);
  1072. }
  1073. CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
  1074. {
  1075. return (char*)print(item, false, &global_hooks);
  1076. }
  1077. CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
  1078. {
  1079. printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
  1080. if (prebuffer < 0)
  1081. {
  1082. return NULL;
  1083. }
  1084. p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
  1085. if (!p.buffer)
  1086. {
  1087. return NULL;
  1088. }
  1089. p.length = (size_t)prebuffer;
  1090. p.offset = 0;
  1091. p.noalloc = false;
  1092. p.format = fmt;
  1093. p.hooks = global_hooks;
  1094. if (!print_value(item, &p))
  1095. {
  1096. global_hooks.deallocate(p.buffer);
  1097. return NULL;
  1098. }
  1099. return (char*)p.buffer;
  1100. }
  1101. CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
  1102. {
  1103. printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
  1104. if ((length < 0) || (buffer == NULL))
  1105. {
  1106. return false;
  1107. }
  1108. p.buffer = (unsigned char*)buffer;
  1109. p.length = (size_t)length;
  1110. p.offset = 0;
  1111. p.noalloc = true;
  1112. p.format = format;
  1113. p.hooks = global_hooks;
  1114. return print_value(item, &p);
  1115. }
  1116. /* Parser core - when encountering text, process appropriately. */
  1117. static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
  1118. {
  1119. if ((input_buffer == NULL) || (input_buffer->content == NULL))
  1120. {
  1121. return false; /* no input */
  1122. }
  1123. /* parse the different types of values */
  1124. /* null */
  1125. if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
  1126. {
  1127. item->type = cJSON_NULL;
  1128. input_buffer->offset += 4;
  1129. return true;
  1130. }
  1131. /* false */
  1132. if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
  1133. {
  1134. item->type = cJSON_False;
  1135. input_buffer->offset += 5;
  1136. return true;
  1137. }
  1138. /* true */
  1139. if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
  1140. {
  1141. item->type = cJSON_True;
  1142. item->valueint = 1;
  1143. input_buffer->offset += 4;
  1144. return true;
  1145. }
  1146. /* string */
  1147. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
  1148. {
  1149. return parse_string(item, input_buffer);
  1150. }
  1151. /* number */
  1152. if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
  1153. {
  1154. return parse_number(item, input_buffer);
  1155. }
  1156. /* array */
  1157. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
  1158. {
  1159. return parse_array(item, input_buffer);
  1160. }
  1161. /* object */
  1162. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
  1163. {
  1164. return parse_object(item, input_buffer);
  1165. }
  1166. return false;
  1167. }
  1168. /* Render a value to text. */
  1169. static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
  1170. {
  1171. unsigned char *output = NULL;
  1172. if ((item == NULL) || (output_buffer == NULL))
  1173. {
  1174. return false;
  1175. }
  1176. switch ((item->type) & 0xFF)
  1177. {
  1178. case cJSON_NULL:
  1179. output = ensure(output_buffer, 5);
  1180. if (output == NULL)
  1181. {
  1182. return false;
  1183. }
  1184. memcpy(output, "null", 5); /* NOLINT */
  1185. return true;
  1186. case cJSON_False:
  1187. output = ensure(output_buffer, 6);
  1188. if (output == NULL)
  1189. {
  1190. return false;
  1191. }
  1192. memcpy(output, "false", 6); /* NOLINT */
  1193. return true;
  1194. case cJSON_True:
  1195. output = ensure(output_buffer, 5);
  1196. if (output == NULL)
  1197. {
  1198. return false;
  1199. }
  1200. memcpy(output, "true", 5); /* NOLINT */
  1201. return true;
  1202. case cJSON_Number:
  1203. return print_number(item, output_buffer);
  1204. case cJSON_Raw:
  1205. {
  1206. size_t raw_length = 0;
  1207. if (item->valuestring == NULL)
  1208. {
  1209. return false;
  1210. }
  1211. raw_length = strlen(item->valuestring) + sizeof("");
  1212. output = ensure(output_buffer, raw_length);
  1213. if (output == NULL)
  1214. {
  1215. return false;
  1216. }
  1217. memcpy(output, item->valuestring, raw_length);
  1218. return true;
  1219. }
  1220. case cJSON_String:
  1221. return print_string(item, output_buffer);
  1222. case cJSON_Array:
  1223. return print_array(item, output_buffer);
  1224. case cJSON_Object:
  1225. return print_object(item, output_buffer);
  1226. default:
  1227. return false;
  1228. }
  1229. }
  1230. /* Build an array from input text. */
  1231. static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
  1232. {
  1233. cJSON *head = NULL; /* head of the linked list */
  1234. cJSON *current_item = NULL;
  1235. if (input_buffer->depth >= CJSON_NESTING_LIMIT)
  1236. {
  1237. return false; /* to deeply nested */
  1238. }
  1239. input_buffer->depth++;
  1240. if (buffer_at_offset(input_buffer)[0] != '[')
  1241. {
  1242. /* not an array */
  1243. goto fail;
  1244. }
  1245. input_buffer->offset++;
  1246. buffer_skip_whitespace(input_buffer);
  1247. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
  1248. {
  1249. /* empty array */
  1250. goto success;
  1251. }
  1252. /* check if we skipped to the end of the buffer */
  1253. if (cannot_access_at_index(input_buffer, 0))
  1254. {
  1255. input_buffer->offset--;
  1256. goto fail;
  1257. }
  1258. /* step back to character in front of the first element */
  1259. input_buffer->offset--;
  1260. /* loop through the comma separated array elements */
  1261. do
  1262. {
  1263. /* allocate next item */
  1264. cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
  1265. if (new_item == NULL)
  1266. {
  1267. goto fail; /* allocation failure */
  1268. }
  1269. /* attach next item to list */
  1270. if (head == NULL)
  1271. {
  1272. /* start the linked list */
  1273. current_item = head = new_item;
  1274. }
  1275. else
  1276. {
  1277. /* add to the end and advance */
  1278. current_item->next = new_item;
  1279. new_item->prev = current_item;
  1280. current_item = new_item;
  1281. }
  1282. /* parse next value */
  1283. input_buffer->offset++;
  1284. buffer_skip_whitespace(input_buffer);
  1285. if (!parse_value(current_item, input_buffer))
  1286. {
  1287. goto fail; /* failed to parse value */
  1288. }
  1289. buffer_skip_whitespace(input_buffer);
  1290. }
  1291. while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
  1292. if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
  1293. {
  1294. goto fail; /* expected end of array */
  1295. }
  1296. success:
  1297. input_buffer->depth--;
  1298. if (head != NULL) {
  1299. head->prev = current_item;
  1300. }
  1301. item->type = cJSON_Array;
  1302. item->child = head;
  1303. input_buffer->offset++;
  1304. return true;
  1305. fail:
  1306. if (head != NULL)
  1307. {
  1308. cJSON_Delete(head);
  1309. }
  1310. return false;
  1311. }
  1312. /* Render an array to text */
  1313. static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
  1314. {
  1315. unsigned char *output_pointer = NULL;
  1316. size_t length = 0;
  1317. cJSON *current_element = item->child;
  1318. if (output_buffer == NULL)
  1319. {
  1320. return false;
  1321. }
  1322. /* Compose the output array. */
  1323. /* opening square bracket */
  1324. output_pointer = ensure(output_buffer, 1);
  1325. if (output_pointer == NULL)
  1326. {
  1327. return false;
  1328. }
  1329. *output_pointer = '[';
  1330. output_buffer->offset++;
  1331. output_buffer->depth++;
  1332. while (current_element != NULL)
  1333. {
  1334. if (!print_value(current_element, output_buffer))
  1335. {
  1336. return false;
  1337. }
  1338. update_offset(output_buffer);
  1339. if (current_element->next)
  1340. {
  1341. length = (size_t) (output_buffer->format ? 2 : 1);
  1342. output_pointer = ensure(output_buffer, length + 1);
  1343. if (output_pointer == NULL)
  1344. {
  1345. return false;
  1346. }
  1347. *output_pointer++ = ',';
  1348. if(output_buffer->format)
  1349. {
  1350. *output_pointer++ = ' ';
  1351. }
  1352. *output_pointer = '\0';
  1353. output_buffer->offset += length;
  1354. }
  1355. current_element = current_element->next;
  1356. }
  1357. output_pointer = ensure(output_buffer, 2);
  1358. if (output_pointer == NULL)
  1359. {
  1360. return false;
  1361. }
  1362. *output_pointer++ = ']';
  1363. *output_pointer = '\0';
  1364. output_buffer->depth--;
  1365. return true;
  1366. }
  1367. /* Build an object from the text. */
  1368. static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
  1369. {
  1370. cJSON *head = NULL; /* linked list head */
  1371. cJSON *current_item = NULL;
  1372. if (input_buffer->depth >= CJSON_NESTING_LIMIT)
  1373. {
  1374. return false; /* to deeply nested */
  1375. }
  1376. input_buffer->depth++;
  1377. if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
  1378. {
  1379. goto fail; /* not an object */
  1380. }
  1381. input_buffer->offset++;
  1382. buffer_skip_whitespace(input_buffer);
  1383. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
  1384. {
  1385. goto success; /* empty object */
  1386. }
  1387. /* check if we skipped to the end of the buffer */
  1388. if (cannot_access_at_index(input_buffer, 0))
  1389. {
  1390. input_buffer->offset--;
  1391. goto fail;
  1392. }
  1393. /* step back to character in front of the first element */
  1394. input_buffer->offset--;
  1395. /* loop through the comma separated array elements */
  1396. do
  1397. {
  1398. /* allocate next item */
  1399. cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
  1400. if (new_item == NULL)
  1401. {
  1402. goto fail; /* allocation failure */
  1403. }
  1404. /* attach next item to list */
  1405. if (head == NULL)
  1406. {
  1407. /* start the linked list */
  1408. current_item = head = new_item;
  1409. }
  1410. else
  1411. {
  1412. /* add to the end and advance */
  1413. current_item->next = new_item;
  1414. new_item->prev = current_item;
  1415. current_item = new_item;
  1416. }
  1417. /* parse the name of the child */
  1418. input_buffer->offset++;
  1419. buffer_skip_whitespace(input_buffer);
  1420. if (!parse_string(current_item, input_buffer))
  1421. {
  1422. goto fail; /* failed to parse name */
  1423. }
  1424. buffer_skip_whitespace(input_buffer);
  1425. /* swap valuestring and string, because we parsed the name */
  1426. current_item->string = current_item->valuestring;
  1427. current_item->valuestring = NULL;
  1428. if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
  1429. {
  1430. goto fail; /* invalid object */
  1431. }
  1432. /* parse the value */
  1433. input_buffer->offset++;
  1434. buffer_skip_whitespace(input_buffer);
  1435. if (!parse_value(current_item, input_buffer))
  1436. {
  1437. goto fail; /* failed to parse value */
  1438. }
  1439. buffer_skip_whitespace(input_buffer);
  1440. }
  1441. while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
  1442. if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
  1443. {
  1444. goto fail; /* expected end of object */
  1445. }
  1446. success:
  1447. input_buffer->depth--;
  1448. if (head != NULL) {
  1449. head->prev = current_item;
  1450. }
  1451. item->type = cJSON_Object;
  1452. item->child = head;
  1453. input_buffer->offset++;
  1454. return true;
  1455. fail:
  1456. if (head != NULL)
  1457. {
  1458. cJSON_Delete(head);
  1459. }
  1460. return false;
  1461. }
  1462. /* Render an object to text. */
  1463. static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
  1464. {
  1465. unsigned char *output_pointer = NULL;
  1466. size_t length = 0;
  1467. cJSON *current_item = item->child;
  1468. if (output_buffer == NULL)
  1469. {
  1470. return false;
  1471. }
  1472. /* Compose the output: */
  1473. length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
  1474. output_pointer = ensure(output_buffer, length + 1);
  1475. if (output_pointer == NULL)
  1476. {
  1477. return false;
  1478. }
  1479. *output_pointer++ = '{';
  1480. output_buffer->depth++;
  1481. if (output_buffer->format)
  1482. {
  1483. *output_pointer++ = '\n';
  1484. }
  1485. output_buffer->offset += length;
  1486. while (current_item)
  1487. {
  1488. if (output_buffer->format)
  1489. {
  1490. size_t i;
  1491. output_pointer = ensure(output_buffer, output_buffer->depth);
  1492. if (output_pointer == NULL)
  1493. {
  1494. return false;
  1495. }
  1496. for (i = 0; i < output_buffer->depth; i++)
  1497. {
  1498. *output_pointer++ = '\t';
  1499. }
  1500. output_buffer->offset += output_buffer->depth;
  1501. }
  1502. /* print key */
  1503. if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
  1504. {
  1505. return false;
  1506. }
  1507. update_offset(output_buffer);
  1508. length = (size_t) (output_buffer->format ? 2 : 1);
  1509. output_pointer = ensure(output_buffer, length);
  1510. if (output_pointer == NULL)
  1511. {
  1512. return false;
  1513. }
  1514. *output_pointer++ = ':';
  1515. if (output_buffer->format)
  1516. {
  1517. *output_pointer++ = '\t';
  1518. }
  1519. output_buffer->offset += length;
  1520. /* print value */
  1521. if (!print_value(current_item, output_buffer))
  1522. {
  1523. return false;
  1524. }
  1525. update_offset(output_buffer);
  1526. /* print comma if not last */
  1527. length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0));
  1528. output_pointer = ensure(output_buffer, length + 1);
  1529. if (output_pointer == NULL)
  1530. {
  1531. return false;
  1532. }
  1533. if (current_item->next)
  1534. {
  1535. *output_pointer++ = ',';
  1536. }
  1537. if (output_buffer->format)
  1538. {
  1539. *output_pointer++ = '\n';
  1540. }
  1541. *output_pointer = '\0';
  1542. output_buffer->offset += length;
  1543. current_item = current_item->next;
  1544. }
  1545. output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
  1546. if (output_pointer == NULL)
  1547. {
  1548. return false;
  1549. }
  1550. if (output_buffer->format)
  1551. {
  1552. size_t i;
  1553. for (i = 0; i < (output_buffer->depth - 1); i++)
  1554. {
  1555. *output_pointer++ = '\t';
  1556. }
  1557. }
  1558. *output_pointer++ = '}';
  1559. *output_pointer = '\0';
  1560. output_buffer->depth--;
  1561. return true;
  1562. }
  1563. /* Get Array size/item / object item. */
  1564. CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
  1565. {
  1566. cJSON *child = NULL;
  1567. size_t size = 0;
  1568. if (array == NULL)
  1569. {
  1570. return 0;
  1571. }
  1572. child = array->child;
  1573. while(child != NULL)
  1574. {
  1575. size++;
  1576. child = child->next;
  1577. }
  1578. /* FIXME: Can overflow here. Cannot be fixed without breaking the API */
  1579. return (int)size;
  1580. }
  1581. static cJSON* get_array_item(const cJSON *array, size_t index)
  1582. {
  1583. cJSON *current_child = NULL;
  1584. if (array == NULL)
  1585. {
  1586. return NULL;
  1587. }
  1588. current_child = array->child;
  1589. while ((current_child != NULL) && (index > 0))
  1590. {
  1591. index--;
  1592. current_child = current_child->next;
  1593. }
  1594. return current_child;
  1595. }
  1596. CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
  1597. {
  1598. if (index < 0)
  1599. {
  1600. return NULL;
  1601. }
  1602. return get_array_item(array, (size_t)index);
  1603. }
  1604. static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
  1605. {
  1606. cJSON *current_element = NULL;
  1607. if ((object == NULL) || (name == NULL))
  1608. {
  1609. return NULL;
  1610. }
  1611. current_element = object->child;
  1612. if (case_sensitive)
  1613. {
  1614. while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
  1615. {
  1616. current_element = current_element->next;
  1617. }
  1618. }
  1619. else
  1620. {
  1621. while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
  1622. {
  1623. current_element = current_element->next;
  1624. }
  1625. }
  1626. if ((current_element == NULL) || (current_element->string == NULL)) {
  1627. return NULL;
  1628. }
  1629. return current_element;
  1630. }
  1631. CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
  1632. {
  1633. return get_object_item(object, string, false);
  1634. }
  1635. CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
  1636. {
  1637. return get_object_item(object, string, true);
  1638. }
  1639. CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
  1640. {
  1641. return cJSON_GetObjectItem(object, string) ? 1 : 0;
  1642. }
  1643. /* Utility for array list handling. */
  1644. static void suffix_object(cJSON *prev, cJSON *item)
  1645. {
  1646. prev->next = item;
  1647. item->prev = prev;
  1648. }
  1649. /* Utility for handling references. */
  1650. static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
  1651. {
  1652. cJSON *reference = NULL;
  1653. if (item == NULL)
  1654. {
  1655. return NULL;
  1656. }
  1657. reference = cJSON_New_Item(hooks);
  1658. if (reference == NULL)
  1659. {
  1660. return NULL;
  1661. }
  1662. memcpy(reference, item, sizeof(cJSON));
  1663. reference->string = NULL;
  1664. reference->type |= cJSON_IsReference;
  1665. reference->next = reference->prev = NULL;
  1666. return reference;
  1667. }
  1668. static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
  1669. {
  1670. cJSON *child = NULL;
  1671. if ((item == NULL) || (array == NULL) || (array == item))
  1672. {
  1673. return false;
  1674. }
  1675. child = array->child;
  1676. /*
  1677. * To find the last item in array quickly, we use prev in array
  1678. */
  1679. if (child == NULL)
  1680. {
  1681. /* list is empty, start new one */
  1682. array->child = item;
  1683. item->prev = item;
  1684. item->next = NULL;
  1685. }
  1686. else
  1687. {
  1688. /* append to the end */
  1689. if (child->prev)
  1690. {
  1691. suffix_object(child->prev, item);
  1692. array->child->prev = item;
  1693. }
  1694. }
  1695. return true;
  1696. }
  1697. /* Add item to array/object. */
  1698. CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
  1699. {
  1700. return add_item_to_array(array, item);
  1701. }
  1702. #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
  1703. #pragma GCC diagnostic push
  1704. #endif
  1705. #ifdef __GNUC__
  1706. #if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 1)))
  1707. #pragma GCC diagnostic ignored "-Wcast-qual"
  1708. #endif
  1709. #endif
  1710. /* helper function to cast away const */
  1711. static void* cast_away_const(const void* string)
  1712. {
  1713. return (void*)string;
  1714. }
  1715. #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
  1716. #pragma GCC diagnostic pop
  1717. #endif
  1718. static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key)
  1719. {
  1720. char *new_key = NULL;
  1721. int new_type = cJSON_Invalid;
  1722. if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item))
  1723. {
  1724. return false;
  1725. }
  1726. if (constant_key)
  1727. {
  1728. new_key = (char*)cast_away_const(string);
  1729. new_type = item->type | cJSON_StringIsConst;
  1730. }
  1731. else
  1732. {
  1733. new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
  1734. if (new_key == NULL)
  1735. {
  1736. return false;
  1737. }
  1738. new_type = item->type & ~cJSON_StringIsConst;
  1739. }
  1740. if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
  1741. {
  1742. hooks->deallocate(item->string);
  1743. }
  1744. item->string = new_key;
  1745. item->type = new_type;
  1746. return add_item_to_array(object, item);
  1747. }
  1748. CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
  1749. {
  1750. return add_item_to_object(object, string, item, &global_hooks, false);
  1751. }
  1752. /* Add an item to an object with constant string as key */
  1753. CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
  1754. {
  1755. return add_item_to_object(object, string, item, &global_hooks, true);
  1756. }
  1757. CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
  1758. {
  1759. if (array == NULL)
  1760. {
  1761. return false;
  1762. }
  1763. return add_item_to_array(array, create_reference(item, &global_hooks));
  1764. }
  1765. CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
  1766. {
  1767. if ((object == NULL) || (string == NULL))
  1768. {
  1769. return false;
  1770. }
  1771. return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
  1772. }
  1773. CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)
  1774. {
  1775. cJSON *null = cJSON_CreateNull();
  1776. if (add_item_to_object(object, name, null, &global_hooks, false))
  1777. {
  1778. return null;
  1779. }
  1780. cJSON_Delete(null);
  1781. return NULL;
  1782. }
  1783. CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name)
  1784. {
  1785. cJSON *true_item = cJSON_CreateTrue();
  1786. if (add_item_to_object(object, name, true_item, &global_hooks, false))
  1787. {
  1788. return true_item;
  1789. }
  1790. cJSON_Delete(true_item);
  1791. return NULL;
  1792. }
  1793. CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name)
  1794. {
  1795. cJSON *false_item = cJSON_CreateFalse();
  1796. if (add_item_to_object(object, name, false_item, &global_hooks, false))
  1797. {
  1798. return false_item;
  1799. }
  1800. cJSON_Delete(false_item);
  1801. return NULL;
  1802. }
  1803. CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean)
  1804. {
  1805. cJSON *bool_item = cJSON_CreateBool(boolean);
  1806. if (add_item_to_object(object, name, bool_item, &global_hooks, false))
  1807. {
  1808. return bool_item;
  1809. }
  1810. cJSON_Delete(bool_item);
  1811. return NULL;
  1812. }
  1813. CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
  1814. {
  1815. cJSON *number_item = cJSON_CreateNumber(number);
  1816. if (add_item_to_object(object, name, number_item, &global_hooks, false))
  1817. {
  1818. return number_item;
  1819. }
  1820. cJSON_Delete(number_item);
  1821. return NULL;
  1822. }
  1823. CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
  1824. {
  1825. cJSON *string_item = cJSON_CreateString(string);
  1826. if (add_item_to_object(object, name, string_item, &global_hooks, false))
  1827. {
  1828. return string_item;
  1829. }
  1830. cJSON_Delete(string_item);
  1831. return NULL;
  1832. }
  1833. CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw)
  1834. {
  1835. cJSON *raw_item = cJSON_CreateRaw(raw);
  1836. if (add_item_to_object(object, name, raw_item, &global_hooks, false))
  1837. {
  1838. return raw_item;
  1839. }
  1840. cJSON_Delete(raw_item);
  1841. return NULL;
  1842. }
  1843. CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name)
  1844. {
  1845. cJSON *object_item = cJSON_CreateObject();
  1846. if (add_item_to_object(object, name, object_item, &global_hooks, false))
  1847. {
  1848. return object_item;
  1849. }
  1850. cJSON_Delete(object_item);
  1851. return NULL;
  1852. }
  1853. CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name)
  1854. {
  1855. cJSON *array = cJSON_CreateArray();
  1856. if (add_item_to_object(object, name, array, &global_hooks, false))
  1857. {
  1858. return array;
  1859. }
  1860. cJSON_Delete(array);
  1861. return NULL;
  1862. }
  1863. CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
  1864. {
  1865. if ((parent == NULL) || (item == NULL))
  1866. {
  1867. return NULL;
  1868. }
  1869. if (item != parent->child)
  1870. {
  1871. /* not the first element */
  1872. item->prev->next = item->next;
  1873. }
  1874. if (item->next != NULL)
  1875. {
  1876. /* not the last element */
  1877. item->next->prev = item->prev;
  1878. }
  1879. if (item == parent->child)
  1880. {
  1881. /* first element */
  1882. parent->child = item->next;
  1883. }
  1884. else if (item->next == NULL)
  1885. {
  1886. /* last element */
  1887. parent->child->prev = item->prev;
  1888. }
  1889. /* make sure the detached item doesn't point anywhere anymore */
  1890. item->prev = NULL;
  1891. item->next = NULL;
  1892. return item;
  1893. }
  1894. CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
  1895. {
  1896. if (which < 0)
  1897. {
  1898. return NULL;
  1899. }
  1900. return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
  1901. }
  1902. CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
  1903. {
  1904. cJSON_Delete(cJSON_DetachItemFromArray(array, which));
  1905. }
  1906. CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
  1907. {
  1908. cJSON *to_detach = cJSON_GetObjectItem(object, string);
  1909. return cJSON_DetachItemViaPointer(object, to_detach);
  1910. }
  1911. CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string)
  1912. {
  1913. cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
  1914. return cJSON_DetachItemViaPointer(object, to_detach);
  1915. }
  1916. CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
  1917. {
  1918. cJSON_Delete(cJSON_DetachItemFromObject(object, string));
  1919. }
  1920. CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
  1921. {
  1922. cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
  1923. }
  1924. /* Replace array/object items with new ones. */
  1925. CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
  1926. {
  1927. cJSON *after_inserted = NULL;
  1928. if (which < 0)
  1929. {
  1930. return false;
  1931. }
  1932. after_inserted = get_array_item(array, (size_t)which);
  1933. if (after_inserted == NULL)
  1934. {
  1935. return add_item_to_array(array, newitem);
  1936. }
  1937. newitem->next = after_inserted;
  1938. newitem->prev = after_inserted->prev;
  1939. after_inserted->prev = newitem;
  1940. if (after_inserted == array->child)
  1941. {
  1942. array->child = newitem;
  1943. }
  1944. else
  1945. {
  1946. newitem->prev->next = newitem;
  1947. }
  1948. return true;
  1949. }
  1950. CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
  1951. {
  1952. if ((parent == NULL) || (replacement == NULL) || (item == NULL))
  1953. {
  1954. return false;
  1955. }
  1956. if (replacement == item)
  1957. {
  1958. return true;
  1959. }
  1960. replacement->next = item->next;
  1961. replacement->prev = item->prev;
  1962. if (replacement->next != NULL)
  1963. {
  1964. replacement->next->prev = replacement;
  1965. }
  1966. if (parent->child == item)
  1967. {
  1968. if (parent->child->prev == parent->child)
  1969. {
  1970. replacement->prev = replacement;
  1971. }
  1972. parent->child = replacement;
  1973. }
  1974. else
  1975. { /*
  1976. * To find the last item in array quickly, we use prev in array.
  1977. * We can't modify the last item's next pointer where this item was the parent's child
  1978. */
  1979. if (replacement->prev != NULL)
  1980. {
  1981. replacement->prev->next = replacement;
  1982. }
  1983. if (replacement->next == NULL)
  1984. {
  1985. parent->child->prev = replacement;
  1986. }
  1987. }
  1988. item->next = NULL;
  1989. item->prev = NULL;
  1990. cJSON_Delete(item);
  1991. return true;
  1992. }
  1993. CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
  1994. {
  1995. if (which < 0)
  1996. {
  1997. return false;
  1998. }
  1999. return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
  2000. }
  2001. static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
  2002. {
  2003. if ((replacement == NULL) || (string == NULL))
  2004. {
  2005. return false;
  2006. }
  2007. /* replace the name in the replacement */
  2008. if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
  2009. {
  2010. cJSON_free(replacement->string);
  2011. }
  2012. replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
  2013. replacement->type &= ~cJSON_StringIsConst;
  2014. return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
  2015. }
  2016. CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
  2017. {
  2018. return replace_item_in_object(object, string, newitem, false);
  2019. }
  2020. CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
  2021. {
  2022. return replace_item_in_object(object, string, newitem, true);
  2023. }
  2024. /* Create basic types: */
  2025. CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
  2026. {
  2027. cJSON *item = cJSON_New_Item(&global_hooks);
  2028. if(item)
  2029. {
  2030. item->type = cJSON_NULL;
  2031. }
  2032. return item;
  2033. }
  2034. CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
  2035. {
  2036. cJSON *item = cJSON_New_Item(&global_hooks);
  2037. if(item)
  2038. {
  2039. item->type = cJSON_True;
  2040. }
  2041. return item;
  2042. }
  2043. CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
  2044. {
  2045. cJSON *item = cJSON_New_Item(&global_hooks);
  2046. if(item)
  2047. {
  2048. item->type = cJSON_False;
  2049. }
  2050. return item;
  2051. }
  2052. CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
  2053. {
  2054. cJSON *item = cJSON_New_Item(&global_hooks);
  2055. if(item)
  2056. {
  2057. item->type = boolean ? cJSON_True : cJSON_False;
  2058. }
  2059. return item;
  2060. }
  2061. CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
  2062. {
  2063. cJSON *item = cJSON_New_Item(&global_hooks);
  2064. if(item)
  2065. {
  2066. item->type = cJSON_Number;
  2067. item->valuedouble = num;
  2068. /* use saturation in case of overflow */
  2069. if (num >= INT_MAX)
  2070. {
  2071. item->valueint = INT_MAX;
  2072. }
  2073. else if (num <= (double)INT_MIN)
  2074. {
  2075. item->valueint = INT_MIN;
  2076. }
  2077. else
  2078. {
  2079. item->valueint = (int)num;
  2080. }
  2081. }
  2082. return item;
  2083. }
  2084. CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
  2085. {
  2086. cJSON *item = cJSON_New_Item(&global_hooks);
  2087. if(item)
  2088. {
  2089. item->type = cJSON_String;
  2090. item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
  2091. if(!item->valuestring)
  2092. {
  2093. cJSON_Delete(item);
  2094. return NULL;
  2095. }
  2096. }
  2097. return item;
  2098. }
  2099. CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
  2100. {
  2101. cJSON *item = cJSON_New_Item(&global_hooks);
  2102. if (item != NULL)
  2103. {
  2104. item->type = cJSON_String | cJSON_IsReference;
  2105. item->valuestring = (char*)cast_away_const(string);
  2106. }
  2107. return item;
  2108. }
  2109. CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
  2110. {
  2111. cJSON *item = cJSON_New_Item(&global_hooks);
  2112. if (item != NULL) {
  2113. item->type = cJSON_Object | cJSON_IsReference;
  2114. item->child = (cJSON*)cast_away_const(child);
  2115. }
  2116. return item;
  2117. }
  2118. CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
  2119. cJSON *item = cJSON_New_Item(&global_hooks);
  2120. if (item != NULL) {
  2121. item->type = cJSON_Array | cJSON_IsReference;
  2122. item->child = (cJSON*)cast_away_const(child);
  2123. }
  2124. return item;
  2125. }
  2126. CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
  2127. {
  2128. cJSON *item = cJSON_New_Item(&global_hooks);
  2129. if(item)
  2130. {
  2131. item->type = cJSON_Raw;
  2132. item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
  2133. if(!item->valuestring)
  2134. {
  2135. cJSON_Delete(item);
  2136. return NULL;
  2137. }
  2138. }
  2139. return item;
  2140. }
  2141. CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
  2142. {
  2143. cJSON *item = cJSON_New_Item(&global_hooks);
  2144. if(item)
  2145. {
  2146. item->type=cJSON_Array;
  2147. }
  2148. return item;
  2149. }
  2150. CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
  2151. {
  2152. cJSON *item = cJSON_New_Item(&global_hooks);
  2153. if (item)
  2154. {
  2155. item->type = cJSON_Object;
  2156. }
  2157. return item;
  2158. }
  2159. /* Create Arrays: */
  2160. CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
  2161. {
  2162. size_t i = 0;
  2163. cJSON *n = NULL;
  2164. cJSON *p = NULL;
  2165. cJSON *a = NULL;
  2166. if ((count < 0) || (numbers == NULL))
  2167. {
  2168. return NULL;
  2169. }
  2170. a = cJSON_CreateArray();
  2171. for(i = 0; a && (i < (size_t)count); i++)
  2172. {
  2173. n = cJSON_CreateNumber(numbers[i]);
  2174. if (!n)
  2175. {
  2176. cJSON_Delete(a);
  2177. return NULL;
  2178. }
  2179. if(!i)
  2180. {
  2181. a->child = n;
  2182. }
  2183. else
  2184. {
  2185. suffix_object(p, n);
  2186. }
  2187. p = n;
  2188. }
  2189. if (a && a->child) {
  2190. a->child->prev = n;
  2191. }
  2192. return a;
  2193. }
  2194. CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
  2195. {
  2196. size_t i = 0;
  2197. cJSON *n = NULL;
  2198. cJSON *p = NULL;
  2199. cJSON *a = NULL;
  2200. if ((count < 0) || (numbers == NULL))
  2201. {
  2202. return NULL;
  2203. }
  2204. a = cJSON_CreateArray();
  2205. for(i = 0; a && (i < (size_t)count); i++)
  2206. {
  2207. n = cJSON_CreateNumber((double)numbers[i]);
  2208. if(!n)
  2209. {
  2210. cJSON_Delete(a);
  2211. return NULL;
  2212. }
  2213. if(!i)
  2214. {
  2215. a->child = n;
  2216. }
  2217. else
  2218. {
  2219. suffix_object(p, n);
  2220. }
  2221. p = n;
  2222. }
  2223. if (a && a->child) {
  2224. a->child->prev = n;
  2225. }
  2226. return a;
  2227. }
  2228. CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
  2229. {
  2230. size_t i = 0;
  2231. cJSON *n = NULL;
  2232. cJSON *p = NULL;
  2233. cJSON *a = NULL;
  2234. if ((count < 0) || (numbers == NULL))
  2235. {
  2236. return NULL;
  2237. }
  2238. a = cJSON_CreateArray();
  2239. for(i = 0; a && (i < (size_t)count); i++)
  2240. {
  2241. n = cJSON_CreateNumber(numbers[i]);
  2242. if(!n)
  2243. {
  2244. cJSON_Delete(a);
  2245. return NULL;
  2246. }
  2247. if(!i)
  2248. {
  2249. a->child = n;
  2250. }
  2251. else
  2252. {
  2253. suffix_object(p, n);
  2254. }
  2255. p = n;
  2256. }
  2257. if (a && a->child) {
  2258. a->child->prev = n;
  2259. }
  2260. return a;
  2261. }
  2262. CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count)
  2263. {
  2264. size_t i = 0;
  2265. cJSON *n = NULL;
  2266. cJSON *p = NULL;
  2267. cJSON *a = NULL;
  2268. if ((count < 0) || (strings == NULL))
  2269. {
  2270. return NULL;
  2271. }
  2272. a = cJSON_CreateArray();
  2273. for (i = 0; a && (i < (size_t)count); i++)
  2274. {
  2275. n = cJSON_CreateString(strings[i]);
  2276. if(!n)
  2277. {
  2278. cJSON_Delete(a);
  2279. return NULL;
  2280. }
  2281. if(!i)
  2282. {
  2283. a->child = n;
  2284. }
  2285. else
  2286. {
  2287. suffix_object(p,n);
  2288. }
  2289. p = n;
  2290. }
  2291. if (a && a->child) {
  2292. a->child->prev = n;
  2293. }
  2294. return a;
  2295. }
  2296. /* Duplication */
  2297. CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
  2298. {
  2299. cJSON *newitem = NULL;
  2300. cJSON *child = NULL;
  2301. cJSON *next = NULL;
  2302. cJSON *newchild = NULL;
  2303. /* Bail on bad ptr */
  2304. if (!item)
  2305. {
  2306. goto fail;
  2307. }
  2308. /* Create new item */
  2309. newitem = cJSON_New_Item(&global_hooks);
  2310. if (!newitem)
  2311. {
  2312. goto fail;
  2313. }
  2314. /* Copy over all vars */
  2315. newitem->type = item->type & (~cJSON_IsReference);
  2316. newitem->valueint = item->valueint;
  2317. newitem->valuedouble = item->valuedouble;
  2318. if (item->valuestring)
  2319. {
  2320. newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
  2321. if (!newitem->valuestring)
  2322. {
  2323. goto fail;
  2324. }
  2325. }
  2326. if (item->string)
  2327. {
  2328. newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
  2329. if (!newitem->string)
  2330. {
  2331. goto fail;
  2332. }
  2333. }
  2334. /* If non-recursive, then we're done! */
  2335. if (!recurse)
  2336. {
  2337. return newitem;
  2338. }
  2339. /* Walk the ->next chain for the child. */
  2340. child = item->child;
  2341. while (child != NULL)
  2342. {
  2343. newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
  2344. if (!newchild)
  2345. {
  2346. goto fail;
  2347. }
  2348. if (next != NULL)
  2349. {
  2350. /* If newitem->child already set, then crosswire ->prev and ->next and move on */
  2351. next->next = newchild;
  2352. newchild->prev = next;
  2353. next = newchild;
  2354. }
  2355. else
  2356. {
  2357. /* Set newitem->child and move to it */
  2358. newitem->child = newchild;
  2359. next = newchild;
  2360. }
  2361. child = child->next;
  2362. }
  2363. if (newitem && newitem->child)
  2364. {
  2365. newitem->child->prev = newchild;
  2366. }
  2367. return newitem;
  2368. fail:
  2369. if (newitem != NULL)
  2370. {
  2371. cJSON_Delete(newitem);
  2372. }
  2373. return NULL;
  2374. }
  2375. static void skip_oneline_comment(char **input)
  2376. {
  2377. *input += static_strlen("//");
  2378. for (; (*input)[0] != '\0'; ++(*input))
  2379. {
  2380. if ((*input)[0] == '\n') {
  2381. *input += static_strlen("\n");
  2382. return;
  2383. }
  2384. }
  2385. }
  2386. static void skip_multiline_comment(char **input)
  2387. {
  2388. *input += static_strlen("/*");
  2389. for (; (*input)[0] != '\0'; ++(*input))
  2390. {
  2391. if (((*input)[0] == '*') && ((*input)[1] == '/'))
  2392. {
  2393. *input += static_strlen("*/");
  2394. return;
  2395. }
  2396. }
  2397. }
  2398. static void minify_string(char **input, char **output) {
  2399. (*output)[0] = (*input)[0];
  2400. *input += static_strlen("\"");
  2401. *output += static_strlen("\"");
  2402. for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) {
  2403. (*output)[0] = (*input)[0];
  2404. if ((*input)[0] == '\"') {
  2405. (*output)[0] = '\"';
  2406. *input += static_strlen("\"");
  2407. *output += static_strlen("\"");
  2408. return;
  2409. } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) {
  2410. (*output)[1] = (*input)[1];
  2411. *input += static_strlen("\"");
  2412. *output += static_strlen("\"");
  2413. }
  2414. }
  2415. }
  2416. CJSON_PUBLIC(void) cJSON_Minify(char *json)
  2417. {
  2418. char *into = json;
  2419. if (json == NULL)
  2420. {
  2421. return;
  2422. }
  2423. while (json[0] != '\0')
  2424. {
  2425. switch (json[0])
  2426. {
  2427. case ' ':
  2428. case '\t':
  2429. case '\r':
  2430. case '\n':
  2431. json++;
  2432. break;
  2433. case '/':
  2434. if (json[1] == '/')
  2435. {
  2436. skip_oneline_comment(&json);
  2437. }
  2438. else if (json[1] == '*')
  2439. {
  2440. skip_multiline_comment(&json);
  2441. } else {
  2442. json++;
  2443. }
  2444. break;
  2445. case '\"':
  2446. minify_string(&json, (char**)&into);
  2447. break;
  2448. default:
  2449. into[0] = json[0];
  2450. json++;
  2451. into++;
  2452. }
  2453. }
  2454. /* and null-terminate. */
  2455. *into = '\0';
  2456. }
  2457. CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
  2458. {
  2459. if (item == NULL)
  2460. {
  2461. return false;
  2462. }
  2463. return (item->type & 0xFF) == cJSON_Invalid;
  2464. }
  2465. CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
  2466. {
  2467. if (item == NULL)
  2468. {
  2469. return false;
  2470. }
  2471. return (item->type & 0xFF) == cJSON_False;
  2472. }
  2473. CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
  2474. {
  2475. if (item == NULL)
  2476. {
  2477. return false;
  2478. }
  2479. return (item->type & 0xff) == cJSON_True;
  2480. }
  2481. CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
  2482. {
  2483. if (item == NULL)
  2484. {
  2485. return false;
  2486. }
  2487. return (item->type & (cJSON_True | cJSON_False)) != 0;
  2488. }
  2489. CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
  2490. {
  2491. if (item == NULL)
  2492. {
  2493. return false;
  2494. }
  2495. return (item->type & 0xFF) == cJSON_NULL;
  2496. }
  2497. CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
  2498. {
  2499. if (item == NULL)
  2500. {
  2501. return false;
  2502. }
  2503. return (item->type & 0xFF) == cJSON_Number;
  2504. }
  2505. CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
  2506. {
  2507. if (item == NULL)
  2508. {
  2509. return false;
  2510. }
  2511. return (item->type & 0xFF) == cJSON_String;
  2512. }
  2513. CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
  2514. {
  2515. if (item == NULL)
  2516. {
  2517. return false;
  2518. }
  2519. return (item->type & 0xFF) == cJSON_Array;
  2520. }
  2521. CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
  2522. {
  2523. if (item == NULL)
  2524. {
  2525. return false;
  2526. }
  2527. return (item->type & 0xFF) == cJSON_Object;
  2528. }
  2529. CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
  2530. {
  2531. if (item == NULL)
  2532. {
  2533. return false;
  2534. }
  2535. return (item->type & 0xFF) == cJSON_Raw;
  2536. }
  2537. CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
  2538. {
  2539. if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
  2540. {
  2541. return false;
  2542. }
  2543. /* check if type is valid */
  2544. switch (a->type & 0xFF)
  2545. {
  2546. case cJSON_False:
  2547. case cJSON_True:
  2548. case cJSON_NULL:
  2549. case cJSON_Number:
  2550. case cJSON_String:
  2551. case cJSON_Raw:
  2552. case cJSON_Array:
  2553. case cJSON_Object:
  2554. break;
  2555. default:
  2556. return false;
  2557. }
  2558. /* identical objects are equal */
  2559. if (a == b)
  2560. {
  2561. return true;
  2562. }
  2563. switch (a->type & 0xFF)
  2564. {
  2565. /* in these cases and equal type is enough */
  2566. case cJSON_False:
  2567. case cJSON_True:
  2568. case cJSON_NULL:
  2569. return true;
  2570. case cJSON_Number:
  2571. if (compare_double(a->valuedouble, b->valuedouble))
  2572. {
  2573. return true;
  2574. }
  2575. return false;
  2576. case cJSON_String:
  2577. case cJSON_Raw:
  2578. if ((a->valuestring == NULL) || (b->valuestring == NULL))
  2579. {
  2580. return false;
  2581. }
  2582. if (strcmp(a->valuestring, b->valuestring) == 0)
  2583. {
  2584. return true;
  2585. }
  2586. return false;
  2587. case cJSON_Array:
  2588. {
  2589. cJSON *a_element = a->child;
  2590. cJSON *b_element = b->child;
  2591. for (; (a_element != NULL) && (b_element != NULL);)
  2592. {
  2593. if (!cJSON_Compare(a_element, b_element, case_sensitive))
  2594. {
  2595. return false;
  2596. }
  2597. a_element = a_element->next;
  2598. b_element = b_element->next;
  2599. }
  2600. /* one of the arrays is longer than the other */
  2601. if (a_element != b_element) {
  2602. return false;
  2603. }
  2604. return true;
  2605. }
  2606. case cJSON_Object:
  2607. {
  2608. cJSON *a_element = NULL;
  2609. cJSON *b_element = NULL;
  2610. cJSON_ArrayForEach(a_element, a)
  2611. {
  2612. /* TODO This has O(n^2) runtime, which is horrible! */
  2613. b_element = get_object_item(b, a_element->string, case_sensitive);
  2614. if (b_element == NULL)
  2615. {
  2616. return false;
  2617. }
  2618. if (!cJSON_Compare(a_element, b_element, case_sensitive))
  2619. {
  2620. return false;
  2621. }
  2622. }
  2623. /* doing this twice, once on a and b to prevent true comparison if a subset of b
  2624. * TODO: Do this the proper way, this is just a fix for now */
  2625. cJSON_ArrayForEach(b_element, b)
  2626. {
  2627. a_element = get_object_item(a, b_element->string, case_sensitive);
  2628. if (a_element == NULL)
  2629. {
  2630. return false;
  2631. }
  2632. if (!cJSON_Compare(b_element, a_element, case_sensitive))
  2633. {
  2634. return false;
  2635. }
  2636. }
  2637. return true;
  2638. }
  2639. default:
  2640. return false;
  2641. }
  2642. }
  2643. CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
  2644. {
  2645. return global_hooks.allocate(size);
  2646. }
  2647. CJSON_PUBLIC(void) cJSON_free(void *object)
  2648. {
  2649. global_hooks.deallocate(object);
  2650. }