compiler/clib/regex: Switched to NetBSD version of the regex functions.
[AROS.git] / compiler / clib / locale / rune.c
blob3e5fbe9f45606c191d9c5524e0b95368e5a04ed2
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/locale/rune.c,v 1.15 2007/01/09 00:28:00 imp Exp $");
39 #include "namespace.h"
40 #ifdef __AROS__
41 /* until we have arpa/inet.h in libc */
42 #include <exec/types.h>
43 #include <aros/macros.h>
44 #define ntohl(x) AROS_BE2LONG(x)
45 #else
46 #include <arpa/inet.h>
47 #endif
48 #include <errno.h>
49 #include <runetype.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include "un-namespace.h"
57 #include "runefile.h"
59 _RuneLocale *_Read_RuneMagi(FILE *);
61 _RuneLocale *
62 _Read_RuneMagi(FILE *fp)
64 char *fdata, *data;
65 void *lastp;
66 _FileRuneLocale *frl;
67 _RuneLocale *rl;
68 _FileRuneEntry *frr;
69 _RuneEntry *rr;
70 struct stat sb;
71 int x, saverr;
72 void *variable;
73 _FileRuneEntry *runetype_ext_ranges;
74 _FileRuneEntry *maplower_ext_ranges;
75 _FileRuneEntry *mapupper_ext_ranges;
76 int runetype_ext_len = 0;
78 if (_fstat(fileno(fp), &sb) < 0)
79 return (NULL);
81 if ((size_t)sb.st_size < sizeof(_FileRuneLocale)) {
82 errno = EFTYPE;
83 return (NULL);
86 if ((fdata = malloc(sb.st_size)) == NULL)
87 return (NULL);
89 errno = 0;
90 rewind(fp); /* Someone might have read the magic number once already */
91 if (errno) {
92 saverr = errno;
93 free(fdata);
94 errno = saverr;
95 return (NULL);
98 if (fread(fdata, sb.st_size, 1, fp) != 1) {
99 saverr = errno;
100 free(fdata);
101 errno = saverr;
102 return (NULL);
105 frl = (_FileRuneLocale *)fdata;
106 lastp = fdata + sb.st_size;
108 variable = frl + 1;
110 if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof(frl->magic))) {
111 free(fdata);
112 errno = EFTYPE;
113 return (NULL);
116 frl->variable_len = ntohl(frl->variable_len);
117 frl->runetype_ext_nranges = ntohl(frl->runetype_ext_nranges);
118 frl->maplower_ext_nranges = ntohl(frl->maplower_ext_nranges);
119 frl->mapupper_ext_nranges = ntohl(frl->mapupper_ext_nranges);
121 for (x = 0; x < _CACHED_RUNES; ++x) {
122 frl->runetype[x] = ntohl(frl->runetype[x]);
123 frl->maplower[x] = ntohl(frl->maplower[x]);
124 frl->mapupper[x] = ntohl(frl->mapupper[x]);
127 runetype_ext_ranges = (_FileRuneEntry *)variable;
128 variable = runetype_ext_ranges + frl->runetype_ext_nranges;
129 if (variable > lastp) {
130 free(fdata);
131 errno = EFTYPE;
132 return (NULL);
135 maplower_ext_ranges = (_FileRuneEntry *)variable;
136 variable = maplower_ext_ranges + frl->maplower_ext_nranges;
137 if (variable > lastp) {
138 free(fdata);
139 errno = EFTYPE;
140 return (NULL);
143 mapupper_ext_ranges = (_FileRuneEntry *)variable;
144 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
145 if (variable > lastp) {
146 free(fdata);
147 errno = EFTYPE;
148 return (NULL);
151 frr = runetype_ext_ranges;
152 for (x = 0; x < frl->runetype_ext_nranges; ++x) {
153 uint32_t *types;
155 frr[x].min = ntohl(frr[x].min);
156 frr[x].max = ntohl(frr[x].max);
157 frr[x].map = ntohl(frr[x].map);
158 if (frr[x].map == 0) {
159 int len = frr[x].max - frr[x].min + 1;
160 types = variable;
161 variable = types + len;
162 runetype_ext_len += len;
163 if (variable > lastp) {
164 free(fdata);
165 errno = EFTYPE;
166 return (NULL);
168 while (len-- > 0)
169 types[len] = ntohl(types[len]);
173 frr = maplower_ext_ranges;
174 for (x = 0; x < frl->maplower_ext_nranges; ++x) {
175 frr[x].min = ntohl(frr[x].min);
176 frr[x].max = ntohl(frr[x].max);
177 frr[x].map = ntohl(frr[x].map);
180 frr = mapupper_ext_ranges;
181 for (x = 0; x < frl->mapupper_ext_nranges; ++x) {
182 frr[x].min = ntohl(frr[x].min);
183 frr[x].max = ntohl(frr[x].max);
184 frr[x].map = ntohl(frr[x].map);
186 if ((char *)variable + frl->variable_len > (char *)lastp) {
187 free(fdata);
188 errno = EFTYPE;
189 return (NULL);
193 * Convert from disk format to host format.
195 data = malloc(sizeof(_RuneLocale) +
196 (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
197 frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
198 runetype_ext_len * sizeof(*rr->__types) +
199 frl->variable_len);
200 if (data == NULL) {
201 saverr = errno;
202 free(fdata);
203 errno = saverr;
204 return (NULL);
207 rl = (_RuneLocale *)data;
208 rl->__variable = rl + 1;
210 memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
211 memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
212 rl->__invalid_rune = 0;
214 rl->__variable_len = frl->variable_len;
215 rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
216 rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
217 rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
219 for (x = 0; x < _CACHED_RUNES; ++x) {
220 rl->__runetype[x] = frl->runetype[x];
221 rl->__maplower[x] = frl->maplower[x];
222 rl->__mapupper[x] = frl->mapupper[x];
225 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
226 rl->__variable = rl->__runetype_ext.__ranges +
227 rl->__runetype_ext.__nranges;
229 rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
230 rl->__variable = rl->__maplower_ext.__ranges +
231 rl->__maplower_ext.__nranges;
233 rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
234 rl->__variable = rl->__mapupper_ext.__ranges +
235 rl->__mapupper_ext.__nranges;
237 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
238 frr = runetype_ext_ranges;
239 rr = rl->__runetype_ext.__ranges;
240 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
241 uint32_t *types;
243 rr[x].__min = frr[x].min;
244 rr[x].__max = frr[x].max;
245 rr[x].__map = frr[x].map;
246 if (rr[x].__map == 0) {
247 int len = rr[x].__max - rr[x].__min + 1;
248 types = variable;
249 variable = types + len;
250 rr[x].__types = rl->__variable;
251 rl->__variable = rr[x].__types + len;
252 while (len-- > 0)
253 rr[x].__types[len] = types[len];
254 } else
255 rr[x].__types = NULL;
258 frr = maplower_ext_ranges;
259 rr = rl->__maplower_ext.__ranges;
260 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
261 rr[x].__min = frr[x].min;
262 rr[x].__max = frr[x].max;
263 rr[x].__map = frr[x].map;
266 frr = mapupper_ext_ranges;
267 rr = rl->__mapupper_ext.__ranges;
268 for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
269 rr[x].__min = frr[x].min;
270 rr[x].__max = frr[x].max;
271 rr[x].__map = frr[x].map;
274 memcpy(rl->__variable, variable, rl->__variable_len);
275 free(fdata);
278 * Go out and zero pointers that should be zero.
280 if (!rl->__variable_len)
281 rl->__variable = NULL;
283 if (!rl->__runetype_ext.__nranges)
284 rl->__runetype_ext.__ranges = NULL;
286 if (!rl->__maplower_ext.__nranges)
287 rl->__maplower_ext.__ranges = NULL;
289 if (!rl->__mapupper_ext.__nranges)
290 rl->__mapupper_ext.__ranges = NULL;
292 return (rl);