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 * 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
32 * @(#)rune.c 8.1 (Berkeley) 6/4/93
33 * $FreeBSD: head/lib/libc/locale/rune.c 165903 2007-01-09 00:28:16Z imp $
37 #include "namespace.h"
38 #include <arpa/inet.h>
44 #include <sys/types.h>
46 #include "un-namespace.h"
50 _RuneLocale *_Read_RuneMagi(FILE *);
53 _Read_RuneMagi(FILE *fp)
64 _FileRuneEntry *runetype_ext_ranges;
65 _FileRuneEntry *maplower_ext_ranges;
66 _FileRuneEntry *mapupper_ext_ranges;
67 int runetype_ext_len = 0;
69 if (_fstat(fileno(fp), &sb) < 0)
72 if ((size_t)sb.st_size < sizeof(_FileRuneLocale)) {
77 if ((fdata = malloc(sb.st_size)) == NULL)
81 rewind(fp); /* Someone might have read the magic number once already */
89 if (fread(fdata, sb.st_size, 1, fp) != 1) {
96 frl = (_FileRuneLocale *)fdata;
97 lastp = fdata + sb.st_size;
101 if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof(frl->magic))) {
107 frl->variable_len = ntohl(frl->variable_len);
108 frl->runetype_ext_nranges = ntohl(frl->runetype_ext_nranges);
109 frl->maplower_ext_nranges = ntohl(frl->maplower_ext_nranges);
110 frl->mapupper_ext_nranges = ntohl(frl->mapupper_ext_nranges);
112 for (x = 0; x < _CACHED_RUNES; ++x) {
113 frl->runetype[x] = ntohl(frl->runetype[x]);
114 frl->maplower[x] = ntohl(frl->maplower[x]);
115 frl->mapupper[x] = ntohl(frl->mapupper[x]);
118 runetype_ext_ranges = (_FileRuneEntry *)variable;
119 variable = runetype_ext_ranges + frl->runetype_ext_nranges;
120 if (variable > lastp) {
126 maplower_ext_ranges = (_FileRuneEntry *)variable;
127 variable = maplower_ext_ranges + frl->maplower_ext_nranges;
128 if (variable > lastp) {
134 mapupper_ext_ranges = (_FileRuneEntry *)variable;
135 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
136 if (variable > lastp) {
142 frr = runetype_ext_ranges;
143 for (x = 0; x < frl->runetype_ext_nranges; ++x) {
146 frr[x].min = ntohl(frr[x].min);
147 frr[x].max = ntohl(frr[x].max);
148 frr[x].map = ntohl(frr[x].map);
149 if (frr[x].map == 0) {
150 int len = frr[x].max - frr[x].min + 1;
152 variable = types + len;
153 runetype_ext_len += len;
154 if (variable > lastp) {
160 types[len] = ntohl(types[len]);
164 frr = maplower_ext_ranges;
165 for (x = 0; x < frl->maplower_ext_nranges; ++x) {
166 frr[x].min = ntohl(frr[x].min);
167 frr[x].max = ntohl(frr[x].max);
168 frr[x].map = ntohl(frr[x].map);
171 frr = mapupper_ext_ranges;
172 for (x = 0; x < frl->mapupper_ext_nranges; ++x) {
173 frr[x].min = ntohl(frr[x].min);
174 frr[x].max = ntohl(frr[x].max);
175 frr[x].map = ntohl(frr[x].map);
177 if ((char *)variable + frl->variable_len > (char *)lastp) {
184 * Convert from disk format to host format.
186 data = malloc(sizeof(_RuneLocale) +
187 (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
188 frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
189 runetype_ext_len * sizeof(*rr->__types) +
198 rl = (_RuneLocale *)data;
199 rl->__variable = rl + 1;
201 memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
202 memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
203 rl->__invalid_rune = 0;
205 rl->__variable_len = frl->variable_len;
206 rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
207 rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
208 rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
210 for (x = 0; x < _CACHED_RUNES; ++x) {
211 rl->__runetype[x] = frl->runetype[x];
212 rl->__maplower[x] = frl->maplower[x];
213 rl->__mapupper[x] = frl->mapupper[x];
216 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
217 rl->__variable = rl->__runetype_ext.__ranges +
218 rl->__runetype_ext.__nranges;
220 rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
221 rl->__variable = rl->__maplower_ext.__ranges +
222 rl->__maplower_ext.__nranges;
224 rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
225 rl->__variable = rl->__mapupper_ext.__ranges +
226 rl->__mapupper_ext.__nranges;
228 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
229 frr = runetype_ext_ranges;
230 rr = rl->__runetype_ext.__ranges;
231 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
234 rr[x].__min = frr[x].min;
235 rr[x].__max = frr[x].max;
236 rr[x].__map = frr[x].map;
237 if (rr[x].__map == 0) {
238 int len = rr[x].__max - rr[x].__min + 1;
240 variable = types + len;
241 rr[x].__types = rl->__variable;
242 rl->__variable = rr[x].__types + len;
244 rr[x].__types[len] = types[len];
246 rr[x].__types = NULL;
249 frr = maplower_ext_ranges;
250 rr = rl->__maplower_ext.__ranges;
251 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
252 rr[x].__min = frr[x].min;
253 rr[x].__max = frr[x].max;
254 rr[x].__map = frr[x].map;
257 frr = mapupper_ext_ranges;
258 rr = rl->__mapupper_ext.__ranges;
259 for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
260 rr[x].__min = frr[x].min;
261 rr[x].__max = frr[x].max;
262 rr[x].__map = frr[x].map;
265 memcpy(rl->__variable, variable, rl->__variable_len);
269 * Go out and zero pointers that should be zero.
271 if (!rl->__variable_len)
272 rl->__variable = NULL;
274 if (!rl->__runetype_ext.__nranges)
275 rl->__runetype_ext.__ranges = NULL;
277 if (!rl->__maplower_ext.__nranges)
278 rl->__maplower_ext.__ranges = NULL;
280 if (!rl->__mapupper_ext.__nranges)
281 rl->__mapupper_ext.__ranges = NULL;