xref: /titanic_44/usr/src/lib/libc/port/locale/rune.c (revision 3ee6e1231b28188bebd1d5bb4a32a673f5820322)
1 /*
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Paul Borman at Krystal Technologies.
7  *
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.
19  *
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.
31  */
32 
33 /*
34  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
35  * Use is subject to license terms.
36  */
37 
38 #include "lint.h"
39 #include "file64.h"
40 #include <errno.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <netinet/in.h>
47 
48 #include "runetype.h"
49 #include "runefile.h"
50 
51 _RuneLocale *_Read_RuneMagi(FILE *);
52 
53 _RuneLocale *
54 _Read_RuneMagi(FILE *fp)
55 {
56 	char *fdata, *data;
57 	void *lastp;
58 	_FileRuneLocale *frl;
59 	_RuneLocale *rl;
60 	_FileRuneEntry *frr;
61 	_RuneEntry *rr;
62 	struct stat sb;
63 	int x, saverr;
64 	void *variable;
65 	_FileRuneEntry *runetype_ext_ranges;
66 	_FileRuneEntry *maplower_ext_ranges;
67 	_FileRuneEntry *mapupper_ext_ranges;
68 	int runetype_ext_len = 0;
69 
70 	if (fstat(fileno(fp), &sb) < 0)
71 		return (NULL);
72 
73 	if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
74 		errno = EINVAL;
75 		return (NULL);
76 	}
77 
78 	if ((fdata = malloc(sb.st_size)) == NULL)
79 		return (NULL);
80 
81 	errno = 0;
82 	rewind(fp); /* Someone might have read the magic number once already */
83 	if (errno) {
84 		saverr = errno;
85 		free(fdata);
86 		errno = saverr;
87 		return (NULL);
88 	}
89 
90 	if (fread(fdata, sb.st_size, 1, fp) != 1) {
91 		saverr = errno;
92 		free(fdata);
93 		errno = saverr;
94 		return (NULL);
95 	}
96 
97 	frl = (_FileRuneLocale *)(void *)fdata;
98 	lastp = fdata + sb.st_size;
99 
100 	variable = frl + 1;
101 
102 	if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
103 		free(fdata);
104 		errno = EINVAL;
105 		return (NULL);
106 	}
107 
108 	frl->variable_len = ntohl(frl->variable_len);
109 	frl->runetype_ext_nranges = ntohl(frl->runetype_ext_nranges);
110 	frl->maplower_ext_nranges = ntohl(frl->maplower_ext_nranges);
111 	frl->mapupper_ext_nranges = ntohl(frl->mapupper_ext_nranges);
112 
113 	for (x = 0; x < _CACHED_RUNES; ++x) {
114 		frl->runetype[x] = ntohl(frl->runetype[x]);
115 		frl->maplower[x] = ntohl(frl->maplower[x]);
116 		frl->mapupper[x] = ntohl(frl->mapupper[x]);
117 	}
118 
119 	runetype_ext_ranges = (_FileRuneEntry *)variable;
120 	variable = runetype_ext_ranges + frl->runetype_ext_nranges;
121 	if (variable > lastp) {
122 		free(fdata);
123 		errno = EINVAL;
124 		return (NULL);
125 	}
126 
127 	maplower_ext_ranges = (_FileRuneEntry *)variable;
128 	variable = maplower_ext_ranges + frl->maplower_ext_nranges;
129 	if (variable > lastp) {
130 		free(fdata);
131 		errno = EINVAL;
132 		return (NULL);
133 	}
134 
135 	mapupper_ext_ranges = (_FileRuneEntry *)variable;
136 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
137 	if (variable > lastp) {
138 		free(fdata);
139 		errno = EINVAL;
140 		return (NULL);
141 	}
142 
143 	frr = runetype_ext_ranges;
144 	for (x = 0; x < frl->runetype_ext_nranges; ++x) {
145 		uint32_t *types;
146 
147 		frr[x].min = ntohl(frr[x].min);
148 		frr[x].max = ntohl(frr[x].max);
149 		frr[x].map = ntohl(frr[x].map);
150 		if (frr[x].map == 0) {
151 			int len = frr[x].max - frr[x].min + 1;
152 			types = variable;
153 			variable = types + len;
154 			runetype_ext_len += len;
155 			if (variable > lastp) {
156 				free(fdata);
157 				errno = EINVAL;
158 				return (NULL);
159 			}
160 			while (len-- > 0)
161 				types[len] = ntohl(types[len]);
162 		}
163 	}
164 
165 	frr = maplower_ext_ranges;
166 	for (x = 0; x < frl->maplower_ext_nranges; ++x) {
167 		frr[x].min = ntohl(frr[x].min);
168 		frr[x].max = ntohl(frr[x].max);
169 		frr[x].map = ntohl(frr[x].map);
170 	}
171 
172 	frr = mapupper_ext_ranges;
173 	for (x = 0; x < frl->mapupper_ext_nranges; ++x) {
174 		frr[x].min = ntohl(frr[x].min);
175 		frr[x].max = ntohl(frr[x].max);
176 		frr[x].map = ntohl(frr[x].map);
177 	}
178 	if ((char *)variable + frl->variable_len > (char *)lastp) {
179 		free(fdata);
180 		errno = EINVAL;
181 		return (NULL);
182 	}
183 
184 	/*
185 	 * Convert from disk format to host format.
186 	 */
187 	data = malloc(sizeof (_RuneLocale) +
188 	    (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
189 	    frl->mapupper_ext_nranges) * sizeof (_RuneEntry) +
190 	    runetype_ext_len * sizeof (*rr->__types) +
191 	    frl->variable_len);
192 	if (data == NULL) {
193 		saverr = errno;
194 		free(fdata);
195 		errno = saverr;
196 		return (NULL);
197 	}
198 
199 	rl = (_RuneLocale *)(void *)data;
200 	rl->__variable = rl + 1;
201 
202 	(void) memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof (rl->__magic));
203 	(void) memcpy(rl->__encoding, frl->encoding, sizeof (rl->__encoding));
204 
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;
209 
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];
214 	}
215 
216 	rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
217 	rl->__variable = rl->__runetype_ext.__ranges +
218 	    rl->__runetype_ext.__nranges;
219 
220 	rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
221 	rl->__variable = rl->__maplower_ext.__ranges +
222 	    rl->__maplower_ext.__nranges;
223 
224 	rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
225 	rl->__variable = rl->__mapupper_ext.__ranges +
226 	    rl->__mapupper_ext.__nranges;
227 
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) {
232 		uint32_t *types;
233 
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;
239 			types = variable;
240 			variable = types + len;
241 			rr[x].__types = rl->__variable;
242 			rl->__variable = rr[x].__types + len;
243 			while (len-- > 0)
244 				rr[x].__types[len] = types[len];
245 		} else
246 			rr[x].__types = NULL;
247 	}
248 
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;
255 	}
256 
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;
263 	}
264 
265 	(void) memcpy(rl->__variable, variable, rl->__variable_len);
266 	free(fdata);
267 
268 	/*
269 	 * Go out and zero pointers that should be zero.
270 	 */
271 	if (!rl->__variable_len)
272 		rl->__variable = NULL;
273 
274 	if (!rl->__runetype_ext.__nranges)
275 		rl->__runetype_ext.__ranges = NULL;
276 
277 	if (!rl->__maplower_ext.__nranges)
278 		rl->__maplower_ext.__ranges = NULL;
279 
280 	if (!rl->__mapupper_ext.__nranges)
281 		rl->__mapupper_ext.__ranges = NULL;
282 
283 	return (rl);
284 }
285