json 1.2.4 → 1.4.0

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.

Potentially problematic release.


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

@@ -1,180 +0,0 @@
1
- #include "unicode.h"
2
-
3
- #define unicode_escape(buffer, character) \
4
- snprintf(buf, 7, "\\u%04x", (unsigned int) (character)); \
5
- rb_str_buf_cat(buffer, buf, 6);
6
-
7
- /*
8
- * Copyright 2001-2004 Unicode, Inc.
9
- *
10
- * Disclaimer
11
- *
12
- * This source code is provided as is by Unicode, Inc. No claims are
13
- * made as to fitness for any particular purpose. No warranties of any
14
- * kind are expressed or implied. The recipient agrees to determine
15
- * applicability of information provided. If this file has been
16
- * purchased on magnetic or optical media from Unicode, Inc., the
17
- * sole remedy for any claim will be exchange of defective media
18
- * within 90 days of receipt.
19
- *
20
- * Limitations on Rights to Redistribute This Code
21
- *
22
- * Unicode, Inc. hereby grants the right to freely use the information
23
- * supplied in this file in the creation of products supporting the
24
- * Unicode Standard, and to make copies of this file in any form
25
- * for internal or external distribution as long as this notice
26
- * remains attached.
27
- */
28
-
29
- /*
30
- * Index into the table below with the first byte of a UTF-8 sequence to
31
- * get the number of trailing bytes that are supposed to follow it.
32
- * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
33
- * left as-is for anyone who may want to do such conversion, which was
34
- * allowed in earlier algorithms.
35
- */
36
- static const char trailingBytesForUTF8[256] = {
37
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
38
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
39
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
40
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
41
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
42
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
43
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
44
- 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
45
- };
46
-
47
- /*
48
- * Magic values subtracted from a buffer value during UTF8 conversion.
49
- * This table contains as many values as there might be trailing bytes
50
- * in a UTF-8 sequence.
51
- */
52
- static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
53
- 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
54
-
55
- /*
56
- * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
57
- * into the first byte, depending on how many bytes follow. There are
58
- * as many entries in this table as there are UTF-8 sequence types.
59
- * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
60
- * for *legal* UTF-8 will be 4 or fewer bytes total.
61
- */
62
- static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
63
-
64
- /*
65
- * Utility routine to tell whether a sequence of bytes is legal UTF-8.
66
- * This must be called with the length pre-determined by the first byte.
67
- * If not calling this from ConvertUTF8to*, then the length can be set by:
68
- * length = trailingBytesForUTF8[*source]+1;
69
- * and the sequence is illegal right away if there aren't that many bytes
70
- * available.
71
- * If presented with a length > 4, this returns 0. The Unicode
72
- * definition of UTF-8 goes up to 4-byte sequences.
73
- */
74
-
75
- inline static unsigned char isLegalUTF8(const UTF8 *source, int length)
76
- {
77
- UTF8 a;
78
- const UTF8 *srcptr = source+length;
79
- switch (length) {
80
- default: return 0;
81
- /* Everything else falls through when "1"... */
82
- case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
83
- case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
84
- case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
85
-
86
- switch (*source) {
87
- /* no fall-through in this inner switch */
88
- case 0xE0: if (a < 0xA0) return 0; break;
89
- case 0xED: if (a > 0x9F) return 0; break;
90
- case 0xF0: if (a < 0x90) return 0; break;
91
- case 0xF4: if (a > 0x8F) return 0; break;
92
- default: if (a < 0x80) return 0;
93
- }
94
-
95
- case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
96
- }
97
- if (*source > 0xF4) return 0;
98
- return 1;
99
- }
100
-
101
- void JSON_convert_UTF8_to_JSON(VALUE buffer, VALUE string, ConversionFlags flags)
102
- {
103
- char buf[7];
104
- const UTF8* source = (UTF8 *) RSTRING_PTR(string);
105
- const UTF8* sourceEnd = source + RSTRING_LEN(string);
106
-
107
- while (source < sourceEnd) {
108
- UTF32 ch = 0;
109
- unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
110
- if (source + extraBytesToRead >= sourceEnd) {
111
- rb_raise(rb_path2class("JSON::GeneratorError"),
112
- "partial character in source, but hit end");
113
- }
114
- if (!isLegalUTF8(source, extraBytesToRead+1)) {
115
- rb_raise(rb_path2class("JSON::GeneratorError"),
116
- "source sequence is illegal/malformed utf-8");
117
- }
118
- /*
119
- * The cases all fall through. See "Note A" below.
120
- */
121
- switch (extraBytesToRead) {
122
- case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
123
- case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
124
- case 3: ch += *source++; ch <<= 6;
125
- case 2: ch += *source++; ch <<= 6;
126
- case 1: ch += *source++; ch <<= 6;
127
- case 0: ch += *source++;
128
- }
129
- ch -= offsetsFromUTF8[extraBytesToRead];
130
-
131
- if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
132
- /* UTF-16 surrogate values are illegal in UTF-32 */
133
- if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
134
- if (flags == strictConversion) {
135
- source -= (extraBytesToRead+1); /* return to the illegal value itself */
136
- rb_raise(rb_path2class("JSON::GeneratorError"),
137
- "source sequence is illegal/malformed utf-8");
138
- } else {
139
- unicode_escape(buffer, UNI_REPLACEMENT_CHAR);
140
- }
141
- } else {
142
- /* normal case */
143
- if (ch == '"') {
144
- rb_str_buf_cat2(buffer, "\\\"");
145
- } else if (ch == '\\') {
146
- rb_str_buf_cat2(buffer, "\\\\");
147
- } else if (ch >= 0x20 && ch <= 0x7f) {
148
- rb_str_buf_cat(buffer, (char *) source - 1, 1);
149
- } else if (ch == '\n') {
150
- rb_str_buf_cat2(buffer, "\\n");
151
- } else if (ch == '\r') {
152
- rb_str_buf_cat2(buffer, "\\r");
153
- } else if (ch == '\t') {
154
- rb_str_buf_cat2(buffer, "\\t");
155
- } else if (ch == '\f') {
156
- rb_str_buf_cat2(buffer, "\\f");
157
- } else if (ch == '\b') {
158
- rb_str_buf_cat2(buffer, "\\b");
159
- } else if (ch < 0x20) {
160
- unicode_escape(buffer, (UTF16) ch);
161
- } else {
162
- unicode_escape(buffer, (UTF16) ch);
163
- }
164
- }
165
- } else if (ch > UNI_MAX_UTF16) {
166
- if (flags == strictConversion) {
167
- source -= (extraBytesToRead+1); /* return to the start */
168
- rb_raise(rb_path2class("JSON::GeneratorError"),
169
- "source sequence is illegal/malformed utf8");
170
- } else {
171
- unicode_escape(buffer, UNI_REPLACEMENT_CHAR);
172
- }
173
- } else {
174
- /* target is a character in range 0xFFFF - 0x10FFFF. */
175
- ch -= halfBase;
176
- unicode_escape(buffer, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
177
- unicode_escape(buffer, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
178
- }
179
- }
180
- }
@@ -1,53 +0,0 @@
1
- #include "ruby.h"
2
-
3
- #ifndef _GENERATOR_UNICODE_H_
4
- #define _GENERATOR_UNICODE_H_
5
-
6
- typedef enum {
7
- conversionOK = 0, /* conversion successful */
8
- sourceExhausted, /* partial character in source, but hit end */
9
- targetExhausted, /* insuff. room in target for conversion */
10
- sourceIllegal /* source sequence is illegal/malformed */
11
- } ConversionResult;
12
-
13
- typedef enum {
14
- strictConversion = 0,
15
- lenientConversion
16
- } ConversionFlags;
17
-
18
- typedef unsigned long UTF32; /* at least 32 bits */
19
- typedef unsigned short UTF16; /* at least 16 bits */
20
- typedef unsigned char UTF8; /* typically 8 bits */
21
-
22
- #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
23
- #define UNI_MAX_BMP (UTF32)0x0000FFFF
24
- #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
25
- #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
26
- #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
27
-
28
- #define UNI_SUR_HIGH_START (UTF32)0xD800
29
- #define UNI_SUR_HIGH_END (UTF32)0xDBFF
30
- #define UNI_SUR_LOW_START (UTF32)0xDC00
31
- #define UNI_SUR_LOW_END (UTF32)0xDFFF
32
-
33
- static const int halfShift = 10; /* used for shifting by 10 bits */
34
-
35
- static const UTF32 halfBase = 0x0010000UL;
36
- static const UTF32 halfMask = 0x3FFUL;
37
-
38
- void JSON_convert_UTF8_to_JSON(VALUE buffer, VALUE string, ConversionFlags flags);
39
-
40
- #ifndef RARRAY_PTR
41
- #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
42
- #endif
43
- #ifndef RARRAY_LEN
44
- #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
45
- #endif
46
- #ifndef RSTRING_PTR
47
- #define RSTRING_PTR(string) RSTRING(string)->ptr
48
- #endif
49
- #ifndef RSTRING_LEN
50
- #define RSTRING_LEN(string) RSTRING(string)->len
51
- #endif
52
-
53
- #endif
@@ -1,11 +0,0 @@
1
- require 'mkmf'
2
- require 'rbconfig'
3
-
4
- if CONFIG['CC'] =~ /gcc/
5
- $CFLAGS += ' -Wall'
6
- #$CFLAGS += ' -O0 -ggdb'
7
- end
8
-
9
- have_header("ruby/st.h") || have_header("st.h")
10
- have_header("re.h")
11
- create_makefile 'parser'
@@ -1,154 +0,0 @@
1
- #include "unicode.h"
2
-
3
- /*
4
- * Copyright 2001-2004 Unicode, Inc.
5
- *
6
- * Disclaimer
7
- *
8
- * This source code is provided as is by Unicode, Inc. No claims are
9
- * made as to fitness for any particular purpose. No warranties of any
10
- * kind are expressed or implied. The recipient agrees to determine
11
- * applicability of information provided. If this file has been
12
- * purchased on magnetic or optical media from Unicode, Inc., the
13
- * sole remedy for any claim will be exchange of defective media
14
- * within 90 days of receipt.
15
- *
16
- * Limitations on Rights to Redistribute This Code
17
- *
18
- * Unicode, Inc. hereby grants the right to freely use the information
19
- * supplied in this file in the creation of products supporting the
20
- * Unicode Standard, and to make copies of this file in any form
21
- * for internal or external distribution as long as this notice
22
- * remains attached.
23
- */
24
-
25
- /*
26
- * Index into the table below with the first byte of a UTF-8 sequence to
27
- * get the number of trailing bytes that are supposed to follow it.
28
- * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
29
- * left as-is for anyone who may want to do such conversion, which was
30
- * allowed in earlier algorithms.
31
- */
32
- static const char trailingBytesForUTF8[256] = {
33
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
35
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
36
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
37
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
38
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
39
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
40
- 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
41
- };
42
-
43
- /*
44
- * Magic values subtracted from a buffer value during UTF8 conversion.
45
- * This table contains as many values as there might be trailing bytes
46
- * in a UTF-8 sequence.
47
- */
48
- static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
49
- 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
50
-
51
- /*
52
- * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
53
- * into the first byte, depending on how many bytes follow. There are
54
- * as many entries in this table as there are UTF-8 sequence types.
55
- * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
56
- * for *legal* UTF-8 will be 4 or fewer bytes total.
57
- */
58
- static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
59
-
60
- char *JSON_convert_UTF16_to_UTF8 (
61
- VALUE buffer,
62
- char *source,
63
- char *sourceEnd,
64
- ConversionFlags flags)
65
- {
66
- UTF16 *tmp, *tmpPtr, *tmpEnd;
67
- char buf[5];
68
- long n = 0, i;
69
- char *p = source - 1;
70
-
71
- while (p < sourceEnd && p[0] == '\\' && p[1] == 'u') {
72
- p += 6;
73
- n++;
74
- }
75
- p = source + 1;
76
- buf[4] = 0;
77
- tmpPtr = tmp = ALLOC_N(UTF16, n);
78
- tmpEnd = tmp + n;
79
- for (i = 0; i < n; i++) {
80
- buf[0] = *p++;
81
- buf[1] = *p++;
82
- buf[2] = *p++;
83
- buf[3] = *p++;
84
- tmpPtr[i] = (UTF16)strtol(buf, NULL, 16);
85
- p += 2;
86
- }
87
-
88
- while (tmpPtr < tmpEnd) {
89
- UTF32 ch;
90
- unsigned short bytesToWrite = 0;
91
- const UTF32 byteMask = 0xBF;
92
- const UTF32 byteMark = 0x80;
93
- ch = *tmpPtr++;
94
- /* If we have a surrogate pair, convert to UTF32 first. */
95
- if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
96
- /* If the 16 bits following the high surrogate are in the source
97
- * buffer... */
98
- if (tmpPtr < tmpEnd) {
99
- UTF32 ch2 = *tmpPtr;
100
- /* If it's a low surrogate, convert to UTF32. */
101
- if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
102
- ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
103
- + (ch2 - UNI_SUR_LOW_START) + halfBase;
104
- ++tmpPtr;
105
- } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
106
- ruby_xfree(tmp);
107
- rb_raise(rb_path2class("JSON::ParserError"),
108
- "\\uXXXX is illegal/malformed utf-16 near %s", source);
109
- }
110
- } else { /* We don't have the 16 bits following the high surrogate. */
111
- ruby_xfree(tmp);
112
- rb_raise(rb_path2class("JSON::ParserError"),
113
- "partial character in source, but hit end near %s", source);
114
- break;
115
- }
116
- } else if (flags == strictConversion) {
117
- /* UTF-16 surrogate values are illegal in UTF-32 */
118
- if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
119
- ruby_xfree(tmp);
120
- rb_raise(rb_path2class("JSON::ParserError"),
121
- "\\uXXXX is illegal/malformed utf-16 near %s", source);
122
- }
123
- }
124
- /* Figure out how many bytes the result will require */
125
- if (ch < (UTF32) 0x80) {
126
- bytesToWrite = 1;
127
- } else if (ch < (UTF32) 0x800) {
128
- bytesToWrite = 2;
129
- } else if (ch < (UTF32) 0x10000) {
130
- bytesToWrite = 3;
131
- } else if (ch < (UTF32) 0x110000) {
132
- bytesToWrite = 4;
133
- } else {
134
- bytesToWrite = 3;
135
- ch = UNI_REPLACEMENT_CHAR;
136
- }
137
-
138
- buf[0] = 0;
139
- buf[1] = 0;
140
- buf[2] = 0;
141
- buf[3] = 0;
142
- p = buf + bytesToWrite;
143
- switch (bytesToWrite) { /* note: everything falls through. */
144
- case 4: *--p = (UTF8) ((ch | byteMark) & byteMask); ch >>= 6;
145
- case 3: *--p = (UTF8) ((ch | byteMark) & byteMask); ch >>= 6;
146
- case 2: *--p = (UTF8) ((ch | byteMark) & byteMask); ch >>= 6;
147
- case 1: *--p = (UTF8) (ch | firstByteMark[bytesToWrite]);
148
- }
149
- rb_str_buf_cat(buffer, p, bytesToWrite);
150
- }
151
- ruby_xfree(tmp);
152
- source += 5 + (n - 1) * 6;
153
- return source;
154
- }
@@ -1,58 +0,0 @@
1
-
2
- #ifndef _PARSER_UNICODE_H_
3
- #define _PARSER_UNICODE_H_
4
-
5
- #include "ruby.h"
6
-
7
- typedef unsigned long UTF32; /* at least 32 bits */
8
- typedef unsigned short UTF16; /* at least 16 bits */
9
- typedef unsigned char UTF8; /* typically 8 bits */
10
-
11
- #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
12
- #define UNI_MAX_BMP (UTF32)0x0000FFFF
13
- #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
14
- #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
15
- #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
16
-
17
- #define UNI_SUR_HIGH_START (UTF32)0xD800
18
- #define UNI_SUR_HIGH_END (UTF32)0xDBFF
19
- #define UNI_SUR_LOW_START (UTF32)0xDC00
20
- #define UNI_SUR_LOW_END (UTF32)0xDFFF
21
-
22
- static const int halfShift = 10; /* used for shifting by 10 bits */
23
-
24
- static const UTF32 halfBase = 0x0010000UL;
25
- static const UTF32 halfMask = 0x3FFUL;
26
-
27
- typedef enum {
28
- conversionOK = 0, /* conversion successful */
29
- sourceExhausted, /* partial character in source, but hit end */
30
- targetExhausted, /* insuff. room in target for conversion */
31
- sourceIllegal /* source sequence is illegal/malformed */
32
- } ConversionResult;
33
-
34
- typedef enum {
35
- strictConversion = 0,
36
- lenientConversion
37
- } ConversionFlags;
38
-
39
- char *JSON_convert_UTF16_to_UTF8 (
40
- VALUE buffer,
41
- char *source,
42
- char *sourceEnd,
43
- ConversionFlags flags);
44
-
45
- #ifndef RARRAY_PTR
46
- #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
47
- #endif
48
- #ifndef RARRAY_LEN
49
- #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
50
- #endif
51
- #ifndef RSTRING_PTR
52
- #define RSTRING_PTR(string) RSTRING(string)->ptr
53
- #endif
54
- #ifndef RSTRING_LEN
55
- #define RSTRING_LEN(string) RSTRING(string)->len
56
- #endif
57
-
58
- #endif