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
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "namespace.h"
44 #include <arpa/inet.h>
50 #include <sys/types.h>
52 #include "un-namespace.h"
56 _RuneLocale *_Read_RuneMagi(FILE *);
59 _Read_RuneMagi(FILE *fp)
70 _FileRuneEntry *runetype_ext_ranges;
71 _FileRuneEntry *maplower_ext_ranges;
72 _FileRuneEntry *mapupper_ext_ranges;
73 int runetype_ext_len = 0;
75 if (_fstat(fileno(fp), &sb) < 0)
78 if ((size_t)sb.st_size < sizeof(_FileRuneLocale)) {
83 if ((fdata = malloc(sb.st_size)) == NULL)
87 rewind(fp); /* Someone might have read the magic number once already */
95 if (fread(fdata, sb.st_size, 1, fp) != 1) {
102 frl = (_FileRuneLocale *)fdata;
103 lastp = fdata + sb.st_size;
107 if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof(frl->magic))) {
113 frl->variable_len = ntohl(frl->variable_len);
114 frl->runetype_ext_nranges = ntohl(frl->runetype_ext_nranges);
115 frl->maplower_ext_nranges = ntohl(frl->maplower_ext_nranges);
116 frl->mapupper_ext_nranges = ntohl(frl->mapupper_ext_nranges);
118 for (x = 0; x < _CACHED_RUNES; ++x) {
119 frl->runetype[x] = ntohl(frl->runetype[x]);
120 frl->maplower[x] = ntohl(frl->maplower[x]);
121 frl->mapupper[x] = ntohl(frl->mapupper[x]);
124 runetype_ext_ranges = (_FileRuneEntry *)variable;
125 variable = runetype_ext_ranges + frl->runetype_ext_nranges;
126 if (variable > lastp) {
132 maplower_ext_ranges = (_FileRuneEntry *)variable;
133 variable = maplower_ext_ranges + frl->maplower_ext_nranges;
134 if (variable > lastp) {
140 mapupper_ext_ranges = (_FileRuneEntry *)variable;
141 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
142 if (variable > lastp) {
148 frr = runetype_ext_ranges;
149 for (x = 0; x < frl->runetype_ext_nranges; ++x) {
152 frr[x].min = ntohl(frr[x].min);
153 frr[x].max = ntohl(frr[x].max);
154 frr[x].map = ntohl(frr[x].map);
155 if (frr[x].map == 0) {
156 int len = frr[x].max - frr[x].min + 1;
158 variable = types + len;
159 runetype_ext_len += len;
160 if (variable > lastp) {
166 types[len] = ntohl(types[len]);
170 frr = maplower_ext_ranges;
171 for (x = 0; x < frl->maplower_ext_nranges; ++x) {
172 frr[x].min = ntohl(frr[x].min);
173 frr[x].max = ntohl(frr[x].max);
174 frr[x].map = ntohl(frr[x].map);
177 frr = mapupper_ext_ranges;
178 for (x = 0; x < frl->mapupper_ext_nranges; ++x) {
179 frr[x].min = ntohl(frr[x].min);
180 frr[x].max = ntohl(frr[x].max);
181 frr[x].map = ntohl(frr[x].map);
183 if ((char *)variable + frl->variable_len > (char *)lastp) {
190 * Convert from disk format to host format.
192 data = malloc(sizeof(_RuneLocale) +
193 (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
194 frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
195 runetype_ext_len * sizeof(*rr->__types) +
204 rl = (_RuneLocale *)data;
205 rl->__variable = rl + 1;
207 memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
208 memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
209 rl->__invalid_rune = 0;
211 rl->__variable_len = frl->variable_len;
212 rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
213 rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
214 rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
216 for (x = 0; x < _CACHED_RUNES; ++x) {
217 rl->__runetype[x] = frl->runetype[x];
218 rl->__maplower[x] = frl->maplower[x];
219 rl->__mapupper[x] = frl->mapupper[x];
222 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
223 rl->__variable = rl->__runetype_ext.__ranges +
224 rl->__runetype_ext.__nranges;
226 rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
227 rl->__variable = rl->__maplower_ext.__ranges +
228 rl->__maplower_ext.__nranges;
230 rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
231 rl->__variable = rl->__mapupper_ext.__ranges +
232 rl->__mapupper_ext.__nranges;
234 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
235 frr = runetype_ext_ranges;
236 rr = rl->__runetype_ext.__ranges;
237 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
240 rr[x].__min = frr[x].min;
241 rr[x].__max = frr[x].max;
242 rr[x].__map = frr[x].map;
243 if (rr[x].__map == 0) {
244 int len = rr[x].__max - rr[x].__min + 1;
246 variable = types + len;
247 rr[x].__types = rl->__variable;
248 rl->__variable = rr[x].__types + len;
250 rr[x].__types[len] = types[len];
252 rr[x].__types = NULL;
255 frr = maplower_ext_ranges;
256 rr = rl->__maplower_ext.__ranges;
257 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
258 rr[x].__min = frr[x].min;
259 rr[x].__max = frr[x].max;
260 rr[x].__map = frr[x].map;
263 frr = mapupper_ext_ranges;
264 rr = rl->__mapupper_ext.__ranges;
265 for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
266 rr[x].__min = frr[x].min;
267 rr[x].__max = frr[x].max;
268 rr[x].__map = frr[x].map;
271 memcpy(rl->__variable, variable, rl->__variable_len);
275 * Go out and zero pointers that should be zero.
277 if (!rl->__variable_len)
278 rl->__variable = NULL;
280 if (!rl->__runetype_ext.__nranges)
281 rl->__runetype_ext.__ranges = NULL;
283 if (!rl->__maplower_ext.__nranges)
284 rl->__maplower_ext.__ranges = NULL;
286 if (!rl->__mapupper_ext.__nranges)
287 rl->__mapupper_ext.__ranges = NULL;