]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/jansson/value.c
Added loading EMV transaction parameters from json file (#659)
[proxmark3-svn] / client / jansson / value.c
CommitLineData
556826b5
OM
1/*
2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3 *
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6 */
7
8#ifndef _GNU_SOURCE
9#define _GNU_SOURCE
10#endif
11
12#ifdef HAVE_CONFIG_H
13#include <jansson_private_config.h>
14#endif
15
16#include <stddef.h>
17#include <stdlib.h>
18#include <string.h>
19#include <math.h>
20
21#ifdef HAVE_STDINT_H
22#include <stdint.h>
23#endif
24
25#include "jansson.h"
26#include "hashtable.h"
27#include "jansson_private.h"
28#include "utf.h"
29
30/* Work around nonstandard isnan() and isinf() implementations */
31#ifndef isnan
32#ifndef __sun
33static JSON_INLINE int isnan(double x) { return x != x; }
34#endif
35#endif
36#ifndef isinf
37static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); }
38#endif
39
40static JSON_INLINE void json_init(json_t *json, json_type type)
41{
42 json->type = type;
43 json->refcount = 1;
44}
45
46
47/*** object ***/
48
49extern volatile uint32_t hashtable_seed;
50
51json_t *json_object(void)
52{
53 json_object_t *object = jsonp_malloc(sizeof(json_object_t));
54 if(!object)
55 return NULL;
56
57 if (!hashtable_seed) {
58 /* Autoseed */
59 json_object_seed(0);
60 }
61
62 json_init(&object->json, JSON_OBJECT);
63
64 if(hashtable_init(&object->hashtable))
65 {
66 jsonp_free(object);
67 return NULL;
68 }
69
70 return &object->json;
71}
72
73static void json_delete_object(json_object_t *object)
74{
75 hashtable_close(&object->hashtable);
76 jsonp_free(object);
77}
78
79size_t json_object_size(const json_t *json)
80{
81 json_object_t *object;
82
83 if(!json_is_object(json))
84 return 0;
85
86 object = json_to_object(json);
87 return object->hashtable.size;
88}
89
90json_t *json_object_get(const json_t *json, const char *key)
91{
92 json_object_t *object;
93
94 if(!key || !json_is_object(json))
95 return NULL;
96
97 object = json_to_object(json);
98 return hashtable_get(&object->hashtable, key);
99}
100
101int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
102{
103 json_object_t *object;
104
105 if(!value)
106 return -1;
107
108 if(!key || !json_is_object(json) || json == value)
109 {
110 json_decref(value);
111 return -1;
112 }
113 object = json_to_object(json);
114
115 if(hashtable_set(&object->hashtable, key, value))
116 {
117 json_decref(value);
118 return -1;
119 }
120
121 return 0;
122}
123
124int json_object_set_new(json_t *json, const char *key, json_t *value)
125{
126 if(!key || !utf8_check_string(key, strlen(key)))
127 {
128 json_decref(value);
129 return -1;
130 }
131
132 return json_object_set_new_nocheck(json, key, value);
133}
134
135int json_object_del(json_t *json, const char *key)
136{
137 json_object_t *object;
138
139 if(!key || !json_is_object(json))
140 return -1;
141
142 object = json_to_object(json);
143 return hashtable_del(&object->hashtable, key);
144}
145
146int json_object_clear(json_t *json)
147{
148 json_object_t *object;
149
150 if(!json_is_object(json))
151 return -1;
152
153 object = json_to_object(json);
154 hashtable_clear(&object->hashtable);
155
156 return 0;
157}
158
159int json_object_update(json_t *object, json_t *other)
160{
161 const char *key;
162 json_t *value;
163
164 if(!json_is_object(object) || !json_is_object(other))
165 return -1;
166
167 json_object_foreach(other, key, value) {
168 if(json_object_set_nocheck(object, key, value))
169 return -1;
170 }
171
172 return 0;
173}
174
175int json_object_update_existing(json_t *object, json_t *other)
176{
177 const char *key;
178 json_t *value;
179
180 if(!json_is_object(object) || !json_is_object(other))
181 return -1;
182
183 json_object_foreach(other, key, value) {
184 if(json_object_get(object, key))
185 json_object_set_nocheck(object, key, value);
186 }
187
188 return 0;
189}
190
191int json_object_update_missing(json_t *object, json_t *other)
192{
193 const char *key;
194 json_t *value;
195
196 if(!json_is_object(object) || !json_is_object(other))
197 return -1;
198
199 json_object_foreach(other, key, value) {
200 if(!json_object_get(object, key))
201 json_object_set_nocheck(object, key, value);
202 }
203
204 return 0;
205}
206
207void *json_object_iter(json_t *json)
208{
209 json_object_t *object;
210
211 if(!json_is_object(json))
212 return NULL;
213
214 object = json_to_object(json);
215 return hashtable_iter(&object->hashtable);
216}
217
218void *json_object_iter_at(json_t *json, const char *key)
219{
220 json_object_t *object;
221
222 if(!key || !json_is_object(json))
223 return NULL;
224
225 object = json_to_object(json);
226 return hashtable_iter_at(&object->hashtable, key);
227}
228
229void *json_object_iter_next(json_t *json, void *iter)
230{
231 json_object_t *object;
232
233 if(!json_is_object(json) || iter == NULL)
234 return NULL;
235
236 object = json_to_object(json);
237 return hashtable_iter_next(&object->hashtable, iter);
238}
239
240const char *json_object_iter_key(void *iter)
241{
242 if(!iter)
243 return NULL;
244
245 return hashtable_iter_key(iter);
246}
247
248json_t *json_object_iter_value(void *iter)
249{
250 if(!iter)
251 return NULL;
252
253 return (json_t *)hashtable_iter_value(iter);
254}
255
256int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
257{
258 if(!json_is_object(json) || !iter || !value)
259 {
260 json_decref(value);
261 return -1;
262 }
263
264 hashtable_iter_set(iter, value);
265 return 0;
266}
267
268void *json_object_key_to_iter(const char *key)
269{
270 if(!key)
271 return NULL;
272
273 return hashtable_key_to_iter(key);
274}
275
276static int json_object_equal(const json_t *object1, const json_t *object2)
277{
278 const char *key;
279 const json_t *value1, *value2;
280
281 if(json_object_size(object1) != json_object_size(object2))
282 return 0;
283
284 json_object_foreach((json_t *)object1, key, value1) {
285 value2 = json_object_get(object2, key);
286
287 if(!json_equal(value1, value2))
288 return 0;
289 }
290
291 return 1;
292}
293
294static json_t *json_object_copy(json_t *object)
295{
296 json_t *result;
297
298 const char *key;
299 json_t *value;
300
301 result = json_object();
302 if(!result)
303 return NULL;
304
305 json_object_foreach(object, key, value)
306 json_object_set_nocheck(result, key, value);
307
308 return result;
309}
310
311static json_t *json_object_deep_copy(const json_t *object)
312{
313 json_t *result;
314 void *iter;
315
316 result = json_object();
317 if(!result)
318 return NULL;
319
320 /* Cannot use json_object_foreach because object has to be cast
321 non-const */
322 iter = json_object_iter((json_t *)object);
323 while(iter) {
324 const char *key;
325 const json_t *value;
326 key = json_object_iter_key(iter);
327 value = json_object_iter_value(iter);
328
329 json_object_set_new_nocheck(result, key, json_deep_copy(value));
330 iter = json_object_iter_next((json_t *)object, iter);
331 }
332
333 return result;
334}
335
336
337/*** array ***/
338
339json_t *json_array(void)
340{
341 json_array_t *array = jsonp_malloc(sizeof(json_array_t));
342 if(!array)
343 return NULL;
344 json_init(&array->json, JSON_ARRAY);
345
346 array->entries = 0;
347 array->size = 8;
348
349 array->table = jsonp_malloc(array->size * sizeof(json_t *));
350 if(!array->table) {
351 jsonp_free(array);
352 return NULL;
353 }
354
355 return &array->json;
356}
357
358static void json_delete_array(json_array_t *array)
359{
360 size_t i;
361
362 for(i = 0; i < array->entries; i++)
363 json_decref(array->table[i]);
364
365 jsonp_free(array->table);
366 jsonp_free(array);
367}
368
369size_t json_array_size(const json_t *json)
370{
371 if(!json_is_array(json))
372 return 0;
373
374 return json_to_array(json)->entries;
375}
376
377json_t *json_array_get(const json_t *json, size_t index)
378{
379 json_array_t *array;
380 if(!json_is_array(json))
381 return NULL;
382 array = json_to_array(json);
383
384 if(index >= array->entries)
385 return NULL;
386
387 return array->table[index];
388}
389
390int json_array_set_new(json_t *json, size_t index, json_t *value)
391{
392 json_array_t *array;
393
394 if(!value)
395 return -1;
396
397 if(!json_is_array(json) || json == value)
398 {
399 json_decref(value);
400 return -1;
401 }
402 array = json_to_array(json);
403
404 if(index >= array->entries)
405 {
406 json_decref(value);
407 return -1;
408 }
409
410 json_decref(array->table[index]);
411 array->table[index] = value;
412
413 return 0;
414}
415
416static void array_move(json_array_t *array, size_t dest,
417 size_t src, size_t count)
418{
419 memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
420}
421
422static void array_copy(json_t **dest, size_t dpos,
423 json_t **src, size_t spos,
424 size_t count)
425{
426 memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
427}
428
429static json_t **json_array_grow(json_array_t *array,
430 size_t amount,
431 int copy)
432{
433 size_t new_size;
434 json_t **old_table, **new_table;
435
436 if(array->entries + amount <= array->size)
437 return array->table;
438
439 old_table = array->table;
440
441 new_size = max(array->size + amount, array->size * 2);
442 new_table = jsonp_malloc(new_size * sizeof(json_t *));
443 if(!new_table)
444 return NULL;
445
446 array->size = new_size;
447 array->table = new_table;
448
449 if(copy) {
450 array_copy(array->table, 0, old_table, 0, array->entries);
451 jsonp_free(old_table);
452 return array->table;
453 }
454
455 return old_table;
456}
457
458int json_array_append_new(json_t *json, json_t *value)
459{
460 json_array_t *array;
461
462 if(!value)
463 return -1;
464
465 if(!json_is_array(json) || json == value)
466 {
467 json_decref(value);
468 return -1;
469 }
470 array = json_to_array(json);
471
472 if(!json_array_grow(array, 1, 1)) {
473 json_decref(value);
474 return -1;
475 }
476
477 array->table[array->entries] = value;
478 array->entries++;
479
480 return 0;
481}
482
483int json_array_insert_new(json_t *json, size_t index, json_t *value)
484{
485 json_array_t *array;
486 json_t **old_table;
487
488 if(!value)
489 return -1;
490
491 if(!json_is_array(json) || json == value) {
492 json_decref(value);
493 return -1;
494 }
495 array = json_to_array(json);
496
497 if(index > array->entries) {
498 json_decref(value);
499 return -1;
500 }
501
502 old_table = json_array_grow(array, 1, 0);
503 if(!old_table) {
504 json_decref(value);
505 return -1;
506 }
507
508 if(old_table != array->table) {
509 array_copy(array->table, 0, old_table, 0, index);
510 array_copy(array->table, index + 1, old_table, index,
511 array->entries - index);
512 jsonp_free(old_table);
513 }
514 else
515 array_move(array, index + 1, index, array->entries - index);
516
517 array->table[index] = value;
518 array->entries++;
519
520 return 0;
521}
522
523int json_array_remove(json_t *json, size_t index)
524{
525 json_array_t *array;
526
527 if(!json_is_array(json))
528 return -1;
529 array = json_to_array(json);
530
531 if(index >= array->entries)
532 return -1;
533
534 json_decref(array->table[index]);
535
536 /* If we're removing the last element, nothing has to be moved */
537 if(index < array->entries - 1)
538 array_move(array, index, index + 1, array->entries - index - 1);
539
540 array->entries--;
541
542 return 0;
543}
544
545int json_array_clear(json_t *json)
546{
547 json_array_t *array;
548 size_t i;
549
550 if(!json_is_array(json))
551 return -1;
552 array = json_to_array(json);
553
554 for(i = 0; i < array->entries; i++)
555 json_decref(array->table[i]);
556
557 array->entries = 0;
558 return 0;
559}
560
561int json_array_extend(json_t *json, json_t *other_json)
562{
563 json_array_t *array, *other;
564 size_t i;
565
566 if(!json_is_array(json) || !json_is_array(other_json))
567 return -1;
568 array = json_to_array(json);
569 other = json_to_array(other_json);
570
571 if(!json_array_grow(array, other->entries, 1))
572 return -1;
573
574 for(i = 0; i < other->entries; i++)
575 json_incref(other->table[i]);
576
577 array_copy(array->table, array->entries, other->table, 0, other->entries);
578
579 array->entries += other->entries;
580 return 0;
581}
582
583static int json_array_equal(const json_t *array1, const json_t *array2)
584{
585 size_t i, size;
586
587 size = json_array_size(array1);
588 if(size != json_array_size(array2))
589 return 0;
590
591 for(i = 0; i < size; i++)
592 {
593 json_t *value1, *value2;
594
595 value1 = json_array_get(array1, i);
596 value2 = json_array_get(array2, i);
597
598 if(!json_equal(value1, value2))
599 return 0;
600 }
601
602 return 1;
603}
604
605static json_t *json_array_copy(json_t *array)
606{
607 json_t *result;
608 size_t i;
609
610 result = json_array();
611 if(!result)
612 return NULL;
613
614 for(i = 0; i < json_array_size(array); i++)
615 json_array_append(result, json_array_get(array, i));
616
617 return result;
618}
619
620static json_t *json_array_deep_copy(const json_t *array)
621{
622 json_t *result;
623 size_t i;
624
625 result = json_array();
626 if(!result)
627 return NULL;
628
629 for(i = 0; i < json_array_size(array); i++)
630 json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
631
632 return result;
633}
634
635/*** string ***/
636
637static json_t *string_create(const char *value, size_t len, int own)
638{
639 char *v;
640 json_string_t *string;
641
642 if(!value)
643 return NULL;
644
645 if(own)
646 v = (char *)value;
647 else {
648 v = jsonp_strndup(value, len);
649 if(!v)
650 return NULL;
651 }
652
653 string = jsonp_malloc(sizeof(json_string_t));
654 if(!string) {
655 if(!own)
656 jsonp_free(v);
657 return NULL;
658 }
659 json_init(&string->json, JSON_STRING);
660 string->value = v;
661 string->length = len;
662
663 return &string->json;
664}
665
666json_t *json_string_nocheck(const char *value)
667{
668 if(!value)
669 return NULL;
670
671 return string_create(value, strlen(value), 0);
672}
673
674json_t *json_stringn_nocheck(const char *value, size_t len)
675{
676 return string_create(value, len, 0);
677}
678
679/* this is private; "steal" is not a public API concept */
680json_t *jsonp_stringn_nocheck_own(const char *value, size_t len)
681{
682 return string_create(value, len, 1);
683}
684
685json_t *json_string(const char *value)
686{
687 if(!value)
688 return NULL;
689
690 return json_stringn(value, strlen(value));
691}
692
693json_t *json_stringn(const char *value, size_t len)
694{
695 if(!value || !utf8_check_string(value, len))
696 return NULL;
697
698 return json_stringn_nocheck(value, len);
699}
700
701const char *json_string_value(const json_t *json)
702{
703 if(!json_is_string(json))
704 return NULL;
705
706 return json_to_string(json)->value;
707}
708
709size_t json_string_length(const json_t *json)
710{
711 if(!json_is_string(json))
712 return 0;
713
714 return json_to_string(json)->length;
715}
716
717int json_string_set_nocheck(json_t *json, const char *value)
718{
719 if(!value)
720 return -1;
721
722 return json_string_setn_nocheck(json, value, strlen(value));
723}
724
725int json_string_setn_nocheck(json_t *json, const char *value, size_t len)
726{
727 char *dup;
728 json_string_t *string;
729
730 if(!json_is_string(json) || !value)
731 return -1;
732
733 dup = jsonp_strndup(value, len);
734 if(!dup)
735 return -1;
736
737 string = json_to_string(json);
738 jsonp_free(string->value);
739 string->value = dup;
740 string->length = len;
741
742 return 0;
743}
744
745int json_string_set(json_t *json, const char *value)
746{
747 if(!value)
748 return -1;
749
750 return json_string_setn(json, value, strlen(value));
751}
752
753int json_string_setn(json_t *json, const char *value, size_t len)
754{
755 if(!value || !utf8_check_string(value, len))
756 return -1;
757
758 return json_string_setn_nocheck(json, value, len);
759}
760
761static void json_delete_string(json_string_t *string)
762{
763 jsonp_free(string->value);
764 jsonp_free(string);
765}
766
767static int json_string_equal(const json_t *string1, const json_t *string2)
768{
769 json_string_t *s1, *s2;
770
771 if(!json_is_string(string1) || !json_is_string(string2))
772 return 0;
773
774 s1 = json_to_string(string1);
775 s2 = json_to_string(string2);
776 return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length);
777}
778
779static json_t *json_string_copy(const json_t *string)
780{
781 json_string_t *s;
782
783 if(!json_is_string(string))
784 return NULL;
785
786 s = json_to_string(string);
787 return json_stringn_nocheck(s->value, s->length);
788}
789
790json_t *json_vsprintf(const char *fmt, va_list ap) {
791 int length;
792 char *buf;
793 va_list aq;
794 va_copy(aq, ap);
795
796 length = vsnprintf(NULL, 0, fmt, ap);
797 if (length == 0)
798 return json_string("");
799
800 buf = jsonp_malloc(length + 1);
801 if (!buf)
802 return NULL;
803
804 vsnprintf(buf, length + 1, fmt, aq);
805 if (!utf8_check_string(buf, length)) {
806 jsonp_free(buf);
807 return NULL;
808 }
809
810 return jsonp_stringn_nocheck_own(buf, length);
811}
812
813json_t *json_sprintf(const char *fmt, ...) {
814 json_t *result;
815 va_list ap;
816
817 va_start(ap, fmt);
818 result = json_vsprintf(fmt, ap);
819 va_end(ap);
820
821 return result;
822}
823
824
825/*** integer ***/
826
827json_t *json_integer(json_int_t value)
828{
829 json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
830 if(!integer)
831 return NULL;
832 json_init(&integer->json, JSON_INTEGER);
833
834 integer->value = value;
835 return &integer->json;
836}
837
838json_int_t json_integer_value(const json_t *json)
839{
840 if(!json_is_integer(json))
841 return 0;
842
843 return json_to_integer(json)->value;
844}
845
846int json_integer_set(json_t *json, json_int_t value)
847{
848 if(!json_is_integer(json))
849 return -1;
850
851 json_to_integer(json)->value = value;
852
853 return 0;
854}
855
856static void json_delete_integer(json_integer_t *integer)
857{
858 jsonp_free(integer);
859}
860
861static int json_integer_equal(const json_t *integer1, const json_t *integer2)
862{
863 return json_integer_value(integer1) == json_integer_value(integer2);
864}
865
866static json_t *json_integer_copy(const json_t *integer)
867{
868 return json_integer(json_integer_value(integer));
869}
870
871
872/*** real ***/
873
874json_t *json_real(double value)
875{
876 json_real_t *real;
877
878 if(isnan(value) || isinf(value))
879 return NULL;
880
881 real = jsonp_malloc(sizeof(json_real_t));
882 if(!real)
883 return NULL;
884 json_init(&real->json, JSON_REAL);
885
886 real->value = value;
887 return &real->json;
888}
889
890double json_real_value(const json_t *json)
891{
892 if(!json_is_real(json))
893 return 0;
894
895 return json_to_real(json)->value;
896}
897
898int json_real_set(json_t *json, double value)
899{
900 if(!json_is_real(json) || isnan(value) || isinf(value))
901 return -1;
902
903 json_to_real(json)->value = value;
904
905 return 0;
906}
907
908static void json_delete_real(json_real_t *real)
909{
910 jsonp_free(real);
911}
912
913static int json_real_equal(const json_t *real1, const json_t *real2)
914{
915 return json_real_value(real1) == json_real_value(real2);
916}
917
918static json_t *json_real_copy(const json_t *real)
919{
920 return json_real(json_real_value(real));
921}
922
923
924/*** number ***/
925
926double json_number_value(const json_t *json)
927{
928 if(json_is_integer(json))
929 return (double)json_integer_value(json);
930 else if(json_is_real(json))
931 return json_real_value(json);
932 else
933 return 0.0;
934}
935
936
937/*** simple values ***/
938
939json_t *json_true(void)
940{
941 static json_t the_true = {JSON_TRUE, (size_t)-1};
942 return &the_true;
943}
944
945
946json_t *json_false(void)
947{
948 static json_t the_false = {JSON_FALSE, (size_t)-1};
949 return &the_false;
950}
951
952
953json_t *json_null(void)
954{
955 static json_t the_null = {JSON_NULL, (size_t)-1};
956 return &the_null;
957}
958
959
960/*** deletion ***/
961
962void json_delete(json_t *json)
963{
964 if (!json)
965 return;
966
967 switch(json_typeof(json)) {
968 case JSON_OBJECT:
969 json_delete_object(json_to_object(json));
970 break;
971 case JSON_ARRAY:
972 json_delete_array(json_to_array(json));
973 break;
974 case JSON_STRING:
975 json_delete_string(json_to_string(json));
976 break;
977 case JSON_INTEGER:
978 json_delete_integer(json_to_integer(json));
979 break;
980 case JSON_REAL:
981 json_delete_real(json_to_real(json));
982 break;
983 default:
984 return;
985 }
986
987 /* json_delete is not called for true, false or null */
988}
989
990
991/*** equality ***/
992
993int json_equal(const json_t *json1, const json_t *json2)
994{
995 if(!json1 || !json2)
996 return 0;
997
998 if(json_typeof(json1) != json_typeof(json2))
999 return 0;
1000
1001 /* this covers true, false and null as they are singletons */
1002 if(json1 == json2)
1003 return 1;
1004
1005 switch(json_typeof(json1)) {
1006 case JSON_OBJECT:
1007 return json_object_equal(json1, json2);
1008 case JSON_ARRAY:
1009 return json_array_equal(json1, json2);
1010 case JSON_STRING:
1011 return json_string_equal(json1, json2);
1012 case JSON_INTEGER:
1013 return json_integer_equal(json1, json2);
1014 case JSON_REAL:
1015 return json_real_equal(json1, json2);
1016 default:
1017 return 0;
1018 }
1019}
1020
1021
1022/*** copying ***/
1023
1024json_t *json_copy(json_t *json)
1025{
1026 if(!json)
1027 return NULL;
1028
1029 switch(json_typeof(json)) {
1030 case JSON_OBJECT:
1031 return json_object_copy(json);
1032 case JSON_ARRAY:
1033 return json_array_copy(json);
1034 case JSON_STRING:
1035 return json_string_copy(json);
1036 case JSON_INTEGER:
1037 return json_integer_copy(json);
1038 case JSON_REAL:
1039 return json_real_copy(json);
1040 case JSON_TRUE:
1041 case JSON_FALSE:
1042 case JSON_NULL:
1043 return json;
1044 default:
1045 return NULL;
1046 }
1047
1048 return NULL;
1049}
1050
1051json_t *json_deep_copy(const json_t *json)
1052{
1053 if(!json)
1054 return NULL;
1055
1056 switch(json_typeof(json)) {
1057 case JSON_OBJECT:
1058 return json_object_deep_copy(json);
1059 case JSON_ARRAY:
1060 return json_array_deep_copy(json);
1061 /* for the rest of the types, deep copying doesn't differ from
1062 shallow copying */
1063 case JSON_STRING:
1064 return json_string_copy(json);
1065 case JSON_INTEGER:
1066 return json_integer_copy(json);
1067 case JSON_REAL:
1068 return json_real_copy(json);
1069 case JSON_TRUE:
1070 case JSON_FALSE:
1071 case JSON_NULL:
1072 return (json_t *)json;
1073 default:
1074 return NULL;
1075 }
1076
1077 return NULL;
1078}
Impressum, Datenschutz