-
Notifications
You must be signed in to change notification settings - Fork 0
/
cjson.c
279 lines (258 loc) · 6.38 KB
/
cjson.c
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
#include "cjson.h"
#include <assert.h>
#include <math.h>
#include <string.h>
#ifndef C_PARSE_STACK_INIT_SIZE
#define C_PARSE_STACK_INIT_SIZE 256
#endif
#define EXCEPT(c, ch) \
do \
{ \
assert(*c->json == (ch)); \
c->json++; \
} while (0);
#define ISDIGIT(ch) ((ch) >= '0' && (ch) <= '9')
#define ISDIGIT1TO9(ch) ((ch) >= '1' && (ch) <= '9')
#define PUTC(c, ch) do { *(char*)c_context_push(c, sizeof(char)) = (ch); } while(0)
#define STRING_ERROR(ret) \
do \
{ \
c->top = head; \
return ret; \
} while (0);
typedef struct
{
const char *json;
char *stack;
size_t size, top;
} c_context;
static void* c_context_push(c_context* c, size_t size){
void *ret;
assert(size > 0);
if(c->top+size >= c->size){
if (c->size == 0)
c->size = C_PARSE_STACK_INIT_SIZE;
while (c->top+size >= c->size)
{
c->size += c->size >> 1;
}
c->stack = (char *)realloc(c->stack, c->size);
}
ret = c->stack + c->top;
c->top += size;
return ret;
}
static void* c_context_pop(c_context* c, size_t size){
assert(c->top >= size);
return c->stack + (c->top -= size);
}
static const char c_parse_hex4(const char* p, unsigned* u){
int i;
*u = 0;
for (i = 0; i < 4; i++){
char ch = *p++;
*u <<= 4;
if (ch >= '0' && ch <= '9')
*u |= ch - '0';
else if (ch >= 'A' && ch <= 'F')
*u |= ch - ('A' - 10);
else if (ch >= 'a' && ch <= 'f')
*u |= ch - ('a' - 10);
else
return NULL;
}
return p;
}
static int c_parse_string(c_context* c, c_value* v){
size_t head = c->top, len;
const char *p;
unsigned u;
EXCEPT(c, '\"');
p = c->json;
for (;;)
{
char ch = *p++;
switch (ch)
{
case '\"':
len = c->top - head;
c_set_string(v, (const char *)c_context_pop(c, len), len);
c->json = p;
return C_PARSE_OK;
case '\\':
switch (*p++)
{
case '\"':
PUTC(c, '\"');break;
case '\\':
PUTC(c, '\\'); break;
case '/':
PUTC(c, '/'); break;
case 'b':
PUTC(c, 'b'); break;
case 'f':
PUTC(c, 'f'); break;
case 'n':
PUTC(c, 'n');break;
case 'r':
PUTC(c, 'r');break;
case 't':
PUTC(c, 't');break;
case 'u':
if(!(p = c_parse_hex4(p, &u)))
STRING_ERROR(C_PARSE_INVALID_UNICODE_HEX);
break;
default:
c->top = head;
return C_PARSE_INVALID_STRING_ESCAPE;
}
case '\0':
c->top = head;
return C_PARSE_MISS_QUOTATION_MARK;
default:
if((unsigned char) ch < 0x20){
c->top = head;
return C_PARSE_INVALID_STRING_CHAR;
}
PUTC(c, ch);
}
}
}
static void c_parse_whitespace(c_context* c)
{
const char *p = c->json;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
{
p++;
}
c->json = p;
}
static int c_parse_literal(c_context* c, c_value* v, const char* literal, c_type type){
size_t i;
EXCEPT(c, literal[0]);
for (i = 0; literal[i + 1]; i++)
if(c->json[i] != literal[i+1])
return C_PARSE_INVALID_VALUE;
c->json += i;
v->type = type;
return C_PARSE_OK;
}
static int c_parse_number(c_context* c, c_value* v) {
const char *p = c->json;
if(*p == '-')
p++;
if(*p == '0')
p++;
else{
if(!ISDIGIT1TO9(*p))
return C_PARSE_INVALID_VALUE;
for (p++; ISDIGIT(*p); p++)
;
}
if(*p == '.'){
p++;
if(!ISDIGIT(*p))
return C_PARSE_INVALID_VALUE;
for (p++; ISDIGIT(*p); p++)
;
}
if(*p == 'e' || *p == 'E'){
p++;
if (*p == '+' || *p == '-')
p++;
if (!ISDIGIT(*p))
return C_PARSE_INVALID_VALUE;
for (p++; ISDIGIT(*p); p++)
;
}
v->n = strtod(c->json, NULL);
if (v->n == HUGE_VAL || v->n == -HUGE_VAL)
return C_PARSE_NUMBER_TOO_BIG;
v->type = C_NUMBER;
c->json = p;
return C_PARSE_OK;
}
static int c_parse_value(c_context* c, c_value* v)
{
switch (*c->json)
{
case 'n':
return c_parse_literal(c, v, "null", C_NULL);
case 't':
return c_parse_literal(c, v, "true", C_TRUE);
case 'f':
return c_parse_literal(c, v, "false", C_FALSE);
case '\0':
return C_PARSE_EXPECT_VALUE;
case '"':
return c_parse_string(c, v);
default:
return c_parse_number(c, v);
}
}
int c_parse(c_value* v, const char* json)
{
c_context c;
int ret;
assert(v != NULL);
c.json = json;
c.stack = NULL;
c.size = c.top = 0;
c_init(v);
c_parse_whitespace(&c);
if((ret = c_parse_value(&c, v)) == C_PARSE_OK){
c_parse_whitespace(&c);
if (*c.json != '\0'){
v->type = C_NULL;
ret = C_PARSE_ROOT_NOT_SINGULAR;
}
}
assert(c.top == 0);
free(c.stack);
return ret;
}
c_type c_get_type(const c_value* v)
{
assert(v != NULL);
return v->type;
}
int c_get_boolean(const c_value* v){
assert(v != NULL && (v->type == C_TRUE || v->type == C_FALSE));
return v->type == C_TRUE;
}
double c_get_number(const c_value* v){
assert(v != NULL && v->type == C_NUMBER);
return v->n;
}
void c_free(c_value* v){
assert(v!= NULL);
if(v->type == C_STRING)
free(v->s);
v->type = C_NULL;
}
void c_set_string(c_value* v, const char* s, size_t len){
assert(v != NULL && (s != NULL || len == 0));
c_free(v);
v->s = (char*)malloc(len + 1);
memcpy(v->s, s, len);
v->s[len] = '\0';
v->len = len;
v->type = C_STRING;
}
void c_set_number(c_value* v, double n){
c_free(v);
v->n = n;
v->type = C_NUMBER;
}
void c_set_boolean(c_value*v, int b){
c_free(v);
v->type = b ? C_TRUE : C_FALSE;
}
size_t c_get_string_length(const c_value* v) {
assert(v != NULL && v->type == C_STRING);
return v->len;
}
const char* c_get_string(const c_value* v) {
assert(v != NULL && v->type == C_STRING);
return v->s;
}