summaryrefslogtreecommitdiffstats
path: root/src/util/support/json.c
blob: d23267c174016b85810c6e9336738761d106a767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/support/json.c - JSON parser and unparser */
/*
 * Copyright (c) 2010 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * Copyright (C) 2012 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This file implements a minimal dynamic type system for JSON values and a
 * JSON encoder and decoder.  It is loosely based on the heimbase code from
 * Heimdal.
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <k5-base64.h>
#include <k5-json.h>
#include <k5-buf.h>

#define MAX_DECODE_DEPTH 64
#define MIN_ALLOC_SLOT   16

typedef void (*type_dealloc_fn)(void *val);

typedef struct json_type_st {
    k5_json_tid tid;
    const char *name;
    type_dealloc_fn dealloc;
} *json_type;

struct value_base {
    json_type isa;
    unsigned int ref_cnt;
};

#define PTR2BASE(ptr) (((struct value_base *)ptr) - 1)
#define BASE2PTR(ptr) ((void *)(((struct value_base *)ptr) + 1))

k5_json_value
k5_json_retain(k5_json_value val)
{
    struct value_base *p;

    if (val == NULL)
        return val;
    p = PTR2BASE(val);
    assert(p->ref_cnt != 0);
    p->ref_cnt++;
    return val;
}

void
k5_json_release(k5_json_value val)
{
    struct value_base *p;

    if (val == NULL)
        return;
    p = PTR2BASE(val);
    assert(p->ref_cnt != 0);
    p->ref_cnt--;
    if (p->ref_cnt == 0) {
        if (p->isa->dealloc != NULL)
            p->isa->dealloc(val);
        free(p);
    }
}

/* Get the type description of a k5_json_value. */
static json_type
get_isa(k5_json_value val)
{
    struct value_base *p = PTR2BASE(val);

    return p->isa;
}

k5_json_tid
k5_json_get_tid(k5_json_value val)
{
    json_type isa = get_isa(val);

    return isa->tid;
}

static k5_json_value
alloc_value(json_type type, size_t size)
{
    struct value_base *p = calloc(1, size + sizeof(*p));

    if (p == NULL)
        return NULL;
    p->isa = type;
    p->ref_cnt = 1;

    return BASE2PTR(p);
}

/*** Null type ***/

static struct json_type_st null_type = { K5_JSON_TID_NULL, "null", NULL };

int
k5_json_null_create(k5_json_null *val_out)
{
    *val_out = alloc_value(&null_type, 0);
    return (*val_out == NULL) ? ENOMEM : 0;
}

int
k5_json_null_create_val(k5_json_value *val_out)
{
    *val_out = alloc_value(&null_type, 0);
    return (*val_out == NULL) ? ENOMEM : 0;
}

/*** Boolean type ***/

static struct json_type_st bool_type = { K5_JSON_TID_BOOL, "bool", NULL };

int
k5_json_bool_create(int truth, k5_json_bool *val_out)
{
    k5_json_bool b;

    *val_out = NULL;
    b = alloc_value(&bool_type, 1);
    if (b == NULL)
        return ENOMEM;
    *(unsigned char *)b = !!truth;
    *val_out = b;
    return 0;
}

int
k5_json_bool_value(k5_json_bool bval)
{
    return *(unsigned char *)bval;
}

/*** Array type ***/

struct k5_json_array_st {
    k5_json_value *values;
    size_t len;
    size_t allocated;
};

static void
array_dealloc(void *ptr)
{
    k5_json_array array = ptr;
    size_t i;

    for (i = 0; i < array->len; i++)
        k5_json_release(array->values[i]);
    free(array->values);
}

static struct json_type_st array_type = {
    K5_JSON_TID_ARRAY, "array", array_dealloc
};

int
k5_json_array_create(k5_json_array *val_out)
{
    *val_out = alloc_value(&array_type, sizeof(struct k5_json_array_st));
    return (*val_out == NULL) ? ENOMEM : 0;
}

int
k5_json_array_add(k5_json_array array, k5_json_value val)
{
    k5_json_value *ptr;
    size_t new_alloc;

    if (array->len >= array->allocated) {
        /* Increase the number of slots by 50% (MIN_ALLOC_SLOT minimum). */
        new_alloc = array->len + 1 + (array->len >> 1);
        if (new_alloc < MIN_ALLOC_SLOT)
            new_alloc = MIN_ALLOC_SLOT;
        ptr = realloc(array->values, new_alloc * sizeof(*array->values));
        if (ptr == NULL)
            return ENOMEM;
        array->values = ptr;
        array->allocated = new_alloc;
    }
    array->values[array->len++] = k5_json_retain(val);
    return 0;
}

size_t
k5_json_array_length(k5_json_array array)
{
    return array->len;
}

k5_json_value
k5_json_array_get(k5_json_array array, size_t idx)
{
    if (idx >= array->len)
        abort();
    return array->values[idx];
}

void
k5_json_array_set(k5_json_array array, size_t idx, k5_json_value val)
{
    if (idx >= array->len)
        abort();
    k5_json_release(array->values[idx]);
    array->values[idx] = k5_json_retain(val);
}

int
k5_json_array_fmt(k5_json_array *array_out, const char *template, ...)
{
    const char *p;
    va_list ap;
    const char *cstring;
    unsigned char *data;
    size_t len;
    long long nval;
    k5_json_array array;
    k5_json_value val;
    k5_json_number num;
    k5_json_string str;
    k5_json_bool b;
    k5_json_null null;
    int truth, ret;

    *array_out = NULL;
    if (k5_json_array_create(&array))
        return ENOMEM;
    va_start(ap, template);
    for (p = template; *p != '\0'; p++) {
        switch (*p) {
        case 'v':
            val = k5_json_retain(va_arg(ap, k5_json_value));
            break;
        case 'n':
            if (k5_json_null_create(&null))
                goto err;
            val = null;
            break;
        case 'b':
            truth = va_arg(ap, int);
            if (k5_json_bool_create(truth, &b))
                goto err;
            val = b;
            break;
        case 'i':
            nval = va_arg(ap, int);
            if (k5_json_number_create(nval, &num))
                goto err;
            val = num;
            break;
        case 'L':
            nval = va_arg(ap, long long);
            if (k5_json_number_create(nval, &num))
                goto err;
            val = num;
            break;
        case 's':
            cstring = va_arg(ap, const char *);
            if (cstring == NULL) {
                if (k5_json_null_create(&null))
                    goto err;
                val = null;
            } else {
                if (k5_json_string_create(cstring, &str))
                    goto err;
                val = str;
            }
            break;
        case 'B':
            data = va_arg(ap, unsigned char *);
            len = va_arg(ap, size_t);
            if (k5_json_string_create_base64(data, len, &str))
                goto err;
            val = str;
            break;
        default:
            goto err;
        }
        ret = k5_json_array_add(array, val);
        k5_json_release(val);
        if (ret)
            goto err;
    }
    va_end(ap);
    *array_out = array;
    return 0;

err:
    va_end(ap);
    k5_json_release(array);
    return ENOMEM;
}

/*** Object type (string:value mapping) ***/

struct entry {
    char *key;
    k5_json_value value;
};

struct k5_json_object_st {
    struct entry *entries;
    size_t len;
    size_t allocated;
};

static void
object_dealloc(void *ptr)
{
    k5_json_object obj = ptr;
    size_t i;

    for (i = 0; i < obj->len; i++) {
        free(obj->entries[i].key);
        k5_json_release(obj->entries[i].value);
    }
    free(obj->entries);
}

static struct json_type_st object_type = {
    K5_JSON_TID_OBJECT, "object", object_dealloc
};

int
k5_json_object_create(k5_json_object *val_out)
{
    *val_out = alloc_value(&object_type, sizeof(struct k5_json_object_st));
    return (*val_out == NULL) ? ENOMEM : 0;
}

size_t
k5_json_object_count(k5_json_object obj)
{
    return obj->len;
}

/* Return the entry for key within obj, or NULL if none exists. */
static struct entry *
object_search(k5_json_object obj, const char *key)
{
    size_t i;

    for (i = 0; i < obj->len; i++) {
        if (strcmp(key, obj->entries[i].key) == 0)
            return &obj->entries[i];
    }
    return NULL;
}

k5_json_value
k5_json_object_get(k5_json_object obj, const char *key)
{
    struct entry *ent;

    ent = object_search(obj, key);
    if (ent == NULL)
        return NULL;
    return ent->value;
}

int
k5_json_object_set(k5_json_object obj, const char *key, k5_json_value val)
{
    struct entry *ent, *ptr;
    size_t new_alloc, i;

    ent = object_search(obj, key);
    if (ent != NULL) {
        k5_json_release(ent->value);
        if (val == NULL) {
            /* Remove this key. */
            free(ent->key);
            for (i = ent - obj->entries; i < obj->len - 1; i++)
                obj->entries[i] = obj->entries[i + 1];
            obj->len--;
        } else {
            /* Overwrite this key's value with the new one. */
            ent->value = k5_json_retain(val);
        }
        return 0;
    }

    /* If didn't find a key the caller asked to remove, do nothing. */
    if (val == NULL)
        return 0;

    if (obj->len >= obj->allocated) {
        /* Increase the number of slots by 50% (MIN_ALLOC_SLOT minimum). */
        new_alloc = obj->len + 1 + (obj->len >> 1);
        if (new_alloc < MIN_ALLOC_SLOT)
            new_alloc = MIN_ALLOC_SLOT;
        ptr = realloc(obj->entries, new_alloc * sizeof(*obj->entries));
        if (ptr == NULL)
            return ENOMEM;
        obj->entries = ptr;
        obj->allocated = new_alloc;
    }
    obj->entries[obj->len].key = strdup(key);
    if (obj->entries[obj->len].key == NULL)
        return ENOMEM;
    obj->entries[obj->len].value = k5_json_retain(val);
    obj->len++;
    return 0;
}

void
k5_json_object_iterate(k5_json_object obj, k5_json_object_iterator_fn func,
                       void *arg)
{
    size_t i;

    for (i = 0; i < obj->len; i++)
        func(arg, obj->entries[i].key, obj->entries[i].value);
}

/*** String type ***/

static struct json_type_st string_type = {
    K5_JSON_TID_STRING, "string", NULL
};

int
k5_json_string_create(const char *cstring, k5_json_string *val_out)
{
    return k5_json_string_create_len(cstring, strlen(cstring), val_out);
}

int
k5_json_string_create_len(const void *data, size_t len,
                          k5_json_string *val_out)
{
    char *s;

    *val_out = NULL;
    s = alloc_value(&string_type, len + 1);
    if (s == NULL)
        return ENOMEM;
    if (len > 0)
        memcpy(s, data, len);
    s[len] = '\0';
    *val_out = (k5_json_string)s;
    return 0;
}

int
k5_json_string_create_base64(const void *data, size_t len,
                             k5_json_string *val_out)
{
    char *base64;
    int ret;

    *val_out = NULL;
    base64 = k5_base64_encode(data, len);
    if (base64 == NULL)
        return ENOMEM;
    ret = k5_json_string_create(base64, val_out);
    free(base64);
    return ret;
}

const char *
k5_json_string_utf8(k5_json_string string)
{
    return (const char *)string;
}

int
k5_json_string_unbase64(k5_json_string string, unsigned char **data_out,
                        size_t *len_out)
{
    unsigned char *data;
    size_t len;

    *data_out = NULL;
    *len_out = 0;
    data = k5_base64_decode((const char *)string, &len);
    if (data == NULL)
        return (len == 0) ? ENOMEM : EINVAL;
    *data_out = data;
    *len_out = len;
    return 0;
}

/*** Number type ***/

static struct json_type_st number_type = {
    K5_JSON_TID_NUMBER, "number", NULL
};

int
k5_json_number_create(long long number, k5_json_number *val_out)
{
    k5_json_number n;

    *val_out = NULL;
    n = alloc_value(&number_type, sizeof(long long));
    if (n == NULL)
        return ENOMEM;
    *((long long *)n) = number;
    *val_out = n;
    return 0;
}

long long
k5_json_number_value(k5_json_number number)
{
    return *(long long *)number;
}

/*** JSON encoding ***/

static const char quotemap_json[] = "\"\\/bfnrt";
static const char quotemap_c[] = "\"\\/\b\f\n\r\t";
static const char needs_quote[] = "\\\"\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17"
    "\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37";

static int encode_value(struct k5buf *buf, k5_json_value val);

static void
encode_string(struct k5buf *buf, const char *str)
{
    size_t n;
    const char *p;

    k5_buf_add(buf, "\"");
    while (*str != '\0') {
        n = strcspn(str, needs_quote);
        k5_buf_add_len(buf, str, n);
        str += n;
        if (*str == '\0')
            break;
        k5_buf_add(buf, "\\");
        p = strchr(quotemap_c, *str);
        if (p != NULL)
            k5_buf_add_len(buf, quotemap_json + (p - quotemap_c), 1);
        else
            k5_buf_add_fmt(buf, "u00%02X", (unsigned int)*str);
        str++;
    }
    k5_buf_add(buf, "\"");
}

struct obj_ctx {
    struct k5buf *buf;
    int ret;
    int first;
};

static void
encode_obj_entry(void *ctx, const char *key, k5_json_value value)
{
    struct obj_ctx *j = ctx;

    if (j->ret)
        return;
    if (j->first)
        j->first = 0;
    else
        k5_buf_add(j->buf, ",");
    encode_string(j->buf, key);
    k5_buf_add(j->buf, ":");
    j->ret = encode_value(j->buf, value);
}

static int
encode_value(struct k5buf *buf, k5_json_value val)
{
    k5_json_tid type;
    int ret;
    size_t i, len;
    struct obj_ctx ctx;

    if (val == NULL)
        return EINVAL;

    type = k5_json_get_tid(val);
    switch (type) {
    case K5_JSON_TID_ARRAY:
        k5_buf_add(buf, "[");
        len = k5_json_array_length(val);
        for (i = 0; i < len; i++) {
            if (i != 0)
                k5_buf_add(buf, ",");
            ret = encode_value(buf, k5_json_array_get(val, i));
            if (ret)
                return ret;
        }
        k5_buf_add(buf, "]");
        return 0;

    case K5_JSON_TID_OBJECT:
        k5_buf_add(buf, "{");
        ctx.buf = buf;
        ctx.ret = 0;
        ctx.first = 1;
        k5_json_object_iterate(val, encode_obj_entry, &ctx);
        k5_buf_add(buf, "}");
        return ctx.ret;

    case K5_JSON_TID_STRING:
        encode_string(buf, k5_json_string_utf8(val));
        return 0;

    case K5_JSON_TID_NUMBER:
        k5_buf_add_fmt(buf, "%lld", k5_json_number_value(val));
        return 0;

    case K5_JSON_TID_NULL:
        k5_buf_add(buf, "null");
        return 0;

    case K5_JSON_TID_BOOL:
        k5_buf_add(buf, k5_json_bool_value(val) ? "true" : "false");
        return 0;

    default:
        return EINVAL;
    }
}

int
k5_json_encode(k5_json_value val, char **json_out)
{
    struct k5buf buf;
    int ret;

    *json_out = NULL;
    k5_buf_init_dynamic(&buf);
    ret = encode_value(&buf, val);
    if (ret) {
        k5_free_buf(&buf);
        return ret;
    }
    *json_out = k5_buf_data(&buf);
    return (*json_out == NULL) ? ENOMEM : 0;
}

/*** JSON decoding ***/

struct decode_ctx {
    const unsigned char *p;
    size_t depth;
};

static int parse_value(struct decode_ctx *ctx, k5_json_value *val_out);

/* Consume whitespace.  Return 0 if there is anything left to parse after the
 * whitespace, -1 if not. */
static int
white_spaces(struct decode_ctx *ctx)
{
    unsigned char c;

    for (; *ctx->p != '\0'; ctx->p++) {
        c = *ctx->p;
        if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
            return 0;
    }
    return -1;
}

/* Return true if c is a decimal digit. */
static inline int
is_digit(unsigned char c)
{
    return ('0' <= c && c <= '9');
}

/* Return true if c is a hexadecimal digit (per RFC 5234 HEXDIG). */
static inline int
is_hex_digit(unsigned char c)
{
    return is_digit(c) || ('A' <= c && c <= 'F');
}

/* Return the numeric value of a hex digit; aborts if c is not a hex digit. */
static inline unsigned int
hexval(unsigned char c)
{
    if (is_digit(c))
        return c - '0';
    else if ('A' <= c && c <= 'F')
        return c - 'A' + 10;
    abort();
}

/* Parse a JSON number (which must be an integer in the signed 64-bit range; we
 * do not allow floating-point numbers). */
static int
parse_number(struct decode_ctx *ctx, k5_json_number *val_out)
{
    const unsigned long long umax = ~0ULL, smax = (1ULL << 63) - 1;
    unsigned long long number = 0;
    int neg = 1;

    *val_out = NULL;

    if (*ctx->p == '-') {
        neg = -1;
        ctx->p++;
    }

    if (!is_digit(*ctx->p))
        return EINVAL;

    /* Read the number into an unsigned 64-bit container, ensuring that we
     * don't overflow it. */
    while (is_digit(*ctx->p)) {
        if (number + 1 > umax / 10)
            return EOVERFLOW;
        number = (number * 10) + (*ctx->p - '0');
        ctx->p++;
    }

    /* Make sure the unsigned 64-bit value fits in the signed 64-bit range. */
    if (number > smax + 1 || (number > smax && neg == 1))
        return EOVERFLOW;

    return k5_json_number_create(number * neg, val_out);
}

/* Parse a JSON string (which must not quote Unicode code points above 256). */
static int
parse_string(struct decode_ctx *ctx, char **str_out)
{
    const unsigned char *p, *start, *end = NULL;
    const char *q;
    char *buf, *pos;
    unsigned int code;

    *str_out = NULL;

    /* Find the start and end of the string. */
    if (*ctx->p != '"')
        return EINVAL;
    start = ++ctx->p;
    for (; *ctx->p != '\0'; ctx->p++) {
        if (*ctx->p == '\\') {
            ctx->p++;
            if (*ctx->p == '\0')
                return EINVAL;
        } else if (*ctx->p == '"') {
            end = ctx->p++;
            break;
        }
    }
    if (end == NULL)
        return EINVAL;

    pos = buf = malloc(end - start + 1);
    if (buf == NULL)
        return ENOMEM;
    for (p = start; p < end;) {
        if (*p == '\\') {
            p++;
            if (*p == 'u' && is_hex_digit(p[1]) && is_hex_digit(p[2]) &&
                is_hex_digit(p[3]) && is_hex_digit(p[4])) {
                code = (hexval(p[1]) << 12) | (hexval(p[2]) << 8) |
                    (hexval(p[3]) << 4) | hexval(p[4]);
                if (code <= 0xff) {
                    *pos++ = code;
                } else {
                    /* Code points above 0xff don't need to be quoted, so we
                     * don't implement translating those into UTF-8. */
                    free(buf);
                    return EINVAL;
                }
                p += 5;
            } else {
                q = strchr(quotemap_json, *p);
                if (q != NULL) {
                    *pos++ = quotemap_c[q - quotemap_json];
                } else {
                    free(buf);
                    return EINVAL;
                }
                p++;
            }
        } else {
            *pos++ = *p++;
        }
    }
    *pos = '\0';
    *str_out = buf;
    return 0;
}

/* Parse an object association and place it into obj. */
static int
parse_object_association(k5_json_object obj, struct decode_ctx *ctx)
{
    char *key = NULL;
    k5_json_value val;
    int ret;

    /* Parse the key and value. */
    ret = parse_string(ctx, &key);
    if (ret)
        return ret;
    if (white_spaces(ctx))
        goto invalid;
    if (*ctx->p != ':')
        goto invalid;
    ctx->p++;
    if (white_spaces(ctx))
        goto invalid;
    ret = parse_value(ctx, &val);
    if (ret) {
        free(key);
        return ret;
    }

    /* Add the key and value to obj. */
    ret = k5_json_object_set(obj, key, val);
    free(key);
    k5_json_release(val);
    return ret;

invalid:
    free(key);
    return EINVAL;
}

/* Parse a JSON object. */
static int
parse_object(struct decode_ctx *ctx, k5_json_object *val_out)
{
    k5_json_object obj = NULL;
    int ret;

    *val_out = NULL;

    /* Parse past the opening brace. */
    if (*ctx->p != '{')
        return EINVAL;
    ctx->p++;
    if (white_spaces(ctx))
        return EINVAL;

    ret = k5_json_object_create(&obj);
    if (ret)
        return ret;

    /* Pairs associations until we reach the terminating brace. */
    if (*ctx->p != '}') {
        while (1) {
            ret = parse_object_association(obj, ctx);
            if (ret) {
                k5_json_release(obj);
                return ret;
            }
            if (white_spaces(ctx))
                goto invalid;
            if (*ctx->p == '}')
                break;
            if (*ctx->p != ',')
                goto invalid;
            ctx->p++;
            if (white_spaces(ctx))
                goto invalid;
        }
    }
    ctx->p++;
    *val_out = obj;
    return 0;

invalid:
    k5_json_release(obj);
    return EINVAL;
}

/* Parse an value and place it into array. */
static int
parse_array_item(k5_json_array array, struct decode_ctx *ctx)
{
    k5_json_value val;
    int ret;

    ret = parse_value(ctx, &val);
    if (ret)
        return ret;
    ret = k5_json_array_add(array, val);
    k5_json_release(val);
    return ret;
}

/* Parse a JSON array. */
static int
parse_array(struct decode_ctx *ctx, k5_json_array *val_out)
{
    k5_json_array array = NULL;
    int ret;

    *val_out = NULL;

    /* Parse past the opening bracket. */
    if (*ctx->p != '[')
        return EINVAL;
    ctx->p++;
    if (white_spaces(ctx))
        return EINVAL;

    ret = k5_json_array_create(&array);
    if (ret)
        return ret;

    /* Pairs values until we reach the terminating bracket. */
    if (*ctx->p != ']') {
        while (1) {
            ret = parse_array_item(array, ctx);
            if (ret) {
                k5_json_release(array);
                return ret;
            }
            if (white_spaces(ctx))
                goto invalid;
            if (*ctx->p == ']')
                break;
            if (*ctx->p != ',')
                goto invalid;
            ctx->p++;
            if (white_spaces(ctx))
                goto invalid;
        }
    }
    ctx->p++;
    *val_out = array;
    return 0;

invalid:
    k5_json_release(array);
    return EINVAL;
}

/* Parse a JSON value of any type. */
static int
parse_value(struct decode_ctx *ctx, k5_json_value *val_out)
{
    k5_json_null null;
    k5_json_bool bval;
    k5_json_number num;
    k5_json_string str;
    k5_json_object obj;
    k5_json_array array;
    char *cstring;
    int ret;

    *val_out = NULL;

    if (white_spaces(ctx))
        return EINVAL;

    if (*ctx->p == '"') {
        ret = parse_string(ctx, &cstring);
        if (ret)
            return ret;
        ret = k5_json_string_create(cstring, &str);
        free(cstring);
        if (ret)
            return ret;
        *val_out = str;
    } else if (*ctx->p == '{') {
        if (ctx->depth-- == 1)
            return EINVAL;
        ret = parse_object(ctx, &obj);
        if (ret)
            return ret;
        ctx->depth++;
        *val_out = obj;
    } else if (*ctx->p == '[') {
        if (ctx->depth-- == 1)
            return EINVAL;
        ret = parse_array(ctx, &array);
        ctx->depth++;
        *val_out = array;
    } else if (is_digit(*ctx->p) || *ctx->p == '-') {
        ret = parse_number(ctx, &num);
        if (ret)
            return ret;
        *val_out = num;
    } else if (strncmp((char *)ctx->p, "null", 4) == 0) {
        ctx->p += 4;
        ret = k5_json_null_create(&null);
        if (ret)
            return ret;
        *val_out = null;
    } else if (strncmp((char *)ctx->p, "true", 4) == 0) {
        ctx->p += 4;
        ret = k5_json_bool_create(1, &bval);
        if (ret)
            return ret;
        *val_out = bval;
    } else if (strncmp((char *)ctx->p, "false", 5) == 0) {
        ctx->p += 5;
        ret = k5_json_bool_create(0, &bval);
        if (ret)
            return ret;
        *val_out = bval;
    } else {
        return EINVAL;
    }

    return 0;
}

int
k5_json_decode(const char *string, k5_json_value *val_out)
{
    struct decode_ctx ctx;
    k5_json_value val;
    int ret;

    *val_out = NULL;
    ctx.p = (unsigned char *)string;
    ctx.depth = MAX_DECODE_DEPTH;
    ret = parse_value(&ctx, &val);
    if (ret)
        return ret;
    if (white_spaces(&ctx) == 0) {
        k5_json_release(val);
        return EINVAL;
    }
    *val_out = val;
    return 0;
}