Changeset 5726a0d
- Timestamp:
- 2015-05-04T22:35:13Z (9 years ago)
- Children:
- 8563fe7
- Parents:
- b1dc403
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/parson.c
rb1dc403 r5726a0d 51 51 #define PRINTF_AND_SKIP(str, format, to_append) str += sprintf(str, format, to_append); 52 52 53 #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ 54 53 55 /* Type definitions */ 54 56 typedef union json_value_value { … … 85 87 static char * parson_strndup(const char *string, size_t n); 86 88 static char * parson_strdup(const char *string); 87 static int is_utf(const unsigned char *string); 89 static int is_utf16_hex(const unsigned char *string); 90 static int num_bytes_in_utf8_sequence(unsigned char c); 91 static int verify_utf8_sequence(const unsigned char *string, int *len); 92 static int is_valid_utf8(const char *string, size_t string_len); 88 93 static int is_decimal(const char *string, size_t length); 89 static size_t parson_strlen(const char *string);94 static size_t serialization_strlen(const char *string); 90 95 91 96 /* JSON Object */ … … 150 155 } 151 156 152 static int is_utf (const unsigned char *s) {157 static int is_utf16_hex(const unsigned char *s) { 153 158 return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]); 159 } 160 161 static int num_bytes_in_utf8_sequence(unsigned char c) { 162 if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { 163 return 0; 164 } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ 165 return 1; 166 } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ 167 return 2; 168 } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ 169 return 3; 170 } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ 171 return 4; 172 } 173 return 0; /* won't happen */ 174 } 175 176 static int verify_utf8_sequence(const unsigned char *string, int *len) { 177 unsigned int cp = 0; 178 *len = num_bytes_in_utf8_sequence(string[0]); 179 180 if (*len == 1) { 181 cp = string[0]; 182 } else if (*len == 2 && IS_CONT(string[1])) { 183 cp = string[0] & 0x1F; 184 cp = (cp << 6) | (string[1] & 0x3F); 185 } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { 186 cp = ((unsigned char)string[0]) & 0xF; 187 cp = (cp << 6) | (string[1] & 0x3F); 188 cp = (cp << 6) | (string[2] & 0x3F); 189 } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { 190 cp = string[0] & 0x7; 191 cp = (cp << 6) | (string[1] & 0x3F); 192 cp = (cp << 6) | (string[2] & 0x3F); 193 cp = (cp << 6) | (string[3] & 0x3F); 194 } else { 195 return 0; 196 } 197 198 /* overlong encodings */ 199 if ((cp < 0x80 && *len > 1) || 200 (cp < 0x800 && *len > 2) || 201 (cp < 0x10000 && *len > 3)) { 202 return 0; 203 } 204 205 /* invalid unicode */ 206 if (cp > 0x10FFFF) { 207 return 0; 208 } 209 210 /* surrogate halves */ 211 if (cp >= 0xD800 && cp <= 0xDFFF) { 212 return 0; 213 } 214 215 return 1; 216 } 217 218 static int is_valid_utf8(const char *string, size_t string_len) { 219 int len = 0; 220 const char *string_end = string + string_len; 221 while (string < string_end) { 222 if (!verify_utf8_sequence((const unsigned char*)string, &len)) { 223 return 0; 224 } 225 string += len; 226 } 227 return 1; 154 228 } 155 229 … … 165 239 } 166 240 167 static size_t parson_strlen(const char *string) {241 static size_t serialization_strlen(const char *string) { 168 242 size_t result = 0; 169 243 size_t i = 0, len = strlen(string); … … 180 254 FILE *fp = fopen(filename, "r"); 181 255 size_t file_size; 256 long pos; 182 257 char *file_contents; 183 258 if (!fp) 184 259 return NULL; 185 260 fseek(fp, 0L, SEEK_END); 186 file_size = ftell(fp); 261 pos = ftell(fp); 262 if (pos < 0) { 263 fclose(fp); 264 return NULL; 265 } 266 file_size = pos; 187 267 rewind(fp); 188 268 file_contents = (char*)PARSON_MALLOC(sizeof(char) * (file_size + 1)); … … 247 327 248 328 static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { 249 size_t index; 329 size_t index = 0; 330 if (object == NULL || name == NULL || value == NULL) { 331 return JSONFailure; 332 } 250 333 if (object->count >= object->capacity) { 251 334 size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); … … 259 342 index = object->count; 260 343 object->names[index] = parson_strdup(name); 261 if ( !object->names[index])344 if (object->names[index] == NULL) 262 345 return JSONFailure; 263 346 object->values[index] = value; … … 366 449 const char *unprocessed_ptr = *unprocessed; 367 450 unprocessed_ptr++; /* skips u */ 368 if (!is_utf ((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF)451 if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) 369 452 return JSONFailure; 370 453 if (cp < 0x80) { … … 381 464 unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ 382 465 if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */ 383 !is_utf ((const unsigned char*)unprocessed_ptr) ||466 !is_utf16_hex((const unsigned char*)unprocessed_ptr) || 384 467 sscanf(unprocessed_ptr, "%4x", &trail) == EOF || 385 468 trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ … … 435 518 } 436 519 *output_ptr = '\0'; 437 if (try_realloc((void**)&output, strlen(output) + 1) == JSONFailure)520 if (try_realloc((void**)&output, (size_t)(output_ptr-output) + 1) == JSONFailure) /* resize to new length */ 438 521 goto error; 439 522 return output; … … 637 720 for (i = 0; i < count; i++) { 638 721 key = json_object_get_name(object, i); 639 result_size += parson_strlen(key) + 2; /* string and quotes */722 result_size += serialization_strlen(key) + 2; /* string and quotes */ 640 723 result_size += json_serialization_size_r(json_object_get_value(object, key), buf); 641 724 } 642 725 return result_size; 643 726 case JSONString: 644 return parson_strlen(json_value_get_string(value)) + 2; /* string and quotes */727 return serialization_strlen(json_value_get_string(value)) + 2; /* string and quotes */ 645 728 case JSONBoolean: 646 729 if (json_value_get_boolean(value)) … … 678 761 temp_value = json_array_get_value(array, i); 679 762 buf = json_serialize_to_buffer_r(temp_value, buf); 763 if (buf == NULL) 764 return NULL; 680 765 if (i < (count - 1)) 681 766 PRINT_AND_SKIP(buf, ","); … … 690 775 key = json_object_get_name(object, i); 691 776 buf = json_serialize_string(key, buf); 777 if (buf == NULL) 778 return NULL; 692 779 PRINT_AND_SKIP(buf, ":"); 693 780 temp_value = json_object_get_value(object, key); 694 781 buf = json_serialize_to_buffer_r(temp_value, buf); 782 if (buf == NULL) 783 return NULL; 695 784 if (i < (count - 1)) 696 785 PRINT_AND_SKIP(buf, ","); … … 752 841 char *file_contents = read_file(filename); 753 842 JSON_Value *output_value = NULL; 754 if ( !file_contents)843 if (file_contents == NULL) 755 844 return NULL; 756 845 output_value = json_parse_string(file_contents); … … 762 851 char *file_contents = read_file(filename); 763 852 JSON_Value *output_value = NULL; 764 if ( !file_contents)853 if (file_contents == NULL) 765 854 return NULL; 766 855 output_value = json_parse_string_with_comments(file_contents); … … 773 862 JSON_Value *ret; 774 863 775 if ( !string)864 if (string == NULL) 776 865 return NULL; 777 866 SKIP_WHITESPACES(&string); … … 795 884 char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; 796 885 string_mutable_copy = parson_strdup(string); 797 if ( !string_mutable_copy)886 if (string_mutable_copy == NULL) 798 887 return NULL; 799 888 remove_comments(string_mutable_copy, "/*", "*/"); … … 814 903 815 904 JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { 905 if (object == NULL || name == NULL) 906 return NULL; 816 907 return json_object_nget_value(object, name, strlen(name)); 817 908 } … … 984 1075 985 1076 JSON_Value * json_value_init_string(const char *string) { 986 char *processed_copy = process_string(string, strlen(string)); 987 if (processed_copy == NULL) 988 return NULL; 989 return json_value_init_string_no_copy(processed_copy); 1077 char *copy = NULL; 1078 JSON_Value *value; 1079 size_t string_len = 0; 1080 if (string == NULL) 1081 return NULL; 1082 string_len = strlen(string); 1083 if (!is_valid_utf8(string, string_len)) 1084 return NULL; 1085 copy = parson_strndup(string, string_len); 1086 if (copy == NULL) 1087 return NULL; 1088 value = json_value_init_string_no_copy(copy); 1089 if (value == NULL) 1090 PARSON_FREE(copy); 1091 return value; 990 1092 } 991 1093 … … 1074 1176 if (temp_string_copy == NULL) 1075 1177 return NULL; 1076 return json_value_init_string_no_copy(temp_string_copy); 1178 return_value = json_value_init_string_no_copy(temp_string_copy); 1179 if (return_value == NULL) 1180 PARSON_FREE(temp_string_copy); 1181 return return_value; 1077 1182 case JSONNull: 1078 1183 return json_value_init_null(); … … 1162 1267 1163 1268 JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { 1164 return json_array_replace_value(array, i, json_value_init_string(string)); 1269 JSON_Value *value = json_value_init_string(string); 1270 if (value == NULL) 1271 return JSONFailure; 1272 if (json_array_replace_value(array, i, value) == JSONFailure) { 1273 json_value_free(value); 1274 return JSONFailure; 1275 } 1276 return JSONSuccess; 1165 1277 } 1166 1278 1167 1279 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { 1168 return json_array_replace_value(array, i, json_value_init_number(number)); 1280 JSON_Value *value = json_value_init_number(number); 1281 if (value == NULL) 1282 return JSONFailure; 1283 if (json_array_replace_value(array, i, value) == JSONFailure) { 1284 json_value_free(value); 1285 return JSONFailure; 1286 } 1287 return JSONSuccess; 1169 1288 } 1170 1289 1171 1290 JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { 1172 return json_array_replace_value(array, i, json_value_init_boolean(boolean)); 1291 JSON_Value *value = json_value_init_boolean(boolean); 1292 if (value == NULL) 1293 return JSONFailure; 1294 if (json_array_replace_value(array, i, value) == JSONFailure) { 1295 json_value_free(value); 1296 return JSONFailure; 1297 } 1298 return JSONSuccess; 1173 1299 } 1174 1300 1175 1301 JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { 1176 return json_array_replace_value(array, i, json_value_init_null()); 1302 JSON_Value *value = json_value_init_null(); 1303 if (value == NULL) 1304 return JSONFailure; 1305 if (json_array_replace_value(array, i, value) == JSONFailure) { 1306 json_value_free(value); 1307 return JSONFailure; 1308 } 1309 return JSONSuccess; 1177 1310 } 1178 1311 … … 1195 1328 1196 1329 JSON_Status json_array_append_string(JSON_Array *array, const char *string) { 1197 return json_array_append_value(array, json_value_init_string(string)); 1330 JSON_Value *value = json_value_init_string(string); 1331 if (value == NULL) 1332 return JSONFailure; 1333 if (json_array_append_value(array, value) == JSONFailure) { 1334 json_value_free(value); 1335 return JSONFailure; 1336 } 1337 return JSONSuccess; 1198 1338 } 1199 1339 1200 1340 JSON_Status json_array_append_number(JSON_Array *array, double number) { 1201 return json_array_append_value(array, json_value_init_number(number)); 1341 JSON_Value *value = json_value_init_number(number); 1342 if (value == NULL) 1343 return JSONFailure; 1344 if (json_array_append_value(array, value) == JSONFailure) { 1345 json_value_free(value); 1346 return JSONFailure; 1347 } 1348 return JSONSuccess; 1202 1349 } 1203 1350 1204 1351 JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { 1205 return json_array_append_value(array, json_value_init_boolean(boolean)); 1352 JSON_Value *value = json_value_init_boolean(boolean); 1353 if (value == NULL) 1354 return JSONFailure; 1355 if (json_array_append_value(array, value) == JSONFailure) { 1356 json_value_free(value); 1357 return JSONFailure; 1358 } 1359 return JSONSuccess; 1206 1360 } 1207 1361 1208 1362 JSON_Status json_array_append_null(JSON_Array *array) { 1209 return json_array_append_value(array, json_value_init_null()); 1363 JSON_Value *value = json_value_init_null(); 1364 if (value == NULL) 1365 return JSONFailure; 1366 if (json_array_append_value(array, value) == JSONFailure) { 1367 json_value_free(value); 1368 return JSONFailure; 1369 } 1370 return JSONSuccess; 1210 1371 } 1211 1372 … … 1213 1374 size_t i = 0; 1214 1375 JSON_Value *old_value; 1215 if (object == NULL )1376 if (object == NULL || name == NULL || value == NULL) 1216 1377 return JSONFailure; 1217 1378 old_value = json_object_get_value(object, name); … … 1225 1386 } 1226 1387 } 1227 json_object_add(object, name, value);/* add new key value pair */1228 return JSONSuccess;1388 /* add new key value pair */ 1389 return json_object_add(object, name, value); 1229 1390 } 1230 1391 … … 1246 1407 1247 1408 JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) { 1248 const char *dot_pos = strchr(name, '.');1409 const char *dot_pos = NULL; 1249 1410 const char *current_name = NULL; 1250 1411 JSON_Object *temp_obj = NULL; 1251 1412 JSON_Value *new_value = NULL; 1252 if (value == NULL) { 1253 return JSONFailure; 1254 } else if (dot_pos == NULL) { 1413 if (value == NULL || name == NULL || value == NULL) 1414 return JSONFailure; 1415 dot_pos = strchr(name, '.'); 1416 if (dot_pos == NULL) { 1255 1417 return json_object_set_value(object, name, value); 1256 1418 } else { … … 1273 1435 return json_object_dotset_value(temp_obj, dot_pos + 1, value); 1274 1436 } 1275 return JSONFailure;1276 1437 } 1277 1438 1278 1439 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { 1279 return json_object_dotset_value(object, name, json_value_init_string(string)); 1440 JSON_Value *value = json_value_init_string(string); 1441 if (value == NULL) 1442 return JSONFailure; 1443 if (json_object_dotset_value(object, name, value) == JSONFailure) { 1444 json_value_free(value); 1445 return JSONFailure; 1446 } 1447 return JSONSuccess; 1280 1448 } 1281 1449 1282 1450 JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { 1283 return json_object_dotset_value(object, name, json_value_init_number(number)); 1451 JSON_Value *value = json_value_init_number(number); 1452 if (value == NULL) 1453 return JSONFailure; 1454 if (json_object_dotset_value(object, name, value) == JSONFailure) { 1455 json_value_free(value); 1456 return JSONFailure; 1457 } 1458 return JSONSuccess; 1284 1459 } 1285 1460 1286 1461 JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { 1287 return json_object_dotset_value(object, name, json_value_init_boolean(boolean)); 1462 JSON_Value *value = json_value_init_boolean(boolean); 1463 if (value == NULL) 1464 return JSONFailure; 1465 if (json_object_dotset_value(object, name, value) == JSONFailure) { 1466 json_value_free(value); 1467 return JSONFailure; 1468 } 1469 return JSONSuccess; 1288 1470 } 1289 1471 1290 1472 JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { 1291 return json_object_dotset_value(object, name, json_value_init_null()); 1473 JSON_Value *value = json_value_init_null(); 1474 if (value == NULL) 1475 return JSONFailure; 1476 if (json_object_dotset_value(object, name, value) == JSONFailure) { 1477 json_value_free(value); 1478 return JSONFailure; 1479 } 1480 return JSONSuccess; 1292 1481 } 1293 1482 … … 1328 1517 return json_object_dotremove(temp_obj, dot_pos + 1); 1329 1518 } 1330 return JSONFailure;1331 1519 } 1332 1520
Note: See TracChangeset
for help on using the changeset viewer.