MCPcopy Create free account
hub / github.com/BigPig0/RelayLive / cJSONUtils_FindPointerFromObjectTo

Function cJSONUtils_FindPointerFromObjectTo

ThirdParty/cjson/cJSON_Utils.c:186–246  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

184}
185
186CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target)
187{
188 size_t child_index = 0;
189 cJSON *current_child = 0;
190
191 if ((object == NULL) || (target == NULL))
192 {
193 return NULL;
194 }
195
196 if (object == target)
197 {
198 /* found */
199 return (char*)cJSONUtils_strdup((const unsigned char*)"");
200 }
201
202 /* recursively search all children of the object or array */
203 for (current_child = object->child; current_child != NULL; (void)(current_child = current_child->next), child_index++)
204 {
205 unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target);
206 /* found the target? */
207 if (target_pointer != NULL)
208 {
209 if (cJSON_IsArray(object))
210 {
211 /* reserve enough memory for a 64 bit integer + '/' and '\0' */
212 unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/"));
213 /* check if conversion to unsigned long is valid
214 * This should be eliminated at compile time by dead code elimination
215 * if size_t is an alias of unsigned long, or if it is bigger */
216 if (child_index > ULONG_MAX)
217 {
218 cJSON_free(target_pointer);
219 return NULL;
220 }
221 sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* /<array_index><path> */
222 cJSON_free(target_pointer);
223
224 return (char*)full_pointer;
225 }
226
227 if (cJSON_IsObject(object))
228 {
229 unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + pointer_encoded_length((unsigned char*)current_child->string) + 2);
230 full_pointer[0] = '/';
231 encode_string_as_pointer(full_pointer + 1, (unsigned char*)current_child->string);
232 strcat((char*)full_pointer, (char*)target_pointer);
233 cJSON_free(target_pointer);
234
235 return (char*)full_pointer;
236 }
237
238 /* reached leaf of the tree, found nothing */
239 cJSON_free(target_pointer);
240 return NULL;
241 }
242 }
243

Callers

nothing calls this directly

Calls 7

cJSONUtils_strdupFunction · 0.85
cJSON_IsArrayFunction · 0.85
cJSON_mallocFunction · 0.85
cJSON_freeFunction · 0.85
cJSON_IsObjectFunction · 0.85
pointer_encoded_lengthFunction · 0.85
encode_string_as_pointerFunction · 0.85

Tested by

no test coverage detected