xref: /PHP-7.3/ext/mbstring/oniguruma/src/reggnu.c (revision 1979c5d1)
1 /**********************************************************************
2   reggnu.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2019  K.Kosako
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 #include "oniggnu.h"
32 
33 extern void
re_free_registers(OnigRegion * r)34 re_free_registers(OnigRegion* r)
35 {
36   /* 0: don't free self */
37   onig_region_free(r, 0);
38 }
39 
40 extern int
re_adjust_startpos(regex_t * reg,const char * string,int size,int startpos,int range)41 re_adjust_startpos(regex_t* reg, const char* string, int size,
42                    int startpos, int range)
43 {
44   if (startpos > 0 && ONIGENC_MBC_MAXLEN(reg->enc) != 1 && startpos < size) {
45     UChar *p;
46     UChar *s = (UChar* )string + startpos;
47 
48     if (range > 0) {
49       p = onigenc_get_right_adjust_char_head(reg->enc, (UChar* )string, s);
50     }
51     else {
52       p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg->enc, (UChar* )string, s);
53     }
54     return (int )(p - (UChar* )string);
55   }
56 
57   return startpos;
58 }
59 
60 extern int
re_match(regex_t * reg,const char * str,int size,int pos,struct re_registers * regs)61 re_match(regex_t* reg, const char* str, int size, int pos,
62          struct re_registers* regs)
63 {
64   return onig_match(reg, (UChar* )str, (UChar* )(str + size),
65                     (UChar* )(str + pos), regs, ONIG_OPTION_NONE);
66 }
67 
68 extern int
re_search(regex_t * bufp,const char * string,int size,int startpos,int range,struct re_registers * regs)69 re_search(regex_t* bufp, const char* string, int size, int startpos, int range,
70           struct re_registers* regs)
71 {
72   return onig_search(bufp, (UChar* )string, (UChar* )(string + size),
73                      (UChar* )(string + startpos),
74                      (UChar* )(string + startpos + range),
75                      regs, ONIG_OPTION_NONE);
76 }
77 
78 extern int
re_compile_pattern(const char * pattern,int size,regex_t * reg,char * ebuf)79 re_compile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
80 {
81   int r;
82   OnigErrorInfo einfo;
83 
84   r = onig_compile(reg, (UChar* )pattern, (UChar* )(pattern + size), &einfo);
85   if (r != ONIG_NORMAL) {
86     if (IS_NOT_NULL(ebuf))
87       (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
88   }
89 
90   return r;
91 }
92 
93 extern void
re_free_pattern(regex_t * reg)94 re_free_pattern(regex_t* reg)
95 {
96   onig_free(reg);
97 }
98 
99 extern int
re_alloc_pattern(regex_t ** reg)100 re_alloc_pattern(regex_t** reg)
101 {
102   *reg = (regex_t* )xmalloc(sizeof(regex_t));
103   if (IS_NULL(*reg)) return ONIGERR_MEMORY;
104 
105   return onig_reg_init(*reg, ONIG_OPTION_DEFAULT,
106                        ONIGENC_CASE_FOLD_DEFAULT,
107                        OnigEncDefaultCharEncoding,
108                        OnigDefaultSyntax);
109 }
110 
111 extern void
re_set_casetable(const char * table)112 re_set_casetable(const char* table)
113 {
114   onigenc_set_default_caseconv_table((UChar* )table);
115 }
116 
117 extern void
re_mbcinit(int mb_code)118 re_mbcinit(int mb_code)
119 {
120   OnigEncoding enc;
121 
122   switch (mb_code) {
123   case RE_MBCTYPE_ASCII:
124     enc = ONIG_ENCODING_ASCII;
125     break;
126   case RE_MBCTYPE_EUC:
127     enc = ONIG_ENCODING_EUC_JP;
128     break;
129   case RE_MBCTYPE_SJIS:
130     enc = ONIG_ENCODING_SJIS;
131     break;
132   case RE_MBCTYPE_UTF8:
133     enc = ONIG_ENCODING_UTF8;
134     break;
135   default:
136     return ;
137     break;
138   }
139 
140   onig_initialize(&enc, 1);
141 
142   onigenc_set_default_encoding(enc);
143 }
144