| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <string.h> |
| 20 |
#include <errno.h> |
| 21 |
#include <assert.h> |
| 22 |
|
| 23 |
#include "api/yajl_tree.h" |
| 24 |
#include "api/yajl_parse.h" |
| 25 |
|
| 26 |
#include "yajl_parser.h" |
| 27 |
|
| 28 |
#ifdef _MSC_VER |
| 29 |
#define snprintf sprintf_s |
| 30 |
#endif |
| 31 |
|
| 32 |
#define STATUS_CONTINUE 1 |
| 33 |
#define STATUS_ABORT 0 |
| 34 |
|
| 35 |
struct stack_elem_s; |
| 36 |
typedef struct stack_elem_s stack_elem_t; |
| 37 |
struct stack_elem_s |
| 38 |
{ |
| 39 |
char * key; |
| 40 |
yajl_val value; |
| 41 |
stack_elem_t *next; |
| 42 |
}; |
| 43 |
|
| 44 |
struct context_s |
| 45 |
{ |
| 46 |
stack_elem_t *stack; |
| 47 |
yajl_val root; |
| 48 |
char *errbuf; |
| 49 |
size_t errbuf_size; |
| 50 |
}; |
| 51 |
typedef struct context_s context_t; |
| 52 |
|
| 53 |
#define RETURN_ERROR(ctx,retval,...) { \ |
| 54 |
if ((ctx)->errbuf != NULL) \ |
| 55 |
snprintf ((ctx)->errbuf, (ctx)->errbuf_size, __VA_ARGS__); \ |
| 56 |
return (retval); \ |
| 57 |
} |
| 58 |
|
| 59 |
static yajl_val value_alloc (yajl_type type) |
| 60 |
{ |
| 61 |
yajl_val v; |
| 62 |
|
| 63 |
v = malloc (sizeof (*v)); |
| 64 |
if (v == NULL) return (NULL); |
| 65 |
memset (v, 0, sizeof (*v)); |
| 66 |
v->type = type; |
| 67 |
|
| 68 |
return (v); |
| 69 |
} |
| 70 |
|
| 71 |
static void yajl_object_free (yajl_val v) |
| 72 |
{ |
| 73 |
size_t i; |
| 74 |
|
| 75 |
if (!YAJL_IS_OBJECT(v)) return; |
| 76 |
|
| 77 |
for (i = 0; i < v->u.object.len; i++) |
| 78 |
{ |
| 79 |
free(v->u.object.keys[i]); |
| 80 |
v->u.object.keys[i] = NULL; |
| 81 |
yajl_tree_free (v->u.object.values[i]); |
| 82 |
v->u.object.values[i] = NULL; |
| 83 |
} |
| 84 |
|
| 85 |
free((void*) v->u.object.keys); |
| 86 |
free(v->u.object.values); |
| 87 |
free(v); |
| 88 |
} |
| 89 |
|
| 90 |
static void yajl_array_free (yajl_val v) |
| 91 |
{ |
| 92 |
size_t i; |
| 93 |
|
| 94 |
if (!YAJL_IS_ARRAY(v)) return; |
| 95 |
|
| 96 |
for (i = 0; i < v->u.array.len; i++) |
| 97 |
{ |
| 98 |
yajl_tree_free (v->u.array.values[i]); |
| 99 |
v->u.array.values[i] = NULL; |
| 100 |
} |
| 101 |
|
| 102 |
free(v->u.array.values); |
| 103 |
free(v); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static int context_push(context_t *ctx, yajl_val v) |
| 114 |
{ |
| 115 |
stack_elem_t *stack; |
| 116 |
|
| 117 |
stack = malloc (sizeof (*stack)); |
| 118 |
if (stack == NULL) |
| 119 |
RETURN_ERROR (ctx, ENOMEM, "Out of memory"); |
| 120 |
memset (stack, 0, sizeof (*stack)); |
| 121 |
|
| 122 |
assert ((ctx->stack == NULL) |
| 123 |
|| YAJL_IS_OBJECT (v) |
| 124 |
|| YAJL_IS_ARRAY (v)); |
| 125 |
|
| 126 |
stack->value = v; |
| 127 |
stack->next = ctx->stack; |
| 128 |
ctx->stack = stack; |
| 129 |
|
| 130 |
return (0); |
| 131 |
} |
| 132 |
|
| 133 |
static yajl_val context_pop(context_t *ctx) |
| 134 |
{ |
| 135 |
stack_elem_t *stack; |
| 136 |
yajl_val v; |
| 137 |
|
| 138 |
if (ctx->stack == NULL) |
| 139 |
RETURN_ERROR (ctx, NULL, "context_pop: " |
| 140 |
"Bottom of stack reached prematurely"); |
| 141 |
|
| 142 |
stack = ctx->stack; |
| 143 |
ctx->stack = stack->next; |
| 144 |
|
| 145 |
v = stack->value; |
| 146 |
free (stack->key); |
| 147 |
free (stack); |
| 148 |
|
| 149 |
return (v); |
| 150 |
} |
| 151 |
|
| 152 |
static int object_add_keyval(context_t *ctx, |
| 153 |
yajl_val obj, char *key, yajl_val value) |
| 154 |
{ |
| 155 |
char **tmpk; |
| 156 |
yajl_val *tmpv; |
| 157 |
|
| 158 |
|
| 159 |
assert (ctx != NULL); |
| 160 |
assert (obj != NULL); |
| 161 |
assert (key != NULL); |
| 162 |
assert (value != NULL); |
| 163 |
|
| 164 |
|
| 165 |
assert(YAJL_IS_OBJECT(obj)); |
| 166 |
|
| 167 |
tmpk = realloc((void *) obj->u.object.keys, sizeof(*(obj->u.object.keys)) * (obj->u.object.len + 1)); |
| 168 |
if (tmpk == NULL) |
| 169 |
RETURN_ERROR(ctx, ENOMEM, "Out of memory"); |
| 170 |
obj->u.object.keys = tmpk; |
| 171 |
|
| 172 |
tmpv = realloc(obj->u.object.values, sizeof (*obj->u.object.values) * (obj->u.object.len + 1)); |
| 173 |
if (tmpv == NULL) |
| 174 |
RETURN_ERROR(ctx, ENOMEM, "Out of memory"); |
| 175 |
obj->u.object.values = tmpv; |
| 176 |
|
| 177 |
obj->u.object.keys[obj->u.object.len] = key; |
| 178 |
obj->u.object.values[obj->u.object.len] = value; |
| 179 |
obj->u.object.len++; |
| 180 |
|
| 181 |
return (0); |
| 182 |
} |
| 183 |
|
| 184 |
static int array_add_value (context_t *ctx, |
| 185 |
yajl_val array, yajl_val value) |
| 186 |
{ |
| 187 |
yajl_val *tmp; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
assert (ctx != NULL); |
| 192 |
assert (array != NULL); |
| 193 |
assert (value != NULL); |
| 194 |
|
| 195 |
|
| 196 |
assert(YAJL_IS_ARRAY(array)); |
| 197 |
|
| 198 |
tmp = realloc(array->u.array.values, |
| 199 |
sizeof(*(array->u.array.values)) * (array->u.array.len + 1)); |
| 200 |
if (tmp == NULL) |
| 201 |
RETURN_ERROR(ctx, ENOMEM, "Out of memory"); |
| 202 |
array->u.array.values = tmp; |
| 203 |
array->u.array.values[array->u.array.len] = value; |
| 204 |
array->u.array.len++; |
| 205 |
|
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static int context_add_value (context_t *ctx, yajl_val v) |
| 214 |
{ |
| 215 |
|
| 216 |
assert (ctx != NULL); |
| 217 |
assert (v != NULL); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (ctx->stack == NULL) |
| 231 |
{ |
| 232 |
assert (ctx->root == NULL); |
| 233 |
ctx->root = v; |
| 234 |
return (0); |
| 235 |
} |
| 236 |
else if (YAJL_IS_OBJECT (ctx->stack->value)) |
| 237 |
{ |
| 238 |
if (ctx->stack->key == NULL) |
| 239 |
{ |
| 240 |
if (!YAJL_IS_STRING (v)) |
| 241 |
RETURN_ERROR (ctx, EINVAL, "context_add_value: " |
| 242 |
"Object key is not a string (%#04x)", |
| 243 |
v->type); |
| 244 |
|
| 245 |
ctx->stack->key = v->u.string; |
| 246 |
v->u.string = NULL; |
| 247 |
free(v); |
| 248 |
return (0); |
| 249 |
} |
| 250 |
else |
| 251 |
{ |
| 252 |
char * key; |
| 253 |
|
| 254 |
key = ctx->stack->key; |
| 255 |
ctx->stack->key = NULL; |
| 256 |
return (object_add_keyval (ctx, ctx->stack->value, key, v)); |
| 257 |
} |
| 258 |
} |
| 259 |
else if (YAJL_IS_ARRAY (ctx->stack->value)) |
| 260 |
{ |
| 261 |
return (array_add_value (ctx, ctx->stack->value, v)); |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
RETURN_ERROR (ctx, EINVAL, "context_add_value: Cannot add value to " |
| 266 |
"a value of type %#04x (not a composite type)", |
| 267 |
ctx->stack->value->type); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
static int handle_string (void *ctx, |
| 272 |
const unsigned char *string, size_t string_length) |
| 273 |
{ |
| 274 |
yajl_val v; |
| 275 |
|
| 276 |
v = value_alloc (yajl_t_string); |
| 277 |
if (v == NULL) |
| 278 |
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 279 |
|
| 280 |
v->u.string = malloc (string_length + 1); |
| 281 |
if (v->u.string == NULL) |
| 282 |
{ |
| 283 |
free (v); |
| 284 |
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 285 |
} |
| 286 |
memcpy(v->u.string, string, string_length); |
| 287 |
v->u.string[string_length] = 0; |
| 288 |
|
| 289 |
return ((context_add_value (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 290 |
} |
| 291 |
|
| 292 |
static int handle_number (void *ctx, const char *string, size_t string_length) |
| 293 |
{ |
| 294 |
yajl_val v; |
| 295 |
char *endptr; |
| 296 |
|
| 297 |
v = value_alloc(yajl_t_number); |
| 298 |
if (v == NULL) |
| 299 |
RETURN_ERROR((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 300 |
|
| 301 |
v->u.number.r = malloc(string_length + 1); |
| 302 |
if (v->u.number.r == NULL) |
| 303 |
{ |
| 304 |
free(v); |
| 305 |
RETURN_ERROR((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 306 |
} |
| 307 |
memcpy(v->u.number.r, string, string_length); |
| 308 |
v->u.number.r[string_length] = 0; |
| 309 |
|
| 310 |
v->u.number.flags = 0; |
| 311 |
|
| 312 |
errno = 0; |
| 313 |
v->u.number.i = yajl_parse_integer((const unsigned char *) v->u.number.r, |
| 314 |
strlen(v->u.number.r)); |
| 315 |
if (errno == 0) |
| 316 |
v->u.number.flags |= YAJL_NUMBER_INT_VALID; |
| 317 |
|
| 318 |
endptr = NULL; |
| 319 |
errno = 0; |
| 320 |
v->u.number.d = strtod(v->u.number.r, &endptr); |
| 321 |
if ((errno == 0) && (endptr != NULL) && (*endptr == 0)) |
| 322 |
v->u.number.flags |= YAJL_NUMBER_DOUBLE_VALID; |
| 323 |
|
| 324 |
return ((context_add_value(ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 325 |
} |
| 326 |
|
| 327 |
static int handle_start_map (void *ctx) |
| 328 |
{ |
| 329 |
yajl_val v; |
| 330 |
|
| 331 |
v = value_alloc(yajl_t_object); |
| 332 |
if (v == NULL) |
| 333 |
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 334 |
|
| 335 |
v->u.object.keys = NULL; |
| 336 |
v->u.object.values = NULL; |
| 337 |
v->u.object.len = 0; |
| 338 |
|
| 339 |
return ((context_push (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 340 |
} |
| 341 |
|
| 342 |
static int handle_end_map (void *ctx) |
| 343 |
{ |
| 344 |
yajl_val v; |
| 345 |
|
| 346 |
v = context_pop (ctx); |
| 347 |
if (v == NULL) |
| 348 |
return (STATUS_ABORT); |
| 349 |
|
| 350 |
return ((context_add_value (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 351 |
} |
| 352 |
|
| 353 |
static int handle_start_array (void *ctx) |
| 354 |
{ |
| 355 |
yajl_val v; |
| 356 |
|
| 357 |
v = value_alloc(yajl_t_array); |
| 358 |
if (v == NULL) |
| 359 |
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 360 |
|
| 361 |
v->u.array.values = NULL; |
| 362 |
v->u.array.len = 0; |
| 363 |
|
| 364 |
return ((context_push (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 365 |
} |
| 366 |
|
| 367 |
static int handle_end_array (void *ctx) |
| 368 |
{ |
| 369 |
yajl_val v; |
| 370 |
|
| 371 |
v = context_pop (ctx); |
| 372 |
if (v == NULL) |
| 373 |
return (STATUS_ABORT); |
| 374 |
|
| 375 |
return ((context_add_value (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 376 |
} |
| 377 |
|
| 378 |
static int handle_boolean (void *ctx, int boolean_value) |
| 379 |
{ |
| 380 |
yajl_val v; |
| 381 |
|
| 382 |
v = value_alloc (boolean_value ? yajl_t_true : yajl_t_false); |
| 383 |
if (v == NULL) |
| 384 |
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 385 |
|
| 386 |
return ((context_add_value (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 387 |
} |
| 388 |
|
| 389 |
static int handle_null (void *ctx) |
| 390 |
{ |
| 391 |
yajl_val v; |
| 392 |
|
| 393 |
v = value_alloc (yajl_t_null); |
| 394 |
if (v == NULL) |
| 395 |
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory"); |
| 396 |
|
| 397 |
return ((context_add_value (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
yajl_val yajl_tree_parse (const char *input, |
| 404 |
char *error_buffer, size_t error_buffer_size) |
| 405 |
{ |
| 406 |
static const yajl_callbacks callbacks = |
| 407 |
{ |
| 408 |
handle_null, |
| 409 |
handle_boolean, |
| 410 |
NULL, |
| 411 |
NULL, |
| 412 |
handle_number, |
| 413 |
handle_string, |
| 414 |
handle_start_map, |
| 415 |
handle_string, |
| 416 |
handle_end_map, |
| 417 |
handle_start_array, |
| 418 |
handle_end_array |
| 419 |
}; |
| 420 |
|
| 421 |
yajl_handle handle; |
| 422 |
yajl_status status; |
| 423 |
char * internal_err_str; |
| 424 |
context_t ctx = { NULL, NULL, NULL, 0 }; |
| 425 |
|
| 426 |
ctx.errbuf = error_buffer; |
| 427 |
ctx.errbuf_size = error_buffer_size; |
| 428 |
|
| 429 |
if (error_buffer != NULL) |
| 430 |
memset (error_buffer, 0, error_buffer_size); |
| 431 |
|
| 432 |
handle = yajl_alloc (&callbacks, NULL, &ctx); |
| 433 |
yajl_config(handle, yajl_allow_comments, 1); |
| 434 |
|
| 435 |
status = yajl_parse(handle, |
| 436 |
(const unsigned char *) input, |
| 437 |
strlen (input)); |
| 438 |
status = yajl_complete_parse (handle); |
| 439 |
if (status != yajl_status_ok) { |
| 440 |
if (error_buffer != NULL && error_buffer_size > 0) { |
| 441 |
internal_err_str = (char *) yajl_get_error(handle, 1, |
| 442 |
(const unsigned char *) input, |
| 443 |
strlen(input)); |
| 444 |
snprintf(error_buffer, error_buffer_size, "%s", internal_err_str); |
| 445 |
YA_FREE(&(handle->alloc), internal_err_str); |
| 446 |
} |
| 447 |
while(ctx.stack != NULL) { |
| 448 |
yajl_val v = context_pop(&ctx); |
| 449 |
yajl_tree_free(v); |
| 450 |
} |
| 451 |
yajl_free (handle); |
| 452 |
|
| 453 |
if(ctx.root) |
| 454 |
yajl_tree_free(ctx.root); |
| 455 |
return NULL; |
| 456 |
} |
| 457 |
|
| 458 |
yajl_free (handle); |
| 459 |
return (ctx.root); |
| 460 |
} |
| 461 |
|
| 462 |
yajl_val yajl_tree_get(yajl_val n, const char ** path, yajl_type type) |
| 463 |
{ |
| 464 |
if (!path) return NULL; |
| 465 |
while (n && *path) { |
| 466 |
size_t i; |
| 467 |
size_t len; |
| 468 |
|
| 469 |
if (n->type != yajl_t_object) return NULL; |
| 470 |
len = n->u.object.len; |
| 471 |
for (i = 0; i < len; i++) { |
| 472 |
if (!strcmp(*path, n->u.object.keys[i])) { |
| 473 |
n = n->u.object.values[i]; |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
if (i == len) return NULL; |
| 478 |
path++; |
| 479 |
} |
| 480 |
if (n && type != yajl_t_any && type != n->type) n = NULL; |
| 481 |
return n; |
| 482 |
} |
| 483 |
|
| 484 |
void yajl_tree_free (yajl_val v) |
| 485 |
{ |
| 486 |
if (v == NULL) return; |
| 487 |
|
| 488 |
if (YAJL_IS_STRING(v)) |
| 489 |
{ |
| 490 |
free(v->u.string); |
| 491 |
free(v); |
| 492 |
} |
| 493 |
else if (YAJL_IS_NUMBER(v)) |
| 494 |
{ |
| 495 |
free(v->u.number.r); |
| 496 |
free(v); |
| 497 |
} |
| 498 |
else if (YAJL_GET_OBJECT(v)) |
| 499 |
{ |
| 500 |
yajl_object_free(v); |
| 501 |
} |
| 502 |
else if (YAJL_GET_ARRAY(v)) |
| 503 |
{ |
| 504 |
yajl_array_free(v); |
| 505 |
} |
| 506 |
else |
| 507 |
{ |
| 508 |
free(v); |
| 509 |
} |
| 510 |
} |