oj 2.0.14 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

@@ -0,0 +1,103 @@
1
+ /* buf.h
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ *
11
+ * - Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * - Neither the name of Peter Ohler nor the names of its contributors may be
16
+ * used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #ifndef __OJ_BUF_H__
32
+ #define __OJ_BUF_H__
33
+
34
+ #include "ruby.h"
35
+
36
+ typedef struct _Buf {
37
+ char base[1024];
38
+ char *head;
39
+ char *end;
40
+ char *tail;
41
+ } *Buf;
42
+
43
+ inline static void
44
+ buf_init(Buf buf) {
45
+ buf->head = buf->base;
46
+ buf->end = buf->base + sizeof(buf->base);
47
+ buf->tail = buf->head;
48
+ }
49
+
50
+ inline static void
51
+ buf_cleanup(Buf buf) {
52
+ if (buf->base != buf->head) {
53
+ xfree(buf->head);
54
+ }
55
+ }
56
+
57
+ inline static size_t
58
+ buf_len(Buf buf) {
59
+ return buf->tail - buf->head;
60
+ }
61
+
62
+ inline static void
63
+ buf_append_string(Buf buf, const char *s, size_t slen) {
64
+ if (buf->end <= buf->tail + slen) {
65
+ size_t len = buf->end - buf->head;
66
+ size_t toff = buf->tail - buf->head;
67
+ size_t new_len = len + slen + len / 2;
68
+
69
+ if (buf->base == buf->head) {
70
+ buf->head = ALLOC_N(char, new_len);
71
+ memcpy(buf->head, buf->base, len);
72
+ } else {
73
+ REALLOC_N(buf->head, char, new_len);
74
+ }
75
+ buf->tail = buf->head + toff;
76
+ buf->end = buf->head + new_len;
77
+ }
78
+ memcpy(buf->tail, s, slen);
79
+ buf->tail += slen;
80
+ }
81
+
82
+ inline static void
83
+ buf_append(Buf buf, char c) {
84
+ if (buf->end <= buf->tail) {
85
+ size_t len = buf->end - buf->head;
86
+ size_t toff = buf->tail - buf->head;
87
+ size_t new_len = len + len / 2;
88
+
89
+ if (buf->base == buf->head) {
90
+ buf->head = ALLOC_N(char, new_len);
91
+ memcpy(buf->head, buf->base, len);
92
+ } else {
93
+ REALLOC_N(buf->head, char, new_len);
94
+ }
95
+ buf->tail = buf->head + toff;
96
+ buf->end = buf->head + new_len;
97
+ }
98
+ *buf->tail = c;
99
+ buf->tail++;
100
+ *buf->tail = '\0'; // TBD temp for debugging
101
+ }
102
+
103
+ #endif /* __OJ_BUF_H__ */
@@ -0,0 +1,93 @@
1
+ /* circarray.c
2
+ * Copyright (c) 2012, Peter Ohler
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ *
11
+ * - Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * - Neither the name of Peter Ohler nor the names of its contributors may be
16
+ * used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #include "circarray.h"
32
+
33
+ CircArray
34
+ oj_circ_array_new() {
35
+ CircArray ca;
36
+
37
+ if (0 == (ca = ALLOC(struct _CircArray))) {
38
+ rb_raise(rb_eNoMemError, "not enough memory\n");
39
+ }
40
+ ca->objs = ca->obj_array;
41
+ ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
42
+ ca->cnt = 0;
43
+
44
+ return ca;
45
+ }
46
+
47
+ void
48
+ oj_circ_array_free(CircArray ca) {
49
+ if (ca->objs != ca->obj_array) {
50
+ xfree(ca->objs);
51
+ }
52
+ xfree(ca);
53
+ }
54
+
55
+ void
56
+ oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
57
+ if (0 < id && 0 != ca) {
58
+ unsigned long i;
59
+
60
+ if (ca->size < id) {
61
+ unsigned long cnt = id + 512;
62
+
63
+ if (ca->objs == ca->obj_array) {
64
+ if (0 == (ca->objs = ALLOC_N(VALUE, cnt))) {
65
+ rb_raise(rb_eNoMemError, "not enough memory\n");
66
+ }
67
+ memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
68
+ } else {
69
+ REALLOC_N(ca->objs, VALUE, cnt);
70
+ }
71
+ ca->size = cnt;
72
+ }
73
+ id--;
74
+ for (i = ca->cnt; i < id; i++) {
75
+ ca->objs[i] = Qnil;
76
+ }
77
+ ca->objs[id] = obj;
78
+ if (ca->cnt <= id) {
79
+ ca->cnt = id + 1;
80
+ }
81
+ }
82
+ }
83
+
84
+ VALUE
85
+ oj_circ_array_get(CircArray ca, unsigned long id) {
86
+ VALUE obj = Qnil;
87
+
88
+ if (id <= ca->cnt && 0 != ca) {
89
+ obj = ca->objs[id - 1];
90
+ }
91
+ return obj;
92
+ }
93
+
@@ -0,0 +1,48 @@
1
+ /* circarray.h
2
+ * Copyright (c) 2012, Peter Ohler
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ *
11
+ * - Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * - Neither the name of Peter Ohler nor the names of its contributors may be
16
+ * used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #ifndef __OJ_CIRCARRAY_H__
32
+ #define __OJ_CIRCARRAY_H__
33
+
34
+ #include "ruby.h"
35
+
36
+ typedef struct _CircArray {
37
+ VALUE obj_array[1024];
38
+ VALUE *objs;
39
+ unsigned long size; // allocated size or initial array size
40
+ unsigned long cnt;
41
+ } *CircArray;
42
+
43
+ extern CircArray oj_circ_array_new(void);
44
+ extern void oj_circ_array_free(CircArray ca);
45
+ extern void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id);
46
+ extern VALUE oj_circ_array_get(CircArray ca, unsigned long id);
47
+
48
+ #endif /* __OJ_CIRCARRAY_H__ */
@@ -0,0 +1,112 @@
1
+ /* compat.c
2
+ * Copyright (c) 2012, Peter Ohler
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ *
11
+ * - Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * - Neither the name of Peter Ohler nor the names of its contributors may be
16
+ * used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #include <stdio.h>
32
+
33
+ #include "oj.h"
34
+ #include "err.h"
35
+ #include "parse.h"
36
+ #include "resolve.h"
37
+
38
+ static void
39
+ hash_set_cstr(ParseInfo pi, const char *key, size_t klen, const char *str, size_t len, const char *orig) {
40
+ Val parent = stack_peek(&pi->stack);
41
+
42
+ if (0 != pi->options.create_id &&
43
+ *pi->options.create_id == *key &&
44
+ pi->options.create_id_len == klen &&
45
+ 0 == strncmp(pi->options.create_id, key, klen)) {
46
+ if (str < pi->json || pi->cur < str) {
47
+ parent->classname = strndup(str, len);
48
+ } else {
49
+ parent->classname = str;
50
+ }
51
+ parent->clen = len;
52
+ } else {
53
+ VALUE rstr = rb_str_new(str, len);
54
+ VALUE rkey = rb_str_new(key, klen);
55
+
56
+ #if HAS_ENCODING_SUPPORT
57
+ rb_enc_associate(rstr, oj_utf8_encoding);
58
+ rb_enc_associate(rkey, oj_utf8_encoding);
59
+ #endif
60
+ if (Yes == pi->options.sym_key) {
61
+ rkey = rb_str_intern(rkey);
62
+ }
63
+ rb_hash_aset(parent->val, rkey, rstr);
64
+ }
65
+ }
66
+
67
+ static void
68
+ end_hash(struct _ParseInfo *pi) {
69
+ Val parent = stack_peek(&pi->stack);
70
+
71
+ if (0 != parent->classname) {
72
+ VALUE clas;
73
+
74
+ clas = oj_name2class(pi, parent->classname, parent->clen, 0);
75
+ if (Qundef != clas) { // else an error
76
+ parent->val = rb_funcall(clas, oj_json_create_id, 1, parent->val);
77
+ }
78
+ if (parent->classname < pi->json || pi->cur < parent->classname) {
79
+ xfree((char*)parent->classname);
80
+ parent->classname = 0;
81
+ }
82
+ }
83
+ }
84
+
85
+ void
86
+ oj_set_compat_callbacks(ParseInfo pi) {
87
+ oj_set_strict_callbacks(pi);
88
+ pi->end_hash = end_hash;
89
+ pi->hash_set_cstr = hash_set_cstr;
90
+ }
91
+
92
+ VALUE
93
+ oj_compat_parse(int argc, VALUE *argv, VALUE self) {
94
+ struct _ParseInfo pi;
95
+
96
+ pi.options = oj_default_options;
97
+ oj_set_compat_callbacks(&pi);
98
+
99
+ return oj_pi_parse(argc, argv, &pi, 0);
100
+ }
101
+
102
+ VALUE
103
+ oj_compat_parse_cstr(int argc, VALUE *argv, char *json) {
104
+ struct _ParseInfo pi;
105
+
106
+ pi.options = oj_default_options;
107
+ oj_set_strict_callbacks(&pi);
108
+ pi.end_hash = end_hash;
109
+ pi.hash_set_cstr = hash_set_cstr;
110
+
111
+ return oj_pi_parse(argc, argv, &pi, json);
112
+ }
@@ -40,6 +40,7 @@
40
40
 
41
41
  #include "oj.h"
42
42
  #include "cache8.h"
43
+ #include "odd.h"
43
44
 
44
45
  #if !HAS_ENCODING_SUPPORT || defined(RUBINIUS_RUBY)
45
46
  #define rb_eEncodingError rb_eException
@@ -1304,7 +1305,7 @@ dump_attr_cb(ID key, VALUE value, Out out) {
1304
1305
 
1305
1306
  static void
1306
1307
  dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
1307
- size_t size;
1308
+ size_t size = 0;
1308
1309
  int d2 = depth + 1;
1309
1310
 
1310
1311
  if (out->end - out->cur <= 2) {
@@ -0,0 +1,82 @@
1
+ /* err.c
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ *
11
+ * - Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * - Neither the name of Peter Ohler nor the names of its contributors may be
16
+ * used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #include <stdarg.h>
32
+
33
+ #include "err.h"
34
+
35
+ void
36
+ oj_err_set(Err e, VALUE clas, const char *format, ...) {
37
+ va_list ap;
38
+
39
+ va_start(ap, format);
40
+ e->clas = clas;
41
+ vsnprintf(e->msg, sizeof(e->msg) - 1, format, ap);
42
+ va_end(ap);
43
+ }
44
+
45
+ void
46
+ oj_err_raise(Err e) {
47
+ rb_raise(e->clas, "%s", e->msg);
48
+ }
49
+
50
+ void
51
+ _oj_err_set_with_location(Err err, VALUE eclas, const char *msg, const char *json, const char *current, const char* file, int line) {
52
+ int n = 1;
53
+ int col = 1;
54
+
55
+ for (; json < current && '\n' != *current; current--) {
56
+ col++;
57
+ }
58
+ for (; json < current; current--) {
59
+ if ('\n' == *current) {
60
+ n++;
61
+ }
62
+ }
63
+ oj_err_set(err, eclas, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
64
+ }
65
+
66
+ void
67
+ _oj_raise_error(const char *msg, const char *json, const char *current, const char* file, int line) {
68
+ struct _Err err;
69
+ int n = 1;
70
+ int col = 1;
71
+
72
+ for (; json < current && '\n' != *current; current--) {
73
+ col++;
74
+ }
75
+ for (; json < current; current--) {
76
+ if ('\n' == *current) {
77
+ n++;
78
+ }
79
+ }
80
+ oj_err_set(&err, oj_parse_error_class, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
81
+ rb_raise(err.clas, "%s", err.msg);
82
+ }