cJSON.c 76 KB

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