11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Paul Borman at Krystal Technologies.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 4. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
341573Srgrimesstatic char sccsid[] = "@(#)rune.c	8.1 (Berkeley) 6/4/93";
351573Srgrimes#endif /* LIBC_SCCS and not lint */
3692986Sobrien#include <sys/cdefs.h>
3792986Sobrien__FBSDID("$FreeBSD$");
381573Srgrimes
3971579Sdeischen#include "namespace.h"
4090868Smike#include <arpa/inet.h>
41101498Sache#include <errno.h>
42132820Stjr#include <runetype.h>
431573Srgrimes#include <stdio.h>
4411695Sache#include <string.h>
451573Srgrimes#include <stdlib.h>
4611695Sache#include <sys/types.h>
4711695Sache#include <sys/stat.h>
4871579Sdeischen#include "un-namespace.h"
491573Srgrimes
50146261Sru#include "runefile.h"
51146261Sru
52142582Sru_RuneLocale *_Read_RuneMagi(FILE *);
53142582Sru
5411695Sache_RuneLocale *
55142582Sru_Read_RuneMagi(FILE *fp)
561573Srgrimes{
57142582Sru	char *fdata, *data;
5811695Sache	void *lastp;
59142582Sru	_FileRuneLocale *frl;
601573Srgrimes	_RuneLocale *rl;
61142582Sru	_FileRuneEntry *frr;
6211695Sache	_RuneEntry *rr;
6311695Sache	struct stat sb;
64101498Sache	int x, saverr;
65142582Sru	void *variable;
66142582Sru	_FileRuneEntry *runetype_ext_ranges;
67142582Sru	_FileRuneEntry *maplower_ext_ranges;
68142582Sru	_FileRuneEntry *mapupper_ext_ranges;
69142582Sru	int runetype_ext_len = 0;
701573Srgrimes
7171579Sdeischen	if (_fstat(fileno(fp), &sb) < 0)
72101498Sache		return (NULL);
731573Srgrimes
74142582Sru	if ((size_t)sb.st_size < sizeof(_FileRuneLocale)) {
75101498Sache		errno = EFTYPE;
76101498Sache		return (NULL);
77101498Sache	}
7811695Sache
79142582Sru	if ((fdata = malloc(sb.st_size)) == NULL)
80101498Sache		return (NULL);
8111695Sache
82101498Sache	errno = 0;
8311695Sache	rewind(fp); /* Someone might have read the magic number once already */
84101498Sache	if (errno) {
85101498Sache		saverr = errno;
86142582Sru		free(fdata);
87101498Sache		errno = saverr;
88101498Sache		return (NULL);
89101498Sache	}
9011695Sache
91142582Sru	if (fread(fdata, sb.st_size, 1, fp) != 1) {
92101498Sache		saverr = errno;
93142582Sru		free(fdata);
94101498Sache		errno = saverr;
95101498Sache		return (NULL);
961573Srgrimes	}
971573Srgrimes
98142582Sru	frl = (_FileRuneLocale *)fdata;
99142582Sru	lastp = fdata + sb.st_size;
1001573Srgrimes
101142582Sru	variable = frl + 1;
1021573Srgrimes
103142582Sru	if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof(frl->magic))) {
104142582Sru		free(fdata);
105101498Sache		errno = EFTYPE;
106101498Sache		return (NULL);
10711695Sache	}
1081573Srgrimes
109142582Sru	frl->variable_len = ntohl(frl->variable_len);
110142582Sru	frl->runetype_ext_nranges = ntohl(frl->runetype_ext_nranges);
111142582Sru	frl->maplower_ext_nranges = ntohl(frl->maplower_ext_nranges);
112142582Sru	frl->mapupper_ext_nranges = ntohl(frl->mapupper_ext_nranges);
11311695Sache
11411695Sache	for (x = 0; x < _CACHED_RUNES; ++x) {
115142582Sru		frl->runetype[x] = ntohl(frl->runetype[x]);
116142582Sru		frl->maplower[x] = ntohl(frl->maplower[x]);
117142582Sru		frl->mapupper[x] = ntohl(frl->mapupper[x]);
1181573Srgrimes	}
1191573Srgrimes
120142582Sru	runetype_ext_ranges = (_FileRuneEntry *)variable;
121142582Sru	variable = runetype_ext_ranges + frl->runetype_ext_nranges;
122142582Sru	if (variable > lastp) {
123142582Sru		free(fdata);
124101498Sache		errno = EFTYPE;
125101498Sache		return (NULL);
12611695Sache	}
1271573Srgrimes
128142582Sru	maplower_ext_ranges = (_FileRuneEntry *)variable;
129142582Sru	variable = maplower_ext_ranges + frl->maplower_ext_nranges;
130142582Sru	if (variable > lastp) {
131142582Sru		free(fdata);
132101498Sache		errno = EFTYPE;
133101498Sache		return (NULL);
13411695Sache	}
13511695Sache
136142582Sru	mapupper_ext_ranges = (_FileRuneEntry *)variable;
137142582Sru	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
138142582Sru	if (variable > lastp) {
139142582Sru		free(fdata);
140101498Sache		errno = EFTYPE;
141101498Sache		return (NULL);
14211695Sache	}
14311695Sache
144142582Sru	frr = runetype_ext_ranges;
145142582Sru	for (x = 0; x < frl->runetype_ext_nranges; ++x) {
146142582Sru		uint32_t *types;
147142582Sru
148142582Sru		frr[x].min = ntohl(frr[x].min);
149142582Sru		frr[x].max = ntohl(frr[x].max);
150142582Sru		frr[x].map = ntohl(frr[x].map);
151142582Sru		if (frr[x].map == 0) {
152142582Sru			int len = frr[x].max - frr[x].min + 1;
153142582Sru			types = variable;
154142582Sru			variable = types + len;
155142582Sru			runetype_ext_len += len;
156142582Sru			if (variable > lastp) {
157142582Sru				free(fdata);
158142582Sru				errno = EFTYPE;
159142582Sru				return (NULL);
160142582Sru			}
161142582Sru			while (len-- > 0)
162142582Sru				types[len] = ntohl(types[len]);
163142582Sru		}
164142582Sru	}
165142582Sru
166142582Sru	frr = maplower_ext_ranges;
167142582Sru	for (x = 0; x < frl->maplower_ext_nranges; ++x) {
168142582Sru		frr[x].min = ntohl(frr[x].min);
169142582Sru		frr[x].max = ntohl(frr[x].max);
170142582Sru		frr[x].map = ntohl(frr[x].map);
171142582Sru	}
172142582Sru
173142582Sru	frr = mapupper_ext_ranges;
174142582Sru	for (x = 0; x < frl->mapupper_ext_nranges; ++x) {
175142582Sru		frr[x].min = ntohl(frr[x].min);
176142582Sru		frr[x].max = ntohl(frr[x].max);
177142582Sru		frr[x].map = ntohl(frr[x].map);
178142582Sru	}
179142582Sru	if ((char *)variable + frl->variable_len > (char *)lastp) {
180142582Sru		free(fdata);
181142582Sru		errno = EFTYPE;
182142582Sru		return (NULL);
183142582Sru	}
184142582Sru
185142582Sru	/*
186142582Sru	 * Convert from disk format to host format.
187142582Sru	 */
188142582Sru	data = malloc(sizeof(_RuneLocale) +
189142582Sru	    (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
190142582Sru	    frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
191142582Sru	    runetype_ext_len * sizeof(*rr->__types) +
192142582Sru	    frl->variable_len);
193142582Sru	if (data == NULL) {
194142582Sru		saverr = errno;
195142582Sru		free(fdata);
196142582Sru		errno = saverr;
197142582Sru		return (NULL);
198142582Sru	}
199142582Sru
200142582Sru	rl = (_RuneLocale *)data;
201142582Sru	rl->__variable = rl + 1;
202142582Sru
203142582Sru	memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
204142582Sru	memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
205142582Sru	rl->__invalid_rune = 0;
206142582Sru
207142582Sru	rl->__variable_len = frl->variable_len;
208142582Sru	rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
209142582Sru	rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
210142582Sru	rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
211142582Sru
212142582Sru	for (x = 0; x < _CACHED_RUNES; ++x) {
213142582Sru		rl->__runetype[x] = frl->runetype[x];
214142582Sru		rl->__maplower[x] = frl->maplower[x];
215142582Sru		rl->__mapupper[x] = frl->mapupper[x];
216142582Sru	}
217142582Sru
218142582Sru	rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
219142582Sru	rl->__variable = rl->__runetype_ext.__ranges +
220142582Sru	    rl->__runetype_ext.__nranges;
221142582Sru
222142582Sru	rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
223142582Sru	rl->__variable = rl->__maplower_ext.__ranges +
224142582Sru	    rl->__maplower_ext.__nranges;
225142582Sru
226142582Sru	rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
227142582Sru	rl->__variable = rl->__mapupper_ext.__ranges +
228142582Sru	    rl->__mapupper_ext.__nranges;
229142582Sru
230142582Sru	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
231142582Sru	frr = runetype_ext_ranges;
232142582Sru	rr = rl->__runetype_ext.__ranges;
233130961Stjr	for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
234142582Sru		uint32_t *types;
23511695Sache
236142582Sru		rr[x].__min = frr[x].min;
237142582Sru		rr[x].__max = frr[x].max;
238142582Sru		rr[x].__map = frr[x].map;
239142582Sru		if (rr[x].__map == 0) {
240130961Stjr			int len = rr[x].__max - rr[x].__min + 1;
241142582Sru			types = variable;
242142582Sru			variable = types + len;
243130961Stjr			rr[x].__types = rl->__variable;
244130961Stjr			rl->__variable = rr[x].__types + len;
24511695Sache			while (len-- > 0)
246142582Sru				rr[x].__types[len] = types[len];
24711695Sache		} else
248142582Sru			rr[x].__types = NULL;
24911695Sache	}
25011695Sache
251142582Sru	frr = maplower_ext_ranges;
252142582Sru	rr = rl->__maplower_ext.__ranges;
253130961Stjr	for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
254142582Sru		rr[x].__min = frr[x].min;
255142582Sru		rr[x].__max = frr[x].max;
256142582Sru		rr[x].__map = frr[x].map;
25711695Sache	}
25811695Sache
259142582Sru	frr = mapupper_ext_ranges;
260142582Sru	rr = rl->__mapupper_ext.__ranges;
261130961Stjr	for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
262142582Sru		rr[x].__min = frr[x].min;
263142582Sru		rr[x].__max = frr[x].max;
264142582Sru		rr[x].__map = frr[x].map;
26511695Sache	}
26611695Sache
267142582Sru	memcpy(rl->__variable, variable, rl->__variable_len);
268142582Sru	free(fdata);
269142582Sru
27011695Sache	/*
27111695Sache	 * Go out and zero pointers that should be zero.
27211695Sache	 */
273130961Stjr	if (!rl->__variable_len)
274142582Sru		rl->__variable = NULL;
27511695Sache
276130961Stjr	if (!rl->__runetype_ext.__nranges)
277142582Sru		rl->__runetype_ext.__ranges = NULL;
27811695Sache
279130961Stjr	if (!rl->__maplower_ext.__nranges)
280142582Sru		rl->__maplower_ext.__ranges = NULL;
28111695Sache
282130961Stjr	if (!rl->__mapupper_ext.__nranges)
283142582Sru		rl->__mapupper_ext.__ranges = NULL;
28411695Sache
285101498Sache	return (rl);
2861573Srgrimes}
287