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