static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { size_t index; if (object->count >= object->capacity) { size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); if (new_capacity > OBJECT_MAX_CAPACITY) { return ERROR; } if (json_object_resize(object, new_capacity) == ERROR) { return ERROR; } } if (json_object_get_value(object, name) != NULL) { return ERROR; } index = object->count; object->names[index] = parson_strndup(name, strlen(name)); if (!object->names[index]) { return ERROR; } object->values[index] = value; object->count++; return SUCCESS; }
/* Returns contents of a string inside double quotes and parses escaped characters inside. Example: "\u006Corem ipsum" -> lorem ipsum */ static const char * get_processed_string(const char **string) { const char *string_start = *string; char *output, *processed_ptr, *unprocessed_ptr, current_char; unsigned int utf_val; skip_quotes(string); if (**string == '\0') { return NULL; } output = parson_strndup(string_start + 1, *string - string_start - 2); if (!output) { return NULL; } processed_ptr = unprocessed_ptr = output; while (*unprocessed_ptr) { current_char = *unprocessed_ptr; if (current_char == '\\') { unprocessed_ptr++; current_char = *unprocessed_ptr; switch (current_char) { case '\"': case '\\': case '/': break; case 'b': current_char = '\b'; break; case 'f': current_char = '\f'; break; case 'n': current_char = '\n'; break; case 'r': current_char = '\r'; break; case 't': current_char = '\t'; break; case 'u': unprocessed_ptr++; if (!is_utf((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &utf_val) == EOF) { parson_free(output); return NULL; } if (utf_val < 0x80) { current_char = utf_val; } else if (utf_val < 0x800) { *processed_ptr++ = (utf_val >> 6) | 0xC0; current_char = ((utf_val | 0x80) & 0xBF); } else { *processed_ptr++ = (utf_val >> 12) | 0xE0; *processed_ptr++ = (((utf_val >> 6) | 0x80) & 0xBF); current_char = ((utf_val | 0x80) & 0xBF); } unprocessed_ptr += 3; break; default: parson_free(output); return NULL; break; }
static char * parson_strdup(const char *string) { return parson_strndup(string, strlen(string)); }