00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "json.h"
00022
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <assert.h>
00026 #include <memory.h>
00027 #include <wchar.h>
00028
00029
00030 enum LEX_VALUE
00031 { LEX_MORE = 0,
00032 LEX_INVALID_CHARACTER,
00033 LEX_TRUE,
00034 LEX_FALSE,
00035 LEX_NULL,
00036 LEX_BEGIN_OBJECT,
00037 LEX_END_OBJECT,
00038 LEX_BEGIN_ARRAY,
00039 LEX_END_ARRAY,
00040 LEX_NAME_SEPARATOR,
00041 LEX_VALUE_SEPARATOR,
00042 LEX_STRING,
00043 LEX_NUMBER,
00044 LEX_ERROR,
00045 LEX_MEMORY
00046 };
00047
00048
00049
00050
00051 #define RSTRING_INCSTEP 3
00052
00053 struct rui_cstring
00054 {
00055 char *text;
00056 size_t max;
00057 size_t len;
00058 };
00059
00060 typedef struct rui_cstring rcstring;
00061
00062 enum rui_string_error_codes
00063 { RS_MEMORY, RS_OK = 1, RS_UNKNOWN };
00064
00065 typedef enum rui_string_error_codes rstring_code;
00066
00067
00068 rcstring *
00069 rcs_create (size_t length)
00070 {
00071 rcstring *rcs;
00072 rcs = malloc (sizeof (rcstring));
00073 if (rcs == NULL)
00074 return NULL;
00075
00076 rcs->max = length;
00077 rcs->len = 0;
00078
00079 rcs->text = calloc (rcs->max + 1, sizeof (char));
00080 if (rcs->text == NULL)
00081 {
00082 free (rcs);
00083 return NULL;
00084 }
00085
00086 return rcs;
00087 }
00088
00089
00090 void
00091 rcs_free (rcstring ** rcs)
00092 {
00093 assert (rcs != NULL);
00094 if (*rcs != NULL)
00095 {
00096 if ((*rcs)->text != NULL)
00097 {
00098 free ((*rcs)->text);
00099 (*rcs)->text = NULL;
00100 }
00101 free (*rcs);
00102 *rcs = NULL;
00103 }
00104
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 static inline rstring_code
00119 rcs_resize (rcstring * rcs, size_t length)
00120 {
00121 char *temp;
00122 assert (rcs != NULL);
00123
00124
00125 size_t newmax = rcs->max;
00126 while (newmax < length + 1)
00127 {
00128 newmax *= 2;
00129 }
00130
00131 temp = realloc (rcs->text, sizeof (char) * (newmax + 1));
00132 if (temp == NULL)
00133 {
00134 free (rcs);
00135 return RS_MEMORY;
00136 }
00137 rcs->text = temp;
00138 rcs->max = newmax;
00139 rcs->text[rcs->max] = L'\0';
00140 return RS_OK;
00141 }
00142
00143
00144 static inline rstring_code
00145 rcs_catcs (rcstring * pre, const char *pos, const size_t length)
00146 {
00147 size_t pre_length;
00148
00149 assert (pre != NULL);
00150 assert (pos != NULL);
00151
00152 pre_length = pre->len;
00153
00154 if (pre->max < pre_length + length)
00155 {
00156 if (rcs_resize (pre, pre_length + length + 5) != RS_OK)
00157 return RS_MEMORY;
00158 }
00159 strncpy (pre->text + pre_length, pos, length);
00160 pre->text[pre_length + length] = '\0';
00161 pre->len += length;
00162 return RS_OK;
00163 }
00164
00165
00166 static inline rstring_code
00167 rcs_catc (rcstring * pre, const char c)
00168 {
00169 size_t pre_length;
00170
00171 assert (pre != NULL);
00172
00173 pre_length = pre->len;
00174
00175 if (pre->max <= pre_length)
00176 {
00177 pre->max += RSTRING_INCSTEP;
00178 if (rcs_resize (pre, pre->max) != RS_OK)
00179 return RS_MEMORY;
00180 }
00181 pre->text[pre_length] = c;
00182 pre->text[pre_length + 1] = '\0';
00183 pre->len++;
00184 return RS_OK;
00185 }
00186
00187
00188 char *
00189 rcs_unwrap (rcstring * rcs)
00190 {
00191 char *out;
00192 assert (rcs != NULL);
00193
00194 if (rcs->text == NULL)
00195 out = NULL;
00196 else
00197 out = strdup(rcs->text);
00198
00199 rcs_free (&rcs);
00200 return out;
00201 }
00202
00203
00204
00205 size_t
00206 rcs_length (rcstring * rcs)
00207 {
00208
00209 assert (rcs != NULL);
00210 return rcs->len;
00211 }
00212
00213
00214
00215
00216
00217
00218 json_t *
00219 json_new_value (const enum json_value_type type)
00220 {
00221 json_t *new_object;
00222
00223 new_object = malloc (sizeof (json_t));
00224 if (new_object == NULL)
00225 return NULL;
00226
00227
00228 new_object->text = NULL;
00229 new_object->parent = NULL;
00230 new_object->child = NULL;
00231 new_object->child_end = NULL;
00232 new_object->previous = NULL;
00233 new_object->next = NULL;
00234 new_object->type = type;
00235 return new_object;
00236 }
00237
00238
00239 json_t *
00240 json_new_string (const char *text)
00241 {
00242 json_t *new_object;
00243 size_t length;
00244
00245 assert (text != NULL);
00246
00247
00248 new_object = malloc (sizeof (json_t));
00249 if (new_object == NULL)
00250 return NULL;
00251
00252
00253 length = strlen (text) + 1;
00254 new_object->text = calloc (sizeof (char), length);
00255 if (new_object->text == NULL)
00256 {
00257 free (new_object);
00258 return NULL;
00259 }
00260 strncpy (new_object->text, text, length);
00261 new_object->parent = NULL;
00262 new_object->child = NULL;
00263 new_object->child_end = NULL;
00264 new_object->previous = NULL;
00265 new_object->next = NULL;
00266 new_object->type = JSON_STRING;
00267 return new_object;
00268 }
00269
00270
00271 json_t *
00272 json_new_number (const char *text)
00273 {
00274 json_t *new_object;
00275 size_t length;
00276
00277 assert (text != NULL);
00278
00279
00280 new_object = malloc (sizeof (json_t));
00281 if (new_object == NULL)
00282 return NULL;
00283
00284
00285 length = strlen (text) + 1;
00286 new_object->text = calloc (sizeof (char), length);
00287 if (new_object->text == NULL)
00288 {
00289 free (new_object);
00290 return NULL;
00291 }
00292 strncpy (new_object->text, text, length);
00293 new_object->parent = NULL;
00294 new_object->child = NULL;
00295 new_object->child_end = NULL;
00296 new_object->previous = NULL;
00297 new_object->next = NULL;
00298 new_object->type = JSON_NUMBER;
00299 return new_object;
00300 }
00301
00302
00303 json_t *
00304 json_new_object (void)
00305 {
00306 return json_new_value (JSON_OBJECT);
00307 }
00308
00309
00310 json_t *
00311 json_new_array (void)
00312 {
00313 return json_new_value (JSON_ARRAY);
00314 }
00315
00316
00317 json_t *
00318 json_new_null (void)
00319 {
00320 return json_new_value (JSON_NULL);
00321 }
00322
00323
00324 json_t *
00325 json_new_true (void)
00326 {
00327 return json_new_value (JSON_TRUE);
00328 }
00329
00330
00331 json_t *
00332 json_new_false (void)
00333 {
00334 return json_new_value (JSON_FALSE);
00335 }
00336
00337
00338 void
00339 json_free_value (json_t ** value)
00340 {
00341 assert (value != NULL);
00342 assert ((*value) != NULL);
00343
00344
00345 if ((*value)->child != NULL)
00346 {
00347 json_t *i, *j;
00348 i = (*value)->child_end;
00349 while (i != NULL)
00350 {
00351 j = i->previous;
00352 json_free_value (&i);
00353 i = j;
00354 }
00355 }
00356
00357
00358 if ((*value)->previous && (*value)->next)
00359 {
00360 (*value)->previous->next = (*value)->next;
00361 (*value)->next->previous = (*value)->previous;
00362 }
00363 else
00364 {
00365 if ((*value)->previous)
00366 {
00367 (*value)->previous->next = NULL;
00368 }
00369 if ((*value)->next)
00370 {
00371 (*value)->next->previous = NULL;
00372 }
00373 }
00374
00375
00376 if ((*value)->parent)
00377 {
00378 if ((*value)->parent->child == (*value))
00379 {
00380 if ((*value)->next)
00381 {
00382 (*value)->parent->child = (*value)->next;
00383 }
00384 else
00385 {
00386 if ((*value)->previous)
00387 (*value)->parent->child = (*value)->next;
00388 (*value)->parent->child = NULL;
00389 }
00390 }
00391 }
00392
00393
00394 if ((*value)->text != NULL)
00395 {
00396 free ((*value)->text);
00397 }
00398 free (*value);
00399 (*value) = NULL;
00400 }
00401
00402
00403 enum json_error
00404 json_insert_child (json_t * parent, json_t * child)
00405 {
00406
00407 assert (parent != NULL);
00408 assert (child != NULL);
00409 assert (parent != child);
00410
00411
00412 switch (parent->type)
00413 {
00414 case JSON_STRING:
00415
00416
00417 switch (child->type)
00418 {
00419 case JSON_STRING:
00420 case JSON_NUMBER:
00421 case JSON_TRUE:
00422 case JSON_FALSE:
00423 case JSON_NULL:
00424 if (child->child != NULL)
00425 return JSON_BAD_TREE_STRUCTURE;
00426 break;
00427
00428 case JSON_OBJECT:
00429 case JSON_ARRAY:
00430 break;
00431
00432 default:
00433 return JSON_BAD_TREE_STRUCTURE;
00434 break;
00435 }
00436 break;
00437
00438 case JSON_OBJECT:
00439 if (child->type != JSON_STRING)
00440 return JSON_BAD_TREE_STRUCTURE;
00441 break;
00442
00443 case JSON_ARRAY:
00444 switch (child->type)
00445 {
00446 case JSON_STRING:
00447 case JSON_TRUE:
00448 case JSON_FALSE:
00449 case JSON_NULL:
00450 case JSON_NUMBER:
00451 if (child->child)
00452 return JSON_BAD_TREE_STRUCTURE;
00453 break;
00454
00455 case JSON_OBJECT:
00456 case JSON_ARRAY:
00457 break;
00458 default:
00459 return JSON_BAD_TREE_STRUCTURE;
00460 }
00461 break;
00462
00463 default:
00464 return JSON_BAD_TREE_STRUCTURE;
00465 }
00466
00467 child->parent = parent;
00468 if (parent->child)
00469 {
00470 child->previous = parent->child_end;
00471 parent->child_end->next = child;
00472 parent->child_end = child;
00473 }
00474 else
00475 {
00476 parent->child = child;
00477 parent->child_end = child;
00478 }
00479
00480 return JSON_OK;
00481 }
00482
00483
00484 enum json_error
00485 json_insert_pair_into_object (json_t * parent, const char *text_label, json_t * value)
00486 {
00487 enum json_error error;
00488 json_t *label;
00489
00490 assert (parent != NULL);
00491 assert (text_label != NULL);
00492 assert (value != NULL);
00493 assert (parent != value);
00494
00495
00496 assert (parent->type == JSON_OBJECT);
00497
00498
00499
00500 label = json_new_string (text_label);
00501 if (label == NULL)
00502 return JSON_MEMORY;
00503
00504
00505 error = json_insert_child (label, value);
00506 if (error != JSON_OK)
00507 return error;
00508
00509 error = json_insert_child (parent, label);
00510 if (error != JSON_OK)
00511 return error;
00512
00513 return JSON_OK;
00514 }
00515
00516
00517 enum json_error
00518 json_tree_to_string (json_t * root, char **text)
00519 {
00520 json_t *cursor;
00521 rcstring *output;
00522 assert (root != NULL);
00523 assert (text != NULL);
00524
00525 cursor = root;
00526
00527
00528
00529 output = rcs_create (8192);
00530
00531
00532 state1:
00533 {
00534 if ((cursor->previous) && (cursor != root))
00535 {
00536
00537 if (rcs_catc (output, ',') != RS_OK)
00538 {
00539 return JSON_MEMORY;
00540 }
00541 }
00542 switch (cursor->type)
00543 {
00544 case JSON_STRING:
00545
00546
00547 if (rcs_catc (output, '\"') != RS_OK)
00548 {
00549 return JSON_MEMORY;
00550 }
00551 if (rcs_catcs (output, cursor->text, strlen (cursor->text)) != RS_OK)
00552 {
00553 return JSON_MEMORY;
00554 }
00555 if (rcs_catc (output, '\"') != RS_OK)
00556 {
00557 return JSON_MEMORY;
00558 }
00559
00560 if (cursor->parent != NULL)
00561 {
00562 if (cursor->parent->type == JSON_OBJECT)
00563 {
00564
00565 if (cursor->child != NULL)
00566 {
00567 if (rcs_catc (output, ':') != RS_OK)
00568 {
00569 return JSON_MEMORY;
00570 }
00571 }
00572 else
00573 {
00574
00575 rcs_free (&output);
00576 text = NULL;
00577 return JSON_BAD_TREE_STRUCTURE;
00578 }
00579 }
00580 }
00581 else
00582 {
00583 if (cursor->child != NULL)
00584 {
00585 if (rcs_catc (output, ':') != RS_OK)
00586 {
00587 return JSON_MEMORY;
00588 }
00589 }
00590 else
00591 {
00592
00593 rcs_free (&output);
00594 text = NULL;
00595 return JSON_BAD_TREE_STRUCTURE;
00596 }
00597 }
00598 break;
00599
00600 case JSON_NUMBER:
00601
00602
00603 if (rcs_catcs (output, cursor->text, strlen (cursor->text)) != RS_OK)
00604 {
00605 return JSON_MEMORY;
00606 }
00607 goto state2;
00608 break;
00609
00610 case JSON_OBJECT:
00611 if (rcs_catc (output, '{') != RS_OK)
00612 {
00613 return JSON_MEMORY;
00614 }
00615
00616 if (cursor->child)
00617 {
00618 cursor = cursor->child;
00619 goto state1;
00620 }
00621 else
00622 {
00623 goto state2;
00624 }
00625 break;
00626
00627 case JSON_ARRAY:
00628 if (rcs_catc (output, '[') != RS_OK)
00629 {
00630 return JSON_MEMORY;
00631 }
00632
00633 if (cursor->child != NULL)
00634 {
00635 cursor = cursor->child;
00636 goto state1;
00637 }
00638 else
00639 {
00640 goto state2;
00641 }
00642 break;
00643
00644 case JSON_TRUE:
00645
00646 if (rcs_catcs (output, "true", 4) != RS_OK)
00647 {
00648 return JSON_MEMORY;
00649 }
00650 goto state2;
00651 break;
00652
00653 case JSON_FALSE:
00654
00655 if (rcs_catcs (output, "false", 5) != RS_OK)
00656 {
00657 return JSON_MEMORY;
00658 }
00659 goto state2;
00660 break;
00661
00662 case JSON_NULL:
00663
00664 if (rcs_catcs (output, "null", 4) != RS_OK)
00665 {
00666 return JSON_MEMORY;
00667 }
00668 goto state2;
00669 break;
00670
00671 default:
00672 goto error;
00673 }
00674 if (cursor->child)
00675 {
00676 cursor = cursor->child;
00677 goto state1;
00678 }
00679 else
00680 {
00681
00682 goto state2;
00683 }
00684 }
00685
00686 state2:
00687 {
00688 switch (cursor->type)
00689 {
00690 case JSON_OBJECT:
00691 if (rcs_catc (output, '}') != RS_OK)
00692 {
00693 return JSON_MEMORY;
00694 }
00695 break;
00696
00697 case JSON_ARRAY:
00698 if (rcs_catc (output, ']') != RS_OK)
00699 {
00700 return JSON_MEMORY;
00701 }
00702 break;
00703
00704 case JSON_STRING:
00705 break;
00706 case JSON_NUMBER:
00707 break;
00708 case JSON_TRUE:
00709 break;
00710 case JSON_FALSE:
00711 break;
00712 case JSON_NULL:
00713 break;
00714 default:
00715 goto error;
00716 }
00717 if ((cursor->parent == NULL) || (cursor == root))
00718 {
00719 goto end;
00720 }
00721 else if (cursor->next)
00722 {
00723 cursor = cursor->next;
00724 goto state1;
00725 }
00726 else
00727 {
00728 cursor = cursor->parent;
00729 goto state2;
00730 }
00731 }
00732
00733 error:
00734 {
00735 rcs_free (&output);
00736 return JSON_UNKNOWN_PROBLEM;
00737 }
00738
00739 end:
00740 {
00741 *text = rcs_unwrap(output);
00742 return JSON_OK;
00743 }
00744 }
00745
00746
00747 void
00748 json_strip_white_spaces (char *text)
00749 {
00750 size_t in, out, length;
00751 int state;
00752
00753 assert (text != NULL);
00754
00755 in = 0;
00756 out = 0;
00757 length = strlen (text);
00758 state = 0;
00759
00760 while (in < length)
00761 {
00762 switch (text[in])
00763 {
00764 case '\x20':
00765 case '\x09':
00766 case '\x0A':
00767 case '\x0D':
00768 if (state == 1)
00769 {
00770 text[out++] = text[in];
00771 }
00772 break;
00773
00774 case '\"':
00775 switch (state)
00776 {
00777 case 0:
00778 state = 1;
00779 break;
00780
00781 case 1:
00782 if (text[in - 1] != '\\')
00783 {
00784 state = 0;
00785 }
00786 break;
00787
00788 default:
00789 assert (0);
00790 }
00791 text[out++] = text[in];
00792 break;
00793
00794 default:
00795 text[out++] = text[in];
00796 }
00797 ++in;
00798 }
00799 text[out] = '\0';
00800 }
00801
00802
00803 char *
00804 json_format_string (const char *text)
00805 {
00806 size_t pos = 0, text_length;
00807 unsigned int indentation = 0;
00808 unsigned int i;
00809 char loop;
00810
00811 rcstring *output;
00812 text_length = strlen (text);
00813
00814 output = rcs_create (text_length);
00815 while (pos < text_length)
00816 {
00817 switch (text[pos])
00818 {
00819 case '\x20':
00820 case '\x09':
00821 case '\x0A':
00822 case '\x0D':
00823 pos++;
00824 break;
00825
00826 case '{':
00827 indentation++;
00828 rcs_catcs (output, "{\n", 2);
00829 for (i = 0; i < indentation; i++)
00830 {
00831 rcs_catc (output, '\t');
00832 }
00833 pos++;
00834 break;
00835
00836 case '}':
00837 indentation--;
00838 rcs_catc (output, '\n');
00839 for (i = 0; i < indentation; i++)
00840 {
00841 rcs_catc (output, '\t');
00842 }
00843 rcs_catc (output, '}');
00844 pos++;
00845 break;
00846
00847 case ':':
00848 rcs_catcs (output, ": ", 2);
00849 pos++;
00850 break;
00851
00852 case ',':
00853 rcs_catcs (output, ",\n", 2);
00854 for (i = 0; i < indentation; i++)
00855 {
00856 rcs_catc (output, '\t');
00857 }
00858 pos++;
00859 break;
00860
00861 case '\"':
00862 rcs_catc (output, text[pos]);
00863 pos++;
00864 loop = 1;
00865 while (loop)
00866 {
00867 if (text[pos] == '\\')
00868 {
00869 rcs_catc (output, '\\');
00870 pos++;
00871 if (text[pos] == '\"')
00872 {
00873 rcs_catc (output, '\"');
00874 pos++;
00875 }
00876 }
00877 else if (text[pos] == '\"')
00878 {
00879 loop = 0;
00880 }
00881
00882 rcs_catc (output, text[pos]);
00883
00884 pos++;
00885 if (pos >= text_length)
00886 {
00887 loop = 0;
00888 }
00889 }
00890 break;
00891
00892 default:
00893 rcs_catc (output, text[pos]);
00894 pos++;
00895 break;
00896 }
00897 }
00898
00899 return rcs_unwrap (output);
00900 }
00901
00902
00903 char *
00904 json_escape (char * text)
00905 {
00906 rcstring *output;
00907 size_t i, length;
00908 char buffer[6];
00909
00910 assert (text != NULL);
00911
00912
00913 length = strlen(text);
00914 output = rcs_create (length);
00915 if (output == NULL)
00916 return NULL;
00917 for (i = 0; i < length; i++)
00918 {
00919 if (text[i] == '\\')
00920 {
00921 rcs_catcs (output, "\\\\", 2);
00922 }
00923 else if (text[i] == '\"')
00924 {
00925 rcs_catcs (output, "\\\"", 2);
00926 }
00927 else if (text[i] == '/')
00928 {
00929 rcs_catcs (output, "\\/", 2);
00930 }
00931 else if (text[i] == '\b')
00932 {
00933 rcs_catcs (output, "\\b", 2);
00934 }
00935 else if (text[i] == '\f')
00936 {
00937 rcs_catcs (output, "\\f", 2);
00938 }
00939 else if (text[i] == '\n')
00940 {
00941 rcs_catcs (output, "\\n", 2);
00942 }
00943 else if (text[i] == '\r')
00944 {
00945 rcs_catcs (output, "\\r", 2);
00946 }
00947 else if (text[i] == '\t')
00948 {
00949 rcs_catcs (output, "\\t", 2);
00950 }
00951 else if (text[i] < 0)
00952 {
00953 rcs_catc (output, text[i]);
00954 }
00955 else if (text[i] < 0x20)
00956 {
00957 sprintf(buffer,"\\u%4.4x",text[i]);
00958 rcs_catcs(output,buffer,6);
00959 }
00960 else
00961 {
00962 rcs_catc (output, text[i]);
00963 }
00964 }
00965 return rcs_unwrap (output);
00966 }
00967
00968
00969 void
00970 json_jpi_init (struct json_parsing_info *jpi)
00971 {
00972 assert (jpi != NULL);
00973 jpi->state = 0;
00974 jpi->lex_state = 0;
00975 jpi->lex_text = NULL;
00976 jpi->p = NULL;
00977 jpi->cursor = NULL;
00978 jpi->string_length_limit_reached = 0;
00979 }
00980
00981
00982 int
00983 lexer (char *buffer, char **p, unsigned int *state, rcstring ** text)
00984 {
00985 assert (buffer != NULL);
00986 assert (p != NULL);
00987 assert (state != NULL);
00988 assert (text != NULL);
00989 if (*p == NULL)
00990 *p = buffer;
00991
00992 while (**p != '\0')
00993 {
00994 switch (*state)
00995 {
00996
00997 case 0:
00998 {
00999 switch (*(*p)++)
01000 {
01001 case '\x20':
01002 case '\x09':
01003 case '\x0A':
01004 case '\x0D':
01005 break;
01006
01007 case '{':
01008 return LEX_BEGIN_OBJECT;
01009 case '}':
01010 return LEX_END_OBJECT;
01011 case '[':
01012 return LEX_BEGIN_ARRAY;
01013 case ']':
01014 return LEX_END_ARRAY;
01015 case ':':
01016 return LEX_NAME_SEPARATOR;
01017 case ',':
01018 return LEX_VALUE_SEPARATOR;
01019
01020 case '\"':
01021 *text = rcs_create (5);
01022 if (*text == NULL)
01023 return LEX_MEMORY;
01024 *state = 1;
01025 break;
01026
01027 case 't':
01028 *state = 7;
01029 break;
01030
01031 case 'f':
01032 *state = 10;
01033 break;
01034
01035 case 'n':
01036 *state = 14;
01037 break;
01038
01039 case '-':
01040 *text = rcs_create (5);
01041 if (*text == NULL)
01042 return LEX_MEMORY;
01043 if (rcs_catc (*text, '-') != RS_OK)
01044 return LEX_MEMORY;
01045 *state = 17;
01046 break;
01047
01048 case '0':
01049 *text = rcs_create (5);
01050 if (*text == NULL)
01051 return LEX_MEMORY;
01052 if (rcs_catc (*text, '0') != RS_OK)
01053 return LEX_MEMORY;
01054 *state = 18;
01055 break;
01056
01057 case '1':
01058 case '2':
01059 case '3':
01060 case '4':
01061 case '5':
01062 case '6':
01063 case '7':
01064 case '8':
01065 case '9':
01066 *text = rcs_create (5);
01067 if (*text == NULL)
01068 return LEX_MEMORY;
01069 if (rcs_catc (*text, *(*p - 1)) != RS_OK)
01070 return LEX_MEMORY;
01071 *state = 19;
01072 break;
01073
01074
01075 default:
01076 return LEX_INVALID_CHARACTER;
01077 }
01078 }
01079 break;
01080
01081 case 1:
01082 {
01083 assert (*text != NULL);
01084 switch (**p)
01085 {
01086 case '\"':
01087
01088 *state = 0;
01089 ++*p;
01090 return LEX_STRING;
01091 break;
01092
01093 case '\\':
01094 if (rcs_catc (*text, '\\') != RS_OK)
01095 return LEX_MEMORY;
01096 *state = 2;
01097 break;
01098
01099 default:
01100 if (rcs_catc (*text, **p) != RS_OK)
01101 return LEX_MEMORY;
01102 }
01103 ++*p;
01104 }
01105 break;
01106
01107 case 2:
01108 {
01109 assert (*text != NULL);
01110 switch (**p)
01111 {
01112 case '\\':
01113 case '\"':
01114 case '/':
01115 case 'b':
01116 case 'f':
01117 case 'n':
01118 case 'r':
01119 case 't':
01120 if (rcs_catc (*text, **p) != RS_OK)
01121 return LEX_MEMORY;
01122 *state = 1;
01123 break;
01124
01125 case 'u':
01126 if (rcs_catc (*text, **p) != RS_OK)
01127 return LEX_MEMORY;
01128 *state = 3;
01129 break;
01130
01131 default:
01132 return LEX_INVALID_CHARACTER;
01133 }
01134 ++*p;
01135 }
01136 break;
01137
01138 case 3:
01139 {
01140 assert (*text != NULL);
01141 if ((**p >= 'a') && (**p <= 'e'))
01142 {
01143 if (rcs_catc (*text, **p) != RS_OK)
01144 return LEX_MEMORY;
01145 *state = 4;
01146 }
01147 else if ((**p >= 'A') && (**p <= 'E'))
01148 {
01149 if (rcs_catc (*text, **p) != RS_OK)
01150 return LEX_MEMORY;
01151 *state = 4;
01152 }
01153 else if ((**p >= '0') && (**p <= '9'))
01154 {
01155 if (rcs_catc (*text, **p) != RS_OK)
01156 return LEX_MEMORY;
01157 *state = 4;
01158 }
01159 else
01160 return LEX_INVALID_CHARACTER;
01161 ++*p;
01162 }
01163 break;
01164
01165 case 4:
01166 {
01167 assert (*text != NULL);
01168 if ((**p >= 'a') && (**p <= 'e'))
01169 {
01170 if (rcs_catc (*text, **p) != RS_OK)
01171 return LEX_MEMORY;
01172 *state = 5;
01173 }
01174 else if ((**p >= 'A') && (**p <= 'E'))
01175 {
01176 if (rcs_catc (*text, **p) != RS_OK)
01177 return LEX_MEMORY;
01178 *state = 5;
01179 }
01180 else if ((**p >= '0') && (**p <= '9'))
01181 {
01182 if (rcs_catc (*text, **p) != RS_OK)
01183 return LEX_MEMORY;
01184 *state = 5;
01185 }
01186 else
01187 return LEX_INVALID_CHARACTER;
01188 ++*p;
01189 }
01190
01191 case 5:
01192 {
01193 assert (*text != NULL);
01194 if ((**p >= 'a') && (**p <= 'e'))
01195 {
01196 if (rcs_catc (*text, **p) != RS_OK)
01197 return LEX_MEMORY;
01198 *state = 6;
01199 }
01200 else if ((**p >= 'A') && (**p <= 'E'))
01201 {
01202 if (rcs_catc (*text, **p) != RS_OK)
01203 return LEX_MEMORY;
01204 *state = 6;
01205 }
01206 else if ((**p >= '0') && (**p <= '9'))
01207 {
01208 if (rcs_catc (*text, **p) != RS_OK)
01209 return LEX_MEMORY;
01210 *state = 6;
01211 }
01212 else
01213 return LEX_INVALID_CHARACTER;
01214 ++*p;
01215 }
01216 break;
01217
01218 case 6:
01219 {
01220 assert (*text != NULL);
01221 if ((**p >= 'a') && (**p <= 'e'))
01222 {
01223 if (rcs_catc (*text, **p) != RS_OK)
01224 return LEX_MEMORY;
01225 *state = 1;
01226 }
01227 else if ((**p >= 'A') && (**p <= 'E'))
01228 {
01229 if (rcs_catc (*text, **p) != RS_OK)
01230 return LEX_MEMORY;
01231 *state = 1;
01232 }
01233 else if ((**p >= '0') && (**p <= '9'))
01234 {
01235 if (rcs_catc (*text, **p) != RS_OK)
01236 return LEX_MEMORY;
01237 *state = 1;
01238 }
01239 else
01240 return LEX_INVALID_CHARACTER;
01241 ++*p;
01242 }
01243 break;
01244
01245 case 7:
01246 {
01247 switch (*(*p)++)
01248 {
01249 case 'r':
01250 *state = 8;
01251 break;
01252 default:
01253 return LEX_INVALID_CHARACTER;
01254 break;
01255 }
01256 }
01257 break;
01258
01259 case 8:
01260 {
01261 switch (*(*p)++)
01262 {
01263 case 'u':
01264 *state = 9;
01265 break;
01266 default:
01267 return LEX_INVALID_CHARACTER;
01268 break;
01269 }
01270 }
01271 break;
01272
01273 case 9:
01274 {
01275 switch (*(*p)++)
01276 {
01277 case 'e':
01278 *state = 0;
01279 return LEX_TRUE;
01280 break;
01281 default:
01282 return LEX_INVALID_CHARACTER;
01283 break;
01284 }
01285 }
01286 break;
01287
01288 case 10:
01289 {
01290 switch (*(*p)++)
01291 {
01292 case 'a':
01293 *state = 11;
01294 break;
01295 default:
01296 return LEX_INVALID_CHARACTER;
01297 break;
01298 }
01299 }
01300 break;
01301
01302 case 11:
01303 {
01304 switch (*(*p)++)
01305 {
01306 case 'l':
01307 *state = 12;
01308 break;
01309 default:
01310 return LEX_INVALID_CHARACTER;
01311 break;
01312 }
01313 }
01314 break;
01315
01316 case 12:
01317 {
01318 switch (*(*p)++)
01319 {
01320 case 's':
01321 *state = 13;
01322 break;
01323 default:
01324 return LEX_INVALID_CHARACTER;
01325 break;
01326 }
01327 }
01328 break;
01329
01330 case 13:
01331 {
01332 switch (*(*p)++)
01333 {
01334 case 'e':
01335 *state = 0;
01336 return LEX_FALSE;
01337 break;
01338 default:
01339 return LEX_INVALID_CHARACTER;
01340 break;
01341 }
01342 }
01343 break;
01344
01345 case 14:
01346 {
01347 switch (*(*p)++)
01348 {
01349 case 'u':
01350 *state = 15;
01351 break;
01352 default:
01353 return LEX_INVALID_CHARACTER;
01354 break;
01355 }
01356 }
01357 break;
01358
01359 case 15:
01360 {
01361 switch (*(*p)++)
01362 {
01363 case 'l':
01364 *state = 16;
01365 break;
01366 default:
01367 return LEX_INVALID_CHARACTER;
01368 break;
01369 }
01370 }
01371 break;
01372
01373 case 16:
01374 {
01375 switch (*(*p)++)
01376 {
01377 case 'l':
01378 *state = 0;
01379 return LEX_NULL;
01380 break;
01381 default:
01382 return LEX_INVALID_CHARACTER;
01383 break;
01384 }
01385 }
01386 break;
01387
01388 case 17:
01389 {
01390 assert (*text != NULL);
01391 switch (**p)
01392 {
01393 case '0':
01394 if (rcs_catc (*text, **p) != RS_OK)
01395 return LEX_MEMORY;
01396 ++*p;
01397 *state = 18;
01398 break;
01399
01400 case '1':
01401 case '2':
01402 case '3':
01403 case '4':
01404 case '5':
01405 case '6':
01406 case '7':
01407 case '8':
01408 case '9':
01409 if (rcs_catc (*text, **p) != RS_OK)
01410 return LEX_MEMORY;
01411 ++*p;
01412 *state = 19;
01413 break;
01414
01415 default:
01416 return LEX_INVALID_CHARACTER;
01417 break;
01418 }
01419 }
01420 break;
01421
01422 case 18:
01423 {
01424 assert (*text != NULL);
01425 switch (**p)
01426 {
01427 case '\x20':
01428 case '\x09':
01429 case '\x0A':
01430 case '\x0D':
01431 ++*p;
01432 case ']':
01433 case '}':
01434 case ',':
01435 *state = 0;
01436 return LEX_NUMBER;
01437 break;
01438
01439 case '.':
01440 if (rcs_catc (*text, **p) != RS_OK)
01441 return LEX_MEMORY;
01442
01443 ++*p;
01444 *state = 20;
01445 break;
01446
01447 case 'e':
01448 case 'E':
01449 if (rcs_catc (*text, **p) != RS_OK)
01450 return LEX_MEMORY;
01451 ++*p;
01452 *state = 22;
01453 break;
01454
01455 default:
01456 return LEX_INVALID_CHARACTER;
01457 break;
01458 }
01459 }
01460 break;
01461
01462 case 19:
01463 {
01464 assert (*text != NULL);
01465 switch (**p)
01466 {
01467 case '\x20':
01468 case '\x09':
01469 case '\x0A':
01470 case '\x0D':
01471 ++*p;
01472 case ']':
01473 case '}':
01474 case ',':
01475 *state = 0;
01476 return LEX_NUMBER;
01477 break;
01478
01479 case '.':
01480 if (rcs_catc (*text, **p) != RS_OK)
01481 return LEX_MEMORY;
01482 ++*p;
01483 *state = 20;
01484 break;
01485
01486 case 'e':
01487 case 'E':
01488 if (rcs_catc (*text, **p) != RS_OK)
01489 return LEX_MEMORY;
01490 ++*p;
01491 *state = 22;
01492 break;
01493
01494 case '0':
01495 case '1':
01496 case '2':
01497 case '3':
01498 case '4':
01499 case '5':
01500 case '6':
01501 case '7':
01502 case '8':
01503 case '9':
01504 if (rcs_catc (*text, **p) != RS_OK)
01505 return LEX_MEMORY;
01506 ++*p;
01507 break;
01508
01509 default:
01510 return LEX_INVALID_CHARACTER;
01511 break;
01512 }
01513 }
01514 break;
01515
01516 case 20:
01517 {
01518 assert (*text != NULL);
01519 switch (**p)
01520 {
01521 case '0':
01522 case '1':
01523 case '2':
01524 case '3':
01525 case '4':
01526 case '5':
01527 case '6':
01528 case '7':
01529 case '8':
01530 case '9':
01531 if (rcs_catc (*text, **p) != RS_OK)
01532 return LEX_MEMORY;
01533 ++*p;
01534 *state = 21;
01535 break;
01536
01537 default:
01538 return LEX_INVALID_CHARACTER;
01539 break;
01540 }
01541 }
01542 break;
01543
01544 case 21:
01545 {
01546 assert (*text != NULL);
01547 switch (**p)
01548 {
01549 case '\x20':
01550 case '\x09':
01551 case '\x0A':
01552 case '\x0D':
01553 ++*p;
01554 case ']':
01555 case '}':
01556 case ',':
01557 *state = 0;
01558 return LEX_NUMBER;
01559 break;
01560
01561 case 'e':
01562 case 'E':
01563 if (rcs_catc (*text, **p) != RS_OK)
01564 return LEX_MEMORY;
01565 ++*p;
01566 *state = 22;
01567 break;
01568
01569 case '0':
01570 case '1':
01571 case '2':
01572 case '3':
01573 case '4':
01574 case '5':
01575 case '6':
01576 case '7':
01577 case '8':
01578 case '9':
01579 if (rcs_catc (*text, **p) != RS_OK)
01580 return LEX_MEMORY;
01581 ++*p;
01582 break;
01583
01584 default:
01585 return LEX_INVALID_CHARACTER;
01586 break;
01587 }
01588 }
01589 break;
01590
01591 case 22:
01592 {
01593 assert (*text != NULL);
01594 switch (**p)
01595 {
01596 case '-':
01597 case '+':
01598 if (rcs_catc (*text, **p) != RS_OK)
01599 return LEX_MEMORY;
01600 ++*p;
01601 *state = 23;
01602 break;
01603
01604 case '0':
01605 case '1':
01606 case '2':
01607 case '3':
01608 case '4':
01609 case '5':
01610 case '6':
01611 case '7':
01612 case '8':
01613 case '9':
01614 if (rcs_catc (*text, **p) != RS_OK)
01615 return LEX_MEMORY;
01616 ++*p;
01617 *state = 24;
01618 break;
01619
01620 default:
01621 return LEX_INVALID_CHARACTER;
01622 break;
01623 }
01624 }
01625 break;
01626
01627 case 23:
01628 {
01629 assert (*text != NULL);
01630 switch (**p)
01631 {
01632 case '0':
01633 case '1':
01634 case '2':
01635 case '3':
01636 case '4':
01637 case '5':
01638 case '6':
01639 case '7':
01640 case '8':
01641 case '9':
01642 if (rcs_catc (*text, **p) != RS_OK)
01643 return LEX_MEMORY;
01644 ++*p;
01645 *state = 24;
01646 break;
01647
01648 default:
01649 return LEX_INVALID_CHARACTER;
01650 break;
01651 }
01652 }
01653 break;
01654
01655 case 24:
01656 {
01657 assert (*text != NULL);
01658 switch (**p)
01659 {
01660 case '\x20':
01661 case '\x09':
01662 case '\x0A':
01663 case '\x0D':
01664 ++*p;
01665 case ']':
01666 case '}':
01667 case ',':
01668 *state = 0;
01669 return LEX_NUMBER;
01670 break;
01671
01672 case '0':
01673 case '1':
01674 case '2':
01675 case '3':
01676 case '4':
01677 case '5':
01678 case '6':
01679 case '7':
01680 case '8':
01681 case '9':
01682 if (rcs_catc (*text, **p) != RS_OK)
01683 return LEX_MEMORY;
01684 ++*p;
01685 break;
01686
01687 default:
01688 return LEX_INVALID_CHARACTER;
01689 break;
01690 }
01691 }
01692 break;
01693
01694 default:
01695 printf ("*state missing: %d\n", *state);
01696 return LEX_INVALID_CHARACTER;
01697 }
01698
01699 }
01700
01701 *p = NULL;
01702 return LEX_MORE;
01703 }
01704
01705
01706 enum json_error
01707 json_parse_fragment (struct json_parsing_info *info, char *buffer)
01708 {
01709 json_t *temp = NULL;
01710
01711 assert (info != NULL);
01712 assert (buffer != NULL);
01713
01714 info->p = buffer;
01715 while (*info->p != '\0')
01716 {
01717 switch (info->state)
01718 {
01719 case 0:
01720 {
01721 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
01722 {
01723 case LEX_BEGIN_OBJECT:
01724 info->state = 1;
01725 break;
01726
01727 case LEX_BEGIN_ARRAY:
01728 info->state = 7;
01729 break;
01730
01731 default:
01732 printf ("state %d: defaulted\n", info->state);
01733 return JSON_MALFORMED_DOCUMENT;
01734 break;
01735 }
01736 }
01737 break;
01738
01739 case 1:
01740 {
01741 if (info->cursor == NULL)
01742 {
01743 if ((info->cursor = json_new_object ()) == NULL)
01744 {
01745 return JSON_MEMORY;
01746 }
01747 }
01748 else
01749 {
01750
01751 assert ((info->cursor->type == JSON_STRING) || (info->cursor->type == JSON_ARRAY));
01752
01753 if ((temp = json_new_object ()) == NULL)
01754 {
01755 return JSON_MEMORY;
01756 }
01757 if (json_insert_child (info->cursor, temp) != JSON_OK)
01758 {
01759 return JSON_UNKNOWN_PROBLEM;
01760 }
01761 info->cursor = temp;
01762 temp = NULL;
01763 }
01764 info->state = 2;
01765 }
01766 break;
01767
01768 case 2:
01769 {
01770
01771 assert (info->cursor != NULL);
01772 assert (info->cursor->type == JSON_OBJECT);
01773
01774 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
01775 {
01776 case LEX_STRING:
01777 if ((temp = json_new_value (JSON_STRING)) == NULL)
01778 return JSON_MEMORY;
01779 temp->text = rcs_unwrap ((rcstring *) info->lex_text), info->lex_text = NULL;
01780 if (json_insert_child (info->cursor, temp) != JSON_OK)
01781 {
01782
01783 return JSON_UNKNOWN_PROBLEM;
01784 }
01785 info->cursor = temp;
01786 temp = NULL;
01787 info->state = 5;
01788 break;
01789
01790 case LEX_END_OBJECT:
01791 if (info->cursor->parent == NULL)
01792 {
01793 info->state = 99;
01794 }
01795 else
01796 {
01797 info->cursor = info->cursor->parent;
01798 switch (info->cursor->type)
01799 {
01800 case JSON_STRING:
01801
01802 assert (info->cursor->parent != NULL);
01803
01804 info->cursor = info->cursor->parent;
01805 if (info->cursor->type != JSON_OBJECT)
01806 {
01807 return JSON_BAD_TREE_STRUCTURE;
01808 }
01809 else
01810 {
01811 info->state = 3;
01812 }
01813 break;
01814
01815 case JSON_ARRAY:
01816 info->state = 9;
01817 break;
01818
01819 default:
01820 return JSON_BAD_TREE_STRUCTURE;
01821 }
01822 }
01823 break;
01824
01825 case LEX_MORE:
01826 return JSON_INCOMPLETE_DOCUMENT;
01827 break;
01828
01829 default:
01830 printf ("state %d: defaulted\n", info->state);
01831 return JSON_MALFORMED_DOCUMENT;
01832 break;
01833 }
01834 }
01835 break;
01836
01837 case 3:
01838 {
01839
01840 assert (info->cursor != NULL);
01841 assert (info->cursor->type == JSON_OBJECT);
01842
01843 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
01844 {
01845 case LEX_VALUE_SEPARATOR:
01846 info->state = 4;
01847 break;
01848
01849 case LEX_END_OBJECT:
01850 if (info->cursor->parent == NULL)
01851 {
01852 info->state = 99;
01853 }
01854 else
01855 {
01856 info->cursor = info->cursor->parent;
01857 switch (info->cursor->type)
01858 {
01859 case JSON_STRING:
01860
01861 assert (info->cursor->parent != NULL);
01862
01863 info->cursor = info->cursor->parent;
01864 if (info->cursor->type != JSON_OBJECT)
01865 {
01866 return JSON_BAD_TREE_STRUCTURE;
01867 }
01868 else
01869 {
01870 info->state = 3;
01871 }
01872 break;
01873
01874 case JSON_ARRAY:
01875 info->state = 9;
01876 break;
01877
01878 default:
01879 return JSON_BAD_TREE_STRUCTURE;
01880 }
01881 }
01882 break;
01883
01884 case LEX_MORE:
01885 return JSON_INCOMPLETE_DOCUMENT;
01886 break;
01887
01888 default:
01889 printf ("state %d: defaulted\n", info->state);
01890 return JSON_MALFORMED_DOCUMENT;
01891 break;
01892 }
01893 }
01894 break;
01895
01896 case 4:
01897 {
01898 assert (info->cursor != NULL);
01899 assert (info->cursor->type == JSON_OBJECT);
01900
01901 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
01902 {
01903 case LEX_STRING:
01904 if ((temp = json_new_value (JSON_STRING)) == NULL)
01905 return JSON_MEMORY;
01906 temp->text = rcs_unwrap ((rcstring *) info->lex_text), info->lex_text = NULL;
01907 if (json_insert_child (info->cursor, temp) != JSON_OK)
01908 {
01909 return JSON_UNKNOWN_PROBLEM;
01910 }
01911 info->cursor = temp;
01912 temp = NULL;
01913 info->state = 5;
01914 break;
01915
01916 case LEX_MORE:
01917 return JSON_INCOMPLETE_DOCUMENT;
01918 break;
01919
01920 default:
01921 printf ("state %d: defaulted\n", info->state);
01922 return JSON_MALFORMED_DOCUMENT;
01923 break;
01924 }
01925 }
01926 break;
01927
01928 case 5:
01929 {
01930
01931 assert (info->cursor != NULL);
01932 assert (info->cursor->type == JSON_STRING);
01933
01934 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
01935 {
01936 case LEX_NAME_SEPARATOR:
01937 info->state = 6;
01938 break;
01939
01940 case LEX_MORE:
01941 return JSON_INCOMPLETE_DOCUMENT;
01942 break;
01943
01944 default:
01945 printf ("state %d: defaulted\n", info->state);
01946 return JSON_MALFORMED_DOCUMENT;
01947 break;
01948 }
01949 }
01950 break;
01951
01952 case 6:
01953 {
01954 unsigned int value;
01955
01956 assert (info->cursor != NULL);
01957 assert (info->cursor->type == JSON_STRING);
01958
01959 switch (value = lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
01960 {
01961 case LEX_STRING:
01962 if ((temp = json_new_value (JSON_STRING)) == NULL)
01963 return JSON_MEMORY;
01964 temp->text = rcs_unwrap ((rcstring *) info->lex_text), info->lex_text = NULL;
01965 if (json_insert_child (info->cursor, temp) != JSON_OK)
01966 {
01967
01968 return JSON_UNKNOWN_PROBLEM;
01969 }
01970 if (info->cursor->parent == NULL)
01971 {
01972 info->state = 99;
01973 }
01974 else
01975 {
01976 info->cursor = info->cursor->parent;
01977 }
01978 temp = NULL;
01979 info->state = 3;
01980 break;
01981
01982 case LEX_NUMBER:
01983 if ((temp = json_new_value (JSON_NUMBER)) == NULL)
01984 return JSON_MEMORY;
01985 temp->text = rcs_unwrap ((rcstring *) info->lex_text), info->lex_text = NULL;
01986 if (json_insert_child (info->cursor, temp) != JSON_OK)
01987 {
01988
01989 return JSON_UNKNOWN_PROBLEM;
01990 }
01991 if (info->cursor->parent == NULL)
01992 {
01993 info->state = 99;
01994 }
01995 else
01996 {
01997 info->cursor = info->cursor->parent;
01998 }
01999 temp = NULL;
02000 info->state = 3;
02001 break;
02002
02003 case LEX_TRUE:
02004 if ((temp = json_new_value (JSON_TRUE)) == NULL)
02005 return JSON_MEMORY;
02006 if (json_insert_child (info->cursor, temp) != JSON_OK)
02007 {
02008
02009 return JSON_UNKNOWN_PROBLEM;
02010 }
02011 if (info->cursor->parent == NULL)
02012 {
02013 info->state = 99;
02014 }
02015 else
02016 {
02017 info->cursor = info->cursor->parent;
02018 }
02019 temp = NULL;
02020 info->state = 3;
02021 break;
02022
02023 case LEX_FALSE:
02024 if ((temp = json_new_value (JSON_FALSE)) == NULL)
02025 return JSON_MEMORY;
02026 if (json_insert_child (info->cursor, temp) != JSON_OK)
02027 {
02028
02029 return JSON_UNKNOWN_PROBLEM;
02030 }
02031 if (info->cursor->parent == NULL)
02032 {
02033 info->state = 99;
02034 }
02035 else
02036 {
02037 info->cursor = info->cursor->parent;
02038 }
02039 temp = NULL;
02040 info->state = 3;
02041 break;
02042
02043 case LEX_NULL:
02044 if ((temp = json_new_value (JSON_NULL)) == NULL)
02045 return JSON_MEMORY;
02046 if (json_insert_child (info->cursor, temp) != JSON_OK)
02047 {
02048
02049 return JSON_UNKNOWN_PROBLEM;
02050 }
02051 if (info->cursor->parent == NULL)
02052 {
02053 info->state = 99;
02054 }
02055 else
02056 {
02057 info->cursor = info->cursor->parent;
02058 }
02059 temp = NULL;
02060 info->state = 3;
02061 break;
02062
02063 case LEX_BEGIN_OBJECT:
02064 info->state = 1;
02065 break;
02066
02067 case LEX_BEGIN_ARRAY:
02068 info->state = 7;
02069 break;
02070
02071 case LEX_MORE:
02072 return JSON_INCOMPLETE_DOCUMENT;
02073 break;
02074
02075 case LEX_MEMORY:
02076 return JSON_MEMORY;
02077 break;
02078
02079 default:
02080 printf ("state %d: defaulted\n", info->state);
02081 return JSON_MALFORMED_DOCUMENT;
02082 break;
02083 }
02084 }
02085 break;
02086
02087 case 7:
02088 {
02089 if (info->cursor == NULL)
02090 {
02091 if ((info->cursor = json_new_array ()) == NULL)
02092 {
02093 return JSON_MEMORY;
02094 }
02095 }
02096 else
02097 {
02098
02099 assert ((info->cursor->type == JSON_ARRAY) || (info->cursor->type == JSON_STRING));
02100
02101 if ((temp = json_new_array ()) == NULL)
02102 {
02103 return JSON_MEMORY;
02104 }
02105 if (json_insert_child (info->cursor, temp) != JSON_OK)
02106 {
02107 return JSON_UNKNOWN_PROBLEM;
02108 }
02109 info->cursor = temp;
02110 temp = NULL;
02111 }
02112 info->state = 8;
02113 }
02114 break;
02115
02116 case 8:
02117 {
02118
02119 assert (info->cursor != NULL);
02120 assert (info->cursor->type == JSON_ARRAY);
02121
02122 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
02123 {
02124 case LEX_STRING:
02125 if ((temp = json_new_value (JSON_STRING)) == NULL)
02126 return JSON_MEMORY;
02127 temp->text = rcs_unwrap ((rcstring *) info->lex_text), info->lex_text = NULL;
02128 if (json_insert_child (info->cursor, temp) != JSON_OK)
02129 {
02130 return JSON_UNKNOWN_PROBLEM;
02131 }
02132 temp = NULL;
02133 info->state = 9;
02134 break;
02135
02136 case LEX_NUMBER:
02137 if ((temp = json_new_value (JSON_NUMBER)) == NULL)
02138 return JSON_MEMORY;
02139 temp->text = rcs_unwrap ((rcstring *) info->lex_text), info->lex_text = NULL;
02140 if (json_insert_child (info->cursor, temp) != JSON_OK)
02141 {
02142 return JSON_UNKNOWN_PROBLEM;
02143 }
02144 temp = NULL;
02145 info->state = 9;
02146 break;
02147
02148 case LEX_TRUE:
02149 if ((temp = json_new_value (JSON_TRUE)) == NULL)
02150 return JSON_MEMORY;
02151 if (json_insert_child (info->cursor, temp) != JSON_OK)
02152 {
02153 return JSON_UNKNOWN_PROBLEM;
02154 }
02155 info->state = 9;
02156 break;
02157
02158 case LEX_FALSE:
02159 if ((temp = json_new_value (JSON_FALSE)) == NULL)
02160 return JSON_MEMORY;
02161 if (json_insert_child (info->cursor, temp) != JSON_OK)
02162 {
02163 return JSON_UNKNOWN_PROBLEM;
02164 }
02165 info->state = 9;
02166 break;
02167
02168 case LEX_NULL:
02169 if ((temp = json_new_value (JSON_NULL)) == NULL)
02170 return JSON_MEMORY;
02171 if (json_insert_child (info->cursor, temp) != JSON_OK)
02172 {
02173 return JSON_UNKNOWN_PROBLEM;
02174 }
02175 info->state = 9;
02176 break;
02177
02178 case LEX_BEGIN_ARRAY:
02179 info->state = 7;
02180 break;
02181
02182 case LEX_END_ARRAY:
02183 if (info->cursor->parent == NULL)
02184 {
02185
02186 info->state = 99;
02187 }
02188 else
02189 {
02190 info->cursor = info->cursor->parent;
02191 switch (info->cursor->type)
02192 {
02193 case JSON_STRING:
02194 if (info->cursor->parent == NULL)
02195 return JSON_BAD_TREE_STRUCTURE;
02196 else
02197 {
02198 info->cursor = info->cursor->parent;
02199 if (info->cursor->type != JSON_OBJECT)
02200 {
02201 return JSON_BAD_TREE_STRUCTURE;
02202 }
02203
02204 info->state = 3;
02205 }
02206 break;
02207
02208 case JSON_ARRAY:
02209 info->state = 9;
02210 break;
02211
02212 default:
02213 return JSON_BAD_TREE_STRUCTURE;
02214 }
02215 }
02216 break;
02217
02218 case LEX_BEGIN_OBJECT:
02219 info->state = 1;
02220 break;
02221
02222 case LEX_MORE:
02223 return JSON_INCOMPLETE_DOCUMENT;
02224 break;
02225
02226 default:
02227 printf ("state %d: defaulted\n", info->state);
02228 return JSON_MALFORMED_DOCUMENT;
02229 break;
02230 }
02231 }
02232 break;
02233
02234 case 9:
02235 {
02236
02237 assert (info->cursor != NULL);
02238 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
02239 {
02240 case LEX_VALUE_SEPARATOR:
02241 info->state = 8;
02242 break;
02243
02244 case LEX_END_ARRAY:
02245 if (info->cursor->parent == NULL)
02246 {
02247 info->state = 99;
02248 }
02249 else
02250 {
02251 info->cursor = info->cursor->parent;
02252 switch (info->cursor->type)
02253 {
02254 case JSON_STRING:
02255 if (info->cursor->parent == NULL)
02256 {
02257 info->state = 99;
02258 }
02259 else
02260 {
02261 info->cursor = info->cursor->parent;
02262 if (info->cursor->type != JSON_OBJECT)
02263 {
02264 return JSON_BAD_TREE_STRUCTURE;
02265 }
02266 else
02267 {
02268 info->state = 3;
02269 }
02270 }
02271 break;
02272
02273 case JSON_ARRAY:
02274 info->state = 9;
02275 break;
02276
02277 default:
02278 return JSON_BAD_TREE_STRUCTURE;
02279 }
02280 }
02281 break;
02282
02283 case LEX_MORE:
02284 return JSON_INCOMPLETE_DOCUMENT;
02285 break;
02286
02287 default:
02288 printf ("state %d: defaulted\n", info->state);
02289 return JSON_MALFORMED_DOCUMENT;
02290 break;
02291 }
02292 }
02293 break;
02294
02295 case 99:
02296 {
02297
02298 assert (info->cursor->parent == NULL);
02299 switch (lexer (buffer, &info->p, &info->lex_state, (rcstring **) & info->lex_text))
02300 {
02301 case LEX_MORE:
02302 return JSON_WAITING_FOR_EOF;
02303 break;
02304
02305 case LEX_MEMORY:
02306 return JSON_MEMORY;
02307 break;
02308
02309 default:
02310 return JSON_MALFORMED_DOCUMENT;
02311 break;
02312 }
02313 }
02314 break;
02315
02316 default:
02317 printf ("invalid parser state %d: defaulted\n", info->state);
02318 return JSON_UNKNOWN_PROBLEM;
02319 }
02320 }
02321 info->p = NULL;
02322 if (info->state == 99)
02323 return JSON_WAITING_FOR_EOF;
02324 else
02325 return JSON_INCOMPLETE_DOCUMENT;
02326 }
02327
02328
02329
02330 enum json_error json_parse_document (json_t **root, char *text)
02331 {
02332 enum json_error error;
02333 struct json_parsing_info *jpi;
02334
02335 assert(root != NULL);
02336 assert(*root == NULL);
02337 assert(text != NULL);
02338
02339
02340 jpi = malloc(sizeof(struct json_parsing_info));
02341 if(jpi == NULL)
02342 {
02343 return JSON_MEMORY;
02344 }
02345 json_jpi_init (jpi);
02346
02347 error = json_parse_fragment (jpi, text);
02348 if ( (error == JSON_WAITING_FOR_EOF) || (error == JSON_OK))
02349 {
02350 *root = jpi->cursor;
02351 free(jpi);
02352 return JSON_OK;
02353 }
02354 else
02355 {
02356 free(jpi);
02357 return error;
02358 }
02359 }
02360
02361
02362 enum json_error
02363 json_saxy_parse (struct json_saxy_parser_status *jsps, struct json_saxy_functions *jsf, char c)
02364 {
02365
02366
02367 rcstring *temp;
02368
02369
02370 assert (jsps != NULL);
02371 assert (jsf != NULL);
02372 temp = NULL;
02373
02374
02375 switch (jsps->state)
02376 {
02377 case 0:
02378 goto state0;
02379 break;
02380 case 1:
02381 goto state1;
02382 break;
02383 case 2:
02384 goto state2;
02385 break;
02386 case 3:
02387 goto state3;
02388 break;
02389 case 4:
02390 goto state4;
02391 break;
02392 case 5:
02393 goto state5;
02394 break;
02395 case 6:
02396 goto state6;
02397 break;
02398 case 7:
02399 goto state7;
02400 break;
02401 case 8:
02402 goto state8;
02403 break;
02404 case 9:
02405 goto state9;
02406 break;
02407 case 10:
02408 goto state10;
02409 break;
02410 case 11:
02411 goto state11;
02412 break;
02413 case 12:
02414 goto state12;
02415 break;
02416 case 13:
02417 goto state13;
02418 break;
02419 case 14:
02420 goto state14;
02421 break;
02422 case 15:
02423 goto state15;
02424 break;
02425 case 16:
02426 goto state16;
02427 break;
02428 case 17:
02429 goto state17;
02430 break;
02431 case 18:
02432 goto state18;
02433 break;
02434 case 19:
02435 goto state19;
02436 break;
02437 case 20:
02438 goto state20;
02439 break;
02440 case 21:
02441 goto state21;
02442 break;
02443 case 22:
02444 goto state22;
02445 break;
02446 case 23:
02447 goto state23;
02448 break;
02449 case 24:
02450 goto state24;
02451 break;
02452 case 25:
02453 goto state25;
02454 break;
02455 case 26:
02456 goto state26;
02457 break;
02458 case 27:
02459 goto state27;
02460 break;
02461
02462 default:
02463 return JSON_UNKNOWN_PROBLEM;
02464 }
02465
02466 state0:
02467 {
02468 switch (c)
02469 {
02470 case '\x20':
02471 case '\x09':
02472 case '\x0A':
02473 case '\x0D':
02474 break;
02475
02476 case '\"':
02477 jsps->string_length_limit_reached = 0;
02478 jsps->state = 1;
02479 break;
02480
02481 case '{':
02482 if (jsf->open_object != NULL)
02483 jsf->open_object ();
02484 jsps->state = 25;
02485 break;
02486
02487 case '}':
02488 if (jsf->close_object != NULL)
02489 jsf->close_object ();
02490 jsps->state = 26;
02491 break;
02492
02493 case '[':
02494 if (jsf->open_array != NULL)
02495 jsf->open_array ();
02496
02497 break;
02498
02499 case ']':
02500 if (jsf->close_array != NULL)
02501 jsf->close_array ();
02502 jsps->state = 26;
02503 break;
02504
02505 case 't':
02506 jsps->state = 7;
02507 break;
02508
02509 case 'f':
02510 jsps->state = 10;
02511 break;
02512
02513 case 'n':
02514 jsps->state = 14;
02515 break;
02516
02517 case ':':
02518 if (jsf->label_value_separator != NULL)
02519 jsf->label_value_separator ();
02520
02521 break;
02522
02523 case ',':
02524 if (jsf->sibling_separator != NULL)
02525 jsf->sibling_separator ();
02526 jsps->state = 27;
02527 break;
02528
02529 case '0':
02530 jsps->string_length_limit_reached = 0;
02531 jsps->state = 17;
02532 if ((jsps->temp = rcs_create (5)) == NULL)
02533 {
02534 return JSON_MEMORY;
02535 }
02536 if (rcs_catc (((rcstring *) jsps->temp), '0') != RS_OK)
02537 {
02538 return JSON_MEMORY;
02539 }
02540 break;
02541
02542 case '1':
02543 case '2':
02544 case '3':
02545 case '4':
02546 case '5':
02547 case '6':
02548 case '7':
02549 case '8':
02550 case '9':
02551 jsps->string_length_limit_reached = 0;
02552 jsps->state = 24;
02553 if ((jsps->temp = rcs_create (5)) == NULL)
02554 {
02555 return JSON_MEMORY;
02556 }
02557 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
02558 {
02559 return JSON_MEMORY;
02560 }
02561 break;
02562
02563 case '-':
02564 jsps->string_length_limit_reached = 0;
02565 jsps->state = 23;
02566 jsps->temp = NULL;
02567 if ((jsps->temp = rcs_create (5)) == NULL)
02568 {
02569 return JSON_MEMORY;
02570 }
02571 if (rcs_catc (((rcstring *) jsps->temp), '-') != RS_OK)
02572 {
02573 return JSON_MEMORY;
02574 }
02575
02576 break;
02577
02578 default:
02579 return JSON_ILLEGAL_CHARACTER;
02580 break;
02581 }
02582 return JSON_OK;
02583 }
02584
02585 state1:
02586 {
02587 switch (c)
02588 {
02589 case '\\':
02590 if (!jsps->string_length_limit_reached)
02591 {
02592 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH - 1)
02593 {
02594 if (rcs_catc (((rcstring *) jsps->temp), '\\') != RS_OK)
02595 {
02596 return JSON_MEMORY;
02597 }
02598 }
02599 else
02600 {
02601 jsps->string_length_limit_reached = 1;
02602 }
02603 }
02604 jsps->state = 2;
02605 break;
02606
02607 case '\"':
02608 if (((rcstring *) jsps->temp) != NULL)
02609 {
02610 jsps->state = 0;
02611 if (jsf->new_string != NULL)
02612 jsf->new_string (((rcstring *) ((rcstring *) jsps->temp))->text);
02613 rcs_free ((rcstring **) & jsps->temp);
02614 }
02615 else
02616 return JSON_UNKNOWN_PROBLEM;
02617 break;
02618
02619 default:
02620 if (!jsps->string_length_limit_reached)
02621 {
02622 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH)
02623 {
02624 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
02625 {
02626 return JSON_MEMORY;
02627 }
02628 }
02629 else
02630 {
02631 jsps->string_length_limit_reached = 1;
02632 }
02633 }
02634 break;
02635 }
02636 return JSON_OK;
02637 }
02638
02639 state2:
02640 {
02641 switch (c)
02642 {
02643 case '\"':
02644 case '\\':
02645 case '/':
02646 case 'b':
02647 case 'f':
02648 case 'n':
02649 case 'r':
02650 case 't':
02651 if (!jsps->string_length_limit_reached)
02652 {
02653 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH)
02654 {
02655 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
02656 {
02657 return JSON_MEMORY;
02658 }
02659 }
02660 else
02661 {
02662 jsps->string_length_limit_reached = 1;
02663 }
02664 }
02665 break;
02666
02667 case 'u':
02668 if (!jsps->string_length_limit_reached)
02669 {
02670 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH - 4)
02671 {
02672 if (rcs_catc (((rcstring *) jsps->temp), 'u') != RS_OK)
02673 {
02674 return JSON_MEMORY;
02675 }
02676 }
02677 else
02678 {
02679 jsps->string_length_limit_reached = 1;
02680 }
02681 }
02682 jsps->state = 3;
02683 break;
02684
02685 default:
02686 return JSON_ILLEGAL_CHARACTER;
02687 break;
02688 }
02689 return JSON_OK;
02690 }
02691
02692 state3:
02693 {
02694 switch (c)
02695 {
02696 case '0':
02697 case '1':
02698 case '2':
02699 case '3':
02700 case '4':
02701 case '5':
02702 case '6':
02703 case '7':
02704 case '8':
02705 case '9':
02706 case 'a':
02707 case 'b':
02708 case 'c':
02709 case 'd':
02710 case 'e':
02711 case 'f':
02712 case 'A':
02713 case 'B':
02714 case 'C':
02715 case 'D':
02716 case 'E':
02717 case 'F':
02718 if (!jsps->string_length_limit_reached)
02719 {
02720 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH - 3)
02721 {
02722 if (rcs_catc (((rcstring *) jsps->temp), 'u') != RS_OK)
02723 {
02724 return JSON_MEMORY;
02725 }
02726 }
02727 else
02728 {
02729 jsps->string_length_limit_reached = 1;
02730 }
02731 }
02732 jsps->state = 4;
02733 break;
02734
02735 default:
02736 return JSON_ILLEGAL_CHARACTER;
02737 }
02738 return JSON_OK;
02739 }
02740
02741 state4:
02742 {
02743 switch (c)
02744 {
02745 case '0':
02746 case '1':
02747 case '2':
02748 case '3':
02749 case '4':
02750 case '5':
02751 case '6':
02752 case '7':
02753 case '8':
02754 case '9':
02755 case 'a':
02756 case 'b':
02757 case 'c':
02758 case 'd':
02759 case 'e':
02760 case 'f':
02761 case 'A':
02762 case 'B':
02763 case 'C':
02764 case 'D':
02765 case 'E':
02766 case 'F':
02767 if (!jsps->string_length_limit_reached)
02768 {
02769 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH - 2)
02770 {
02771 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
02772 {
02773 return JSON_MEMORY;
02774 }
02775 }
02776 else
02777 {
02778 jsps->string_length_limit_reached = 1;
02779 }
02780 }
02781 jsps->state = 5;
02782 break;
02783
02784 default:
02785 return JSON_ILLEGAL_CHARACTER;
02786 }
02787 return JSON_OK;
02788 }
02789
02790 state5:
02791 {
02792 switch (c)
02793 {
02794 case '0':
02795 case '1':
02796 case '2':
02797 case '3':
02798 case '4':
02799 case '5':
02800 case '6':
02801 case '7':
02802 case '8':
02803 case '9':
02804 case 'a':
02805 case 'b':
02806 case 'c':
02807 case 'd':
02808 case 'e':
02809 case 'f':
02810 case 'A':
02811 case 'B':
02812 case 'C':
02813 case 'D':
02814 case 'E':
02815 case 'F':
02816 if (!jsps->string_length_limit_reached)
02817 {
02818 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH - 1)
02819 {
02820 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
02821 {
02822 return JSON_MEMORY;
02823 }
02824 }
02825 else
02826 {
02827 jsps->string_length_limit_reached = 1;
02828 }
02829 }
02830 jsps->state = 6;
02831 break;
02832
02833 default:
02834 return JSON_ILLEGAL_CHARACTER;
02835 }
02836 return JSON_OK;
02837 }
02838
02839 state6:
02840 {
02841 switch (c)
02842 {
02843 case '0':
02844 case '1':
02845 case '2':
02846 case '3':
02847 case '4':
02848 case '5':
02849 case '6':
02850 case '7':
02851 case '8':
02852 case '9':
02853 case 'a':
02854 case 'b':
02855 case 'c':
02856 case 'd':
02857 case 'e':
02858 case 'f':
02859 case 'A':
02860 case 'B':
02861 case 'C':
02862 case 'D':
02863 case 'E':
02864 case 'F':
02865 if (!jsps->string_length_limit_reached)
02866 {
02867 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH)
02868 {
02869 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
02870 {
02871 return JSON_MEMORY;
02872 }
02873 }
02874 else
02875 {
02876 jsps->string_length_limit_reached = 1;
02877 }
02878 }
02879 jsps->state = 1;
02880 break;
02881
02882 default:
02883 return JSON_ILLEGAL_CHARACTER;
02884 }
02885 return JSON_OK;
02886 }
02887
02888 state7:
02889 {
02890 if (c != 'r')
02891 {
02892 return JSON_ILLEGAL_CHARACTER;
02893 }
02894
02895 jsps->state = 8;
02896 return JSON_OK;
02897 }
02898
02899 state8:
02900 {
02901 if (c != 'u')
02902 {
02903 return JSON_ILLEGAL_CHARACTER;
02904 }
02905
02906 jsps->state = 9;
02907 return JSON_OK;
02908 }
02909
02910 state9:
02911 {
02912 if (c != 'e')
02913 {
02914 return JSON_ILLEGAL_CHARACTER;
02915 }
02916
02917 jsps->state = 0;
02918 if (jsf->new_true != NULL)
02919 jsf->new_true ();
02920 return JSON_OK;
02921 }
02922
02923 state10:
02924 {
02925 if (c != 'a')
02926 {
02927 return JSON_ILLEGAL_CHARACTER;
02928 }
02929
02930 jsps->state = 11;
02931 return JSON_OK;
02932 }
02933
02934 state11:
02935 {
02936 if (c != 'l')
02937 {
02938 return JSON_ILLEGAL_CHARACTER;
02939 }
02940
02941 jsps->state = 12;
02942 return JSON_OK;
02943 }
02944
02945 state12:
02946 {
02947 if (c != 's')
02948 {
02949 return JSON_ILLEGAL_CHARACTER;
02950 }
02951
02952 jsps->state = 13;
02953 return JSON_OK;
02954 }
02955
02956 state13:
02957 {
02958 if (c != 'e')
02959 {
02960 return JSON_ILLEGAL_CHARACTER;
02961 }
02962
02963 jsps->state = 0;
02964 if (jsf->new_false != NULL)
02965 jsf->new_false ();
02966 return JSON_OK;
02967 }
02968
02969 state14:
02970 {
02971 if (c != 'u')
02972 {
02973 return JSON_ILLEGAL_CHARACTER;
02974 }
02975
02976 jsps->state = 15;
02977 return JSON_OK;
02978 }
02979
02980 state15:
02981 {
02982 if (c != 'l')
02983 {
02984 return JSON_ILLEGAL_CHARACTER;
02985 }
02986
02987 jsps->state = 16;
02988 return JSON_OK;
02989 }
02990
02991 state16:
02992 {
02993 if (c != 'l')
02994 {
02995 return JSON_ILLEGAL_CHARACTER;
02996 }
02997
02998 jsps->state = 0;
02999 if (jsf->new_null != NULL)
03000 jsf->new_null ();
03001 return JSON_OK;
03002 }
03003
03004 state17:
03005 {
03006 switch (c)
03007 {
03008 case '.':
03009 if ((jsps->temp = rcs_create (5)) == NULL)
03010 {
03011 return JSON_MEMORY;
03012 }
03013 if (rcs_catc (((rcstring *) jsps->temp), '.') != RS_OK)
03014 {
03015 return JSON_MEMORY;
03016 }
03017 jsps->state = 18;
03018 break;
03019
03020 case '\x20':
03021 case '\x09':
03022 case '\x0A':
03023 case '\x0D':
03024 if (((rcstring *) jsps->temp) == NULL)
03025 return JSON_MEMORY;
03026 if (jsf->new_number != NULL)
03027 {
03028 jsf->new_number (((rcstring *) jsps->temp)->text);
03029 }
03030 rcs_free ((rcstring **) & jsps->temp);
03031
03032 jsps->state = 0;
03033 break;
03034
03035 case '}':
03036 if (((rcstring *) jsps->temp) == NULL)
03037 return JSON_MEMORY;
03038 if (jsf->new_number != NULL)
03039 {
03040 jsf->new_number (((rcstring *) jsps->temp)->text);
03041 }
03042 rcs_free ((rcstring **) & jsps->temp);
03043
03044 if (jsf->open_object != NULL)
03045 jsf->close_object ();
03046 jsps->state = 26;
03047 break;
03048
03049 case ']':
03050
03051 if (((rcstring *) jsps->temp) == NULL)
03052 return JSON_MEMORY;
03053 if (jsf->new_number != NULL)
03054 {
03055 jsf->new_number (((rcstring *) jsps->temp)->text);
03056 }
03057 rcs_free ((rcstring **) & jsps->temp);
03058
03059 if (jsf->open_object != NULL)
03060 jsf->close_array ();
03061 jsps->state = 26;
03062 break;
03063
03064 case ',':
03065
03066 if (((rcstring *) jsps->temp) == NULL)
03067 return JSON_MEMORY;
03068 if (jsf->new_number != NULL)
03069 {
03070 jsf->new_number (((rcstring *) jsps->temp)->text);
03071 }
03072 rcs_free ((rcstring **) & jsps->temp);
03073
03074 if (jsf->open_object != NULL)
03075 jsf->label_value_separator ();
03076 jsps->state = 27;
03077 break;
03078
03079 default:
03080 return JSON_ILLEGAL_CHARACTER;
03081 break;
03082 }
03083
03084 return JSON_OK;
03085 }
03086
03087 state18:
03088 {
03089 switch (c)
03090 {
03091 case '0':
03092 case '1':
03093 case '2':
03094 case '3':
03095 case '4':
03096 case '5':
03097 case '6':
03098 case '7':
03099 case '8':
03100 case '9':
03101 if (!jsps->string_length_limit_reached)
03102 {
03103 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
03104 {
03105 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03106 {
03107 return JSON_MEMORY;
03108 }
03109 }
03110 else
03111 {
03112 jsps->string_length_limit_reached = 1;
03113 }
03114 }
03115 jsps->state = 19;
03116 break;
03117
03118 default:
03119 return JSON_ILLEGAL_CHARACTER;
03120 break;
03121 }
03122 return JSON_OK;
03123 }
03124
03125 state19:
03126 {
03127 switch (c)
03128 {
03129 case '0':
03130 case '1':
03131 case '2':
03132 case '3':
03133 case '4':
03134 case '5':
03135 case '6':
03136 case '7':
03137 case '8':
03138 case '9':
03139 if (!jsps->string_length_limit_reached)
03140 {
03141 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
03142 {
03143 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03144 {
03145 return JSON_MEMORY;
03146 }
03147 }
03148 else
03149 {
03150 jsps->string_length_limit_reached = 1;
03151 }
03152 }
03153
03154 break;
03155
03156 case 'e':
03157 case 'E':
03158 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03159 {
03160 return JSON_MEMORY;
03161 }
03162
03163 jsps->state = 20;
03164 break;
03165
03166
03167 case '\x20':
03168 case '\x09':
03169 case '\x0A':
03170 case '\x0D':
03171
03172 if (((rcstring *) jsps->temp) == NULL)
03173 return JSON_MEMORY;
03174 if (jsf->new_number != NULL)
03175 {
03176 jsf->new_number (((rcstring *) jsps->temp)->text);
03177 }
03178 rcs_free ((rcstring **) & jsps->temp);
03179
03180 jsps->state = 0;
03181 break;
03182
03183 case '}':
03184
03185 if (((rcstring *) jsps->temp) == NULL)
03186 return JSON_MEMORY;
03187 if (jsf->new_number != NULL)
03188 {
03189 jsf->new_number (((rcstring *) jsps->temp)->text);
03190 }
03191 rcs_free ((rcstring **) & jsps->temp);
03192
03193 if (jsf->open_object != NULL)
03194 jsf->close_object ();
03195 jsps->state = 26;
03196 break;
03197
03198 case ']':
03199 if (jsf->new_number != NULL)
03200 {
03201 if (((rcstring *) jsps->temp) == NULL)
03202 return JSON_MEMORY;
03203 jsf->new_number (((rcstring *) jsps->temp)->text);
03204 rcs_free ((rcstring **) & jsps->temp);
03205 }
03206 else
03207 {
03208 rcs_free ((rcstring **) & jsps->temp);
03209 jsps->temp = NULL;
03210 }
03211 if (jsf->open_object != NULL)
03212 jsf->close_array ();
03213 jsps->state = 26;
03214 break;
03215
03216 case ',':
03217
03218 if (((rcstring *) jsps->temp) == NULL)
03219 return JSON_MEMORY;
03220 if (jsf->new_number != NULL)
03221 {
03222 jsf->new_number (((rcstring *) jsps->temp)->text);
03223 }
03224 rcs_free ((rcstring **) & jsps->temp);
03225
03226 if (jsf->label_value_separator != NULL)
03227 jsf->label_value_separator ();
03228 jsps->state = 27;
03229 break;
03230
03231
03232 default:
03233 return JSON_ILLEGAL_CHARACTER;
03234 break;
03235 }
03236 return JSON_OK;
03237 }
03238
03239 state20:
03240 {
03241 switch (c)
03242 {
03243 case '+':
03244 case '-':
03245 jsps->string_length_limit_reached = 0;
03246 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03247 {
03248 return JSON_MEMORY;
03249 }
03250
03251 jsps->state = 22;
03252 break;
03253
03254 case '0':
03255 case '1':
03256 case '2':
03257 case '3':
03258 case '4':
03259 case '5':
03260 case '6':
03261 case '7':
03262 case '8':
03263 case '9':
03264 if (!jsps->string_length_limit_reached)
03265 {
03266 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH)
03267 {
03268 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03269 {
03270 return JSON_MEMORY;
03271 }
03272
03273 }
03274 else
03275 {
03276 jsps->string_length_limit_reached = 1;
03277 }
03278 }
03279 jsps->state = 21;
03280 break;
03281
03282 default:
03283 return JSON_ILLEGAL_CHARACTER;
03284 break;
03285 }
03286 return JSON_OK;
03287 }
03288
03289 state21:
03290 {
03291 switch (c)
03292 {
03293 case '0':
03294 case '1':
03295 case '2':
03296 case '3':
03297 case '4':
03298 case '5':
03299 case '6':
03300 case '7':
03301 case '8':
03302 case '9':
03303 if (!jsps->string_length_limit_reached)
03304 {
03305 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH)
03306 {
03307 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03308 {
03309 return JSON_MEMORY;
03310 }
03311 }
03312 else
03313 {
03314 jsps->string_length_limit_reached = 1;
03315 }
03316 }
03317
03318 break;
03319
03320 case '\x20':
03321 case '\x09':
03322 case '\x0A':
03323 case '\x0D':
03324
03325 if (((rcstring *) jsps->temp) == NULL)
03326 return JSON_MEMORY;
03327 if (jsf->new_number != NULL)
03328 {
03329 jsf->new_number (((rcstring *) jsps->temp)->text);
03330 }
03331 rcs_free ((rcstring **) & jsps->temp);
03332
03333 jsps->state = 0;
03334 break;
03335
03336 case '}':
03337 if (((rcstring *) jsps->temp) == NULL)
03338 return JSON_MEMORY;
03339 if (jsf->new_number != NULL)
03340 {
03341 jsf->new_number (((rcstring *) jsps->temp)->text);
03342 }
03343 rcs_free ((rcstring **) & jsps->temp);
03344
03345 if (jsf->open_object != NULL)
03346 jsf->close_object ();
03347 jsps->state = 26;
03348 break;
03349
03350 case ']':
03351 if (jsf->new_number != NULL)
03352 {
03353 if (((rcstring *) jsps->temp) == NULL)
03354 return JSON_MEMORY;
03355 jsf->new_number (((rcstring *) jsps->temp)->text);
03356 free ((rcstring *) jsps->temp);
03357 jsps->temp = NULL;
03358 }
03359 else
03360 {
03361 free (((rcstring *) jsps->temp));
03362 jsps->temp = NULL;
03363 }
03364 if (jsf->open_object != NULL)
03365 jsf->close_array ();
03366 jsps->state = 26;
03367 break;
03368
03369 case ',':
03370 if (jsf->new_number != NULL)
03371 {
03372 if (((rcstring *) jsps->temp) == NULL)
03373 return JSON_MEMORY;
03374 jsf->new_number (((rcstring *) jsps->temp)->text);
03375 free (((rcstring *) jsps->temp));
03376 jsps->temp = NULL;
03377 }
03378 else
03379 {
03380 free ((rcstring *) jsps->temp);
03381 jsps->temp = NULL;
03382 }
03383 if (jsf->label_value_separator != NULL)
03384 jsf->label_value_separator ();
03385 jsps->state = 27;
03386 break;
03387
03388 default:
03389 return JSON_ILLEGAL_CHARACTER;
03390 break;
03391 }
03392 return JSON_OK;
03393 }
03394
03395 state22:
03396 {
03397 switch (c)
03398 {
03399 case '0':
03400 case '1':
03401 case '2':
03402 case '3':
03403 case '4':
03404 case '5':
03405 case '6':
03406 case '7':
03407 case '8':
03408 case '9':
03409 if (!jsps->string_length_limit_reached)
03410 {
03411 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH)
03412 {
03413 rcs_catc (((rcstring *) jsps->temp), c);
03414 }
03415 else
03416 {
03417 jsps->string_length_limit_reached = 1;
03418 }
03419 }
03420 jsps->state = 21;
03421 break;
03422
03423 default:
03424 return JSON_ILLEGAL_CHARACTER;
03425 break;
03426 }
03427 return JSON_OK;
03428 }
03429
03430 state23:
03431 {
03432 switch (c)
03433 {
03434 case '0':
03435 rcs_catc (((rcstring *) jsps->temp), c);
03436 jsps->state = 17;
03437 break;
03438
03439 case '1':
03440 case '2':
03441 case '3':
03442 case '4':
03443 case '5':
03444 case '6':
03445 case '7':
03446 case '8':
03447 case '9':
03448 if (!jsps->string_length_limit_reached)
03449 {
03450 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
03451 {
03452 if ((jsps->temp = rcs_create (5)) == NULL)
03453 {
03454 return JSON_MEMORY;
03455 }
03456 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03457 {
03458 return JSON_MEMORY;
03459 }
03460 else
03461 {
03462 jsps->string_length_limit_reached = 1;
03463 }
03464 }
03465 }
03466 jsps->state = 24;
03467 break;
03468
03469 default:
03470 return JSON_ILLEGAL_CHARACTER;
03471 break;
03472 }
03473 return JSON_OK;
03474 }
03475
03476 state24:
03477 {
03478 switch (c)
03479 {
03480 case '0':
03481 case '1':
03482 case '2':
03483 case '3':
03484 case '4':
03485 case '5':
03486 case '6':
03487 case '7':
03488 case '8':
03489 case '9':
03490 if (!jsps->string_length_limit_reached)
03491 {
03492 if (rcs_length (((rcstring *) jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
03493 {
03494 if ((jsps->temp = rcs_create (5)) == NULL)
03495 {
03496 return JSON_MEMORY;
03497 }
03498 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03499 {
03500 return JSON_MEMORY;
03501 }
03502 }
03503 else
03504 {
03505 jsps->string_length_limit_reached = 1;
03506 }
03507 }
03508
03509 break;
03510
03511 case '.':
03512 if ((jsps->temp = rcs_create (5)) == NULL)
03513 {
03514 return JSON_MEMORY;
03515 }
03516 if (rcs_catc (((rcstring *) jsps->temp), '.') != RS_OK)
03517 {
03518 return JSON_MEMORY;
03519 }
03520
03521 jsps->state = 18;
03522 break;
03523
03524 case 'e':
03525 case 'E':
03526 if ((jsps->temp = rcs_create (5)) == NULL)
03527 {
03528 return JSON_MEMORY;
03529 }
03530 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03531 {
03532 return JSON_MEMORY;
03533 }
03534
03535 jsps->string_length_limit_reached = 0;
03536 jsps->state = 20;
03537 break;
03538
03539 case '\x20':
03540 case '\x09':
03541 case '\x0A':
03542 case '\x0D':
03543 if (((rcstring *) jsps->temp) == NULL)
03544 return JSON_MEMORY;
03545 if (jsf->new_number != NULL)
03546 {
03547 jsf->new_number (((rcstring *) jsps->temp)->text);
03548 }
03549 rcs_free ((rcstring **) & jsps->temp);
03550
03551 jsps->state = 0;
03552 break;
03553
03554 case '}':
03555 if (((rcstring *) jsps->temp) == NULL)
03556 return JSON_MEMORY;
03557 if (jsf->new_number != NULL)
03558 {
03559 jsf->new_number (((rcstring *) jsps->temp)->text);
03560 }
03561 rcs_free ((rcstring **) & jsps->temp);
03562
03563 if (jsf->open_object != NULL)
03564 jsf->close_object ();
03565 jsps->state = 26;
03566 break;
03567
03568 case ']':
03569 if (((rcstring *) jsps->temp) == NULL)
03570 return JSON_MEMORY;
03571 if (jsf->new_number != NULL)
03572 {
03573 jsf->new_number (((rcstring *) jsps->temp)->text);
03574 }
03575 rcs_free ((rcstring **) & jsps->temp);
03576
03577 if (jsf->open_object != NULL)
03578 jsf->close_array ();
03579 jsps->state = 26;
03580 break;
03581
03582 case ',':
03583 if (((rcstring *) jsps->temp) == NULL)
03584 return JSON_MEMORY;
03585 if (jsf->new_number != NULL)
03586 {
03587 jsf->new_number (((rcstring *) jsps->temp)->text);
03588 }
03589 rcs_free ((rcstring **) & jsps->temp);
03590
03591 if (jsf->label_value_separator != NULL)
03592 jsf->label_value_separator ();
03593 jsps->state = 27;
03594 break;
03595
03596 default:
03597 return JSON_ILLEGAL_CHARACTER;
03598 break;
03599 }
03600 return JSON_OK;
03601 }
03602
03603 state25:
03604 {
03605 switch (c)
03606 {
03607 case '\x20':
03608 case '\x09':
03609 case '\x0A':
03610 case '\x0D':
03611 break;
03612
03613 case '\"':
03614 jsps->temp = NULL;
03615 jsps->state = 1;
03616 break;
03617
03618 case '}':
03619 if (jsf->close_object != NULL)
03620 jsf->close_object ();
03621 jsps->state = 26;
03622 break;
03623
03624 default:
03625 return JSON_ILLEGAL_CHARACTER;
03626 break;
03627 }
03628 return JSON_OK;
03629 }
03630
03631 state26:
03632 {
03633 switch (c)
03634 {
03635 case '\x20':
03636 case '\x09':
03637 case '\x0A':
03638 case '\x0D':
03639 break;
03640
03641 case '}':
03642 if (jsf->close_object != NULL)
03643 jsf->close_object ();
03644
03645 break;
03646
03647 case ']':
03648 if (jsf->close_array != NULL)
03649 jsf->close_array ();
03650
03651 break;
03652
03653 case ',':
03654 if (jsf->sibling_separator != NULL)
03655 jsf->sibling_separator ();
03656 jsps->state = 27;
03657 break;
03658
03659 default:
03660 return JSON_ILLEGAL_CHARACTER;
03661 break;
03662 }
03663 return JSON_OK;
03664 }
03665
03666 state27:
03667 {
03668 switch (c)
03669 {
03670 case '\x20':
03671 case '\x09':
03672 case '\x0A':
03673 case '\x0D':
03674 break;
03675
03676 case '\"':
03677 jsps->state = 1;
03678 jsps->temp = NULL;
03679 break;
03680
03681 case '{':
03682 if (jsf->open_object != NULL)
03683 jsf->open_object ();
03684 jsps->state = 25;
03685 break;
03686
03687 case '[':
03688 if (jsf->open_array != NULL)
03689 jsf->open_array ();
03690
03691 break;
03692
03693 case 't':
03694 jsps->state = 7;
03695 break;
03696
03697 case 'f':
03698 jsps->state = 10;
03699 break;
03700
03701 case 'n':
03702 jsps->state = 14;
03703 break;
03704
03705 case '0':
03706 jsps->state = 17;
03707 if ((jsps->temp = rcs_create (5)) == NULL)
03708 {
03709 return JSON_MEMORY;
03710 }
03711 if (rcs_catc (((rcstring *) jsps->temp), '0') != RS_OK)
03712 {
03713 return JSON_MEMORY;
03714 }
03715 break;
03716
03717 case '1':
03718 case '2':
03719 case '3':
03720 case '4':
03721 case '5':
03722 case '6':
03723 case '7':
03724 case '8':
03725 case '9':
03726 jsps->state = 24;
03727 if ((jsps->temp = rcs_create (5)) == NULL)
03728 {
03729 return JSON_MEMORY;
03730 }
03731 if (rcs_catc (((rcstring *) jsps->temp), c) != RS_OK)
03732 {
03733 return JSON_MEMORY;
03734 }
03735 break;
03736
03737 case '-':
03738 jsps->state = 23;
03739 if ((jsps->temp = rcs_create (5)) == NULL)
03740 {
03741 return JSON_MEMORY;
03742 }
03743 if (rcs_catc (((rcstring *) jsps->temp), '-') != RS_OK)
03744 {
03745 return JSON_MEMORY;
03746 }
03747 break;
03748
03749 default:
03750 return JSON_ILLEGAL_CHARACTER;
03751 break;
03752 }
03753 return JSON_OK;
03754 }
03755
03756 return JSON_UNKNOWN_PROBLEM;
03757 }
03758
03759
03760 json_t *
03761 json_find_first_label (const json_t * object, const char *text_label)
03762 {
03763 json_t *cursor;
03764
03765 assert (object != NULL);
03766 assert (text_label != NULL);
03767 assert (object->type == JSON_OBJECT);
03768
03769 if (object->child == NULL)
03770 return NULL;
03771 cursor = object->child;
03772 while (cursor != NULL)
03773 {
03774 if (strncmp (cursor->text, text_label, strlen (text_label)) == 0)
03775 return cursor;
03776 cursor = cursor->next;
03777 }
03778 return NULL;
03779 }