]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/objson/json_parser.c
added some debugging, safety measures
[OpenSRF.git] / src / objson / json_parser.c
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@gmail.com>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 */
15
16
17 #include "json_parser.h"
18
19 /* keep a copy of the length of the current json string so we don't 
20  * have to calculate it in each function
21  */
22 int current_strlen; /* XXX need to move this into the function params for thread support */
23
24 object* json_parse_string(char* string) {
25
26         if(string == NULL) return NULL;
27
28         current_strlen = strlen(string);
29
30         if(current_strlen == 0) 
31                 return NULL;
32
33         object* obj = new_object(NULL);
34         unsigned long index = 0;
35
36         int status = _json_parse_string(string, &index, obj);
37         if(!status)
38                 return obj;
39
40         if(status == -2)
41                 return NULL;
42
43         return NULL;
44 }
45
46
47 int _json_parse_string(char* string, unsigned long* index, object* obj) {
48         assert(string && index && *index < current_strlen);
49
50         int status = 0; /* return code from parsing routines */
51         char* classname = NULL; /* object class hint */
52         json_eat_ws(string, index, 1); /* remove leading whitespace */
53
54         char c = string[*index];
55
56         /* remove any leading comments */
57         if( c == '/' ) { 
58
59                 while(1) {
60                         (*index)++; /* move to second comment char */
61                         status = json_eat_comment(string, index, &classname, 1);
62                         if(status) return status;
63
64                         json_eat_ws(string, index, 1);
65                         c = string[*index];
66                         if(c != '/')
67                                 break;
68                 }
69         }
70
71         json_eat_ws(string, index, 1); /* remove leading whitespace */
72
73         if(*index >= current_strlen)
74                 return -2;
75
76         switch(c) {
77                                 
78                 /* json string */
79                 case '"': 
80                         (*index)++;
81                         status = json_parse_json_string(string, index, obj);
82                         break;
83
84                 /* json array */
85                 case '[':
86                         (*index)++;
87                         status = json_parse_json_array(string, index, obj);                     
88                         break;
89
90                 /* json object */
91                 case '{':
92                         (*index)++;
93                         status = json_parse_json_object(string, index, obj);
94                         break;
95
96                 /* NULL */
97                 case 'n':
98                 case 'N':
99                         status = json_parse_json_null(string, index, obj);
100                         break;
101                         
102
103                 /* true, false */
104                 case 'f':
105                 case 'F':
106                 case 't':
107                 case 'T':
108                         status = json_parse_json_bool(string, index, obj);
109                         break;
110
111                 default:
112                         if(is_number(c) || c == '.' || c == '-') { /* are we a number? */
113                                 status = json_parse_json_number(string, index, obj);    
114                                 if(status) return status;
115                                 break;
116                         }
117
118                         (*index)--;
119                         /* we should never get here */
120                         return json_handle_error(string, index, "_json_parse_string() final switch clause");
121         }       
122
123         if(status) return status;
124
125         json_eat_ws(string, index, 1);
126
127         if( *index < current_strlen ) {
128                 /* remove any trailing comments */
129                 c = string[*index];
130                 if( c == '/' ) { 
131                         (*index)++;
132                         status = json_eat_comment(string, index, NULL, 0);
133                         if(status) return status;
134                 }
135         }
136
137         if(classname){
138                 obj->set_class(obj, classname);
139                 free(classname);
140         }
141
142         return 0;
143 }
144
145
146 int json_parse_json_null(char* string, unsigned long* index, object* obj) {
147
148         if(*index >= (current_strlen - 3)) {
149                 return json_handle_error(string, index, 
150                         "_parse_json_string(): invalid null" );
151         }
152
153         if(!strncasecmp(string + (*index), "null", 4)) {
154                 (*index) += 4;
155                 obj->is_null = 1;
156                 return 0;
157         } else {
158                 return json_handle_error(string, index,
159                         "_parse_json_string(): invalid null" );
160         }
161 }
162
163 /* should be at the first character of the bool at this point */
164 int json_parse_json_bool(char* string, unsigned long* index, object* obj) {
165         assert(string && obj && *index < current_strlen);
166
167         char* ret = "json_parse_json_bool(): truncated bool";
168
169         if( *index >= (current_strlen - 5))
170                 return json_handle_error(string, index, ret);
171         
172         if(!strncasecmp( string + (*index), "false", 5)) {
173                 (*index) += 5;
174                 obj->bool_value = 0;
175                 obj->is_bool = 1;
176                 return 0;
177         }
178
179         if( *index >= (current_strlen - 4))
180                 return json_handle_error(string, index, ret);
181
182         if(!strncasecmp( string + (*index), "true", 4)) {
183                 (*index) += 4;
184                 obj->bool_value = 1;
185                 obj->is_bool = 1;
186                 return 0;
187         }
188
189         return json_handle_error(string, index, ret);
190 }
191
192
193 /* expecting the first character of the number */
194 int json_parse_json_number(char* string, unsigned long* index, object* obj) {
195         assert(string && obj && *index < current_strlen);
196
197         growing_buffer* buf = buffer_init(64);
198         char c = string[*index];
199
200         int done = 0;
201         int dot_seen = 0;
202
203         /* negative number? */
204         if(c == '-') { buffer_add(buf, "-"); (*index)++; }
205
206         while(*index < current_strlen) {
207
208                 if(is_number(c))
209                         buffer_add_char(buf, c);
210
211                 else if( c == '.' ) {
212                         if(dot_seen) {
213                                 return json_handle_error(string, index, 
214                                         "json_parse_json_number(): malformed json number");
215                         }
216                         dot_seen = 1;
217                         buffer_add_char(buf, c);
218                 } else {
219                         done = 1; break;
220                 }
221                 (*index)++;
222                 c = string[*index];
223                 if(done) break;
224         }
225
226         if(dot_seen) {
227                 obj->is_double = 1;
228                 obj->double_value = strtod(buf->buf, NULL);
229                 buffer_free(buf);
230                 return 0;
231
232         } else {
233                 obj->is_number = 1;
234                 obj->num_value = atol(buf->buf);
235                 buffer_free(buf);
236                 return 0;
237         }
238 }
239
240 /* index should point to the character directly following the '['.  when done
241  * index will point to the character directly following the ']' character
242  */
243 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
244         assert(string && obj && index && *index < current_strlen);
245
246         int status;
247         int in_parse = 0; /* true if this array already contains one item */
248         obj->is_array = 1;
249         while(*index < current_strlen) {
250
251                 json_eat_ws(string, index, 1);
252
253                 if(string[*index] == ']') {
254                         (*index)++;
255                         break;
256                 }
257
258                 if(in_parse) {
259                         json_eat_ws(string, index, 1);
260                         if(string[*index] != ',') {
261                                 return json_handle_error(string, index,
262                                         "json_parse_json_array(): array not followed by a ','");
263                         }
264                         (*index)++;
265                         json_eat_ws(string, index, 1);
266                 }
267
268                 object* item = new_object(NULL);
269                 status = _json_parse_string(string, index, item);
270
271                 if(status) return status;
272                 obj->push(obj, item);
273                 in_parse = 1;
274         }
275
276         return 0;
277 }
278
279
280 /* index should point to the character directly following the '{'.  when done
281  * index will point to the character directly following the '}'
282  */
283 int json_parse_json_object(char* string, unsigned long* index, object* obj) {
284         assert(string && obj && index && *index < current_strlen);
285
286         obj->is_hash = 1;
287         int status;
288         int in_parse = 0; /* true if we've already added one item to this object */
289
290         while(*index < current_strlen) {
291
292                 json_eat_ws(string, index, 1);
293
294                 if(string[*index] == '}') {
295                         (*index)++;
296                         break;
297                 }
298
299                 if(in_parse) {
300                         if(string[*index] != ',') {
301                                 return json_handle_error(string, index,
302                                         "json_parse_json_object(): object missing ',' betweenn elements" );
303                         }
304                         (*index)++;
305                         json_eat_ws(string, index, 1);
306                 }
307
308                 /* first we grab the hash key */
309                 object* key_obj = new_object(NULL);
310                 status = _json_parse_string(string, index, key_obj);
311                 if(status) return status;
312
313                 if(!key_obj->is_string) {
314                         return json_handle_error(string, index, 
315                                 "_json_parse_json_object(): hash key not a string");
316                 }
317
318                 char* key = key_obj->string_data;
319
320                 json_eat_ws(string, index, 1);
321
322                 if(string[*index] != ':') {
323                         return json_handle_error(string, index, 
324                                 "json_parse_json_object(): hash key not followed by ':' character");
325                 }
326
327                 (*index)++;
328
329                 /* now grab the value object */
330                 json_eat_ws(string, index, 1);
331                 object* value_obj = new_object(NULL);
332                 status = _json_parse_string(string, index, value_obj);
333                 if(status) return status;
334
335                 /* put the data into the object and continue */
336                 obj->add_key(obj, key, value_obj);
337                 free_object(key_obj);
338                 in_parse = 1;
339         }
340         return 0;
341 }
342
343
344
345 /* when done, index will point to the character after the closing quote */
346 int json_parse_json_string(char* string, unsigned long* index, object* obj) {
347         assert(string && index && *index < current_strlen);
348
349         int in_escape = 0;      
350         int done = 0;
351         growing_buffer* buf = buffer_init(64);
352
353         while(*index < current_strlen) {
354
355                 char c = string[*index]; 
356
357                 switch(c) {
358
359                         case '\\':
360                                 if(in_escape) {
361                                         buffer_add(buf, "\\");
362                                         in_escape = 0;
363                                 } else 
364                                         in_escape = 1;
365                                 break;
366
367                         case '"':
368                                 if(in_escape) {
369                                         buffer_add(buf, "\"");
370                                         in_escape = 0;
371                                 } else 
372                                         done = 1;
373                                 break;
374
375                         case 't':
376                                 if(in_escape) {
377                                         buffer_add(buf,"\t");
378                                         in_escape = 0;
379                                 } else 
380                                         buffer_add_char(buf, c);
381                                 break;
382
383                         case 'b':
384                                 if(in_escape) {
385                                         buffer_add(buf,"\b");
386                                         in_escape = 0;
387                                 } else 
388                                         buffer_add_char(buf, c);
389                                 break;
390
391                         case 'f':
392                                 if(in_escape) {
393                                         buffer_add(buf,"\f");
394                                         in_escape = 0;
395                                 } else 
396                                         buffer_add_char(buf, c);
397                                 break;
398
399                         case 'r':
400                                 if(in_escape) {
401                                         buffer_add(buf,"\r");
402                                         in_escape = 0;
403                                 } else 
404                                         buffer_add_char(buf, c);
405                                 break;
406
407                         case 'n':
408                                 if(in_escape) {
409                                         buffer_add(buf,"\n");
410                                         in_escape = 0;
411                                 } else 
412                                         buffer_add_char(buf, c);
413                                 break;
414
415                         case 'u':
416                                 if(in_escape) {
417                                         (*index)++;
418
419                                         if(*index >= (current_strlen - 4)) {
420                                                 return json_handle_error(string, index,
421                                                         "json_parse_json_string(): truncated escaped unicode"); }
422
423                                         char buff[5];
424                                         memset(buff,0,5);
425                                         memcpy(buff, string + (*index), 4);
426
427
428                                         /* ----------------------------------------------------------------------- */
429                                         /* ----------------------------------------------------------------------- */
430                                         /* The following chunk was borrowed with permission from 
431                                                 json-c http://oss.metaparadigm.com/json-c/ */
432                                         unsigned char utf_out[3];
433                                         memset(utf_out,0,3);
434
435                                         #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
436
437                                         unsigned int ucs_char =
438                                                 (hexdigit(string[*index] ) << 12) +
439                                                 (hexdigit(string[*index + 1]) << 8) +
440                                                 (hexdigit(string[*index + 2]) << 4) +
441                                                 hexdigit(string[*index + 3]);
442         
443                                         if (ucs_char < 0x80) {
444                                                 utf_out[0] = ucs_char;
445                                                 buffer_add(buf, utf_out);
446
447                                         } else if (ucs_char < 0x800) {
448                                                 utf_out[0] = 0xc0 | (ucs_char >> 6);
449                                                 utf_out[1] = 0x80 | (ucs_char & 0x3f);
450                                                 buffer_add(buf, utf_out);
451
452                                         } else {
453                                                 utf_out[0] = 0xe0 | (ucs_char >> 12);
454                                                 utf_out[1] = 0x80 | ((ucs_char >> 6) & 0x3f);
455                                                 utf_out[2] = 0x80 | (ucs_char & 0x3f);
456                                                 buffer_add(buf, utf_out);
457                                         }
458                                         /* ----------------------------------------------------------------------- */
459                                         /* ----------------------------------------------------------------------- */
460
461                                         (*index) += 3;
462                                         in_escape = 0;
463
464                                 } else {
465
466                                         buffer_add_char(buf, c);
467                                 }
468
469                                 break;
470
471                         default:
472                                 buffer_add_char(buf, c);
473                 }
474
475                 (*index)++;
476                 if(done) break;
477         }
478
479         obj->set_string(obj, buf->buf);
480         buffer_free(buf);
481         return 0;
482 }
483
484
485 void json_eat_ws(char* string, unsigned long* index, int eat_all) {
486         assert(string && index);
487         if(*index >= current_strlen)
488                 return;
489
490         if( eat_all ) { /* removes newlines, etc */
491                 while(string[*index] == ' '     || 
492                                 string[*index] == '\n'  ||
493                                 string[*index] == '\t') 
494                         (*index)++;
495         }
496
497         else    
498                 while(string[*index] == ' ') (*index)++;
499 }
500
501
502 /* index should be at the '*' character at the beginning of the comment.
503  * when done, index will point to the first character after the final /
504  */
505 int json_eat_comment(char* string, unsigned long* index, char** buffer, int parse_class) {
506         assert(string && index && *index < current_strlen);
507
508         if(string[*index] != '*' && string[*index] != '/' )
509                 return json_handle_error(string, index, 
510                         "json_eat_comment(): invalid character after /");
511
512         /* chop out any // style comments */
513         if(string[*index] == '/') {
514                 (*index)++;
515                 char c = string[*index];
516                 while(*index < current_strlen) {
517                         (*index)++;
518                         if(c == '\n') 
519                                 return 0;
520                         c = string[*index];
521                 }
522                 return 0;
523         }
524
525         (*index)++;
526
527         int on_star                     = 0; /* true if we just saw a '*' character */
528
529         /* we're just past the '*' */
530         if(!parse_class) { /* we're not concerned with class hints */
531                 while(*index < current_strlen) {
532                         if(string[*index] == '/') {
533                                 if(on_star) {
534                                         (*index)++;
535                                         return 0;
536                                 }
537                         }
538
539                         if(string[*index] == '*') on_star = 1;
540                         else on_star = 0;
541
542                         (*index)++;
543                 }
544                 return 0;
545         }
546
547
548
549         growing_buffer* buf = buffer_init(64);
550
551         int first_dash          = 0;
552         int second_dash = 0;
553         int third_dash          = 0;
554         int fourth_dash = 0;
555
556         int in_hint                     = 0;
557         int done                                = 0;
558
559         /*--S hint--*/   /* <-- Hints  look like this */
560         /*--E hint--*/
561
562         while(*index < current_strlen) {
563                 char c = string[*index];
564
565                 switch(c) {
566
567                         case '-':
568                                 on_star = 0;
569                                 if(third_dash)                  fourth_dash = 1;
570                                 else if(in_hint)                third_dash      = 1;
571                                 else if(first_dash)     second_dash = 1;
572                                 else                                            first_dash = 1;
573                                 break;
574
575                         case 'S':
576                                 on_star = 0;
577                                 if(second_dash && !in_hint) {
578                                         (*index)++;
579                                         json_eat_ws(string, index, 1);
580                                         (*index)--; /* this will get incremented at the bottom of the loop */
581                                         in_hint = 1;
582                                         break;
583                                 }
584
585                         case 'E':
586                                 on_star = 0;
587                                 if(second_dash && !in_hint) {
588                                         (*index)++;
589                                         json_eat_ws(string, index, 1);
590                                         (*index)--; /* this will get incremented at the bottom of the loop */
591                                         in_hint = 1;
592                                         break;
593                                 }
594
595                         case '*':
596                                 on_star = 1;
597                                 break;
598
599                         case '/':
600                                 if(on_star) 
601                                         done = 1;
602                                 else
603                                 on_star = 0;
604                                 break;
605
606                         default:
607                                 on_star = 0;
608                                 if(in_hint)
609                                         buffer_add_char(buf, c);
610                 }
611
612                 (*index)++;
613                 if(done) break;
614         }
615
616         if( buf->n_used > 0 && buffer)
617                 *buffer = buffer_data(buf);
618
619         buffer_free(buf);
620         return 0;
621 }
622
623 int is_number(char c) {
624         switch(c) {
625                 case '0':
626                 case '1':
627                 case '2':
628                 case '3':
629                 case '4':
630                 case '5':
631                 case '6':
632                 case '7':
633                 case '8':
634                 case '9':
635                         return 1;
636         }
637         return 0;
638 }
639
640 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
641
642         char buf[60];
643         memset(buf, 0, 60);
644
645         if(*index > 30)
646                 strncpy( buf, string + (*index - 30), 59 );
647         else
648                 strncpy( buf, string, 59 );
649
650         fprintf(stderr, 
651                         "\nError parsing json string at charracter %c "
652                         "(code %d) and index %ld\nMsg:\t%s\nNear:\t%s\n\n", 
653                         string[*index], string[*index], *index, err_msg, buf );
654         return -1;
655 }
656
657