1 /**********************************************************************
2 regext.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2006 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 "regint.h"
31
32 static void
conv_ext0be32(const UChar * s,const UChar * end,UChar * conv)33 conv_ext0be32(const UChar* s, const UChar* end, UChar* conv)
34 {
35 while (s < end) {
36 *conv++ = '\0';
37 *conv++ = '\0';
38 *conv++ = '\0';
39 *conv++ = *s++;
40 }
41 }
42
43 static void
conv_ext0le32(const UChar * s,const UChar * end,UChar * conv)44 conv_ext0le32(const UChar* s, const UChar* end, UChar* conv)
45 {
46 while (s < end) {
47 *conv++ = *s++;
48 *conv++ = '\0';
49 *conv++ = '\0';
50 *conv++ = '\0';
51 }
52 }
53
54 static void
conv_ext0be(const UChar * s,const UChar * end,UChar * conv)55 conv_ext0be(const UChar* s, const UChar* end, UChar* conv)
56 {
57 while (s < end) {
58 *conv++ = '\0';
59 *conv++ = *s++;
60 }
61 }
62
63 static void
conv_ext0le(const UChar * s,const UChar * end,UChar * conv)64 conv_ext0le(const UChar* s, const UChar* end, UChar* conv)
65 {
66 while (s < end) {
67 *conv++ = *s++;
68 *conv++ = '\0';
69 }
70 }
71
72 static void
conv_swap4bytes(const UChar * s,const UChar * end,UChar * conv)73 conv_swap4bytes(const UChar* s, const UChar* end, UChar* conv)
74 {
75 while (s < end) {
76 *conv++ = s[3];
77 *conv++ = s[2];
78 *conv++ = s[1];
79 *conv++ = s[0];
80 s += 4;
81 }
82 }
83
84 static void
conv_swap2bytes(const UChar * s,const UChar * end,UChar * conv)85 conv_swap2bytes(const UChar* s, const UChar* end, UChar* conv)
86 {
87 while (s < end) {
88 *conv++ = s[1];
89 *conv++ = s[0];
90 s += 2;
91 }
92 }
93
94 static int
conv_encoding(OnigEncoding from,OnigEncoding to,const UChar * s,const UChar * end,UChar ** conv,UChar ** conv_end)95 conv_encoding(OnigEncoding from, OnigEncoding to, const UChar* s, const UChar* end,
96 UChar** conv, UChar** conv_end)
97 {
98 int len = end - s;
99
100 if (to == ONIG_ENCODING_UTF16_BE) {
101 if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
102 *conv = (UChar* )xmalloc(len * 2);
103 CHECK_NULL_RETURN_VAL(*conv, ONIGERR_MEMORY);
104 *conv_end = *conv + (len * 2);
105 conv_ext0be(s, end, *conv);
106 return 0;
107 }
108 else if (from == ONIG_ENCODING_UTF16_LE) {
109 swap16:
110 *conv = (UChar* )xmalloc(len);
111 CHECK_NULL_RETURN_VAL(*conv, ONIGERR_MEMORY);
112 *conv_end = *conv + len;
113 conv_swap2bytes(s, end, *conv);
114 return 0;
115 }
116 }
117 else if (to == ONIG_ENCODING_UTF16_LE) {
118 if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
119 *conv = (UChar* )xmalloc(len * 2);
120 CHECK_NULL_RETURN_VAL(*conv, ONIGERR_MEMORY);
121 *conv_end = *conv + (len * 2);
122 conv_ext0le(s, end, *conv);
123 return 0;
124 }
125 else if (from == ONIG_ENCODING_UTF16_BE) {
126 goto swap16;
127 }
128 }
129 if (to == ONIG_ENCODING_UTF32_BE) {
130 if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
131 *conv = (UChar* )xmalloc(len * 4);
132 CHECK_NULL_RETURN_VAL(*conv, ONIGERR_MEMORY);
133 *conv_end = *conv + (len * 4);
134 conv_ext0be32(s, end, *conv);
135 return 0;
136 }
137 else if (from == ONIG_ENCODING_UTF32_LE) {
138 swap32:
139 *conv = (UChar* )xmalloc(len);
140 CHECK_NULL_RETURN_VAL(*conv, ONIGERR_MEMORY);
141 *conv_end = *conv + len;
142 conv_swap4bytes(s, end, *conv);
143 return 0;
144 }
145 }
146 else if (to == ONIG_ENCODING_UTF32_LE) {
147 if (from == ONIG_ENCODING_ASCII || from == ONIG_ENCODING_ISO_8859_1) {
148 *conv = (UChar* )xmalloc(len * 4);
149 CHECK_NULL_RETURN_VAL(*conv, ONIGERR_MEMORY);
150 *conv_end = *conv + (len * 4);
151 conv_ext0le32(s, end, *conv);
152 return 0;
153 }
154 else if (from == ONIG_ENCODING_UTF32_BE) {
155 goto swap32;
156 }
157 }
158
159 return ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION;
160 }
161
162 extern int
onig_new_deluxe(regex_t ** reg,const UChar * pattern,const UChar * pattern_end,OnigCompileInfo * ci,OnigErrorInfo * einfo)163 onig_new_deluxe(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
164 OnigCompileInfo* ci, OnigErrorInfo* einfo)
165 {
166 int r;
167 UChar *cpat, *cpat_end;
168
169 if (IS_NOT_NULL(einfo)) einfo->par = (UChar* )NULL;
170
171 if (ci->pattern_enc != ci->target_enc) {
172 r = conv_encoding(ci->pattern_enc, ci->target_enc, pattern, pattern_end,
173 &cpat, &cpat_end);
174 if (r) return r;
175 }
176 else {
177 cpat = (UChar* )pattern;
178 cpat_end = (UChar* )pattern_end;
179 }
180
181 r = onig_alloc_init(reg, ci->option, ci->ambig_flag, ci->target_enc,
182 ci->syntax);
183 if (r) goto err;
184
185 r = onig_compile(*reg, cpat, cpat_end, einfo);
186 if (r) {
187 onig_free(*reg);
188 *reg = NULL;
189 }
190
191 err:
192 if (cpat != pattern) xfree(cpat);
193
194 return r;
195 }
196
197 #ifdef USE_RECOMPILE_API
198 extern int
onig_recompile_deluxe(regex_t * reg,const UChar * pattern,const UChar * pattern_end,OnigCompileInfo * ci,OnigErrorInfo * einfo)199 onig_recompile_deluxe(regex_t* reg, const UChar* pattern, const UChar* pattern_end,
200 OnigCompileInfo* ci, OnigErrorInfo* einfo)
201 {
202 int r;
203 regex_t *new_reg;
204
205 r = onig_new_deluxe(&new_reg, pattern, pattern_end, ci, einfo);
206 if (r) return r;
207 if (ONIG_STATE(reg) == ONIG_STATE_NORMAL) {
208 onig_transfer(reg, new_reg);
209 }
210 else {
211 onig_chain_link_add(reg, new_reg);
212 }
213 return 0;
214 }
215 #endif
216