yaji 0.2.3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ /*
2
+ * Author:: Couchbase <info@couchbase.com>
3
+ * Copyright:: 2011 Couchbase, Inc.
4
+ * License:: Apache License, Version 2.0
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "api/yajl_parse.h"
20
+
21
+ #include <ruby.h>
22
+
23
+ #ifdef HAVE_RUBY_ENCODING_H
24
+ #include <ruby/encoding.h>
25
+ static rb_encoding *utf8_encoding;
26
+ #endif
27
+
28
+ /* Older versions of Ruby (< 1.8.6) need these */
29
+ #ifndef RSTRING_PTR
30
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
31
+ #endif
32
+ #ifndef RSTRING_LEN
33
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
34
+ #endif
35
+ #ifndef RARRAY_PTR
36
+ #define RARRAY_PTR(s) (RARRAY(s)->ptr)
37
+ #endif
38
+ #ifndef RARRAY_LEN
39
+ #define RARRAY_LEN(s) (RARRAY(s)->len)
40
+ #endif
41
+
42
+ static VALUE m_yaji, c_yaji_parser, c_parse_error, c_stringio;
43
+
44
+ static ID id_call, id_read, id_parse, id_perform, id_on_body, id_bytesize, id_strip;
45
+ static ID sym_allow_comments, sym_check_utf8, sym_symbolize_keys, sym_with_path,
46
+ sym_read_buffer_size, sym_null, sym_boolean, sym_number, sym_string,
47
+ sym_hash_key, sym_start_hash, sym_end_hash, sym_start_array,
48
+ sym_end_array;
49
+
50
+ static int yaji_null(void *ctx);
51
+ static int yaji_boolean(void *ctx, int val);
52
+ static int yaji_number(void *ctx, const char *val, unsigned int len);
53
+ static int yaji_string(void *ctx, const unsigned char *val, unsigned int len);
54
+ static int yaji_hash_key(void *ctx, const unsigned char *val, unsigned int len);
55
+ static int yaji_start_hash(void *ctx);
56
+ static int yaji_end_hash(void *ctx);
57
+ static int yaji_start_array(void *ctx);
58
+ static int yaji_end_array(void *ctx);
59
+
60
+ static yajl_callbacks yaji_callbacks = {
61
+ yaji_null,
62
+ yaji_boolean,
63
+ NULL,
64
+ NULL,
65
+ yaji_number,
66
+ yaji_string,
67
+ yaji_start_hash,
68
+ yaji_hash_key,
69
+ yaji_end_hash,
70
+ yaji_start_array,
71
+ yaji_end_array
72
+ };
73
+
74
+ typedef struct {
75
+ int symbolize_keys;
76
+ int key_in_use;
77
+ VALUE input;
78
+ VALUE rbufsize;
79
+ VALUE events;
80
+ VALUE path;
81
+ VALUE path_str;
82
+ VALUE parser_cb;
83
+ VALUE chunk;
84
+ yajl_handle handle;
85
+ yajl_parser_config config;
86
+ } yaji_parser;
87
+
88
+ static VALUE rb_yaji_parser_new(int argc, VALUE *argv, VALUE klass);
89
+ static VALUE rb_yaji_parser_init(int argc, VALUE *argv, VALUE self);
90
+ static VALUE rb_yaji_parser_parse(int argc, VALUE *argv, VALUE self);
91
+ static void rb_yaji_parser_free(void *parser);
92
+ static void rb_yaji_parser_mark(void *parser);
93
+
94
+ void Init_parser_ext();
data/ext/yaji/yajl.c ADDED
@@ -0,0 +1,159 @@
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 "api/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
+
42
+ const char *
43
+ yajl_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
+
63
+ yajl_handle
64
+ yajl_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
+
107
+ void
108
+ yajl_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
+
116
+ yajl_status
117
+ yajl_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
+
125
+ yajl_status
126
+ yajl_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
+
137
+ unsigned char *
138
+ yajl_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
+
144
+ unsigned int
145
+ yajl_get_bytes_consumed(yajl_handle hand)
146
+ {
147
+ if (!hand) return 0;
148
+ else return hand->bytesConsumed;
149
+ }
150
+
151
+
152
+ void
153
+ yajl_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 */
@@ -0,0 +1,65 @@
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
+ /**
34
+ * \file yajl_alloc.h
35
+ * default memory allocation routines for yajl which use malloc/realloc and
36
+ * free
37
+ */
38
+
39
+ #include "yajl_alloc.h"
40
+ #include <stdlib.h>
41
+
42
+ static void * yajl_internal_malloc(void *ctx, unsigned int sz)
43
+ {
44
+ return malloc(sz);
45
+ }
46
+
47
+ static void * yajl_internal_realloc(void *ctx, void * previous,
48
+ unsigned int sz)
49
+ {
50
+ return realloc(previous, sz);
51
+ }
52
+
53
+ static void yajl_internal_free(void *ctx, void * ptr)
54
+ {
55
+ free(ptr);
56
+ }
57
+
58
+ void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf)
59
+ {
60
+ yaf->malloc = yajl_internal_malloc;
61
+ yaf->free = yajl_internal_free;
62
+ yaf->realloc = yajl_internal_realloc;
63
+ yaf->ctx = NULL;
64
+ }
65
+
@@ -0,0 +1,51 @@
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
+ /**
34
+ * \file yajl_alloc.h
35
+ * default memory allocation routines for yajl which use malloc/realloc and
36
+ * free
37
+ */
38
+
39
+ #ifndef __YAJL_ALLOC_H__
40
+ #define __YAJL_ALLOC_H__
41
+
42
+ #include "api/yajl_common.h"
43
+
44
+ #define YA_MALLOC(afs, sz) (afs)->malloc((afs)->ctx, (sz))
45
+ #define YA_FREE(afs, ptr) (afs)->free((afs)->ctx, (ptr))
46
+ #define YA_REALLOC(afs, ptr, sz) (afs)->realloc((afs)->ctx, (ptr), (sz))
47
+
48
+ YAJL_API
49
+ void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf);
50
+
51
+ #endif
@@ -0,0 +1,119 @@
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_buf.h"
34
+
35
+ #include <assert.h>
36
+ #include <stdlib.h>
37
+ #include <string.h>
38
+
39
+ #define YAJL_BUF_INIT_SIZE 2048
40
+
41
+ struct yajl_buf_t {
42
+ unsigned int len;
43
+ unsigned int used;
44
+ unsigned char * data;
45
+ yajl_alloc_funcs * alloc;
46
+ };
47
+
48
+ static
49
+ void yajl_buf_ensure_available(yajl_buf buf, unsigned int want)
50
+ {
51
+ unsigned int need;
52
+
53
+ assert(buf != NULL);
54
+
55
+ /* first call */
56
+ if (buf->data == NULL) {
57
+ buf->len = YAJL_BUF_INIT_SIZE;
58
+ buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
59
+ buf->data[0] = 0;
60
+ }
61
+
62
+ need = buf->len;
63
+
64
+ while (want >= (need - buf->used)) need <<= 1;
65
+
66
+ if (need != buf->len) {
67
+ buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
68
+ buf->len = need;
69
+ }
70
+ }
71
+
72
+ yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
73
+ {
74
+ yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
75
+ memset((void *) b, 0, sizeof(struct yajl_buf_t));
76
+ b->alloc = alloc;
77
+ return b;
78
+ }
79
+
80
+ void yajl_buf_free(yajl_buf buf)
81
+ {
82
+ assert(buf != NULL);
83
+ if (buf->data) YA_FREE(buf->alloc, buf->data);
84
+ YA_FREE(buf->alloc, buf);
85
+ }
86
+
87
+ void yajl_buf_append(yajl_buf buf, const void * data, unsigned int len)
88
+ {
89
+ yajl_buf_ensure_available(buf, len);
90
+ if (len > 0) {
91
+ assert(data != NULL);
92
+ memcpy(buf->data + buf->used, data, len);
93
+ buf->used += len;
94
+ buf->data[buf->used] = 0;
95
+ }
96
+ }
97
+
98
+ void yajl_buf_clear(yajl_buf buf)
99
+ {
100
+ buf->used = 0;
101
+ if (buf->data) buf->data[buf->used] = 0;
102
+ }
103
+
104
+ const unsigned char * yajl_buf_data(yajl_buf buf)
105
+ {
106
+ return buf->data;
107
+ }
108
+
109
+ unsigned int yajl_buf_len(yajl_buf buf)
110
+ {
111
+ return buf->used;
112
+ }
113
+
114
+ void
115
+ yajl_buf_truncate(yajl_buf buf, unsigned int len)
116
+ {
117
+ assert(len <= buf->used);
118
+ buf->used = len;
119
+ }