xref: /PHP-7.2/ext/mbstring/oniguruma/src/utf16_le.c (revision 0ae2f95b)
1 /**********************************************************************
2   utf16_le.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2016  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 "regenc.h"
31 
32 static const int EncLen_UTF16[] = {
33   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
34   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
35   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
36   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
37   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
38   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
39   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
40   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
41   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
42   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
43   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
44   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
45   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
46   2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2,
47   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
49 };
50 
51 static int
utf16le_code_to_mbclen(OnigCodePoint code)52 utf16le_code_to_mbclen(OnigCodePoint code)
53 {
54   return (code > 0xffff ? 4 : 2);
55 }
56 
57 static int
utf16le_mbc_enc_len(const UChar * p)58 utf16le_mbc_enc_len(const UChar* p)
59 {
60   return EncLen_UTF16[*(p+1)];
61 }
62 
63 static int
is_valid_mbc_string(const UChar * p,const UChar * end)64 is_valid_mbc_string(const UChar* p, const UChar* end)
65 {
66   const UChar* end1 = end - 1;
67 
68   while (p < end1) {
69     p += utf16le_mbc_enc_len(p);
70   }
71 
72   if (p != end)
73     return FALSE;
74   else
75     return TRUE;
76 }
77 
78 static int
utf16le_is_mbc_newline(const UChar * p,const UChar * end)79 utf16le_is_mbc_newline(const UChar* p, const UChar* end)
80 {
81   if (p + 1 < end) {
82     if (*p == 0x0a && *(p+1) == 0x00)
83       return 1;
84 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
85     if ((
86 #ifndef USE_CRNL_AS_LINE_TERMINATOR
87          *p == 0x0d ||
88 #endif
89          *p == 0x85) && *(p+1) == 0x00)
90       return 1;
91 
92     if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28))
93       return 1;
94 #endif
95   }
96   return 0;
97 }
98 
99 static OnigCodePoint
utf16le_mbc_to_code(const UChar * p,const UChar * end)100 utf16le_mbc_to_code(const UChar* p, const UChar* end)
101 {
102   OnigCodePoint code;
103   UChar c0 = *p;
104   UChar c1 = *(p+1);
105 
106   if (UTF16_IS_SURROGATE_FIRST(c1)) {
107     if (end - p < 4) return 0;
108     code = ((((c1 - 0xd8) << 2) + ((c0  & 0xc0) >> 6) + 1) << 16)
109          + ((((c0 & 0x3f) << 2) + (p[3] - 0xdc)) << 8)
110          + p[2];
111   }
112   else {
113     code = c1 * 256 + p[0];
114   }
115   return code;
116 }
117 
118 static int
utf16le_code_to_mbc(OnigCodePoint code,UChar * buf)119 utf16le_code_to_mbc(OnigCodePoint code, UChar *buf)
120 {
121   UChar* p = buf;
122 
123   if (code > 0xffff) {
124     unsigned int plane, high;
125 
126     plane = (code >> 16) - 1;
127     high = (code & 0xff00) >> 8;
128 
129     *p++ = ((plane & 0x03) << 6) + (high >> 2);
130     *p++ = (plane >> 2) + 0xd8;
131     *p++ = (UChar )(code & 0xff);
132     *p   = (high & 0x03) + 0xdc;
133     return 4;
134   }
135   else {
136     *p++ = (UChar )(code & 0xff);
137     *p++ = (UChar )((code & 0xff00) >> 8);
138     return 2;
139   }
140 }
141 
142 static int
utf16le_mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end,UChar * fold)143 utf16le_mbc_case_fold(OnigCaseFoldType flag,
144 		      const UChar** pp, const UChar* end, UChar* fold)
145 {
146   const UChar* p = *pp;
147 
148   if (ONIGENC_IS_ASCII_CODE(*p) && *(p+1) == 0) {
149 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
150     if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
151       if (*p == 0x49) {
152         *fold++ = 0x31;
153         *fold   = 0x01;
154         (*pp) += 2;
155         return 2;
156       }
157     }
158 #endif
159 
160     *fold++ = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
161     *fold   = 0;
162     *pp += 2;
163     return 2;
164   }
165   else
166     return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF16_LE, flag, pp, end,
167 					 fold);
168 }
169 
170 #if 0
171 static int
172 utf16le_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp,
173 			 const UChar* end)
174 {
175   const UChar* p = *pp;
176 
177   (*pp) += EncLen_UTF16[*(p+1)];
178 
179   if (*(p+1) == 0) {
180     int c, v;
181 
182     if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
183       return TRUE;
184     }
185 
186     c = *p;
187     v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
188                        (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
189     if ((v | BIT_CTYPE_LOWER) != 0) {
190       /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
191       if (c >= 0xaa && c <= 0xba)
192         return FALSE;
193       else
194         return TRUE;
195     }
196     return (v != 0 ? TRUE : FALSE);
197   }
198 
199   return FALSE;
200 }
201 #endif
202 
203 static UChar*
utf16le_left_adjust_char_head(const UChar * start,const UChar * s)204 utf16le_left_adjust_char_head(const UChar* start, const UChar* s)
205 {
206   if (s <= start) return (UChar* )s;
207 
208   if ((s - start) % 2 == 1) {
209     s--;
210   }
211 
212   if (UTF16_IS_SURROGATE_SECOND(*(s+1)) && s > start + 1)
213     s -= 2;
214 
215   return (UChar* )s;
216 }
217 
218 static int
utf16le_get_case_fold_codes_by_str(OnigCaseFoldType flag,const OnigUChar * p,const OnigUChar * end,OnigCaseFoldCodeItem items[])219 utf16le_get_case_fold_codes_by_str(OnigCaseFoldType flag,
220     const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
221 {
222   return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF16_LE,
223 						    flag, p, end, items);
224 }
225 
226 OnigEncodingType OnigEncodingUTF16_LE = {
227   utf16le_mbc_enc_len,
228   "UTF-16LE",   /* name */
229   4,            /* max byte length */
230   2,            /* min byte length */
231   utf16le_is_mbc_newline,
232   utf16le_mbc_to_code,
233   utf16le_code_to_mbclen,
234   utf16le_code_to_mbc,
235   utf16le_mbc_case_fold,
236   onigenc_unicode_apply_all_case_fold,
237   utf16le_get_case_fold_codes_by_str,
238   onigenc_unicode_property_name_to_ctype,
239   onigenc_unicode_is_code_ctype,
240   onigenc_utf16_32_get_ctype_code_range,
241   utf16le_left_adjust_char_head,
242   onigenc_always_false_is_allowed_reverse_match,
243   NULL, /* init */
244   NULL, /* is_initialized */
245   is_valid_mbc_string
246 };
247