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