1 /**********************************************************************
2   utf32_le.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2020  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 "regenc.h"
31 
32 static int
utf32le_mbc_enc_len(const UChar * p ARG_UNUSED)33 utf32le_mbc_enc_len(const UChar* p ARG_UNUSED)
34 {
35   return 4;
36 }
37 
38 static int
is_valid_mbc_string(const UChar * s,const UChar * end)39 is_valid_mbc_string(const UChar* s, const UChar* end)
40 {
41   return onigenc_length_check_is_valid_mbc_string(ONIG_ENCODING_UTF32_LE, s, end);
42 }
43 
44 static int
utf32le_is_mbc_newline(const UChar * p,const UChar * end)45 utf32le_is_mbc_newline(const UChar* p, const UChar* end)
46 {
47   if (p + 3 < end) {
48     if (*p == NEWLINE_CODE && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0)
49       return 1;
50 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
51     if ((
52 #ifndef USE_CRNL_AS_LINE_TERMINATOR
53          *p == 0x0d ||
54 #endif
55          *p == 0x85)
56         && *(p+1) == 0x00 && (p+2) == 0x00 && *(p+3) == 0x00)
57       return 1;
58 
59     if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28)
60         && *(p+2) == 0x00 && *(p+3) == 0x00)
61       return 1;
62 #endif
63   }
64   return 0;
65 }
66 
67 static OnigCodePoint
utf32le_mbc_to_code(const UChar * p,const UChar * end ARG_UNUSED)68 utf32le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
69 {
70   OnigCodePoint code;
71 
72   code = (OnigCodePoint )((((p[3] & 0x7f) * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
73   return code;
74 }
75 
76 static int
utf32le_code_to_mbclen(OnigCodePoint code ARG_UNUSED)77 utf32le_code_to_mbclen(OnigCodePoint code ARG_UNUSED)
78 {
79   return 4;
80 }
81 
82 static int
utf32le_code_to_mbc(OnigCodePoint code,UChar * buf)83 utf32le_code_to_mbc(OnigCodePoint code, UChar *buf)
84 {
85   UChar* p = buf;
86 
87   *p++ = (UChar ) (code & 0xff);
88   *p++ = (UChar )((code & 0xff00)     >> 8);
89   *p++ = (UChar )((code & 0xff0000)   >>16);
90   *p++ = (UChar )((code & 0xff000000) >>24);
91   return 4;
92 }
93 
94 static int
utf32le_mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end,UChar * fold)95 utf32le_mbc_case_fold(OnigCaseFoldType flag,
96                       const UChar** pp, const UChar* end, UChar* fold)
97 {
98   const UChar* p = *pp;
99 
100   if (ONIGENC_IS_ASCII_CODE(*p) && *(p+1) == 0 && *(p+2) == 0 && *(p+3) == 0) {
101 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
102     if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
103       if (*p == 0x49) {
104         *fold++ = 0x31;
105         *fold++ = 0x01;
106       }
107     }
108     else {
109 #endif
110       *fold++ = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
111       *fold++ = 0;
112 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
113     }
114 #endif
115 
116     *fold++ = 0;
117     *fold   = 0;
118     *pp += 4;
119     return 4;
120   }
121   else
122     return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF32_LE, flag, pp, end,
123                                          fold);
124 }
125 
126 static UChar*
utf32le_left_adjust_char_head(const UChar * start,const UChar * s)127 utf32le_left_adjust_char_head(const UChar* start, const UChar* s)
128 {
129   int rem;
130 
131   if (s <= start) return (UChar* )s;
132 
133   rem = (s - start) % 4;
134   return (UChar* )(s - rem);
135 }
136 
137 static int
utf32le_get_case_fold_codes_by_str(OnigCaseFoldType flag,const OnigUChar * p,const OnigUChar * end,OnigCaseFoldCodeItem items[])138 utf32le_get_case_fold_codes_by_str(OnigCaseFoldType flag,
139     const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
140 {
141   return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF32_LE,
142                                                     flag, p, end, items);
143 }
144 
145 OnigEncodingType OnigEncodingUTF32_LE = {
146   utf32le_mbc_enc_len,
147   "UTF-32LE",   /* name */
148   4,            /* max enc length */
149   4,            /* min enc length */
150   utf32le_is_mbc_newline,
151   utf32le_mbc_to_code,
152   utf32le_code_to_mbclen,
153   utf32le_code_to_mbc,
154   utf32le_mbc_case_fold,
155   onigenc_unicode_apply_all_case_fold,
156   utf32le_get_case_fold_codes_by_str,
157   onigenc_unicode_property_name_to_ctype,
158   onigenc_unicode_is_code_ctype,
159   onigenc_utf16_32_get_ctype_code_range,
160   utf32le_left_adjust_char_head,
161   onigenc_always_false_is_allowed_reverse_match,
162   NULL, /* init */
163   NULL, /* is_initialized */
164   is_valid_mbc_string,
165   ENC_FLAG_UNICODE|ENC_FLAG_SKIP_OFFSET_1,
166   0, 0
167 };
168