1 /**********************************************************************
2 reggnu.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2008 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * All rights reserved.
7 *
8 * (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include "regint.h"
33
34 #ifndef ONIGGNU_H
35 #include "oniggnu.h"
36 #endif
37
38 extern void
re_free_registers(OnigRegion * r)39 re_free_registers(OnigRegion* r)
40 {
41 /* 0: don't free self */
42 onig_region_free(r, 0);
43 }
44
45 extern int
re_adjust_startpos(regex_t * reg,const char * string,int size,int startpos,int range)46 re_adjust_startpos(regex_t* reg, const char* string, int size,
47 int startpos, int range)
48 {
49 if (startpos > 0 && ONIGENC_MBC_MAXLEN(reg->enc) != 1 && startpos < size) {
50 UChar *p;
51 UChar *s = (UChar* )string + startpos;
52
53 if (range > 0) {
54 p = onigenc_get_right_adjust_char_head(reg->enc, (UChar* )string, s);
55 }
56 else {
57 p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg->enc, (UChar* )string, s);
58 }
59 return (int)(p - (UChar* )string);
60 }
61
62 return startpos;
63 }
64
65 extern int
re_match(regex_t * reg,const char * str,int size,int pos,struct re_registers * regs)66 re_match(regex_t* reg, const char* str, int size, int pos,
67 struct re_registers* regs)
68 {
69 return onig_match(reg, (UChar* )str, (UChar* )(str + size),
70 (UChar* )(str + pos), regs, ONIG_OPTION_NONE);
71 }
72
73 extern int
re_search(regex_t * bufp,const char * string,int size,int startpos,int range,struct re_registers * regs)74 re_search(regex_t* bufp, const char* string, int size, int startpos, int range,
75 struct re_registers* regs)
76 {
77 return onig_search(bufp, (UChar* )string, (UChar* )(string + size),
78 (UChar* )(string + startpos),
79 (UChar* )(string + startpos + range),
80 regs, ONIG_OPTION_NONE);
81 }
82
83 extern int
re_compile_pattern(const char * pattern,int size,regex_t * reg,char * ebuf)84 re_compile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
85 {
86 int r;
87 OnigErrorInfo einfo;
88
89 r = onig_compile(reg, (UChar* )pattern, (UChar* )(pattern + size), &einfo);
90 if (r != ONIG_NORMAL) {
91 if (IS_NOT_NULL(ebuf))
92 (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
93 }
94
95 return r;
96 }
97
98 #ifdef USE_RECOMPILE_API
99 extern int
re_recompile_pattern(const char * pattern,int size,regex_t * reg,char * ebuf)100 re_recompile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
101 {
102 int r;
103 OnigErrorInfo einfo;
104 OnigEncoding enc;
105
106 /* I think encoding and options should be arguments of this function.
107 But this is adapted to present re.c. (2002/11/29)
108 */
109 enc = OnigEncDefaultCharEncoding;
110
111 r = onig_recompile(reg, (UChar* )pattern, (UChar* )(pattern + size),
112 reg->options, enc, OnigDefaultSyntax, &einfo);
113 if (r != ONIG_NORMAL) {
114 if (IS_NOT_NULL(ebuf))
115 (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
116 }
117 return r;
118 }
119 #endif
120
121 extern void
re_free_pattern(regex_t * reg)122 re_free_pattern(regex_t* reg)
123 {
124 onig_free(reg);
125 }
126
127 extern int
re_alloc_pattern(regex_t ** reg)128 re_alloc_pattern(regex_t** reg)
129 {
130 *reg = (regex_t* )xmalloc(sizeof(regex_t));
131 if (IS_NULL(*reg)) return ONIGERR_MEMORY;
132
133 return onig_reg_init(*reg, ONIG_OPTION_DEFAULT,
134 ONIGENC_CASE_FOLD_DEFAULT,
135 OnigEncDefaultCharEncoding,
136 OnigDefaultSyntax);
137 }
138
139 extern void
re_set_casetable(const char * table)140 re_set_casetable(const char* table)
141 {
142 onigenc_set_default_caseconv_table((UChar* )table);
143 }
144
145 extern void
re_mbcinit(int mb_code)146 re_mbcinit(int mb_code)
147 {
148 OnigEncoding enc;
149
150 switch (mb_code) {
151 case RE_MBCTYPE_ASCII:
152 enc = ONIG_ENCODING_ASCII;
153 break;
154 case RE_MBCTYPE_EUC:
155 enc = ONIG_ENCODING_EUC_JP;
156 break;
157 case RE_MBCTYPE_SJIS:
158 enc = ONIG_ENCODING_SJIS;
159 break;
160 case RE_MBCTYPE_UTF8:
161 enc = ONIG_ENCODING_UTF8;
162 break;
163 default:
164 return ;
165 break;
166 }
167
168 onigenc_set_default_encoding(enc);
169 }
170