]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/jansson/value.c
2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
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.
13 #include <jansson_private_config.h>
26 #include "hashtable.h"
27 #include "jansson_private.h"
30 /* Work around nonstandard isnan() and isinf() implementations */
33 static JSON_INLINE
int isnan(double x
) { return x
!= x
; }
37 static JSON_INLINE
int isinf(double x
) { return !isnan(x
) && isnan(x
- x
); }
40 static JSON_INLINE
void json_init(json_t
*json
, json_type type
)
49 extern volatile uint32_t hashtable_seed
;
51 json_t
*json_object(void)
53 json_object_t
*object
= jsonp_malloc(sizeof(json_object_t
));
57 if (!hashtable_seed
) {
62 json_init(&object
->json
, JSON_OBJECT
);
64 if(hashtable_init(&object
->hashtable
))
73 static void json_delete_object(json_object_t
*object
)
75 hashtable_close(&object
->hashtable
);
79 size_t json_object_size(const json_t
*json
)
81 json_object_t
*object
;
83 if(!json_is_object(json
))
86 object
= json_to_object(json
);
87 return object
->hashtable
.size
;
90 json_t
*json_object_get(const json_t
*json
, const char *key
)
92 json_object_t
*object
;
94 if(!key
|| !json_is_object(json
))
97 object
= json_to_object(json
);
98 return hashtable_get(&object
->hashtable
, key
);
101 int json_object_set_new_nocheck(json_t
*json
, const char *key
, json_t
*value
)
103 json_object_t
*object
;
108 if(!key
|| !json_is_object(json
) || json
== value
)
113 object
= json_to_object(json
);
115 if(hashtable_set(&object
->hashtable
, key
, value
))
124 int json_object_set_new(json_t
*json
, const char *key
, json_t
*value
)
126 if(!key
|| !utf8_check_string(key
, strlen(key
)))
132 return json_object_set_new_nocheck(json
, key
, value
);
135 int json_object_del(json_t
*json
, const char *key
)
137 json_object_t
*object
;
139 if(!key
|| !json_is_object(json
))
142 object
= json_to_object(json
);
143 return hashtable_del(&object
->hashtable
, key
);
146 int json_object_clear(json_t
*json
)
148 json_object_t
*object
;
150 if(!json_is_object(json
))
153 object
= json_to_object(json
);
154 hashtable_clear(&object
->hashtable
);
159 int json_object_update(json_t
*object
, json_t
*other
)
164 if(!json_is_object(object
) || !json_is_object(other
))
167 json_object_foreach(other
, key
, value
) {
168 if(json_object_set_nocheck(object
, key
, value
))
175 int json_object_update_existing(json_t
*object
, json_t
*other
)
180 if(!json_is_object(object
) || !json_is_object(other
))
183 json_object_foreach(other
, key
, value
) {
184 if(json_object_get(object
, key
))
185 json_object_set_nocheck(object
, key
, value
);
191 int json_object_update_missing(json_t
*object
, json_t
*other
)
196 if(!json_is_object(object
) || !json_is_object(other
))
199 json_object_foreach(other
, key
, value
) {
200 if(!json_object_get(object
, key
))
201 json_object_set_nocheck(object
, key
, value
);
207 void *json_object_iter(json_t
*json
)
209 json_object_t
*object
;
211 if(!json_is_object(json
))
214 object
= json_to_object(json
);
215 return hashtable_iter(&object
->hashtable
);
218 void *json_object_iter_at(json_t
*json
, const char *key
)
220 json_object_t
*object
;
222 if(!key
|| !json_is_object(json
))
225 object
= json_to_object(json
);
226 return hashtable_iter_at(&object
->hashtable
, key
);
229 void *json_object_iter_next(json_t
*json
, void *iter
)
231 json_object_t
*object
;
233 if(!json_is_object(json
) || iter
== NULL
)
236 object
= json_to_object(json
);
237 return hashtable_iter_next(&object
->hashtable
, iter
);
240 const char *json_object_iter_key(void *iter
)
245 return hashtable_iter_key(iter
);
248 json_t
*json_object_iter_value(void *iter
)
253 return (json_t
*)hashtable_iter_value(iter
);
256 int json_object_iter_set_new(json_t
*json
, void *iter
, json_t
*value
)
258 if(!json_is_object(json
) || !iter
|| !value
)
264 hashtable_iter_set(iter
, value
);
268 void *json_object_key_to_iter(const char *key
)
273 return hashtable_key_to_iter(key
);
276 static int json_object_equal(const json_t
*object1
, const json_t
*object2
)
279 const json_t
*value1
, *value2
;
281 if(json_object_size(object1
) != json_object_size(object2
))
284 json_object_foreach((json_t
*)object1
, key
, value1
) {
285 value2
= json_object_get(object2
, key
);
287 if(!json_equal(value1
, value2
))
294 static json_t
*json_object_copy(json_t
*object
)
301 result
= json_object();
305 json_object_foreach(object
, key
, value
)
306 json_object_set_nocheck(result
, key
, value
);
311 static json_t
*json_object_deep_copy(const json_t
*object
)
316 result
= json_object();
320 /* Cannot use json_object_foreach because object has to be cast
322 iter
= json_object_iter((json_t
*)object
);
326 key
= json_object_iter_key(iter
);
327 value
= json_object_iter_value(iter
);
329 json_object_set_new_nocheck(result
, key
, json_deep_copy(value
));
330 iter
= json_object_iter_next((json_t
*)object
, iter
);
339 json_t
*json_array(void)
341 json_array_t
*array
= jsonp_malloc(sizeof(json_array_t
));
344 json_init(&array
->json
, JSON_ARRAY
);
349 array
->table
= jsonp_malloc(array
->size
* sizeof(json_t
*));
358 static void json_delete_array(json_array_t
*array
)
362 for(i
= 0; i
< array
->entries
; i
++)
363 json_decref(array
->table
[i
]);
365 jsonp_free(array
->table
);
369 size_t json_array_size(const json_t
*json
)
371 if(!json_is_array(json
))
374 return json_to_array(json
)->entries
;
377 json_t
*json_array_get(const json_t
*json
, size_t index
)
380 if(!json_is_array(json
))
382 array
= json_to_array(json
);
384 if(index
>= array
->entries
)
387 return array
->table
[index
];
390 int json_array_set_new(json_t
*json
, size_t index
, json_t
*value
)
397 if(!json_is_array(json
) || json
== value
)
402 array
= json_to_array(json
);
404 if(index
>= array
->entries
)
410 json_decref(array
->table
[index
]);
411 array
->table
[index
] = value
;
416 static void array_move(json_array_t
*array
, size_t dest
,
417 size_t src
, size_t count
)
419 memmove(&array
->table
[dest
], &array
->table
[src
], count
* sizeof(json_t
*));
422 static void array_copy(json_t
**dest
, size_t dpos
,
423 json_t
**src
, size_t spos
,
426 memcpy(&dest
[dpos
], &src
[spos
], count
* sizeof(json_t
*));
429 static json_t
**json_array_grow(json_array_t
*array
,
434 json_t
**old_table
, **new_table
;
436 if(array
->entries
+ amount
<= array
->size
)
439 old_table
= array
->table
;
441 new_size
= max(array
->size
+ amount
, array
->size
* 2);
442 new_table
= jsonp_malloc(new_size
* sizeof(json_t
*));
446 array
->size
= new_size
;
447 array
->table
= new_table
;
450 array_copy(array
->table
, 0, old_table
, 0, array
->entries
);
451 jsonp_free(old_table
);
458 int json_array_append_new(json_t
*json
, json_t
*value
)
465 if(!json_is_array(json
) || json
== value
)
470 array
= json_to_array(json
);
472 if(!json_array_grow(array
, 1, 1)) {
477 array
->table
[array
->entries
] = value
;
483 int json_array_insert_new(json_t
*json
, size_t index
, json_t
*value
)
491 if(!json_is_array(json
) || json
== value
) {
495 array
= json_to_array(json
);
497 if(index
> array
->entries
) {
502 old_table
= json_array_grow(array
, 1, 0);
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
);
515 array_move(array
, index
+ 1, index
, array
->entries
- index
);
517 array
->table
[index
] = value
;
523 int json_array_remove(json_t
*json
, size_t index
)
527 if(!json_is_array(json
))
529 array
= json_to_array(json
);
531 if(index
>= array
->entries
)
534 json_decref(array
->table
[index
]);
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);
545 int json_array_clear(json_t
*json
)
550 if(!json_is_array(json
))
552 array
= json_to_array(json
);
554 for(i
= 0; i
< array
->entries
; i
++)
555 json_decref(array
->table
[i
]);
561 int json_array_extend(json_t
*json
, json_t
*other_json
)
563 json_array_t
*array
, *other
;
566 if(!json_is_array(json
) || !json_is_array(other_json
))
568 array
= json_to_array(json
);
569 other
= json_to_array(other_json
);
571 if(!json_array_grow(array
, other
->entries
, 1))
574 for(i
= 0; i
< other
->entries
; i
++)
575 json_incref(other
->table
[i
]);
577 array_copy(array
->table
, array
->entries
, other
->table
, 0, other
->entries
);
579 array
->entries
+= other
->entries
;
583 static int json_array_equal(const json_t
*array1
, const json_t
*array2
)
587 size
= json_array_size(array1
);
588 if(size
!= json_array_size(array2
))
591 for(i
= 0; i
< size
; i
++)
593 json_t
*value1
, *value2
;
595 value1
= json_array_get(array1
, i
);
596 value2
= json_array_get(array2
, i
);
598 if(!json_equal(value1
, value2
))
605 static json_t
*json_array_copy(json_t
*array
)
610 result
= json_array();
614 for(i
= 0; i
< json_array_size(array
); i
++)
615 json_array_append(result
, json_array_get(array
, i
));
620 static json_t
*json_array_deep_copy(const json_t
*array
)
625 result
= json_array();
629 for(i
= 0; i
< json_array_size(array
); i
++)
630 json_array_append_new(result
, json_deep_copy(json_array_get(array
, i
)));
637 static json_t
*string_create(const char *value
, size_t len
, int own
)
640 json_string_t
*string
;
648 v
= jsonp_strndup(value
, len
);
653 string
= jsonp_malloc(sizeof(json_string_t
));
658 json_init(&string
->json
, JSON_STRING
);
660 string
->length
= len
;
662 return &string
->json
;
665 json_t
*json_string_nocheck(const char *value
)
670 return string_create(value
, strlen(value
), 0);
673 json_t
*json_stringn_nocheck(const char *value
, size_t len
)
675 return string_create(value
, len
, 0);
678 /* this is private; "steal" is not a public API concept */
679 json_t
*jsonp_stringn_nocheck_own(const char *value
, size_t len
)
681 return string_create(value
, len
, 1);
684 json_t
*json_string(const char *value
)
689 return json_stringn(value
, strlen(value
));
692 json_t
*json_stringn(const char *value
, size_t len
)
694 if(!value
|| !utf8_check_string(value
, len
))
697 return json_stringn_nocheck(value
, len
);
700 const char *json_string_value(const json_t
*json
)
702 if(!json_is_string(json
))
705 return json_to_string(json
)->value
;
708 size_t json_string_length(const json_t
*json
)
710 if(!json_is_string(json
))
713 return json_to_string(json
)->length
;
716 int json_string_set_nocheck(json_t
*json
, const char *value
)
721 return json_string_setn_nocheck(json
, value
, strlen(value
));
724 int json_string_setn_nocheck(json_t
*json
, const char *value
, size_t len
)
727 json_string_t
*string
;
729 if(!json_is_string(json
) || !value
)
732 dup
= jsonp_strndup(value
, len
);
736 string
= json_to_string(json
);
737 jsonp_free(string
->value
);
739 string
->length
= len
;
744 int json_string_set(json_t
*json
, const char *value
)
749 return json_string_setn(json
, value
, strlen(value
));
752 int json_string_setn(json_t
*json
, const char *value
, size_t len
)
754 if(!value
|| !utf8_check_string(value
, len
))
757 return json_string_setn_nocheck(json
, value
, len
);
760 static void json_delete_string(json_string_t
*string
)
762 jsonp_free(string
->value
);
766 static int json_string_equal(const json_t
*string1
, const json_t
*string2
)
768 json_string_t
*s1
, *s2
;
770 s1
= json_to_string(string1
);
771 s2
= json_to_string(string2
);
772 return s1
->length
== s2
->length
&& !memcmp(s1
->value
, s2
->value
, s1
->length
);
775 static json_t
*json_string_copy(const json_t
*string
)
779 s
= json_to_string(string
);
780 return json_stringn_nocheck(s
->value
, s
->length
);
783 json_t
*json_vsprintf(const char *fmt
, va_list ap
) {
790 length
= vsnprintf(NULL
, 0, fmt
, ap
);
792 json
= json_string("");
796 buf
= jsonp_malloc(length
+ 1);
800 vsnprintf(buf
, length
+ 1, fmt
, aq
);
801 if (!utf8_check_string(buf
, length
)) {
806 json
= jsonp_stringn_nocheck_own(buf
, length
);
813 json_t
*json_sprintf(const char *fmt
, ...) {
818 result
= json_vsprintf(fmt
, ap
);
827 json_t
*json_integer(json_int_t value
)
829 json_integer_t
*integer
= jsonp_malloc(sizeof(json_integer_t
));
832 json_init(&integer
->json
, JSON_INTEGER
);
834 integer
->value
= value
;
835 return &integer
->json
;
838 json_int_t
json_integer_value(const json_t
*json
)
840 if(!json_is_integer(json
))
843 return json_to_integer(json
)->value
;
846 int json_integer_set(json_t
*json
, json_int_t value
)
848 if(!json_is_integer(json
))
851 json_to_integer(json
)->value
= value
;
856 static void json_delete_integer(json_integer_t
*integer
)
861 static int json_integer_equal(const json_t
*integer1
, const json_t
*integer2
)
863 return json_integer_value(integer1
) == json_integer_value(integer2
);
866 static json_t
*json_integer_copy(const json_t
*integer
)
868 return json_integer(json_integer_value(integer
));
874 json_t
*json_real(double value
)
878 if(isnan(value
) || isinf(value
))
881 real
= jsonp_malloc(sizeof(json_real_t
));
884 json_init(&real
->json
, JSON_REAL
);
890 double json_real_value(const json_t
*json
)
892 if(!json_is_real(json
))
895 return json_to_real(json
)->value
;
898 int json_real_set(json_t
*json
, double value
)
900 if(!json_is_real(json
) || isnan(value
) || isinf(value
))
903 json_to_real(json
)->value
= value
;
908 static void json_delete_real(json_real_t
*real
)
913 static int json_real_equal(const json_t
*real1
, const json_t
*real2
)
915 return json_real_value(real1
) == json_real_value(real2
);
918 static json_t
*json_real_copy(const json_t
*real
)
920 return json_real(json_real_value(real
));
926 double json_number_value(const json_t
*json
)
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
);
937 /*** simple values ***/
939 json_t
*json_true(void)
941 static json_t the_true
= {JSON_TRUE
, (size_t)-1};
946 json_t
*json_false(void)
948 static json_t the_false
= {JSON_FALSE
, (size_t)-1};
953 json_t
*json_null(void)
955 static json_t the_null
= {JSON_NULL
, (size_t)-1};
962 void json_delete(json_t
*json
)
967 switch(json_typeof(json
)) {
969 json_delete_object(json_to_object(json
));
972 json_delete_array(json_to_array(json
));
975 json_delete_string(json_to_string(json
));
978 json_delete_integer(json_to_integer(json
));
981 json_delete_real(json_to_real(json
));
987 /* json_delete is not called for true, false or null */
993 int json_equal(const json_t
*json1
, const json_t
*json2
)
998 if(json_typeof(json1
) != json_typeof(json2
))
1001 /* this covers true, false and null as they are singletons */
1005 switch(json_typeof(json1
)) {
1007 return json_object_equal(json1
, json2
);
1009 return json_array_equal(json1
, json2
);
1011 return json_string_equal(json1
, json2
);
1013 return json_integer_equal(json1
, json2
);
1015 return json_real_equal(json1
, json2
);
1024 json_t
*json_copy(json_t
*json
)
1029 switch(json_typeof(json
)) {
1031 return json_object_copy(json
);
1033 return json_array_copy(json
);
1035 return json_string_copy(json
);
1037 return json_integer_copy(json
);
1039 return json_real_copy(json
);
1049 json_t
*json_deep_copy(const json_t
*json
)
1054 switch(json_typeof(json
)) {
1056 return json_object_deep_copy(json
);
1058 return json_array_deep_copy(json
);
1059 /* for the rest of the types, deep copying doesn't differ from
1062 return json_string_copy(json
);
1064 return json_integer_copy(json
);
1066 return json_real_copy(json
);
1070 return (json_t
*)json
;