1 /**********************************************************************
2 utf32_le.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2007 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
utf32le_mbc_enc_len(const UChar * p ARG_UNUSED)33 utf32le_mbc_enc_len(const UChar* p ARG_UNUSED)
34 {
35 return 4;
36 }
37
38 static int
utf32le_is_mbc_newline(const UChar * p,const UChar * end)39 utf32le_is_mbc_newline(const UChar* p, const UChar* end)
40 {
41 if (p + 3 < end) {
42 if (*p == 0x0a && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0)
43 return 1;
44 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
45 if ((
46 #ifndef USE_CRNL_AS_LINE_TERMINATOR
47 *p == 0x0d ||
48 #endif
49 *p == 0x85)
50 && *(p+1) == 0x00 && (p+2) == 0x00 && *(p+3) == 0x00)
51 return 1;
52 if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28)
53 && *(p+2) == 0x00 && *(p+3) == 0x00)
54 return 1;
55 #endif
56 }
57 return 0;
58 }
59
60 static OnigCodePoint
utf32le_mbc_to_code(const UChar * p,const UChar * end ARG_UNUSED)61 utf32le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
62 {
63 if (end - p < 4) return 0;
64 return (OnigCodePoint )(((p[3] * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
65 }
66
67 static int
utf32le_code_to_mbclen(OnigCodePoint code ARG_UNUSED)68 utf32le_code_to_mbclen(OnigCodePoint code ARG_UNUSED)
69 {
70 return 4;
71 }
72
73 static int
utf32le_code_to_mbc(OnigCodePoint code,UChar * buf)74 utf32le_code_to_mbc(OnigCodePoint code, UChar *buf)
75 {
76 UChar* p = buf;
77
78 *p++ = (UChar ) (code & 0xff);
79 *p++ = (UChar )((code & 0xff00) >> 8);
80 *p++ = (UChar )((code & 0xff0000) >>16);
81 *p++ = (UChar )((code & 0xff000000) >>24);
82 return 4;
83 }
84
85 static int
utf32le_mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end,UChar * fold)86 utf32le_mbc_case_fold(OnigCaseFoldType flag,
87 const UChar** pp, const UChar* end, UChar* fold)
88 {
89 const UChar* p = *pp;
90
91 if (ONIGENC_IS_ASCII_CODE(*p) && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
92 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
93 if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
94 if (*p == 0x49) {
95 *fold++ = 0x31;
96 *fold++ = 0x01;
97 }
98 }
99 else {
100 #endif
101 *fold++ = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
102 *fold++ = 0;
103 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
104 }
105 #endif
106
107 *fold++ = 0;
108 *fold = 0;
109 *pp += 4;
110 return 4;
111 }
112 else
113 return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF32_LE, flag, pp, end,
114 fold);
115 }
116
117 #if 0
118 static int
119 utf32le_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp, const UChar* end)
120 {
121 const UChar* p = *pp;
122
123 (*pp) += 4;
124
125 if (*(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
126 int c, v;
127
128 if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
129 return TRUE;
130 }
131
132 c = *p;
133 v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
134 (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
135 if ((v | BIT_CTYPE_LOWER) != 0) {
136 /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
137 if (c >= 0xaa && c <= 0xba)
138 return FALSE;
139 else
140 return TRUE;
141 }
142 return (v != 0 ? TRUE : FALSE);
143 }
144
145 return FALSE;
146 }
147 #endif
148
149 static UChar*
utf32le_left_adjust_char_head(const UChar * start,const UChar * s)150 utf32le_left_adjust_char_head(const UChar* start, const UChar* s)
151 {
152 int rem;
153
154 if (s <= start) return (UChar* )s;
155
156 rem = (s - start) % 4;
157 return (UChar* )(s - rem);
158 }
159
160 static int
utf32le_get_case_fold_codes_by_str(OnigCaseFoldType flag,const OnigUChar * p,const OnigUChar * end,OnigCaseFoldCodeItem items[])161 utf32le_get_case_fold_codes_by_str(OnigCaseFoldType flag,
162 const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
163 {
164 return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF32_LE,
165 flag, p, end, items);
166 }
167
168 OnigEncodingType OnigEncodingUTF32_LE = {
169 utf32le_mbc_enc_len,
170 "UTF-32LE", /* name */
171 4, /* max byte length */
172 4, /* min byte length */
173 utf32le_is_mbc_newline,
174 utf32le_mbc_to_code,
175 utf32le_code_to_mbclen,
176 utf32le_code_to_mbc,
177 utf32le_mbc_case_fold,
178 onigenc_unicode_apply_all_case_fold,
179 utf32le_get_case_fold_codes_by_str,
180 onigenc_unicode_property_name_to_ctype,
181 onigenc_unicode_is_code_ctype,
182 onigenc_utf16_32_get_ctype_code_range,
183 utf32le_left_adjust_char_head,
184 onigenc_always_false_is_allowed_reverse_match
185 };
186