1 : /**********************************************************************
2 : utf32_be.c - Oniguruma (regular expression library)
3 : **********************************************************************/
4 : /*-
5 : * Copyright (c) 2002-2009 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 : * All rights reserved.
7 : *
8 : * Redistribution and use in source and binary forms, with or without
9 : * modification, are permitted provided that the following conditions
10 : * are met:
11 : * 1. Redistributions of source code must retain the above copyright
12 : * notice, this list of conditions and the following disclaimer.
13 : * 2. Redistributions in binary form must reproduce the above copyright
14 : * notice, this list of conditions and the following disclaimer in the
15 : * documentation and/or other materials provided with the distribution.
16 : *
17 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 : * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 : * SUCH DAMAGE.
28 : */
29 :
30 : #include "regenc.h"
31 :
32 : static int
33 : utf32be_mbc_enc_len(const UChar* p)
34 0 : {
35 0 : return 4;
36 : }
37 :
38 : static int
39 : utf32be_is_mbc_newline(const UChar* p, const UChar* end)
40 0 : {
41 0 : if (p + 3 < end) {
42 0 : if (*(p+3) == 0x0a && *(p+2) == 0 && *(p+1) == 0 && *p == 0)
43 0 : return 1;
44 : #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
45 : if ((*(p+3) == 0x0d || *(p+3) == 0x85)
46 : && *(p+2) == 0 && *(p+1) == 0 && *p == 0x00)
47 : return 1;
48 : if (*(p+2) == 0x20 && (*(p+3) == 0x29 || *(p+3) == 0x28)
49 : && *(p+1) == 0 && *p == 0)
50 : return 1;
51 : #endif
52 : }
53 0 : return 0;
54 : }
55 :
56 : static OnigCodePoint
57 : utf32be_mbc_to_code(const UChar* p, const UChar* end)
58 0 : {
59 0 : return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
60 : }
61 :
62 : static int
63 : utf32be_code_to_mbclen(OnigCodePoint code)
64 0 : {
65 0 : return 4;
66 : }
67 :
68 : static int
69 : utf32be_code_to_mbc(OnigCodePoint code, UChar *buf)
70 0 : {
71 0 : UChar* p = buf;
72 :
73 0 : *p++ = (UChar )((code & 0xff000000) >>24);
74 0 : *p++ = (UChar )((code & 0xff0000) >>16);
75 0 : *p++ = (UChar )((code & 0xff00) >> 8);
76 0 : *p++ = (UChar ) (code & 0xff);
77 0 : return 4;
78 : }
79 :
80 : static int
81 : utf32be_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
82 : UChar* lower)
83 0 : {
84 0 : const UChar* p = *pp;
85 :
86 0 : if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
87 0 : p += 3;
88 0 : if (end > p + 4 &&
89 : (flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0 &&
90 : ((*p == 's' && *(p+4) == 's') ||
91 : ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
92 : (*p == 'S' && *(p+4) == 'S'))) &&
93 : *(p+3) == 0 && *(p+2) == 0 && *(p+1) == 0) {
94 0 : *lower++ = '\0';
95 0 : *lower++ = '\0';
96 0 : *lower++ = '\0';
97 0 : *lower = 0xdf;
98 0 : (*pp) += 8;
99 0 : return 4;
100 : }
101 :
102 0 : *lower++ = '\0';
103 0 : *lower++ = '\0';
104 0 : *lower++ = '\0';
105 0 : if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
106 : ONIGENC_IS_MBC_ASCII(p)) ||
107 : ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
108 : !ONIGENC_IS_MBC_ASCII(p))) {
109 0 : *lower = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
110 : }
111 : else {
112 0 : *lower = *p;
113 : }
114 :
115 0 : (*pp) += 4;
116 0 : return 4; /* return byte length of converted char to lower */
117 : }
118 : else {
119 0 : int len = 4;
120 0 : if (lower != p) {
121 : int i;
122 0 : for (i = 0; i < len; i++) {
123 0 : *lower++ = *p++;
124 : }
125 : }
126 0 : (*pp) += len;
127 0 : return len; /* return byte length of converted char to lower */
128 : }
129 : }
130 :
131 : static int
132 : utf32be_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
133 0 : {
134 0 : const UChar* p = *pp;
135 :
136 0 : (*pp) += 4;
137 :
138 0 : if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
139 : int c, v;
140 :
141 0 : p += 3;
142 0 : if ((flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0) {
143 0 : if (end > p + 4 &&
144 : ((*p == 's' && *(p+4) == 's') ||
145 : ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
146 : (*p == 'S' && *(p+4) == 'S'))) &&
147 : *(p+3) == 0 && *(p+2) == 0 && *(p+1) == 0) {
148 0 : (*pp) += 4;
149 0 : return TRUE;
150 : }
151 0 : else if (*p == 0xdf) {
152 0 : return TRUE;
153 : }
154 : }
155 :
156 0 : if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
157 : ONIGENC_IS_MBC_ASCII(p)) ||
158 : ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
159 : !ONIGENC_IS_MBC_ASCII(p))) {
160 0 : c = *p;
161 0 : v = ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(c,
162 : (ONIGENC_CTYPE_UPPER | ONIGENC_CTYPE_LOWER));
163 : if ((v | ONIGENC_CTYPE_LOWER) != 0) {
164 : /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
165 0 : if (c >= 0xaa && c <= 0xba)
166 0 : return FALSE;
167 : else
168 0 : return TRUE;
169 : }
170 : return (v != 0 ? TRUE : FALSE);
171 : }
172 : }
173 :
174 0 : return FALSE;
175 : }
176 :
177 : static UChar*
178 : utf32be_left_adjust_char_head(const UChar* start, const UChar* s)
179 0 : {
180 : int rem;
181 :
182 0 : if (s <= start) return (UChar* )s;
183 :
184 0 : rem = (s - start) % 4;
185 0 : return (UChar* )(s - rem);
186 : }
187 :
188 : OnigEncodingType OnigEncodingUTF32_BE = {
189 : utf32be_mbc_enc_len,
190 : "UTF-32BE", /* name */
191 : 4, /* max byte length */
192 : 4, /* min byte length */
193 : (ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE |
194 : ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE |
195 : ONIGENC_AMBIGUOUS_MATCH_COMPOUND),
196 : {
197 : (OnigCodePoint )'\\' /* esc */
198 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
199 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
200 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
201 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
202 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
203 : },
204 : utf32be_is_mbc_newline,
205 : utf32be_mbc_to_code,
206 : utf32be_code_to_mbclen,
207 : utf32be_code_to_mbc,
208 : utf32be_mbc_to_normalize,
209 : utf32be_is_mbc_ambiguous,
210 : onigenc_iso_8859_1_get_all_pair_ambig_codes,
211 : onigenc_ess_tsett_get_all_comp_ambig_codes,
212 : onigenc_unicode_is_code_ctype,
213 : onigenc_unicode_get_ctype_code_range,
214 : utf32be_left_adjust_char_head,
215 : onigenc_always_false_is_allowed_reverse_match
216 : };
|