1 : /**********************************************************************
2 : utf32_le.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 : utf32le_mbc_enc_len(const UChar* p)
34 0 : {
35 0 : return 4;
36 : }
37 :
38 : static int
39 : utf32le_is_mbc_newline(const UChar* p, const UChar* end)
40 0 : {
41 0 : if (p + 3 < end) {
42 0 : if (*p == 0x0a && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0)
43 0 : return 1;
44 : #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
45 : if ((*p == 0x0d || *p == 0x85) && *(p+1) == 0x00
46 : && (p+2) == 0x00 && *(p+3) == 0x00)
47 : return 1;
48 : if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28)
49 : && *(p+2) == 0x00 && *(p+3) == 0x00)
50 : return 1;
51 : #endif
52 : }
53 0 : return 0;
54 : }
55 :
56 : static OnigCodePoint
57 : utf32le_mbc_to_code(const UChar* p, const UChar* end)
58 0 : {
59 0 : return (OnigCodePoint )(((p[3] * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
60 : }
61 :
62 : static int
63 : utf32le_code_to_mbclen(OnigCodePoint code)
64 0 : {
65 0 : return 4;
66 : }
67 :
68 : static int
69 : utf32le_code_to_mbc(OnigCodePoint code, UChar *buf)
70 0 : {
71 0 : UChar* p = buf;
72 :
73 0 : *p++ = (UChar ) (code & 0xff);
74 0 : *p++ = (UChar )((code & 0xff00) >> 8);
75 0 : *p++ = (UChar )((code & 0xff0000) >>16);
76 0 : *p++ = (UChar )((code & 0xff000000) >>24);
77 0 : return 4;
78 : }
79 :
80 : static int
81 : utf32le_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+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
87 0 : if (end > p + 7 &&
88 : (flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0 &&
89 : ((*p == 's' && *(p+4) == 's') ||
90 : ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
91 : (*p == 'S' && *(p+4) == 'S'))) &&
92 : *(p+5) == 0 && *(p+6) == 0 && *(p+7) == 0) {
93 0 : *lower++ = 0xdf;
94 0 : *lower++ = '\0';
95 0 : *lower++ = '\0';
96 0 : *lower = '\0';
97 0 : (*pp) += 8;
98 0 : return 4;
99 : }
100 :
101 0 : if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
102 : ONIGENC_IS_MBC_ASCII(p)) ||
103 : ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
104 : !ONIGENC_IS_MBC_ASCII(p))) {
105 0 : *lower++ = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
106 : }
107 : else {
108 0 : *lower++ = *p;
109 : }
110 0 : *lower++ = '\0';
111 0 : *lower++ = '\0';
112 0 : *lower = '\0';
113 :
114 0 : (*pp) += 4;
115 0 : return 4; /* return byte length of converted char to lower */
116 : }
117 : else {
118 0 : int len = 4;
119 0 : if (lower != p) {
120 : int i;
121 0 : for (i = 0; i < len; i++) {
122 0 : *lower++ = *p++;
123 : }
124 : }
125 0 : (*pp) += len;
126 0 : return len; /* return byte length of converted char to lower */
127 : }
128 : }
129 :
130 : static int
131 : utf32le_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
132 0 : {
133 0 : const UChar* p = *pp;
134 :
135 0 : (*pp) += 4;
136 :
137 0 : if (*(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
138 : int c, v;
139 :
140 0 : if ((flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0) {
141 0 : if (end > p + 7 &&
142 : ((*p == 's' && *(p+4) == 's') ||
143 : ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
144 : (*p == 'S' && *(p+4) == 'S'))) &&
145 : *(p+5) == 0 && *(p+6) == 0 && *(p+7) == 0) {
146 0 : (*pp) += 4;
147 0 : return TRUE;
148 : }
149 0 : else if (*p == 0xdf) {
150 0 : return TRUE;
151 : }
152 : }
153 :
154 0 : if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
155 : ONIGENC_IS_MBC_ASCII(p)) ||
156 : ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
157 : !ONIGENC_IS_MBC_ASCII(p))) {
158 0 : c = *p;
159 0 : v = ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(c,
160 : (ONIGENC_CTYPE_UPPER | ONIGENC_CTYPE_LOWER));
161 : if ((v | ONIGENC_CTYPE_LOWER) != 0) {
162 : /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
163 0 : if (c >= 0xaa && c <= 0xba)
164 0 : return FALSE;
165 : else
166 0 : return TRUE;
167 : }
168 : return (v != 0 ? TRUE : FALSE);
169 : }
170 : }
171 :
172 0 : return FALSE;
173 : }
174 :
175 : static UChar*
176 : utf32le_left_adjust_char_head(const UChar* start, const UChar* s)
177 0 : {
178 : int rem;
179 :
180 0 : if (s <= start) return (UChar* )s;
181 :
182 0 : rem = (s - start) % 4;
183 0 : return (UChar* )(s - rem);
184 : }
185 :
186 : OnigEncodingType OnigEncodingUTF32_LE = {
187 : utf32le_mbc_enc_len,
188 : "UTF-32LE", /* name */
189 : 4, /* max byte length */
190 : 4, /* min byte length */
191 : (ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE |
192 : ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE |
193 : ONIGENC_AMBIGUOUS_MATCH_COMPOUND),
194 : {
195 : (OnigCodePoint )'\\' /* esc */
196 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
197 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
198 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
199 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
200 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
201 : },
202 : utf32le_is_mbc_newline,
203 : utf32le_mbc_to_code,
204 : utf32le_code_to_mbclen,
205 : utf32le_code_to_mbc,
206 : utf32le_mbc_to_normalize,
207 : utf32le_is_mbc_ambiguous,
208 : onigenc_iso_8859_1_get_all_pair_ambig_codes,
209 : onigenc_ess_tsett_get_all_comp_ambig_codes,
210 : onigenc_unicode_is_code_ctype,
211 : onigenc_unicode_get_ctype_code_range,
212 : utf32le_left_adjust_char_head,
213 : onigenc_always_false_is_allowed_reverse_match
214 : };
|