[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / enc / utf_32be.c
blob17841e52a4e82deeda3b7799c44d090e52093899
1 /**********************************************************************
2 utf_32be.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * All rights reserved.
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.
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.
30 #include "regenc.h"
31 #include "iso_8859.h"
33 static OnigCodePoint utf32be_mbc_to_code(const UChar* p, const UChar* end, OnigEncoding enc);
34 static int
35 utf32be_mbc_enc_len(const UChar* p ARG_UNUSED, const OnigUChar* e,
36 OnigEncoding enc)
38 if (e < p) {
39 return ONIGENC_CONSTRUCT_MBCLEN_INVALID();
41 else if (e-p < 4) {
42 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(4-(int)(e-p));
44 else {
45 OnigCodePoint c = utf32be_mbc_to_code(p, e, enc);
46 if (!UNICODE_VALID_CODEPOINT_P(c))
47 return ONIGENC_CONSTRUCT_MBCLEN_INVALID();
48 return ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(4);
52 static int
53 utf32be_is_mbc_newline(const UChar* p, const UChar* end,
54 OnigEncoding enc ARG_UNUSED)
56 if (p + 3 < end) {
57 if (*(p+3) == 0x0a && *(p+2) == 0 && *(p+1) == 0 && *p == 0)
58 return 1;
59 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
60 if ((*(p+3) == 0x0b || *(p+3) == 0x0c || *(p+3) == 0x0d || *(p+3) == 0x85)
61 && *(p+2) == 0 && *(p+1) == 0 && *p == 0x00)
62 return 1;
63 if (*(p+2) == 0x20 && (*(p+3) == 0x29 || *(p+3) == 0x28)
64 && *(p+1) == 0 && *p == 0)
65 return 1;
66 #endif
68 return 0;
71 static OnigCodePoint
72 utf32be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED,
73 OnigEncoding enc ARG_UNUSED)
75 return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
78 static int
79 utf32be_code_to_mbclen(OnigCodePoint code ARG_UNUSED,
80 OnigEncoding enc ARG_UNUSED)
82 return 4;
85 static int
86 utf32be_code_to_mbc(OnigCodePoint code, UChar *buf,
87 OnigEncoding enc ARG_UNUSED)
89 UChar* p = buf;
91 *p++ = (UChar )((code & 0xff000000) >>24);
92 *p++ = (UChar )((code & 0xff0000) >>16);
93 *p++ = (UChar )((code & 0xff00) >> 8);
94 *p++ = (UChar ) (code & 0xff);
95 return 4;
98 static int
99 utf32be_mbc_case_fold(OnigCaseFoldType flag,
100 const UChar** pp, const UChar* end, UChar* fold,
101 OnigEncoding enc)
103 const UChar* p = *pp;
105 if (ONIGENC_IS_ASCII_CODE(*(p+3)) && *(p+2) == 0 && *(p+1) == 0 && *p == 0) {
106 *fold++ = 0;
107 *fold++ = 0;
109 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
110 if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
111 if (*(p+3) == 0x49) {
112 *fold++ = 0x01;
113 *fold = 0x31;
114 (*pp) += 4;
115 return 4;
118 #endif
120 *fold++ = 0;
121 *fold = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*(p+3));
122 *pp += 4;
123 return 4;
125 else
126 return onigenc_unicode_mbc_case_fold(enc, flag, pp,
127 end, fold);
130 #if 0
131 static int
132 utf32be_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp, const UChar* end)
134 const UChar* p = *pp;
136 (*pp) += 4;
138 if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
139 int c, v;
141 p += 3;
142 if (*p == SHARP_s && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
143 return TRUE;
146 c = *p;
147 v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
148 (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
149 if ((v | BIT_CTYPE_LOWER) != 0) {
150 /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
151 if (c >= 0xaa && c <= 0xba)
152 return FALSE;
153 else
154 return TRUE;
156 return (v != 0 ? TRUE : FALSE);
159 return FALSE;
161 #endif
163 static UChar*
164 utf32be_left_adjust_char_head(const UChar* start, const UChar* s, const UChar* end,
165 OnigEncoding enc ARG_UNUSED)
167 ptrdiff_t rem;
169 if (s <= start) return (UChar* )s;
171 rem = (int )((s - start) % 4);
172 return (UChar* )(s - rem);
175 static int
176 utf32be_get_case_fold_codes_by_str(OnigCaseFoldType flag,
177 const OnigUChar* p, const OnigUChar* end,
178 OnigCaseFoldCodeItem items[],
179 OnigEncoding enc)
181 return onigenc_unicode_get_case_fold_codes_by_str(enc,
182 flag, p, end, items);
185 OnigEncodingDefine(utf_32be, UTF_32BE) = {
186 utf32be_mbc_enc_len,
187 "UTF-32BE", /* name */
188 4, /* max byte length */
189 4, /* min byte length */
190 utf32be_is_mbc_newline,
191 utf32be_mbc_to_code,
192 utf32be_code_to_mbclen,
193 utf32be_code_to_mbc,
194 utf32be_mbc_case_fold,
195 onigenc_unicode_apply_all_case_fold,
196 utf32be_get_case_fold_codes_by_str,
197 onigenc_unicode_property_name_to_ctype,
198 onigenc_unicode_is_code_ctype,
199 onigenc_utf16_32_get_ctype_code_range,
200 utf32be_left_adjust_char_head,
201 onigenc_always_false_is_allowed_reverse_match,
202 onigenc_unicode_case_map,
204 ONIGENC_FLAG_UNICODE,
206 ENC_ALIAS("UCS-4BE", "UTF-32BE")