MCPcopy Create free account
hub / github.com/Netis/cloud-probe / cJSON_ParseWithLengthOpts

Function cJSON_ParseWithLengthOpts

cpworker/src/cJSON/cJSON.c:1104–1181  ·  view source on GitHub ↗

Parse an object - create a new root, and populate. */

Source from the content-addressed store, hash-verified

1102
1103/* Parse an object - create a new root, and populate. */
1104CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated)
1105{
1106 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
1107 cJSON *item = NULL;
1108
1109 /* reset error position */
1110 global_error.json = NULL;
1111 global_error.position = 0;
1112
1113 if (value == NULL || 0 == buffer_length)
1114 {
1115 goto fail;
1116 }
1117
1118 buffer.content = (const unsigned char*)value;
1119 buffer.length = buffer_length;
1120 buffer.offset = 0;
1121 buffer.hooks = global_hooks;
1122
1123 item = cJSON_New_Item(&global_hooks);
1124 if (item == NULL) /* memory fail */
1125 {
1126 goto fail;
1127 }
1128
1129 if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
1130 {
1131 /* parse failure. ep is set. */
1132 goto fail;
1133 }
1134
1135 /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
1136 if (require_null_terminated)
1137 {
1138 buffer_skip_whitespace(&buffer);
1139 if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
1140 {
1141 goto fail;
1142 }
1143 }
1144 if (return_parse_end)
1145 {
1146 *return_parse_end = (const char*)buffer_at_offset(&buffer);
1147 }
1148
1149 return item;
1150
1151fail:
1152 if (item != NULL)
1153 {
1154 cJSON_Delete(item);
1155 }
1156
1157 if (value != NULL)
1158 {
1159 error local_error;
1160 local_error.json = (const unsigned char*)value;
1161 local_error.position = 0;

Callers 2

cJSON_ParseWithOptsFunction · 0.85
cJSON_ParseWithLengthFunction · 0.85

Calls 5

cJSON_New_ItemFunction · 0.85
parse_valueFunction · 0.85
buffer_skip_whitespaceFunction · 0.85
skip_utf8_bomFunction · 0.85
cJSON_DeleteFunction · 0.85

Tested by

no test coverage detected