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