1 #include "config.h"
2 #include <razorback/debug.h>
3 #include <razorback/hash.h>
4 #include <razorback/list.h>
5 #include <razorback/uuids.h>
6 #include <razorback/block_id.h>
7 #include <razorback/block.h>
8 #include <razorback/event.h>
9 #include <razorback/judgment.h>
10 #include <razorback/string_list.h>
11
12 #include <razorback/json_buffer.h>
13 #ifdef _MSC_VER
14 #include <WinSock2.h>
15 #include <Ws2tcpip.h>
16 #include "bobins.h"
17 #include <stdio.h>
18 #define NUM_FMT "%I64u"
19 #else //_MSC_VER
20 #include <arpa/inet.h>
21 #include <sys/socket.h>
22 #define NUM_FMT "%ju"
23 #endif //_MSC_VER
24
25
26 #include <openssl/sha.h>
27 #include <openssl/hmac.h>
28 #include <openssl/evp.h>
29 #include <openssl/bio.h>
30 #include <openssl/buffer.h>
31
32 #include <string.h>
33
JsonBuffer_Put_uint8_t(json_object * parent,const char * name,uint8_t p_iValue)34 SO_PUBLIC bool JsonBuffer_Put_uint8_t (json_object * parent,
35 const char *name, uint8_t p_iValue)
36 {
37 json_object *new;
38 ASSERT( parent != NULL);
39 ASSERT(name != NULL);
40 if (parent == NULL)
41 return false;
42 if (name == NULL)
43 return false;
44
45 if ((new = json_object_new_int(p_iValue)) == NULL)
46 return false;
47
48 json_object_object_add(parent, name, new);
49 return true;
50 }
JsonBuffer_Put_uint16_t(json_object * parent,const char * name,uint16_t p_iValue)51 SO_PUBLIC bool JsonBuffer_Put_uint16_t (json_object * parent, const char * name, uint16_t p_iValue)
52 {
53 json_object *new;
54 ASSERT( parent != NULL);
55 ASSERT(name != NULL);
56 if (parent == NULL)
57 return false;
58 if (name == NULL)
59 return false;
60
61 if ((new = json_object_new_int(p_iValue)) == NULL)
62 return false;
63
64 json_object_object_add(parent, name, new);
65
66 return true;
67 }
68
JsonBuffer_Put_uint32_t(json_object * parent,const char * name,uint32_t p_iValue)69 SO_PUBLIC bool JsonBuffer_Put_uint32_t (json_object * parent, const char * name, uint32_t p_iValue)
70 {
71 json_object *new;
72 char *str;
73 ASSERT( parent != NULL);
74 ASSERT(name != NULL);
75 if (parent == NULL)
76 return false;
77 if (name == NULL)
78 return false;
79 // Larger numbers dont fit in the API so send them as strings
80 if (asprintf(&str, "%u", p_iValue) == -1)
81 return false;
82
83 if ((new = json_object_new_string(str)) == NULL)
84 return false;
85
86 json_object_object_add(parent, name, new);
87 free(str);
88 return true;
89 }
90
JsonBuffer_Put_uint64_t(json_object * parent,const char * name,uint64_t p_iValue)91 SO_PUBLIC bool JsonBuffer_Put_uint64_t (json_object * parent, const char * name, uint64_t p_iValue)
92 {
93 json_object *new;
94 char *str;
95 ASSERT( parent != NULL);
96 ASSERT(name != NULL);
97 if (parent == NULL)
98 return false;
99 if (name == NULL)
100 return false;
101
102 // Larger numbers dont fit in the API so send them as strings
103 if (asprintf(&str, NUM_FMT, p_iValue) == -1)
104 return false;
105
106 if ((new = json_object_new_string(str)) == NULL)
107 return false;
108
109 json_object_object_add(parent, name, new);
110 free(str);
111 return true;
112 }
113
JsonBuffer_Put_ByteArray(json_object * parent,const char * name,uint32_t p_iSize,const uint8_t * p_pByteArray)114 SO_PUBLIC bool JsonBuffer_Put_ByteArray (json_object * parent, const char *name,
115 uint32_t p_iSize,
116 const uint8_t * p_pByteArray)
117 {
118 BIO *bmem, *b64;
119 BUF_MEM *bptr;
120 char *buff;
121 ASSERT( parent != NULL);
122 ASSERT(name != NULL);
123 if (parent == NULL)
124 return false;
125 if (name == NULL)
126 return false;
127
128 b64 = BIO_new(BIO_f_base64());
129 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
130
131 bmem = BIO_new(BIO_s_mem());
132 BIO_push(b64, bmem);
133 BIO_write(b64, p_pByteArray, p_iSize);
134 //BIO_flush(b64);
135 BIO_get_mem_ptr(b64, &bptr);
136
137 buff = (char *)malloc(bptr->length+1);
138 memcpy(buff, bptr->data, bptr->length);
139 buff[bptr->length] = '\0';
140
141 BIO_free_all(b64);
142
143 if (!JsonBuffer_Put_String(parent, name, buff))
144 return false;
145 free(buff);
146 return true;
147 }
148
JsonBuffer_Put_String(json_object * parent,const char * name,const char * p_sString)149 SO_PUBLIC bool JsonBuffer_Put_String (json_object * parent, const char * name,
150 const char * p_sString)
151 {
152 json_object *new;
153 ASSERT( parent != NULL);
154 ASSERT(name != NULL);
155 if (parent == NULL)
156 return false;
157 if (name == NULL)
158 return false;
159
160 if ((new = json_object_new_string((const char *)p_sString)) == NULL)
161 return false;
162
163 json_object_object_add(parent, name, new);
164
165 return true;
166 }
167
JsonBuffer_Get_uint8_t(json_object * parent,const char * name,uint8_t * p_pValue)168 SO_PUBLIC bool JsonBuffer_Get_uint8_t (json_object * parent, const char * name, uint8_t * p_pValue)
169 {
170 int tmp;
171 json_object *object;
172 ASSERT( parent != NULL);
173 ASSERT(name != NULL);
174 if (parent == NULL)
175 return false;
176 if (name == NULL)
177 return false;
178 if ((object = json_object_object_get(parent, name)) == NULL)
179 return false;
180 if (json_object_get_type(object) != json_type_int)
181 return false;
182
183 tmp = json_object_get_int(object);
184 *p_pValue = (uint8_t) tmp;
185 return true;
186 }
187
JsonBuffer_Get_uint16_t(json_object * parent,const char * name,uint16_t * p_pValue)188 SO_PUBLIC bool JsonBuffer_Get_uint16_t (json_object * parent, const char * name,
189 uint16_t * p_pValue)
190 {
191 int tmp;
192 json_object *object;
193 ASSERT( parent != NULL);
194 ASSERT(name != NULL);
195 if (parent == NULL)
196 return false;
197 if (name == NULL)
198 return false;
199 if ((object = json_object_object_get(parent, name)) == NULL)
200 return false;
201 if (json_object_get_type(object) != json_type_int)
202 return false;
203
204 tmp = json_object_get_int(object);
205 *p_pValue = (uint16_t) tmp;
206
207 return true;
208 }
209
JsonBuffer_Get_uint32_t(json_object * parent,const char * name,uint32_t * p_pValue)210 SO_PUBLIC bool JsonBuffer_Get_uint32_t (json_object * parent, const char *name,
211 uint32_t * p_pValue)
212 {
213 const char *tmp;
214 uint32_t val;
215 json_object *object;
216 ASSERT( parent != NULL);
217 ASSERT(name != NULL);
218 if (parent == NULL)
219 return false;
220 if (name == NULL)
221 return false;
222 if ((object = json_object_object_get(parent, name)) == NULL)
223 return false;
224 if (json_object_get_type(object) != json_type_string)
225 return false;
226 tmp = json_object_get_string(object);
227 if (sscanf(tmp, "%u", &val) != 1)
228 return false;
229 *p_pValue = val;
230 return true;
231 }
232
JsonBuffer_Get_uint64_t(json_object * parent,const char * name,uint64_t * p_pValue)233 SO_PUBLIC bool JsonBuffer_Get_uint64_t (json_object * parent, const char *name,
234 uint64_t * p_pValue)
235 {
236 const char *tmp;
237 uint64_t val;
238 json_object *object;
239 ASSERT( parent != NULL);
240 ASSERT(name != NULL);
241 if (parent == NULL)
242 return false;
243 if (name == NULL)
244 return false;
245 if ((object = json_object_object_get(parent, name)) == NULL)
246 return false;
247 if (json_object_get_type(object) != json_type_string)
248 return false;
249 tmp = json_object_get_string(object);
250 if (sscanf(tmp, NUM_FMT, &val) != 1)
251 return false;
252 *p_pValue = val;
253
254 return true;
255 }
256
JsonBuffer_Get_String(json_object * parent,const char * name)257 SO_PUBLIC char * JsonBuffer_Get_String (json_object * parent, const char * name)
258 {
259 const char *tmp;
260 char * ret;
261 json_object *object;
262 ASSERT( parent != NULL);
263 ASSERT(name != NULL);
264 if (parent == NULL)
265 return NULL;
266 if (name == NULL)
267 return NULL;
268 if ((object = json_object_object_get(parent, name)) == NULL)
269 return false;
270 if (json_object_get_type(object) != json_type_string)
271 return false;
272 tmp = json_object_get_string(object);
273 if (asprintf(&ret, "%s", tmp) == -1)
274 return NULL;
275 return ret;
276 }
277
JsonBuffer_Get_ByteArray(json_object * parent,const char * name,uint32_t * p_iSize,uint8_t ** p_pByteArray)278 SO_PUBLIC bool JsonBuffer_Get_ByteArray (json_object * parent, const char * name,
279 uint32_t *p_iSize,
280 uint8_t **p_pByteArray)
281 {
282 json_object *object;
283 char *input;
284 uint8_t *output;
285 BIO *bmem, *b64;
286 size_t length;
287 ASSERT( parent != NULL);
288 ASSERT(name != NULL);
289 if (parent == NULL)
290 return false;
291 if (name == NULL)
292 return false;
293 if ((object = json_object_object_get(parent, name)) == NULL)
294 return false;
295 if (json_object_get_type(object) != json_type_string)
296 return false;
297 input = (char *)json_object_get_string(object);
298 length = strlen(input);
299 if ((output = calloc(length, sizeof(char))) == NULL)
300 return false;
301
302 b64 = BIO_new (BIO_f_base64());
303 BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
304 bmem = BIO_new_mem_buf (input, strlen(input));
305 bmem = BIO_push (b64, bmem);
306 *p_iSize = BIO_read (bmem, output, length);
307 BIO_free_all (bmem);
308 *p_pByteArray = output;
309 return true;
310 }
311
JsonBuffer_Get_UUID(json_object * parent,const char * name,uuid_t p_uuid)312 SO_PUBLIC bool JsonBuffer_Get_UUID (json_object * parent, const char *name, uuid_t p_uuid)
313 {
314 json_object *object;
315 char *tmp;
316 ASSERT( parent != NULL);
317 ASSERT(name != NULL);
318 if (parent == NULL)
319 return false;
320 if (name == NULL)
321 return false;
322 // get the container
323 if ((object = json_object_object_get(parent, name)) == NULL)
324 return false;
325 if (json_object_get_type(object) != json_type_object)
326 return false;
327 if ((tmp =(char *) JsonBuffer_Get_String(object, "id")) == NULL)
328 return false;
329
330 uuid_parse(tmp, p_uuid);
331 free(tmp);
332 return true;
333 }
334
JsonBuffer_Put_UUID(json_object * parent,const char * name,uuid_t p_uuid)335 SO_PUBLIC bool JsonBuffer_Put_UUID (json_object * parent, const char * name, uuid_t p_uuid)
336 {
337 char uuid[UUID_STRING_LENGTH];
338 json_object *new;
339 ASSERT( parent != NULL);
340 ASSERT(name != NULL);
341 if (parent == NULL)
342 return false;
343 if (name == NULL)
344 return false;
345 if ((new = json_object_new_object()) == NULL)
346 return false;
347
348 json_object_object_add(parent, name, new);
349 uuid_unparse(p_uuid, uuid);
350 parent = new;
351 if (!JsonBuffer_Put_String(new, "id", uuid))
352 return false;
353 // TODO: Add other UUID attributes
354
355 return true;
356 }
357
358 static bool
JsonBuffer_Get_NTLVItem(struct List * list,json_object * parent)359 JsonBuffer_Get_NTLVItem (struct List *list, json_object *parent )
360 {
361 char *str = NULL;
362 uint8_t *byteData = NULL;
363 uint32_t size = 0;
364 uuid_t name, type, uuid;
365 uint16_t port =0;
366 uint8_t proto =0;
367
368 if (!JsonBuffer_Get_UUID(parent, "Name", name))
369 return false;
370
371 if (!JsonBuffer_Get_UUID(parent, "Type", type))
372 return false;
373
374 if (json_object_object_get(parent, "String_Value") != NULL)
375 str = JsonBuffer_Get_String(parent, "String_Value");
376
377 if (json_object_object_get(parent, "Bin_Value") != NULL)
378 JsonBuffer_Get_ByteArray(parent, "Bin_Value", &size, &byteData);
379
380 if (( str == NULL) && (byteData == NULL))
381 return false;
382
383 UUID_Get_UUID(NTLV_TYPE_STRING, UUID_TYPE_NTLV_TYPE, uuid);
384 if (uuid_compare(type, uuid) == 0)
385 NTLVList_Add(list, name, type, strlen(str)+1, (uint8_t*)str);
386
387 UUID_Get_UUID(NTLV_TYPE_PORT, UUID_TYPE_NTLV_TYPE, uuid);
388 if (uuid_compare(type, uuid) == 0)
389 {
390 sscanf(str, "%hu", &port);
391 NTLVList_Add(list, name, type, 2, (uint8_t*)&port);
392 }
393 UUID_Get_UUID(NTLV_TYPE_IPPROTO, UUID_TYPE_NTLV_TYPE, uuid);
394 if (uuid_compare(type, uuid) == 0)
395 {
396 sscanf(str, "%hhu", &proto);
397 NTLVList_Add(list, name, type, 1, (uint8_t*)&proto);
398 }
399
400 UUID_Get_UUID(NTLV_TYPE_IPv4_ADDR, UUID_TYPE_NTLV_TYPE, uuid);
401 if (uuid_compare(type, uuid) == 0)
402 {
403 if ((byteData = calloc(4, sizeof(char))) == NULL)
404 return false;
405 inet_pton(AF_INET, str, byteData);
406 NTLVList_Add(list, name, type, 4, byteData);
407
408 }
409
410 UUID_Get_UUID(NTLV_TYPE_IPv6_ADDR, UUID_TYPE_NTLV_TYPE, uuid);
411 if (uuid_compare(type, uuid) == 0)
412 {
413 if ((str = calloc(16, sizeof(char))) == NULL)
414 return false;
415 inet_pton(AF_INET6, str, byteData);
416 NTLVList_Add(list, name, type, 16, byteData);
417
418 }
419
420
421 if (str != NULL)
422 free(str);
423 if (byteData != NULL)
424 free(byteData);
425
426 return true;
427 }
428
429 static int
JsonBuffer_Put_NTLVItem(struct NTLVItem * p_pItem,json_object * parent)430 JsonBuffer_Put_NTLVItem (struct NTLVItem *p_pItem, json_object *parent )
431 {
432 json_object *object;
433 char *str = NULL;
434 bool doFree = false;
435 uuid_t uuid;
436 uint16_t port =0;
437 uint8_t proto =0;
438
439 if ((object = json_object_new_object()) == NULL)
440 return false;
441
442 json_object_array_add(parent, object);
443
444 if (!JsonBuffer_Put_UUID(object, "Name", p_pItem->uuidName))
445 return LIST_EACH_ERROR;
446
447 if (!JsonBuffer_Put_UUID(object, "Type", p_pItem->uuidType))
448 return LIST_EACH_ERROR;
449
450 UUID_Get_UUID(NTLV_TYPE_STRING, UUID_TYPE_NTLV_TYPE, uuid);
451 if (uuid_compare(uuid, p_pItem->uuidType) == 0)
452 str = (char *)p_pItem->pData;
453
454 UUID_Get_UUID(NTLV_TYPE_PORT, UUID_TYPE_NTLV_TYPE, uuid);
455 if (uuid_compare(uuid, p_pItem->uuidType) == 0)
456 {
457 memcpy(&port, p_pItem->pData, 2);
458
459 if (asprintf(&str, "%hu", port) == -1)
460 return LIST_EACH_ERROR;
461
462 doFree = true;
463 }
464 UUID_Get_UUID(NTLV_TYPE_IPPROTO, UUID_TYPE_NTLV_TYPE, uuid);
465 if (uuid_compare(uuid, p_pItem->uuidType) == 0)
466 {
467 memcpy(&proto, p_pItem->pData, 1);
468
469 if (asprintf(&str, "%hhu", port) == -1)
470 return LIST_EACH_ERROR;
471
472 doFree = true;
473 }
474 UUID_Get_UUID(NTLV_TYPE_IPv4_ADDR, UUID_TYPE_NTLV_TYPE, uuid);
475 if (uuid_compare(uuid, p_pItem->uuidType) == 0)
476 {
477 if ((str = calloc(1, INET_ADDRSTRLEN)) == NULL)
478 return LIST_EACH_ERROR;
479 doFree=true;
480 inet_ntop(AF_INET, p_pItem->pData, str, INET_ADDRSTRLEN);
481 }
482
483 UUID_Get_UUID(NTLV_TYPE_IPv6_ADDR, UUID_TYPE_NTLV_TYPE, uuid);
484 if (uuid_compare(uuid, p_pItem->uuidType) == 0)
485 {
486 if ((str = calloc(1, INET_ADDRSTRLEN)) == NULL)
487 return LIST_EACH_ERROR;
488 doFree=true;
489 inet_ntop(AF_INET6, p_pItem->pData, str, INET6_ADDRSTRLEN);
490 }
491
492
493 if (str == NULL)
494 {
495 if (!JsonBuffer_Put_ByteArray(object, "Bin_Value", p_pItem->iLength, p_pItem->pData))
496 return LIST_EACH_ERROR;
497 }
498 else
499 if (!JsonBuffer_Put_String(object, "String_Value", str))
500 return LIST_EACH_ERROR;
501
502 if (doFree)
503 free(str);
504
505 return LIST_EACH_OK;
506 }
507
JsonBuffer_Put_NTLVList(json_object * parent,const char * name,struct List * p_pList)508 SO_PUBLIC bool JsonBuffer_Put_NTLVList (json_object * parent, const char * name,
509 struct List *p_pList)
510 {
511 json_object *object;
512 ASSERT( parent != NULL);
513 ASSERT(name != NULL);
514 if (parent == NULL)
515 return false;
516 if (name == NULL)
517 return false;
518 if ((object = json_object_new_array()) == NULL)
519 return false;
520
521 json_object_object_add(parent, name, object);
522
523 if (!List_ForEach(p_pList, (int (*)(void*,void*))JsonBuffer_Put_NTLVItem, object))
524 return false;
525
526 return true;
527 }
528
JsonBuffer_Get_NTLVList(json_object * parent,const char * name,struct List ** p_pList)529 SO_PUBLIC bool JsonBuffer_Get_NTLVList (json_object * parent, const char * name,
530 struct List **p_pList)
531 {
532 struct List *list;
533 json_object *object;
534 int i;
535
536 ASSERT( parent != NULL);
537 ASSERT(name != NULL);
538 if (parent == NULL)
539 return false;
540 if (name == NULL)
541 return false;
542 if ((object = json_object_object_get(parent, name)) == NULL)
543 return false;
544 if (json_object_get_type(object) != json_type_array)
545 return false;
546 parent = object;
547 if ((list = NTLVList_Create()) == NULL)
548 return false;
549
550 for (i = 0; i < json_object_array_length(parent); i++)
551 {
552 if (((object = json_object_array_get_idx(parent, i)) == NULL) ||
553 (json_object_get_type(object) != json_type_object) )
554 {
555 List_Destroy(list);
556 return false;
557 }
558 if (!JsonBuffer_Get_NTLVItem(list, object))
559 {
560 List_Destroy(list);
561 return false;
562 }
563 }
564 *p_pList = list;
565 return true;
566 }
567
JsonBuffer_Put_Hash(json_object * parent,const char * name,const struct Hash * p_pHash)568 SO_PUBLIC bool JsonBuffer_Put_Hash (json_object * parent, const char * name,
569 const struct Hash *p_pHash)
570 {
571 json_object *object;
572 const char *str = NULL;
573 ASSERT( parent != NULL);
574 ASSERT(name != NULL);
575 if (parent == NULL)
576 return false;
577 if (name == NULL)
578 return false;
579 if ((object = json_object_new_object()) == NULL)
580 return false;
581 json_object_object_add(parent, name, object);
582 parent = object;
583 switch (p_pHash->iType)
584 {
585 case HASH_TYPE_MD5:
586 str= "MD5";
587 break;
588 case HASH_TYPE_SHA1:
589 str= "SHA1";
590 break;
591 case HASH_TYPE_SHA224:
592 str= "SHA224";
593 break;
594 case HASH_TYPE_SHA256:
595 str= "SHA256";
596 break;
597 case HASH_TYPE_SHA512:
598 str= "SHA512";
599 break;
600 default:
601 return false;
602 }
603 if (!JsonBuffer_Put_String(parent, "Type", str))
604 return false;
605 if ((str = Hash_ToText(p_pHash)) == NULL)
606 return false;
607 if (!JsonBuffer_Put_String(parent, "Value", str))
608 return false;
609 free((void *)str);
610 return true;
611 }
612
JsonBuffer_Get_Hash(json_object * parent,const char * name,struct Hash ** p_pHash)613 SO_PUBLIC bool JsonBuffer_Get_Hash (json_object * parent, const char *name, struct Hash **p_pHash)
614 {
615 struct Hash *hash;
616 json_object *object, *object2;
617 const char *type, *data, *pos;
618 size_t i;
619 #ifdef _MSC_VER
620 char tmp[3] = { '\0', '\0','\0' };
621 unsigned long b;
622 #endif
623 ASSERT( parent != NULL);
624 ASSERT(name != NULL);
625
626 if (parent == NULL)
627 return false;
628 if (name == NULL)
629 return false;
630 // Get the container
631 if ((object = json_object_object_get(parent, name)) == NULL)
632 return false;
633 if (json_object_get_type(object) != json_type_object)
634 return false;
635 parent = object;
636
637 if ((object = json_object_object_get(parent, "Type")) == NULL)
638 return false;
639 if (json_object_get_type(object) != json_type_string)
640 return false;
641 type = json_object_get_string(object);
642 if ((object2 = json_object_object_get(parent, "Value")) == NULL)
643 return false;
644 if (json_object_get_type(object2) != json_type_string)
645 return false;
646 data = json_object_get_string(object2);
647
648
649 if ((hash = calloc(1, sizeof (struct Hash))) == NULL)
650 return false;
651
652 if (strcmp(type, "MD5") == 0)
653 hash->iType = HASH_TYPE_MD5;
654 else if (strcmp(type, "SHA1") == 0)
655 hash->iType = HASH_TYPE_SHA1;
656 else if (strcmp(type, "SHA224") == 0)
657 hash->iType = HASH_TYPE_SHA224;
658 else if (strcmp(type, "SHA256") == 0)
659 hash->iType = HASH_TYPE_SHA256;
660 else if (strcmp(type, "SHA512") == 0)
661 hash->iType = HASH_TYPE_SHA512;
662
663 hash->iSize = strlen(data)/2;
664 if ((hash->pData = calloc(hash->iSize, sizeof(uint8_t))) == NULL)
665 {
666 Hash_Destroy(hash);
667 return false;
668 }
669 pos = data;
670 for(i = 0; i < hash->iSize; i++) {
671 #ifdef _MSC_VER
672 tmp[0] = *pos;
673 tmp[1] = *(pos+1);
674 b = strtoul(tmp,NULL, 16);
675 hash->pData[i] = (uint8_t) b;
676 #else
677 sscanf(pos, "%2hhx", &hash->pData[i]);
678 #endif
679 pos += 2;
680 }
681 hash->iFlags = HASH_FLAG_FINAL;
682 *p_pHash = hash;
683 return true;
684 }
685
JsonBuffer_Put_BlockId(json_object * parent,const char * name,struct BlockId * p_pId)686 SO_PUBLIC bool JsonBuffer_Put_BlockId (json_object * parent, const char *name,
687 struct BlockId *p_pId)
688 {
689 json_object *object;
690 ASSERT( parent != NULL);
691 ASSERT(name != NULL);
692 if (parent == NULL)
693 return false;
694 if (name == NULL)
695 return false;
696 if ((object = json_object_new_object()) == NULL)
697 return false;
698 json_object_object_add(parent, name, object);
699 parent = object;
700 if (!JsonBuffer_Put_Hash(parent, "Hash", p_pId->pHash))
701 return false;
702 if (!JsonBuffer_Put_uint64_t(parent, "Size", p_pId->iLength))
703 return false;
704 if (!JsonBuffer_Put_UUID(parent, "Data_Type", p_pId->uuidDataType))
705 return false;
706 return true;
707 }
708
709
JsonBuffer_Get_BlockId(json_object * parent,const char * name,struct BlockId ** p_pId)710 SO_PUBLIC bool JsonBuffer_Get_BlockId (json_object * parent, const char * name,
711 struct BlockId **p_pId)
712 {
713 struct BlockId *id;
714 json_object *object;
715 ASSERT( parent != NULL);
716 ASSERT(name != NULL);
717 if (parent == NULL)
718 return false;
719 if (name == NULL)
720 return false;
721 // Get the container
722 if ((object = json_object_object_get(parent, name)) == NULL)
723 return false;
724 if (json_object_get_type(object) != json_type_object)
725 return false;
726 parent = object;
727 if ((id = calloc(1, sizeof(struct BlockId))) == NULL)
728 return false;
729
730 if (!JsonBuffer_Get_Hash(parent, "Hash", &id->pHash))
731 {
732 BlockId_Destroy(id);
733 return false;
734 }
735 if (!JsonBuffer_Get_uint64_t(parent, "Size", &id->iLength))
736 {
737 BlockId_Destroy(id);
738 return false;
739 }
740 if (!JsonBuffer_Get_UUID(parent, "Data_Type", id->uuidDataType))
741 {
742 BlockId_Destroy(id);
743 return false;
744 }
745 *p_pId = id;
746 return true;
747 }
748
JsonBuffer_Put_Block(json_object * parent,const char * name,struct Block * p_pBlock)749 SO_PUBLIC bool JsonBuffer_Put_Block (json_object * parent, const char * name,
750 struct Block *p_pBlock)
751 {
752 json_object *object;
753 ASSERT( parent != NULL);
754 ASSERT(name != NULL);
755 if (parent == NULL)
756 return false;
757 if (name == NULL)
758 return false;
759 if ((object = json_object_new_object()) == NULL)
760 return false;
761 json_object_object_add(parent, name, object);
762 parent = object;
763
764 if (!JsonBuffer_Put_BlockId(parent, "Id", p_pBlock->pId))
765 return false;
766
767 if (p_pBlock->pParentId != NULL)
768 if (!JsonBuffer_Put_BlockId(parent, "Parent_Id", p_pBlock->pParentId))
769 return false;
770
771 if (p_pBlock->pParentBlock != NULL)
772 if (!JsonBuffer_Put_Block(parent, "Parent", p_pBlock->pParentBlock))
773 return false;
774
775 if (p_pBlock->pMetaDataList != NULL)
776 if (!JsonBuffer_Put_NTLVList(parent, "Metadata", p_pBlock->pMetaDataList))
777 return false;
778
779
780 return true;
781 }
782
JsonBuffer_Get_Block(json_object * parent,const char * name,struct Block ** p_pBlock)783 SO_PUBLIC bool JsonBuffer_Get_Block (json_object * parent, const char * name,
784 struct Block **p_pBlock)
785 {
786 struct Block *block;
787 json_object *object;
788 ASSERT( parent != NULL);
789 ASSERT(name != NULL);
790 if (parent == NULL)
791 return false;
792 if (name == NULL)
793 return false;
794 // Get the container
795 if ((object = json_object_object_get(parent, name)) == NULL)
796 return false;
797 if (json_object_get_type(object) != json_type_object)
798 return false;
799 parent = object;
800 if ((block = calloc(1, sizeof(struct Block))) == NULL)
801 return false;
802
803 if (!JsonBuffer_Get_BlockId(parent, "Id", &block->pId))
804 {
805 Block_Destroy(block);
806 return false;
807 }
808
809 if (json_object_object_get(parent, "Parent_Id") != NULL)
810 {
811 if (!JsonBuffer_Get_BlockId(parent, "Parent_Id", &block->pParentId))
812 {
813 Block_Destroy(block);
814 return false;
815 }
816 }
817 if (json_object_object_get(parent, "Parent") != NULL)
818 {
819 if (!JsonBuffer_Get_Block(parent, "Parent", &block->pParentBlock))
820 {
821 Block_Destroy(block);
822 return false;
823 }
824 }
825 if (json_object_object_get(parent, "Metadata") != NULL)
826 {
827 if (!JsonBuffer_Get_NTLVList(parent, "Metadata", &block->pMetaDataList))
828 {
829 Block_Destroy(block);
830 return false;
831 }
832 }
833
834 *p_pBlock = block;
835 return true;
836 }
837
JsonBuffer_Put_Event(json_object * parent,const char * name,struct Event * p_pEvent)838 SO_PUBLIC bool JsonBuffer_Put_Event (json_object * parent, const char * name,
839 struct Event *p_pEvent)
840 {
841 json_object *object;
842 ASSERT( parent != NULL);
843 ASSERT(name != NULL);
844 if (parent == NULL)
845 return false;
846 if (name == NULL)
847 return false;
848 if ((object = json_object_new_object()) == NULL)
849 return false;
850 json_object_object_add(parent, name, object);
851 parent = object;
852 if (!JsonBuffer_Put_EventId(parent, "Id", p_pEvent->pId))
853 return false;
854
855 if (p_pEvent->pParentId != NULL)
856 if (!JsonBuffer_Put_EventId(parent, "Parent_Id", p_pEvent->pParentId))
857 return false;
858
859 if (p_pEvent->pParent != NULL)
860 if (!JsonBuffer_Put_Event(parent, "Parent", p_pEvent->pParent))
861 return false;
862
863 if (p_pEvent->pMetaDataList != NULL)
864 if (!JsonBuffer_Put_NTLVList(parent, "Metadata", p_pEvent->pMetaDataList))
865 return false;
866
867 if (p_pEvent->pBlock != NULL)
868 if (!JsonBuffer_Put_Block(parent, "Block", p_pEvent->pBlock))
869 return false;
870 return true;
871 }
872
JsonBuffer_Get_Event(json_object * parent,const char * name,struct Event ** p_pEvent)873 SO_PUBLIC bool JsonBuffer_Get_Event (json_object * parent, const char * name,
874 struct Event **p_pEvent)
875 {
876 struct Event *event;
877 json_object *object;
878 ASSERT( parent != NULL);
879 ASSERT(name != NULL);
880 if (parent == NULL)
881 return false;
882 if (name == NULL)
883 return false;
884 // Get the container
885 if ((object = json_object_object_get(parent, name)) == NULL)
886 return false;
887 if (json_object_get_type(object) != json_type_object)
888 return false;
889 parent = object;
890 if ((event = calloc(1, sizeof(struct Event))) == NULL)
891 return false;
892 if (!JsonBuffer_Get_EventId(parent, "Id", &event->pId))
893 return false;
894
895 if (json_object_object_get(parent, "Parent_Id") != NULL)
896 {
897 if (!JsonBuffer_Get_EventId(parent, "Parent_Id", &event->pParentId))
898 {
899 Event_Destroy(event);
900 return false;
901 }
902 }
903
904 if (json_object_object_get(parent, "Parent") != NULL)
905 {
906 if (!JsonBuffer_Get_Event(parent, "Parent", &event->pParent))
907 {
908 Event_Destroy(event);
909 return false;
910 }
911 }
912 if (json_object_object_get(parent, "Metadata") != NULL)
913 {
914 if (!JsonBuffer_Get_NTLVList(parent, "Metadata", &event->pMetaDataList))
915 {
916 Event_Destroy(event);
917 return false;
918 }
919 }
920 else
921 {
922 if ((event->pMetaDataList = NTLVList_Create()) == NULL)
923 {
924 Event_Destroy(event);
925 return false;
926 }
927
928 }
929 if (json_object_object_get(parent, "Block") != NULL)
930 {
931 if (!JsonBuffer_Get_Block(parent, "Block", &event->pBlock))
932 {
933 Event_Destroy(event);
934 return false;
935 }
936 }
937
938 *p_pEvent = event;
939 return true;
940 }
941
942
JsonBuffer_Put_EventId(json_object * parent,const char * name,struct EventId * p_pEventId)943 SO_PUBLIC bool JsonBuffer_Put_EventId (json_object * parent, const char * name,
944 struct EventId *p_pEventId)
945 {
946 json_object *object;
947 ASSERT( parent != NULL);
948 ASSERT(name != NULL);
949 if (parent == NULL)
950 return false;
951 if (name == NULL)
952 return false;
953 if ((object = json_object_new_object()) == NULL)
954 return false;
955 json_object_object_add(parent, name, object);
956 parent = object;
957 // XXX: Refactor this!!
958 if ((object = json_object_new_object()) == NULL)
959 return false;
960 json_object_object_add(parent, "Nugget", object);
961 if (!JsonBuffer_Put_UUID(object, "Id", p_pEventId->uuidNuggetId))
962 return false;
963 // XXX: End
964
965 if (!JsonBuffer_Put_uint64_t(parent, "Seconds", p_pEventId->iSeconds))
966 return false;
967
968 if (!JsonBuffer_Put_uint64_t(parent, "Nano_Seconds", p_pEventId->iNanoSecs))
969 return false;
970
971 return true;
972 }
973
JsonBuffer_Get_EventId(json_object * parent,const char * name,struct EventId ** p_pEventId)974 SO_PUBLIC bool JsonBuffer_Get_EventId (json_object * parent, const char * name,
975 struct EventId **p_pEventId)
976 {
977 struct EventId *eventId;
978 json_object *object;
979 ASSERT( parent != NULL);
980 ASSERT(name != NULL);
981 if (parent == NULL)
982 return false;
983 if (name == NULL)
984 return false;
985
986 // Get the container
987 if ((object = json_object_object_get(parent, name)) == NULL)
988 return false;
989 if (json_object_get_type(object) != json_type_object)
990 return false;
991 parent = object;
992 // XXX: Refactor this!!
993 if ((object = json_object_object_get(parent, "Nugget")) == NULL)
994 return false;
995 if (json_object_get_type(object) != json_type_object)
996 return false;
997 // XXX: End
998
999 if ((eventId = calloc(1, sizeof(struct EventId))) == NULL)
1000 return false;
1001
1002 // XXX: Refactor this!!
1003 if (!JsonBuffer_Get_UUID(object, "Id", eventId->uuidNuggetId))
1004 {
1005 EventId_Destroy(eventId);
1006 return false;
1007 }
1008 // XXX: End
1009
1010 if (!JsonBuffer_Get_uint64_t(parent, "Seconds", &eventId->iSeconds))
1011 {
1012 EventId_Destroy(eventId);
1013 return false;
1014 }
1015
1016 if (!JsonBuffer_Get_uint64_t(parent, "Nano_Seconds", &eventId->iNanoSecs))
1017 {
1018 EventId_Destroy(eventId);
1019 return false;
1020 }
1021 *p_pEventId = eventId;
1022 return true;
1023 }
1024
JsonBuffer_Put_Judgment(json_object * parent,const char * name,struct Judgment * p_pJudgment)1025 SO_PUBLIC bool JsonBuffer_Put_Judgment (json_object * parent, const char * name,
1026 struct Judgment *p_pJudgment)
1027 {
1028 json_object *object;
1029 ASSERT( parent != NULL);
1030 ASSERT(name != NULL);
1031 if (parent == NULL)
1032 return false;
1033 if (name == NULL)
1034 return false;
1035 if ((object = json_object_new_object()) == NULL)
1036 return false;
1037 json_object_object_add(parent, name, object);
1038 parent = object;
1039 if (!JsonBuffer_Put_UUID(parent, "Nugget_ID", p_pJudgment->uuidNuggetId))
1040 return false;
1041
1042 if (!JsonBuffer_Put_uint64_t(parent, "Seconds", p_pJudgment->iSeconds))
1043 return false;
1044
1045 if (!JsonBuffer_Put_uint64_t(parent, "Nano_Seconds", p_pJudgment->iNanoSecs))
1046 return false;
1047
1048 if (!JsonBuffer_Put_EventId(parent, "Event_ID", p_pJudgment->pEventId))
1049 return false;
1050
1051 if (!JsonBuffer_Put_BlockId(parent, "Block_ID", p_pJudgment->pBlockId))
1052 return false;
1053
1054 if (!JsonBuffer_Put_uint8_t(parent, "Priority", p_pJudgment->iPriority))
1055 return false;
1056
1057 if (!JsonBuffer_Put_NTLVList(parent, "Metadata", p_pJudgment->pMetaDataList))
1058 return false;
1059
1060 if (!JsonBuffer_Put_uint32_t(parent, "GID", p_pJudgment->iGID))
1061 return false;
1062
1063 if (!JsonBuffer_Put_uint32_t(parent, "SID", p_pJudgment->iSID))
1064 return false;
1065
1066 if (!JsonBuffer_Put_uint32_t(parent, "Set_SF_Flags", p_pJudgment->Set_SfFlags))
1067 return false;
1068
1069 if (!JsonBuffer_Put_uint32_t(parent, "Set_Ent_Flags", p_pJudgment->Set_EntFlags))
1070 return false;
1071
1072 if (!JsonBuffer_Put_uint32_t(parent, "Unset_SF_Flags", p_pJudgment->Unset_SfFlags))
1073 return false;
1074
1075 if (!JsonBuffer_Put_uint32_t(parent, "Unset_Ent_Flags", p_pJudgment->Unset_EntFlags))
1076 return false;
1077
1078 if (p_pJudgment->sMessage != NULL)
1079 if (!JsonBuffer_Put_String(parent, "Message", (char *)p_pJudgment->sMessage))
1080 return false;
1081
1082 return true;
1083 }
1084
JsonBuffer_Get_Judgment(json_object * parent,const char * name,struct Judgment ** p_pJudgment)1085 SO_PUBLIC bool JsonBuffer_Get_Judgment (json_object * parent, const char * name,
1086 struct Judgment **p_pJudgment)
1087 {
1088 struct Judgment *judgment;
1089 json_object *object;
1090 ASSERT( parent != NULL);
1091 ASSERT(name != NULL);
1092 if (parent == NULL)
1093 return false;
1094 if (name == NULL)
1095 return false;
1096 // Get the container
1097 if ((object = json_object_object_get(parent, name)) == NULL)
1098 return false;
1099 if (json_object_get_type(object) != json_type_object)
1100 return false;
1101 parent = object;
1102 if ((judgment = calloc(1, sizeof(struct Judgment))) == NULL)
1103 return false;
1104
1105 if (!JsonBuffer_Get_UUID(parent, "Nugget_ID", judgment->uuidNuggetId))
1106 {
1107 Judgment_Destroy(judgment);
1108 return false;
1109 }
1110
1111 if (!JsonBuffer_Get_uint64_t(parent, "Seconds", &judgment->iSeconds))
1112 {
1113 Judgment_Destroy(judgment);
1114 return false;
1115 }
1116
1117 if (!JsonBuffer_Get_uint64_t(parent, "Nano_Seconds", &judgment->iNanoSecs))
1118 {
1119 Judgment_Destroy(judgment);
1120 return false;
1121 }
1122
1123 if (!JsonBuffer_Get_EventId(parent, "Event_ID", &judgment->pEventId))
1124 {
1125 Judgment_Destroy(judgment);
1126 return false;
1127 }
1128
1129 if (!JsonBuffer_Get_BlockId(parent, "Block_ID", &judgment->pBlockId))
1130 {
1131 Judgment_Destroy(judgment);
1132 return false;
1133 }
1134
1135 if (!JsonBuffer_Get_uint8_t(parent, "Priority", &judgment->iPriority))
1136 {
1137 Judgment_Destroy(judgment);
1138 return false;
1139 }
1140
1141 if (!JsonBuffer_Get_NTLVList(parent, "Metadata", &judgment->pMetaDataList))
1142 {
1143 Judgment_Destroy(judgment);
1144 return false;
1145 }
1146
1147 if (!JsonBuffer_Get_uint32_t(parent, "GID", &judgment->iGID))
1148 {
1149 Judgment_Destroy(judgment);
1150 return false;
1151 }
1152
1153 if (!JsonBuffer_Get_uint32_t(parent, "SID", &judgment->iSID))
1154 {
1155 Judgment_Destroy(judgment);
1156 return false;
1157 }
1158
1159 if (!JsonBuffer_Get_uint32_t(parent, "Set_SF_Flags", &judgment->Set_SfFlags))
1160 {
1161 Judgment_Destroy(judgment);
1162 return false;
1163 }
1164
1165 if (!JsonBuffer_Get_uint32_t(parent, "Set_Ent_Flags", &judgment->Set_EntFlags))
1166 {
1167 Judgment_Destroy(judgment);
1168 return false;
1169 }
1170
1171 if (!JsonBuffer_Get_uint32_t(parent, "Unset_SF_Flags", &judgment->Unset_SfFlags))
1172 {
1173 Judgment_Destroy(judgment);
1174 return false;
1175 }
1176
1177 if (!JsonBuffer_Get_uint32_t(parent, "Unset_Ent_Flags", &judgment->Unset_EntFlags))
1178 {
1179 Judgment_Destroy(judgment);
1180 return false;
1181 }
1182
1183 if (json_object_object_get(parent, "Message") != NULL)
1184 {
1185 if ((judgment->sMessage = (uint8_t *)JsonBuffer_Get_String(parent, "Message")) == NULL)
1186 {
1187 Judgment_Destroy(judgment);
1188 return false;
1189 }
1190 }
1191 *p_pJudgment = judgment;
1192 return true;
1193 }
1194
JsonBuffer_Put_Nugget(json_object * parent,const char * name,struct Nugget * nugget)1195 SO_PUBLIC bool JsonBuffer_Put_Nugget (json_object * parent, const char * name,
1196 struct Nugget * nugget)
1197 {
1198 json_object *object;
1199 ASSERT( parent != NULL);
1200 ASSERT(name != NULL);
1201 if (parent == NULL)
1202 return false;
1203 if (name == NULL)
1204 return false;
1205 if ((object = json_object_new_object()) == NULL)
1206 return false;
1207 json_object_object_add(parent, name, object);
1208 parent = object;
1209
1210 if (!JsonBuffer_Put_UUID(parent, "Nugget_ID", nugget->uuidNuggetId))
1211 return false;
1212
1213 if (!JsonBuffer_Put_UUID(parent, "App_Type", nugget->uuidApplicationType))
1214 return false;
1215
1216 if (!JsonBuffer_Put_UUID(parent, "Nugget_Type", nugget->uuidNuggetType))
1217 return false;
1218
1219 if (nugget->sName != NULL)
1220 if (!JsonBuffer_Put_String(parent, "Name", nugget->sName))
1221 return false;
1222
1223 if (nugget->sLocation != NULL)
1224 if (!JsonBuffer_Put_String(parent, "Location", nugget->sLocation))
1225 return false;
1226
1227 if (nugget->sContact != NULL)
1228 if (!JsonBuffer_Put_String(parent, "Contact", nugget->sContact))
1229 return false;
1230
1231 return true;
1232 }
1233
JsonBuffer_Get_Nugget(json_object * parent,const char * name,struct Nugget ** r_nugget)1234 SO_PUBLIC bool JsonBuffer_Get_Nugget (json_object * parent, const char * name,
1235 struct Nugget ** r_nugget)
1236 {
1237 struct Nugget *nugget;
1238 json_object *object;
1239 ASSERT( parent != NULL);
1240 ASSERT(name != NULL);
1241 if (parent == NULL)
1242 return false;
1243 if (name == NULL)
1244 return false;
1245 // Get the container
1246 if ((object = json_object_object_get(parent, name)) == NULL)
1247 return false;
1248 if (json_object_get_type(object) != json_type_object)
1249 return false;
1250 parent = object;
1251
1252 if ((nugget = calloc(1, sizeof(struct Nugget))) == NULL)
1253 return false;
1254
1255 if (!JsonBuffer_Get_UUID(parent, "Nugget_ID", nugget->uuidNuggetId))
1256 {
1257 return false;
1258 }
1259
1260 if (!JsonBuffer_Get_UUID(parent, "App_Type", nugget->uuidApplicationType))
1261 {
1262 return false;
1263 }
1264 if (!JsonBuffer_Get_UUID(parent, "Nugget_Type", nugget->uuidNuggetType))
1265 {
1266 return false;
1267 }
1268 if (json_object_object_get(parent, "Name") != NULL)
1269 {
1270 if ((nugget->sName = JsonBuffer_Get_String(parent, "Name")) == NULL)
1271 {
1272 return false;
1273 }
1274 }
1275 if (json_object_object_get(parent, "Location") != NULL)
1276 {
1277 if ((nugget->sLocation = JsonBuffer_Get_String(parent, "Location")) == NULL)
1278 {
1279 return false;
1280 }
1281 }
1282
1283 if (json_object_object_get(parent, "Contact") != NULL)
1284 {
1285 if ((nugget->sContact = JsonBuffer_Get_String(parent, "Contact")) == NULL)
1286 {
1287 return false;
1288 }
1289 }
1290 *r_nugget=nugget;
1291 return true;
1292 }
1293
1294 static int
JsonBuffer_Put_UUIDList_Add(void * vnode,void * varray)1295 JsonBuffer_Put_UUIDList_Add(void *vnode, void *varray)
1296 {
1297 struct UUIDListNode *node = vnode;
1298 char uuid[UUID_STRING_LENGTH];
1299 json_object *array = varray;
1300 json_object *object;
1301 if ((object = json_object_new_object()) == NULL)
1302 return LIST_EACH_ERROR;
1303
1304 uuid_unparse(node->uuid, uuid);
1305 JsonBuffer_Put_String(object, "id", uuid);
1306 if (node->sName != NULL)
1307 JsonBuffer_Put_String(object, "name", node->sName);
1308 if (node->sDescription != NULL)
1309 JsonBuffer_Put_String(object, "description", node->sDescription);
1310
1311 json_object_array_add(array, object);
1312
1313 return LIST_EACH_OK;
1314 }
1315
1316 SO_PUBLIC bool
JsonBuffer_Put_UUIDList(json_object * parent,const char * name,struct List * list)1317 JsonBuffer_Put_UUIDList (json_object * parent, const char * name,
1318 struct List * list)
1319 {
1320 json_object *object;
1321 ASSERT( parent != NULL);
1322 ASSERT(name != NULL);
1323 if (parent == NULL)
1324 return false;
1325 if (name == NULL)
1326 return false;
1327 if ((object = json_object_new_array()) == NULL)
1328 return false;
1329 json_object_object_add(parent, name, object);
1330 parent = object;
1331 List_ForEach(list, JsonBuffer_Put_UUIDList_Add, object);
1332 return true;
1333 }
1334
JsonBuffer_Get_UUIDList(json_object * parent,const char * name,struct List ** r_list)1335 SO_PUBLIC bool JsonBuffer_Get_UUIDList (json_object * parent, const char * name,
1336 struct List ** r_list)
1337 {
1338 struct List *list;
1339 json_object *object;
1340 uuid_t uuid;
1341 const char *uuidS, *nameS, *desc;
1342 int i;
1343 ASSERT( parent != NULL);
1344 ASSERT(name != NULL);
1345 if (parent == NULL)
1346 return false;
1347 if (name == NULL)
1348 return false;
1349 if ((object = json_object_object_get(parent, name)) == NULL)
1350 return false;
1351 if (json_object_get_type(object) != json_type_array)
1352 return false;
1353 parent = object;
1354 if ((list = UUID_Create_List()) == NULL)
1355 return false;
1356
1357 for (i = 0; i < json_object_array_length(parent); i++)
1358 {
1359 if (((object = json_object_array_get_idx(parent, i)) == NULL) ||
1360 (json_object_get_type(object) != json_type_object) )
1361 {
1362 List_Destroy(list);
1363 return false;
1364 }
1365 uuidS = JsonBuffer_Get_String(object, "id");
1366 nameS = JsonBuffer_Get_String(object, "name");
1367 desc = JsonBuffer_Get_String(object, "description");
1368 uuid_parse(uuidS, uuid);
1369 UUID_Add_List_Entry(list, uuid, nameS, desc);
1370 free((void *)nameS);
1371 free((void *)uuidS);
1372 free((void *)desc);
1373 }
1374
1375 *r_list = list;
1376 return true;
1377 }
1378
1379 static int
JsonBuffer_Put_StringList_Add(void * vnode,void * varray)1380 JsonBuffer_Put_StringList_Add(void *vnode, void *varray)
1381 {
1382 char *node = vnode;
1383 json_object *array = varray;
1384 json_object *object;
1385 if ((object = json_object_new_string(node)) == NULL)
1386 return LIST_EACH_ERROR;
1387
1388 json_object_array_add(array, object);
1389
1390 return LIST_EACH_OK;
1391 }
1392
1393 SO_PUBLIC bool
JsonBuffer_Put_StringList(json_object * parent,const char * name,struct List * list)1394 JsonBuffer_Put_StringList (json_object * parent, const char * name,
1395 struct List * list)
1396 {
1397 json_object *object;
1398 ASSERT( parent != NULL);
1399 ASSERT(name != NULL);
1400 if (parent == NULL)
1401 return false;
1402 if (name == NULL)
1403 return false;
1404 if ((object = json_object_new_array()) == NULL)
1405 return false;
1406 json_object_object_add(parent, name, object);
1407 parent = object;
1408 List_ForEach(list, JsonBuffer_Put_StringList_Add, object);
1409 return true;
1410 }
1411
1412 SO_PUBLIC bool
JsonBuffer_Get_StringList(json_object * parent,const char * name,struct List ** r_list)1413 JsonBuffer_Get_StringList (json_object * parent, const char * name,
1414 struct List ** r_list)
1415 {
1416 struct List *list;
1417 json_object *object;
1418 const char *string;
1419 int i;
1420 ASSERT( parent != NULL);
1421 ASSERT(name != NULL);
1422 if (parent == NULL)
1423 return false;
1424 if (name == NULL)
1425 return false;
1426 if ((object = json_object_object_get(parent, name)) == NULL)
1427 return false;
1428 if (json_object_get_type(object) != json_type_array)
1429 return false;
1430 parent = object;
1431 if ((list = StringList_Create()) == NULL)
1432 return false;
1433 for (i = 0; i < json_object_array_length(parent); i++)
1434 {
1435 if (((object = json_object_array_get_idx(parent, i)) == NULL) ||
1436 (json_object_get_type(object) != json_type_string) ||
1437 ((string = json_object_get_string(object))== NULL))
1438 {
1439 List_Destroy(list);
1440 return false;
1441 }
1442 StringList_Add(list, string);
1443 }
1444
1445 *r_list = list;
1446 return true;
1447 }
1448
1449
1450 SO_PUBLIC bool
JsonBuffer_Put_uint8List(json_object * parent,const char * name,uint8_t * list,uint32_t count)1451 JsonBuffer_Put_uint8List (json_object * parent, const char * name,
1452 uint8_t *list, uint32_t count)
1453 {
1454 json_object *object;
1455 uint32_t i;
1456 ASSERT( parent != NULL);
1457 ASSERT(name != NULL);
1458 if (parent == NULL)
1459 return false;
1460 if (name == NULL)
1461 return false;
1462 if ((object = json_object_new_array()) == NULL)
1463 return false;
1464 json_object_object_add(parent, name, object);
1465 parent = object;
1466 for (i = 0; i < count; i++)
1467 {
1468 if ((object = json_object_new_int(list[i])) == NULL)
1469 return false;
1470
1471 json_object_array_add(parent, object);
1472 }
1473 return true;
1474 }
1475
1476 SO_PUBLIC bool
JsonBuffer_Get_uint8List(json_object * parent,const char * name,uint8_t ** list,uint32_t * count)1477 JsonBuffer_Get_uint8List (json_object * parent, const char * name,
1478 uint8_t **list, uint32_t *count)
1479 {
1480 json_object *object;
1481 uint32_t c =0;
1482 uint8_t *items = NULL;
1483 uint32_t i;
1484 ASSERT( parent != NULL);
1485 ASSERT(name != NULL);
1486 if (parent == NULL)
1487 return false;
1488 if (name == NULL)
1489 return false;
1490 if ((object = json_object_object_get(parent, name)) == NULL)
1491 return false;
1492 if (json_object_get_type(object) != json_type_array)
1493 return false;
1494 parent = object;
1495 c = json_object_array_length(parent);
1496 if (c > 0)
1497 if ((items = calloc(c, sizeof(uint8_t))) == NULL)
1498 return false;
1499
1500 for ( i = 0; i < c; i++)
1501 {
1502 if (((object = json_object_array_get_idx(parent, i)) == NULL) ||
1503 (json_object_get_type(object) != json_type_int))
1504 items[i] = json_object_get_int(object);
1505 }
1506
1507 *list = items;
1508 *count = c;
1509 return true;
1510 }
1511