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