summaryrefslogtreecommitdiff
path: root/imap/src/c-client/json.c
blob: 9a3e16716f1c0ab20be4f5f4b871c63b63ddf188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * Copyright 2018 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 */
#ifdef STANDALONE
#include "headers.h"
#include "mem.h"
#include "readfile.h"
#include "json.h"
#else
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include "c-client.h"
#include "json.h"
#endif /* STANDALONE */

#define NIL 0

#define json_ws(X) (((X) == ' ' || (X) == '\t' || (X) == '\n' || (X) == '\r') ? 1 : 0)

#define json_skipws(X) for(; json_ws(*(X)); (X)++)

#define json_skipchar(X) {					\
	(X)++;							\
	json_skipws((X));					\
}

#define json_last_char(X) (((X) == ',' || (X) == ']'  || (X) == '}') ? 1 : 0)

JObjType json_number_type(unsigned char *);
unsigned char *json_strchr(unsigned char *, unsigned char);
JSON_S *json_parse_work(unsigned char **);
JSON_S *json_parse_pairs(unsigned char **);

JSON_X *json_value_parse(unsigned char **);
void    json_value_free(JSON_X **);

/* An array is a JSON object where the name is null */
JSON_S *json_array_parse_work(unsigned char **);
JSON_S *json_array_parse(unsigned char **);
void    json_array_free(JSON_S **);

#ifdef STANDALONE
int compare_cstring (unsigned char *,unsigned char *);
int compare_uchar (unsigned char,unsigned char);
int compare_ulong (unsigned long,unsigned long);

int compare_ulong (unsigned long l1,unsigned long l2)
{
  if (l1 < l2) return -1;
  if (l1 > l2) return 1;
  return 0;
} 

int compare_uchar (unsigned char c1,unsigned char c2)
{
  return compare_ulong (((c1 >= 'a') && (c1 <= 'z')) ? c1 - ('a' - 'A') : c1,
                        ((c2 >= 'a') && (c2 <= 'z')) ? c2 - ('a' - 'A') : c2);
}

int compare_cstring (unsigned char *s1,unsigned char *s2)
{
  int i;
  if (!s1) return s2 ? -1 : 0;  /* empty string cases */
  else if (!s2) return 1;
  for (; *s1 && *s2; s1++,s2++) if ((i = (compare_uchar (*s1,*s2))) != 0) return i;
  if (*s1) return 1;            /* first string is longer */
  return *s2 ? -1 : 0;          /* second string longer : strings identical */
}
#endif /* STANDALONE */

/* we are parsing from the text of the json object, so it is
 * never possible to have a null character, unless something
 * is corrupt, in whose case we return JNumberError.
 */
JObjType json_number_type(unsigned char *s)
{
  unsigned char *w = s, *u;
  int PossibleDecimal, PossibleExponential;

  PossibleDecimal = PossibleExponential = 0;

  if(*w == '+' || *w == '-')
    w++;

  if (*w == '0'){
     u = w + 1;
     if(json_ws(*u)) json_skipws(u);
     if(json_last_char(*u)) return JLong;
     else if(*(w+1) == '.'){
	for(u = w+2; *u >= '0' && *u <= '9'; u++);
	if(json_ws(*u)) json_skipws(u);
	if(json_last_char(*u)) return JDecimal;
	else return JNumberError;
     }
     else return JNumberError;
  }

  if(*w < '1' || *w > '9')
    return JNumberError;

  u = w + 1;
  if(json_ws(*u)) json_skipws(u);
  if(json_last_char(*u)) return JLong;
  else if (*(w+1) == 'e' || *(w+1) == 'E'){
    PossibleExponential++;
    w += 2;
  }
  else if(*(w+1) == '.'){
    PossibleDecimal++;
    PossibleExponential++;
    w += 2;
  } else {
    for(; *w >= '0' && *w <= '9'; w++);
    if(*w == '.'){
      PossibleDecimal++;
      w++;
    }
    else {
      if(json_ws(*w)) json_skipws(w);
      if(json_last_char(*w)) return JLong;
      else return JNumberError;
    }
  }

  if(PossibleDecimal){
    for(; *w >= '0' && *w <= '9'; w++);
    u = w;
    if(json_ws(*u)) json_skipws(u);
    if(json_last_char(*u)) return JDecimal;
    else if (*w == 'e' || *w == 'E'){
       PossibleExponential++;
       w++;
    }
    else return JNumberError;
  }

  if(PossibleExponential){
     if(*w == '+' || *w == '-')
	w++;
     if(*w == '0'){
       u = w + 1;
       if(json_ws(*u)) json_skipws(u);
       if(json_last_char(*u)) return JExponential;
       else return JNumberError;
     }
     if(*w < '1' || *w > '9')
	return JNumberError;
     for(; *w >= '0' && *w <= '9'; w++);
     if(json_ws(*w)) json_skipws(w);
     if(json_last_char(*w)) return JExponential;
     else return JNumberError;
  }
  return JNumberError;	/* never reached */
}


JSON_X *
json_body_value(JSON_S *j, unsigned char *s)
{
  JSON_S *js;

  if(!j || !j->value|| j->value->jtype != JObject) return NIL;

  for(js = (JSON_S *) j->value->value; js ; js = js->next)
    if(js->name && !compare_cstring(js->name, s))
	break;

  return js ? js->value : NIL;
}

unsigned char *
json_strchr(unsigned char *s, unsigned char c)
{
   unsigned char *t, d;
   int escaped, extended;

   if(c == '\0'){
      t = s + strlen((char *) s);
      return t;
   }

   escaped = extended = 0;
   for(t = s; (d = *t) != '\0';){
      if(escaped){
	if (d == '\"' || d == '\\' || d == '/' || d == 'b'
	   || d == 'f' || d == 'n' || d == 'r' || d == 't'){
	   escaped = 0;
	   t++;
	   continue;
	}
	else{
	   if(d == 'u'){
	     escaped  = 0;
	     extended = 1;
	     t++;
	   }
	   else
	     return NIL;
	}
      }
      else {
	if(d == '\\'){
	  escaped = 1;
	  t++;
	  continue;
	}
	else if(extended){
	   int i;
	   static char HEX[] = "abcdefABCDEF0123456789";

	   if(strlen((char *) t) < 4)
	      return NIL;

	   for(i = 0; i < 4; i++){
	      if(!strchr(HEX, *t))
		return NIL;
	      else
		t++;
	   }
	   extended = 0;
	}
	else if (d == c)
	   break;
	else t++;
      }
   }
   return (*t == '\0') ? NULL : t;
}

JSON_X *
json_value_parse(unsigned char **s)
{
  JSON_X *jx;
  unsigned char *u, *w;
  unsigned long *l;

  w = *s;
  json_skipws(w);
  jx = fs_get(sizeof(JSON_X));
  memset((void **) jx, 0, sizeof(JSON_X));
  jx->jtype = JEnd;
  switch(*w){
     case '\"': jx->jtype = JString; break;
     case '{' : jx->jtype = JObject; break;
     case '[' : jx->jtype = JArray; break;
     case 'f' : if(strlen((char *) w) > 5
		    && !strncmp((char *) w, "false", 5)){
		   u = w+5;
		   json_skipws(u);
		   if(json_last_char(*u))
		     jx->jtype = JBoolean;
		}
		break;
     case 'n' : if(strlen((char *) w) > 4
		    && !strncmp((char *) w, "null", 4)){
		   u = w+4;
		   json_skipws(u);
		   if(json_last_char(*u))
		     jx->jtype = JNull;
		}
		break;
     case 't' : if(strlen((char *) w) > 4 
		    && !strncmp((char *) w, "true", 4)){
		   u = w+4;
		   json_skipws(u);
		   if(json_last_char(*u))
		     jx->jtype = JBoolean;
		}
		break;
       default: jx->jtype = json_number_type(w);
		break;		
  }

  switch(jx->jtype){
     case JString: u = json_strchr(w+1, '\"');
		   if(u != NULL){
		     *u = '\0';
		     jx->value = (void *) cpystr((char *) w+1);
		     *u = '\"';
		     json_skipchar(u);
		     w = u;
		   }
		   break;

     case JObject: jx->value = (void *) json_parse(&w);
		   break;

     case JLong  : l = fs_get(sizeof(unsigned long));
		   *l = strtoul((char *) w, (char **) &w, 10);
		   jx->value = (void *) l;
		   json_skipws(w);
		   break;

     case JDecimal :
     case JExponential: l = fs_get(sizeof(double));
		     *l = strtod((char *) w, (char **) &w);
		     jx->value = (void *) l;
		     json_skipws(w);
		     break;

     case JBoolean: if(*w == 't'){
		      jx->value = (void *) cpystr("true");
		      w += 4;
		    }
		    else{
		      jx->value = (void *) cpystr("false");
		      w += 5;
		    }
		    json_skipws(w);
		    break;

     case  JNull: jx->value = (void *) cpystr("null");
		  w += 4;
		  json_skipws(w);
		  break;

     case JArray: jx->value = json_array_parse(&w);
		  json_skipchar(w);
		 break;

	default:  break;   /* let caller handle this */
  }
  *s = w;
  return jx;
}

JSON_S *
json_array_parse(unsigned char **s)
{
  JSON_S *j = NIL;
  unsigned char *w = *s;

  json_skipws(w);
  if(*w == '['){
     json_skipchar(w);
     j = json_array_parse_work(&w);
  }
  *s = w;
  return j;
}

JSON_S *
json_array_parse_work(unsigned char **s)
{
  unsigned char *w = *s;
  JSON_S *j;

  json_skipws(w);
  j = fs_get(sizeof(JSON_S));
  memset((void *) j, 0, sizeof(JSON_S));
  j->value = json_value_parse(&w);
  json_skipws(w);
  switch(*w){
        case ',' : json_skipchar(w);
		   j->next = json_array_parse_work(&w);
		   break;

        case ']' : break;
         default : json_free(&j);
  }
  *s = w;
  return j;
}

void
json_array_free(JSON_S **j)
{
  json_free(j);
}

JSON_S *
json_parse_pairs(unsigned char **s)
{
  JSON_S *j;
  unsigned char *u, *w = *s;

  json_skipws(w);
  if(*w++ != '\"')
    return NIL;

  u = json_strchr(w, '\"');
  if(!u)
    return NIL;

  *u = '\0';
  j = fs_get(sizeof(JSON_S));
  memset((void *) j, 0, sizeof(JSON_S));
  j->name = (unsigned char *) cpystr((char *) w);

  *u = '\"';
  json_skipchar(u);
  if(*u != ':')
    return j;
  json_skipchar(u);

  j->value = json_value_parse(&u);
  json_skipws(u);
  if (*u == ','){
    json_skipchar(u);
    j->next = json_parse_pairs(&u);
  }
  *s = u;
  return j;
}

void
json_value_free(JSON_X **jxp)
{
  if(!jxp || !*jxp)
    return;

  switch((*jxp)->jtype){
	case  JString:
	case  JLong  :
	case JDecimal:
	case JExponential:
	case JBoolean:
	case    JNull: fs_give((void **)  &(*jxp)->value);
		       break;

	case   JArray: json_array_free((JSON_S **) &(*jxp)->value);
		       break;

	case  JObject: json_free((JSON_S **) &(*jxp)->value);
		       break;

	      default: printf("Unhandled case in json_value_free");
			exit(1);
  }
}

void
json_free(JSON_S **jp)
{

  if(jp == NULL || *jp == NULL) return;

  if((*jp)->name) fs_give((void **) &(*jp)->name);
  if((*jp)->value) json_value_free(&(*jp)->value);
  if((*jp)->next) json_free(&(*jp)->next);
  fs_give((void **) jp);
}

JSON_S *
json_parse(unsigned char **s)
{
  JSON_S *j = NULL;
  JSON_X *jx;
  unsigned char *w = *s;

  json_skipws(w);
  if(*w == '{'){
     json_skipchar(w);
     jx = fs_get(sizeof(JSON_X));
     memset((void *) jx, 0, sizeof(JSON_X));
     jx->jtype = JObject;
     jx->value = (void *) json_parse_pairs(&w);
     j = fs_get(sizeof(JSON_S));
     memset((void *) j, 0, sizeof(JSON_S));
     j->value = jx;
     json_skipws(w);
     if(*w == '}'){
        json_skipchar(w);
     }
     else
        json_free(&j);
  }
  *s = w;
  return j;  
}

#ifdef STANDALONE
int main (int argc, char *argv[])
{
 unsigned char *s, *t;
 JSON_S *json;
 JSON_X *jx;

 t = NULL;
 readfile(argv[1], (char **) &t, NULL);
 s = t;
 json = json_parse(&s);
 if(!*s) printf("Success!\n");
// jx = json_body_value(json, (unsigned char *) "subject");
// if(jx) printf("subject = %s\n", (char *) jx->value);
 if(t) fs_give((void **)&t);
 exit(0);
}
#endif /* STANDALONE */