libyajl2 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +65 -10
- data/ext/libyajl2/api/yajl_common.h +75 -0
- data/ext/libyajl2/api/yajl_gen.h +167 -0
- data/ext/libyajl2/api/yajl_parse.h +226 -0
- data/ext/libyajl2/api/yajl_tree.h +186 -0
- data/ext/libyajl2/api/yajl_version.h +23 -0
- data/ext/libyajl2/extconf.rb +24 -177
- data/ext/libyajl2/patches/000-mingw-gcc.patch +26 -0
- data/ext/libyajl2/yajl/yajl_common.h +75 -0
- data/ext/libyajl2/yajl/yajl_gen.h +167 -0
- data/ext/libyajl2/yajl/yajl_parse.h +226 -0
- data/ext/libyajl2/yajl/yajl_tree.h +186 -0
- data/ext/libyajl2/yajl/yajl_version.h +23 -0
- data/ext/libyajl2/yajl.c +175 -0
- data/ext/libyajl2/yajl_alloc.c +52 -0
- data/ext/libyajl2/yajl_alloc.h +34 -0
- data/ext/libyajl2/yajl_buf.c +103 -0
- data/ext/libyajl2/yajl_buf.h +57 -0
- data/ext/libyajl2/yajl_bytestack.h +69 -0
- data/ext/libyajl2/yajl_encode.c +220 -0
- data/ext/libyajl2/yajl_encode.h +34 -0
- data/ext/libyajl2/yajl_gen.c +362 -0
- data/ext/libyajl2/yajl_lex.c +763 -0
- data/ext/libyajl2/yajl_lex.h +117 -0
- data/ext/libyajl2/yajl_parser.c +498 -0
- data/ext/libyajl2/yajl_parser.h +78 -0
- data/ext/libyajl2/yajl_tree.c +503 -0
- data/ext/libyajl2/yajl_version.c +7 -0
- data/lib/libyajl2/version.rb +1 -1
- metadata +28 -3
- data/ext/libyajl2/Makefile +0 -9
data/ext/libyajl2/yajl.c
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include "api/yajl_parse.h"
|
18
|
+
#include "yajl_lex.h"
|
19
|
+
#include "yajl_parser.h"
|
20
|
+
#include "yajl_alloc.h"
|
21
|
+
|
22
|
+
#include <stdlib.h>
|
23
|
+
#include <string.h>
|
24
|
+
#include <stdarg.h>
|
25
|
+
#include <assert.h>
|
26
|
+
|
27
|
+
const char *
|
28
|
+
yajl_status_to_string(yajl_status stat)
|
29
|
+
{
|
30
|
+
const char * statStr = "unknown";
|
31
|
+
switch (stat) {
|
32
|
+
case yajl_status_ok:
|
33
|
+
statStr = "ok, no error";
|
34
|
+
break;
|
35
|
+
case yajl_status_client_canceled:
|
36
|
+
statStr = "client canceled parse";
|
37
|
+
break;
|
38
|
+
case yajl_status_error:
|
39
|
+
statStr = "parse error";
|
40
|
+
break;
|
41
|
+
}
|
42
|
+
return statStr;
|
43
|
+
}
|
44
|
+
|
45
|
+
yajl_handle
|
46
|
+
yajl_alloc(const yajl_callbacks * callbacks,
|
47
|
+
yajl_alloc_funcs * afs,
|
48
|
+
void * ctx)
|
49
|
+
{
|
50
|
+
yajl_handle hand = NULL;
|
51
|
+
yajl_alloc_funcs afsBuffer;
|
52
|
+
|
53
|
+
/* first order of business is to set up memory allocation routines */
|
54
|
+
if (afs != NULL) {
|
55
|
+
if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
|
56
|
+
{
|
57
|
+
return NULL;
|
58
|
+
}
|
59
|
+
} else {
|
60
|
+
yajl_set_default_alloc_funcs(&afsBuffer);
|
61
|
+
afs = &afsBuffer;
|
62
|
+
}
|
63
|
+
|
64
|
+
hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
|
65
|
+
|
66
|
+
/* copy in pointers to allocation routines */
|
67
|
+
memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
|
68
|
+
|
69
|
+
hand->callbacks = callbacks;
|
70
|
+
hand->ctx = ctx;
|
71
|
+
hand->lexer = NULL;
|
72
|
+
hand->bytesConsumed = 0;
|
73
|
+
hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
|
74
|
+
hand->flags = 0;
|
75
|
+
yajl_bs_init(hand->stateStack, &(hand->alloc));
|
76
|
+
yajl_bs_push(hand->stateStack, yajl_state_start);
|
77
|
+
|
78
|
+
return hand;
|
79
|
+
}
|
80
|
+
|
81
|
+
int
|
82
|
+
yajl_config(yajl_handle h, yajl_option opt, ...)
|
83
|
+
{
|
84
|
+
int rv = 1;
|
85
|
+
va_list ap;
|
86
|
+
va_start(ap, opt);
|
87
|
+
|
88
|
+
switch(opt) {
|
89
|
+
case yajl_allow_comments:
|
90
|
+
case yajl_dont_validate_strings:
|
91
|
+
case yajl_allow_trailing_garbage:
|
92
|
+
case yajl_allow_multiple_values:
|
93
|
+
case yajl_allow_partial_values:
|
94
|
+
if (va_arg(ap, int)) h->flags |= opt;
|
95
|
+
else h->flags &= ~opt;
|
96
|
+
break;
|
97
|
+
default:
|
98
|
+
rv = 0;
|
99
|
+
}
|
100
|
+
va_end(ap);
|
101
|
+
|
102
|
+
return rv;
|
103
|
+
}
|
104
|
+
|
105
|
+
void
|
106
|
+
yajl_free(yajl_handle handle)
|
107
|
+
{
|
108
|
+
yajl_bs_free(handle->stateStack);
|
109
|
+
yajl_buf_free(handle->decodeBuf);
|
110
|
+
if (handle->lexer) {
|
111
|
+
yajl_lex_free(handle->lexer);
|
112
|
+
handle->lexer = NULL;
|
113
|
+
}
|
114
|
+
YA_FREE(&(handle->alloc), handle);
|
115
|
+
}
|
116
|
+
|
117
|
+
yajl_status
|
118
|
+
yajl_parse(yajl_handle hand, const unsigned char * jsonText,
|
119
|
+
size_t jsonTextLen)
|
120
|
+
{
|
121
|
+
yajl_status status;
|
122
|
+
|
123
|
+
/* lazy allocation of the lexer */
|
124
|
+
if (hand->lexer == NULL) {
|
125
|
+
hand->lexer = yajl_lex_alloc(&(hand->alloc),
|
126
|
+
hand->flags & yajl_allow_comments,
|
127
|
+
!(hand->flags & yajl_dont_validate_strings));
|
128
|
+
}
|
129
|
+
|
130
|
+
status = yajl_do_parse(hand, jsonText, jsonTextLen);
|
131
|
+
return status;
|
132
|
+
}
|
133
|
+
|
134
|
+
|
135
|
+
yajl_status
|
136
|
+
yajl_complete_parse(yajl_handle hand)
|
137
|
+
{
|
138
|
+
/* The lexer is lazy allocated in the first call to parse. if parse is
|
139
|
+
* never called, then no data was provided to parse at all. This is a
|
140
|
+
* "premature EOF" error unless yajl_allow_partial_values is specified.
|
141
|
+
* allocating the lexer now is the simplest possible way to handle this
|
142
|
+
* case while preserving all the other semantics of the parser
|
143
|
+
* (multiple values, partial values, etc). */
|
144
|
+
if (hand->lexer == NULL) {
|
145
|
+
hand->lexer = yajl_lex_alloc(&(hand->alloc),
|
146
|
+
hand->flags & yajl_allow_comments,
|
147
|
+
!(hand->flags & yajl_dont_validate_strings));
|
148
|
+
}
|
149
|
+
|
150
|
+
return yajl_do_finish(hand);
|
151
|
+
}
|
152
|
+
|
153
|
+
unsigned char *
|
154
|
+
yajl_get_error(yajl_handle hand, int verbose,
|
155
|
+
const unsigned char * jsonText, size_t jsonTextLen)
|
156
|
+
{
|
157
|
+
return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
|
158
|
+
}
|
159
|
+
|
160
|
+
size_t
|
161
|
+
yajl_get_bytes_consumed(yajl_handle hand)
|
162
|
+
{
|
163
|
+
if (!hand) return 0;
|
164
|
+
else return hand->bytesConsumed;
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
void
|
169
|
+
yajl_free_error(yajl_handle hand, unsigned char * str)
|
170
|
+
{
|
171
|
+
/* use memory allocation functions if set */
|
172
|
+
YA_FREE(&(hand->alloc), str);
|
173
|
+
}
|
174
|
+
|
175
|
+
/* XXX: add utility routines to parse from file */
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/**
|
18
|
+
* \file yajl_alloc.h
|
19
|
+
* default memory allocation routines for yajl which use malloc/realloc and
|
20
|
+
* free
|
21
|
+
*/
|
22
|
+
|
23
|
+
#include "yajl_alloc.h"
|
24
|
+
#include <stdlib.h>
|
25
|
+
|
26
|
+
static void * yajl_internal_malloc(void *ctx, size_t sz)
|
27
|
+
{
|
28
|
+
(void)ctx;
|
29
|
+
return malloc(sz);
|
30
|
+
}
|
31
|
+
|
32
|
+
static void * yajl_internal_realloc(void *ctx, void * previous,
|
33
|
+
size_t sz)
|
34
|
+
{
|
35
|
+
(void)ctx;
|
36
|
+
return realloc(previous, sz);
|
37
|
+
}
|
38
|
+
|
39
|
+
static void yajl_internal_free(void *ctx, void * ptr)
|
40
|
+
{
|
41
|
+
(void)ctx;
|
42
|
+
free(ptr);
|
43
|
+
}
|
44
|
+
|
45
|
+
void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf)
|
46
|
+
{
|
47
|
+
yaf->malloc = yajl_internal_malloc;
|
48
|
+
yaf->free = yajl_internal_free;
|
49
|
+
yaf->realloc = yajl_internal_realloc;
|
50
|
+
yaf->ctx = NULL;
|
51
|
+
}
|
52
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/**
|
18
|
+
* \file yajl_alloc.h
|
19
|
+
* default memory allocation routines for yajl which use malloc/realloc and
|
20
|
+
* free
|
21
|
+
*/
|
22
|
+
|
23
|
+
#ifndef __YAJL_ALLOC_H__
|
24
|
+
#define __YAJL_ALLOC_H__
|
25
|
+
|
26
|
+
#include "api/yajl_common.h"
|
27
|
+
|
28
|
+
#define YA_MALLOC(afs, sz) (afs)->malloc((afs)->ctx, (sz))
|
29
|
+
#define YA_FREE(afs, ptr) (afs)->free((afs)->ctx, (ptr))
|
30
|
+
#define YA_REALLOC(afs, ptr, sz) (afs)->realloc((afs)->ctx, (ptr), (sz))
|
31
|
+
|
32
|
+
void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf);
|
33
|
+
|
34
|
+
#endif
|
@@ -0,0 +1,103 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include "yajl_buf.h"
|
18
|
+
|
19
|
+
#include <assert.h>
|
20
|
+
#include <stdlib.h>
|
21
|
+
#include <string.h>
|
22
|
+
|
23
|
+
#define YAJL_BUF_INIT_SIZE 2048
|
24
|
+
|
25
|
+
struct yajl_buf_t {
|
26
|
+
size_t len;
|
27
|
+
size_t used;
|
28
|
+
unsigned char * data;
|
29
|
+
yajl_alloc_funcs * alloc;
|
30
|
+
};
|
31
|
+
|
32
|
+
static
|
33
|
+
void yajl_buf_ensure_available(yajl_buf buf, size_t want)
|
34
|
+
{
|
35
|
+
size_t need;
|
36
|
+
|
37
|
+
assert(buf != NULL);
|
38
|
+
|
39
|
+
/* first call */
|
40
|
+
if (buf->data == NULL) {
|
41
|
+
buf->len = YAJL_BUF_INIT_SIZE;
|
42
|
+
buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
|
43
|
+
buf->data[0] = 0;
|
44
|
+
}
|
45
|
+
|
46
|
+
need = buf->len;
|
47
|
+
|
48
|
+
while (want >= (need - buf->used)) need <<= 1;
|
49
|
+
|
50
|
+
if (need != buf->len) {
|
51
|
+
buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
|
52
|
+
buf->len = need;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
|
57
|
+
{
|
58
|
+
yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
|
59
|
+
memset((void *) b, 0, sizeof(struct yajl_buf_t));
|
60
|
+
b->alloc = alloc;
|
61
|
+
return b;
|
62
|
+
}
|
63
|
+
|
64
|
+
void yajl_buf_free(yajl_buf buf)
|
65
|
+
{
|
66
|
+
assert(buf != NULL);
|
67
|
+
if (buf->data) YA_FREE(buf->alloc, buf->data);
|
68
|
+
YA_FREE(buf->alloc, buf);
|
69
|
+
}
|
70
|
+
|
71
|
+
void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
|
72
|
+
{
|
73
|
+
yajl_buf_ensure_available(buf, len);
|
74
|
+
if (len > 0) {
|
75
|
+
assert(data != NULL);
|
76
|
+
memcpy(buf->data + buf->used, data, len);
|
77
|
+
buf->used += len;
|
78
|
+
buf->data[buf->used] = 0;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
void yajl_buf_clear(yajl_buf buf)
|
83
|
+
{
|
84
|
+
buf->used = 0;
|
85
|
+
if (buf->data) buf->data[buf->used] = 0;
|
86
|
+
}
|
87
|
+
|
88
|
+
const unsigned char * yajl_buf_data(yajl_buf buf)
|
89
|
+
{
|
90
|
+
return buf->data;
|
91
|
+
}
|
92
|
+
|
93
|
+
size_t yajl_buf_len(yajl_buf buf)
|
94
|
+
{
|
95
|
+
return buf->used;
|
96
|
+
}
|
97
|
+
|
98
|
+
void
|
99
|
+
yajl_buf_truncate(yajl_buf buf, size_t len)
|
100
|
+
{
|
101
|
+
assert(len <= buf->used);
|
102
|
+
buf->used = len;
|
103
|
+
}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#ifndef __YAJL_BUF_H__
|
18
|
+
#define __YAJL_BUF_H__
|
19
|
+
|
20
|
+
#include "api/yajl_common.h"
|
21
|
+
#include "yajl_alloc.h"
|
22
|
+
|
23
|
+
/*
|
24
|
+
* Implementation/performance notes. If this were moved to a header
|
25
|
+
* only implementation using #define's where possible we might be
|
26
|
+
* able to sqeeze a little performance out of the guy by killing function
|
27
|
+
* call overhead. YMMV.
|
28
|
+
*/
|
29
|
+
|
30
|
+
/**
|
31
|
+
* yajl_buf is a buffer with exponential growth. the buffer ensures that
|
32
|
+
* you are always null padded.
|
33
|
+
*/
|
34
|
+
typedef struct yajl_buf_t * yajl_buf;
|
35
|
+
|
36
|
+
/* allocate a new buffer */
|
37
|
+
yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc);
|
38
|
+
|
39
|
+
/* free the buffer */
|
40
|
+
void yajl_buf_free(yajl_buf buf);
|
41
|
+
|
42
|
+
/* append a number of bytes to the buffer */
|
43
|
+
void yajl_buf_append(yajl_buf buf, const void * data, size_t len);
|
44
|
+
|
45
|
+
/* empty the buffer */
|
46
|
+
void yajl_buf_clear(yajl_buf buf);
|
47
|
+
|
48
|
+
/* get a pointer to the beginning of the buffer */
|
49
|
+
const unsigned char * yajl_buf_data(yajl_buf buf);
|
50
|
+
|
51
|
+
/* get the length of the buffer */
|
52
|
+
size_t yajl_buf_len(yajl_buf buf);
|
53
|
+
|
54
|
+
/* truncate the buffer */
|
55
|
+
void yajl_buf_truncate(yajl_buf buf, size_t len);
|
56
|
+
|
57
|
+
#endif
|
@@ -0,0 +1,69 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/*
|
18
|
+
* A header only implementation of a simple stack of bytes, used in YAJL
|
19
|
+
* to maintain parse state.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#ifndef __YAJL_BYTESTACK_H__
|
23
|
+
#define __YAJL_BYTESTACK_H__
|
24
|
+
|
25
|
+
#include "api/yajl_common.h"
|
26
|
+
|
27
|
+
#define YAJL_BS_INC 128
|
28
|
+
|
29
|
+
typedef struct yajl_bytestack_t
|
30
|
+
{
|
31
|
+
unsigned char * stack;
|
32
|
+
size_t size;
|
33
|
+
size_t used;
|
34
|
+
yajl_alloc_funcs * yaf;
|
35
|
+
} yajl_bytestack;
|
36
|
+
|
37
|
+
/* initialize a bytestack */
|
38
|
+
#define yajl_bs_init(obs, _yaf) { \
|
39
|
+
(obs).stack = NULL; \
|
40
|
+
(obs).size = 0; \
|
41
|
+
(obs).used = 0; \
|
42
|
+
(obs).yaf = (_yaf); \
|
43
|
+
} \
|
44
|
+
|
45
|
+
|
46
|
+
/* initialize a bytestack */
|
47
|
+
#define yajl_bs_free(obs) \
|
48
|
+
if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
|
49
|
+
|
50
|
+
#define yajl_bs_current(obs) \
|
51
|
+
(assert((obs).used > 0), (obs).stack[(obs).used - 1])
|
52
|
+
|
53
|
+
#define yajl_bs_push(obs, byte) { \
|
54
|
+
if (((obs).size - (obs).used) == 0) { \
|
55
|
+
(obs).size += YAJL_BS_INC; \
|
56
|
+
(obs).stack = (obs).yaf->realloc((obs).yaf->ctx,\
|
57
|
+
(void *) (obs).stack, (obs).size);\
|
58
|
+
} \
|
59
|
+
(obs).stack[((obs).used)++] = (byte); \
|
60
|
+
}
|
61
|
+
|
62
|
+
/* removes the top item of the stack, returns nothing */
|
63
|
+
#define yajl_bs_pop(obs) { ((obs).used)--; }
|
64
|
+
|
65
|
+
#define yajl_bs_set(obs, byte) \
|
66
|
+
(obs).stack[((obs).used) - 1] = (byte);
|
67
|
+
|
68
|
+
|
69
|
+
#endif
|