~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to ext/json/ext/generator/unicode.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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");
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");
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 == '/') {
148
 
                    rb_str_buf_cat2(buffer, "\\/");
149
 
                } else if (ch >= 0x20 && ch <= 0x7f) {
150
 
                    rb_str_buf_cat(buffer, (char *) source - 1, 1);
151
 
                } else if (ch == '\n') {
152
 
                    rb_str_buf_cat2(buffer, "\\n");
153
 
                } else if (ch == '\r') {
154
 
                    rb_str_buf_cat2(buffer, "\\r");
155
 
                } else if (ch == '\t') {
156
 
                    rb_str_buf_cat2(buffer, "\\t");
157
 
                } else if (ch == '\f') {
158
 
                    rb_str_buf_cat2(buffer, "\\f");
159
 
                } else if (ch == '\b') {
160
 
                    rb_str_buf_cat2(buffer, "\\b");
161
 
                } else if (ch < 0x20) {
162
 
                    unicode_escape(buffer, (UTF16) ch);
163
 
                } else {
164
 
                    unicode_escape(buffer, (UTF16) ch);
165
 
                }
166
 
            }
167
 
        } else if (ch > UNI_MAX_UTF16) {
168
 
            if (flags == strictConversion) {
169
 
                source -= (extraBytesToRead+1); /* return to the start */
170
 
                rb_raise(rb_path2class("JSON::GeneratorError"),
171
 
                        "source sequence is illegal/malformed");
172
 
            } else {
173
 
                unicode_escape(buffer, UNI_REPLACEMENT_CHAR);
174
 
            }
175
 
        } else {
176
 
            /* target is a character in range 0xFFFF - 0x10FFFF. */
177
 
            ch -= halfBase;
178
 
            unicode_escape(buffer, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
179
 
            unicode_escape(buffer, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
180
 
        }
181
 
    }
182
 
}