xref: /PHP-7.3/ext/mbstring/oniguruma/src/utf16_be.c (revision 1979c5d1)
1 /**********************************************************************
2   utf16_be.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"  /* for USE_CALLOUT */
31 
32 
33 static int
init(void)34 init(void)
35 {
36 #ifdef USE_CALLOUT
37 
38     int id;
39     OnigEncoding enc;
40     char* name;
41     unsigned int args[4];
42     OnigValue opts[4];
43 
44     enc = ONIG_ENCODING_UTF16_BE;
45 
46     name = "\000F\000A\000I\000L\000\000";            BC0_P(name, fail);
47     name = "\000M\000I\000S\000M\000A\000T\000C\000H\000\000"; BC0_P(name, mismatch);
48 
49     name = "\000M\000A\000X\000\000";
50     args[0] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
51     args[1] = ONIG_TYPE_CHAR;
52     opts[0].c = 'X';
53     BC_B_O(name, max, 2, args, 1, opts);
54 
55     name = "\000E\000R\000R\000O\000R\000\000";
56     args[0] = ONIG_TYPE_LONG; opts[0].l = ONIG_ABORT;
57     BC_P_O(name, error, 1, args, 1, opts);
58 
59     name = "\000C\000O\000U\000N\000T\000\000";
60     args[0] = ONIG_TYPE_CHAR; opts[0].c = '>';
61     BC_B_O(name, count, 1, args, 1, opts);
62 
63     name = "\000T\000O\000T\000A\000L\000_\000C\000O\000U\000N\000T\000\000";
64     args[0] = ONIG_TYPE_CHAR; opts[0].c = '>';
65     BC_B_O(name, total_count, 1, args, 1, opts);
66 
67     name = "\000C\000M\000P\000\000";
68     args[0] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
69     args[1] = ONIG_TYPE_STRING;
70     args[2] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
71     BC_P(name, cmp, 3, args);
72 
73 #endif /* USE_CALLOUT */
74 
75   return ONIG_NORMAL;
76 }
77 
78 static const int EncLen_UTF16[] = {
79   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
87   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
88   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
89   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
90   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
91   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
92   2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2,
93   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
94   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
95 };
96 
97 static int
utf16be_mbc_enc_len(const UChar * p)98 utf16be_mbc_enc_len(const UChar* p)
99 {
100   return EncLen_UTF16[*p];
101 }
102 
103 static int
is_valid_mbc_string(const UChar * s,const UChar * end)104 is_valid_mbc_string(const UChar* s, const UChar* end)
105 {
106   while (s < end) {
107     int len = utf16be_mbc_enc_len(s);
108     if (len == 4) {
109       if (s + 2 >= end)
110         return FALSE;
111       if (! UTF16_IS_SURROGATE_SECOND(*(s+2)))
112         return FALSE;
113     }
114     else
115       if (UTF16_IS_SURROGATE_SECOND(*s))
116         return FALSE;
117 
118     s += len;
119   }
120 
121   if (s != end)
122     return FALSE;
123   else
124     return TRUE;
125 }
126 
127 static int
utf16be_is_mbc_newline(const UChar * p,const UChar * end)128 utf16be_is_mbc_newline(const UChar* p, const UChar* end)
129 {
130   if (p + 1 < end) {
131     if (*(p+1) == 0x0a && *p == 0x00)
132       return 1;
133 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
134     if ((
135 #ifndef USE_CRNL_AS_LINE_TERMINATOR
136          *(p+1) == 0x0d ||
137 #endif
138          *(p+1) == 0x85) && *p == 0x00)
139       return 1;
140 
141     if (*p == 0x20 && (*(p+1) == 0x29 || *(p+1) == 0x28))
142       return 1;
143 #endif
144   }
145   return 0;
146 }
147 
148 static OnigCodePoint
utf16be_mbc_to_code(const UChar * p,const UChar * end ARG_UNUSED)149 utf16be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
150 {
151   OnigCodePoint code;
152 
153   if (UTF16_IS_SURROGATE_FIRST(*p)) {
154     code = ((((p[0] - 0xd8) << 2) + ((p[1] & 0xc0) >> 6) + 1) << 16)
155          + ((((p[1] & 0x3f) << 2) + (p[2] - 0xdc)) << 8)
156          + p[3];
157   }
158   else {
159     code = p[0] * 256 + p[1];
160   }
161   return code;
162 }
163 
164 static int
utf16be_code_to_mbclen(OnigCodePoint code)165 utf16be_code_to_mbclen(OnigCodePoint code)
166 {
167   if (code > 0xffff) {
168     if (code > 0x10ffff)
169       return ONIGERR_INVALID_CODE_POINT_VALUE;
170     else
171       return 4;
172   }
173   else {
174     return 2;
175   }
176 }
177 
178 static int
utf16be_code_to_mbc(OnigCodePoint code,UChar * buf)179 utf16be_code_to_mbc(OnigCodePoint code, UChar *buf)
180 {
181   UChar* p = buf;
182 
183   if (code > 0xffff) {
184     unsigned int plane, high;
185 
186     plane = (code >> 16) - 1;
187     *p++ = (plane >> 2) + 0xd8;
188     high = (code & 0xff00) >> 8;
189     *p++ = ((plane & 0x03) << 6) + (high >> 2);
190     *p++ = (high & 0x03) + 0xdc;
191     *p   = (UChar )(code & 0xff);
192     return 4;
193   }
194   else {
195     *p++ = (UChar )((code & 0xff00) >> 8);
196     *p++ = (UChar )(code & 0xff);
197     return 2;
198   }
199 }
200 
201 static int
utf16be_mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end,UChar * fold)202 utf16be_mbc_case_fold(OnigCaseFoldType flag,
203                       const UChar** pp, const UChar* end, UChar* fold)
204 {
205   const UChar* p = *pp;
206 
207   if (ONIGENC_IS_ASCII_CODE(*(p+1)) && *p == 0) {
208     p++;
209 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
210     if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
211       if (*p == 0x49) {
212         *fold++ = 0x01;
213         *fold   = 0x31;
214         (*pp) += 2;
215         return 2;
216       }
217     }
218 #endif
219 
220     *fold++ = 0;
221     *fold   = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
222     *pp += 2;
223     return 2;
224   }
225   else
226     return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF16_BE, flag,
227                                          pp, end, fold);
228 }
229 
230 static UChar*
utf16be_left_adjust_char_head(const UChar * start,const UChar * s)231 utf16be_left_adjust_char_head(const UChar* start, const UChar* s)
232 {
233   if (s <= start) return (UChar* )s;
234 
235   if ((s - start) % 2 == 1) {
236     s--;
237   }
238 
239   if (UTF16_IS_SURROGATE_SECOND(*s) && s > start + 1 &&
240       UTF16_IS_SURROGATE_FIRST(*(s-2)))
241     s -= 2;
242 
243   return (UChar* )s;
244 }
245 
246 static int
utf16be_get_case_fold_codes_by_str(OnigCaseFoldType flag,const OnigUChar * p,const OnigUChar * end,OnigCaseFoldCodeItem items[])247 utf16be_get_case_fold_codes_by_str(OnigCaseFoldType flag,
248     const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
249 {
250   return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF16_BE,
251                                                     flag, p, end, items);
252 }
253 
254 OnigEncodingType OnigEncodingUTF16_BE = {
255   utf16be_mbc_enc_len,
256   "UTF-16BE",   /* name */
257   4,            /* max enc length */
258   2,            /* min enc length */
259   utf16be_is_mbc_newline,
260   utf16be_mbc_to_code,
261   utf16be_code_to_mbclen,
262   utf16be_code_to_mbc,
263   utf16be_mbc_case_fold,
264   onigenc_unicode_apply_all_case_fold,
265   utf16be_get_case_fold_codes_by_str,
266   onigenc_unicode_property_name_to_ctype,
267   onigenc_unicode_is_code_ctype,
268   onigenc_utf16_32_get_ctype_code_range,
269   utf16be_left_adjust_char_head,
270   onigenc_always_false_is_allowed_reverse_match,
271   init,
272   0, /* is_initialized */
273   is_valid_mbc_string,
274   ENC_FLAG_UNICODE|ENC_FLAG_SKIP_OFFSET_2,
275   0, 0
276 };
277