]> 4ch.mooo.com Git - 16.git/blob - src/lib/jsmn/farjsmn.c
jsmn far and near ... wwww
[16.git] / src / lib / jsmn / farjsmn.c
1 #include <stdlib.h>
2 #include <stdio.h>      //fprintf for noisy debugging wwww
3
4 #include "farjsmn.h"
5
6 /**
7  * Allocates a fresh unused token from the token pull.
8  */
9 static jsmntok_t huge *jsmn_alloc_token(jsmn_parser huge *parser,
10                 jsmntok_t huge *tokens, size_t num_tokens) {
11         jsmntok_t huge *tok;
12         if (parser->toknext >= num_tokens) {
13                 return NULL;
14         }
15         tok = &tokens[parser->toknext++];
16         tok->start = tok->end = -1;
17         tok->size = 0;
18 #ifdef JSMN_PARENT_LINKS
19         tok->parent = -1;
20 #endif
21         return tok;
22 }
23
24 /**
25  * Fills token type and boundaries.
26  */
27 static void jsmn_fill_token(jsmntok_t huge *token, jsmntype_t type,
28                             int start, int end) {
29         token->type = type;
30         token->start = start;
31         token->end = end;
32         token->size = 0;
33 }
34
35 /**
36  * Fills next available token with JSON primitive.
37  */
38 static jsmnerr_t jsmn_parse_primitive(jsmn_parser huge *parser, const char huge *js,
39                 size_t len, jsmntok_t huge *tokens, size_t num_tokens) {
40         jsmntok_t huge *token;
41         int start;
42
43         start = parser->pos;
44
45         for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
46                 switch (js[parser->pos]) {
47 #ifndef JSMN_STRICT
48                         /* In strict mode primitive must be followed by "," or "}" or "]" */
49                         case ':':
50 #endif
51                         case '\t' : case '\r' : case '\n' : case ' ' :
52                         case ','  : case ']'  : case '}' :
53                                 goto found;
54                 }
55                 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
56                         parser->pos = start;
57                         return JSMN_ERROR_INVAL;
58                 }
59         }
60 #ifdef JSMN_STRICT
61         /* In strict mode primitive must be followed by a comma/object/array */
62         parser->pos = start;
63         return JSMN_ERROR_PART;
64 #endif
65
66 found:
67         if (tokens == NULL) {
68                 parser->pos--;
69                 return 0;
70         }
71         token = jsmn_alloc_token(parser, tokens, num_tokens);
72         if (token == NULL) {
73                 parser->pos = start;
74                 return JSMN_ERROR_NOMEM;
75         }
76         jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
77 #ifdef JSMN_PARENT_LINKS
78         token->parent = parser->toksuper;
79 #endif
80         parser->pos--;
81         return 0;
82 }
83
84 /**
85  * Filsl next token with JSON string.
86  */
87 static jsmnerr_t jsmn_parse_string(jsmn_parser huge *parser, const char huge *js,
88                 size_t len, jsmntok_t huge *tokens, size_t num_tokens) {
89         jsmntok_t huge *token;
90
91         int start = parser->pos;
92
93         parser->pos++;
94
95         /* Skip starting quote */
96         for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
97                 char c = js[parser->pos];
98
99                 /* Quote: end of string */
100                 if (c == '\"') {
101                         if (tokens == NULL) {
102                                 return 0;
103                         }
104                         token = jsmn_alloc_token(parser, tokens, num_tokens);
105                         if (token == NULL) {
106                                 parser->pos = start;
107                                 return JSMN_ERROR_NOMEM;
108                         }
109                         jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
110 #ifdef JSMN_PARENT_LINKS
111                         token->parent = parser->toksuper;
112 #endif
113                         return 0;
114                 }
115
116                 /* Backslash: Quoted symbol expected */
117                 if (c == '\\' && parser->pos + 1 < len) {
118                         int i;
119                         parser->pos++;
120                         switch (js[parser->pos]) {
121                                 /* Allowed escaped symbols */
122                                 case '\"': case '/' : case '\\' : case 'b' :
123                                 case 'f' : case 'r' : case 'n'  : case 't' :
124                                         break;
125                                 /* Allows escaped symbol \uXXXX */
126                                 case 'u':
127                                         parser->pos++;
128                                         for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
129                                                 /* If it isn't a hex character we have an error */
130                                                 if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
131                                                                         (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
132                                                                         (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
133                                                         parser->pos = start;
134                                                         return JSMN_ERROR_INVAL;
135                                                 }
136                                                 parser->pos++;
137                                         }
138                                         parser->pos--;
139                                         break;
140                                 /* Unexpected symbol */
141                                 default:
142                                         parser->pos = start;
143                                         return JSMN_ERROR_INVAL;
144                         }
145                 }
146         }
147         parser->pos = start;
148         return JSMN_ERROR_PART;
149 }
150
151 /**
152  * Parse JSON string and fill tokens.
153  */
154 jsmnerr_t jsmn_parse(jsmn_parser huge *parser, const char huge *js, size_t len,
155                 jsmntok_t huge *tokens, unsigned int num_tokens) {
156         jsmnerr_t r;
157         int i;
158         jsmntok_t huge *token;
159         int count = 0;
160         static unsigned long pee=0;
161
162         for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
163                 char c;
164                 jsmntype_t type;
165
166 fprintf(stdout, "pee=%lu        count=%u        i=%d    nt=%u\n", pee, count, i, num_tokens);
167 pee++;
168
169                 c = js[parser->pos];
170                 switch (c) {
171                         case '{': case '[':
172                                 count++;
173                                 if (tokens == NULL) {
174                                         break;
175                                 }
176                                 token = jsmn_alloc_token(parser, tokens, num_tokens);
177                                 if (token == NULL)
178                                         return JSMN_ERROR_NOMEM;
179                                 if (parser->toksuper != -1) {
180                                         tokens[parser->toksuper].size++;
181 #ifdef JSMN_PARENT_LINKS
182                                         token->parent = parser->toksuper;
183 #endif
184                                 }
185                                 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
186                                 token->start = parser->pos;
187                                 parser->toksuper = parser->toknext - 1;
188                                 break;
189                         case '}': case ']':
190                                 if (tokens == NULL)
191                                         break;
192                                 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
193 #ifdef JSMN_PARENT_LINKS
194                                 if (parser->toknext < 1) {
195                                         return JSMN_ERROR_INVAL;
196                                 }
197                                 token = &tokens[parser->toknext - 1];
198                                 for (;;) {
199                                         if (token->start != -1 && token->end == -1) {
200                                                 if (token->type != type) {
201                                                         return JSMN_ERROR_INVAL;
202                                                 }
203                                                 token->end = parser->pos + 1;
204                                                 parser->toksuper = token->parent;
205                                                 break;
206                                         }
207                                         if (token->parent == -1) {
208                                                 break;
209                                         }
210                                         token = &tokens[token->parent];
211                                 }
212 #else
213                                 for (i = parser->toknext - 1; i >= 0; i--) {
214                                         token = &tokens[i];
215                                         if (token->start != -1 && token->end == -1) {
216                                                 if (token->type != type) {
217                                                         return JSMN_ERROR_INVAL;
218                                                 }
219                                                 parser->toksuper = -1;
220                                                 token->end = parser->pos + 1;
221                                                 break;
222                                         }
223                                 }
224                                 /* Error if unmatched closing bracket */
225                                 if (i == -1) return JSMN_ERROR_INVAL;
226                                 for (; i >= 0; i--) {
227                                         token = &tokens[i];
228                                         if (token->start != -1 && token->end == -1) {
229                                                 parser->toksuper = i;
230                                                 break;
231                                         }
232                                 }
233 #endif
234                                 break;
235                         case '\"':
236                                 r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
237                                 if (r < 0) return r;
238                                 count++;
239                                 if (parser->toksuper != -1 && tokens != NULL)
240                                         tokens[parser->toksuper].size++;
241                                 break;
242                         case '\t' : case '\r' : case '\n' : case ' ':
243                                 break;
244                         case ':':
245                                 parser->toksuper = parser->toknext - 1;
246                                 break;
247                         case ',':
248                                 if (tokens != NULL &&
249                                                 tokens[parser->toksuper].type != JSMN_ARRAY &&
250                                                 tokens[parser->toksuper].type != JSMN_OBJECT) {
251 #ifdef JSMN_PARENT_LINKS
252                                         parser->toksuper = tokens[parser->toksuper].parent;
253 #else
254                                         for (i = parser->toknext - 1; i >= 0; i--) {
255                                                 if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
256                                                         if (tokens[i].start != -1 && tokens[i].end == -1) {
257                                                                 parser->toksuper = i;
258                                                                 break;
259                                                         }
260                                                 }
261                                         }
262 #endif
263                                 }
264                                 break;
265 #ifdef JSMN_STRICT
266                         /* In strict mode primitives are: numbers and booleans */
267                         case '-': case '0': case '1' : case '2': case '3' : case '4':
268                         case '5': case '6': case '7' : case '8': case '9':
269                         case 't': case 'f': case 'n' :
270                                 /* And they must not be keys of the object */
271                                 if (tokens != NULL) {
272                                         jsmntok_t huge *t = &tokens[parser->toksuper];
273                                         if (t->type == JSMN_OBJECT ||
274                                                         (t->type == JSMN_STRING && t->size != 0)) {
275                                                 return JSMN_ERROR_INVAL;
276                                         }
277                                 }
278 #else
279                         /* In non-strict mode every unquoted value is a primitive */
280                         default:
281 #endif
282                                 r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
283                                 if (r < 0) return r;
284                                 count++;
285                                 if (parser->toksuper != -1 && tokens != NULL)
286                                         tokens[parser->toksuper].size++;
287                                 break;
288
289 #ifdef JSMN_STRICT
290                         /* Unexpected char in strict mode */
291                         default:
292                                 return JSMN_ERROR_INVAL;
293 #endif
294                 }
295         }
296
297         for (i = parser->toknext - 1; i >= 0; i--) {
298                 /* Unmatched opened object or array */
299                 if (tokens[i].start != -1 && tokens[i].end == -1) {
300                         return JSMN_ERROR_PART;
301                 }
302         }
303
304         return count;
305 }
306
307 /**
308  * Creates a new parser based over a given  buffer with an array of tokens
309  * available.
310  */
311 void jsmn_init(jsmn_parser huge *parser) {
312         parser->pos = 0;
313         parser->toknext = 0;
314         parser->toksuper = -1;
315 }
316