xref: /5.5.2/couchdb/src/ejson/yajl/yajl.c (revision 3925e856)
1/*
2 * Copyright 2010, Lloyd Hilaiel.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *  1. Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 *
11 *  2. Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in
13 *     the documentation and/or other materials provided with the
14 *     distribution.
15 *
16 *  3. Neither the name of Lloyd Hilaiel nor the names of its
17 *     contributors may be used to endorse or promote products derived
18 *     from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "yajl_parse.h"
34#include "yajl_lex.h"
35#include "yajl_parser.h"
36#include "yajl_alloc.h"
37
38#include <stdlib.h>
39#include <string.h>
40#include <assert.h>
41
42const char *
43yajl_status_to_string(yajl_status stat)
44{
45    const char * statStr = "unknown";
46    switch (stat) {
47        case yajl_status_ok:
48            statStr = "ok, no error";
49            break;
50        case yajl_status_client_canceled:
51            statStr = "client canceled parse";
52            break;
53        case yajl_status_insufficient_data:
54            statStr = "eof was met before the parse could complete";
55            break;
56        case yajl_status_error:
57            statStr = "parse error";
58            break;
59    }
60    return statStr;
61}
62
63yajl_handle
64yajl_alloc(const yajl_callbacks * callbacks,
65           const yajl_parser_config * config,
66           const yajl_alloc_funcs * afs,
67           void * ctx)
68{
69    unsigned int allowComments = 0;
70    unsigned int validateUTF8 = 0;
71    yajl_handle hand = NULL;
72    yajl_alloc_funcs afsBuffer;
73
74    /* first order of business is to set up memory allocation routines */
75    if (afs != NULL) {
76        if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
77        {
78            return NULL;
79        }
80    } else {
81        yajl_set_default_alloc_funcs(&afsBuffer);
82        afs = &afsBuffer;
83    }
84
85    hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
86
87    /* copy in pointers to allocation routines */
88    memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
89
90    if (config != NULL) {
91        allowComments = config->allowComments;
92        validateUTF8 = config->checkUTF8;
93    }
94
95    hand->callbacks = callbacks;
96    hand->ctx = ctx;
97    hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8);
98    hand->bytesConsumed = 0;
99    hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
100    yajl_bs_init(hand->stateStack, &(hand->alloc));
101
102    yajl_bs_push(hand->stateStack, yajl_state_start);
103
104    return hand;
105}
106
107void
108yajl_free(yajl_handle handle)
109{
110    yajl_bs_free(handle->stateStack);
111    yajl_buf_free(handle->decodeBuf);
112    yajl_lex_free(handle->lexer);
113    YA_FREE(&(handle->alloc), handle);
114}
115
116yajl_status
117yajl_parse(yajl_handle hand, const unsigned char * jsonText,
118           unsigned int jsonTextLen)
119{
120    yajl_status status;
121    status = yajl_do_parse(hand, jsonText, jsonTextLen);
122    return status;
123}
124
125yajl_status
126yajl_parse_complete(yajl_handle hand)
127{
128    /* The particular case we want to handle is a trailing number.
129     * Further input consisting of digits could cause our interpretation
130     * of the number to change (buffered "1" but "2" comes in).
131     * A very simple approach to this is to inject whitespace to terminate
132     * any number in the lex buffer.
133     */
134    return yajl_parse(hand, (const unsigned char *)" ", 1);
135}
136
137unsigned char *
138yajl_get_error(yajl_handle hand, int verbose,
139               const unsigned char * jsonText, unsigned int jsonTextLen)
140{
141    return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
142}
143
144unsigned int
145yajl_get_bytes_consumed(yajl_handle hand)
146{
147    if (!hand) return 0;
148    else return hand->bytesConsumed;
149}
150
151
152void
153yajl_free_error(yajl_handle hand, unsigned char * str)
154{
155    /* use memory allocation functions if set */
156    YA_FREE(&(hand->alloc), str);
157}
158
159/* XXX: add utility routines to parse from file */
160