xref: /freebsd/lib/libc/locale/rune.c (revision ca57a343e86ed3015596db68bda17518ad45436a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
5  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
6  * Copyright (c) 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Paul Borman at Krystal Technologies.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. 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.
23  *
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
34  * SUCH DAMAGE.
35  */
36 
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 #include "namespace.h"
42 #include <arpa/inet.h>
43 #include <errno.h>
44 #include <runetype.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/mman.h>
51 #include <fcntl.h>
52 #include <unistd.h>
53 #include "un-namespace.h"
54 
55 #include "runefile.h"
56 
57 _RuneLocale *
58 _Read_RuneMagi(const char *fname)
59 {
60 	char *fdata, *data;
61 	void *lastp;
62 	_FileRuneLocale *frl;
63 	_RuneLocale *rl;
64 	_FileRuneEntry *frr;
65 	_RuneEntry *rr;
66 	struct stat sb;
67 	int x, saverr;
68 	void *variable;
69 	_FileRuneEntry *runetype_ext_ranges;
70 	_FileRuneEntry *maplower_ext_ranges;
71 	_FileRuneEntry *mapupper_ext_ranges;
72 	int runetype_ext_len = 0;
73 	int fd;
74 
75 	if ((fd = _open(fname, O_RDONLY | O_CLOEXEC)) < 0) {
76 		errno = EINVAL;
77 		return (NULL);
78 	}
79 
80 	if (_fstat(fd, &sb) < 0) {
81 		(void) _close(fd);
82 		errno = EINVAL;
83 		return (NULL);
84 	}
85 
86 	if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
87 		(void) _close(fd);
88 		errno = EINVAL;
89 		return (NULL);
90 	}
91 
92 
93 	fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
94 	(void) _close(fd);
95 	if (fdata == MAP_FAILED) {
96 		errno = EINVAL;
97 		return (NULL);
98 	}
99 
100 	frl = (_FileRuneLocale *)(void *)fdata;
101 	lastp = fdata + sb.st_size;
102 
103 	variable = frl + 1;
104 
105 	if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
106 		goto invalid;
107 	}
108 
109 	runetype_ext_ranges = (_FileRuneEntry *)variable;
110 	variable = runetype_ext_ranges + frl->runetype_ext_nranges;
111 	if (variable > lastp) {
112 		goto invalid;
113 	}
114 
115 	maplower_ext_ranges = (_FileRuneEntry *)variable;
116 	variable = maplower_ext_ranges + frl->maplower_ext_nranges;
117 	if (variable > lastp) {
118 		goto invalid;
119 	}
120 
121 	mapupper_ext_ranges = (_FileRuneEntry *)variable;
122 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
123 	if (variable > lastp) {
124 		goto invalid;
125 	}
126 
127 	frr = runetype_ext_ranges;
128 	for (x = 0; x < frl->runetype_ext_nranges; ++x) {
129 		uint32_t *types;
130 
131 		if (frr[x].map == 0) {
132 			int len = frr[x].max - frr[x].min + 1;
133 			types = variable;
134 			variable = types + len;
135 			runetype_ext_len += len;
136 			if (variable > lastp) {
137 				goto invalid;
138 			}
139 		}
140 	}
141 
142 	if ((char *)variable + frl->variable_len > (char *)lastp) {
143 		goto invalid;
144 	}
145 
146 	/*
147 	 * Convert from disk format to host format.
148 	 */
149 	data = malloc(sizeof(_RuneLocale) +
150 	    (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
151 	    frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
152 	    runetype_ext_len * sizeof(*rr->__types) + frl->variable_len);
153 	if (data == NULL) {
154 		saverr = errno;
155 		munmap(fdata, sb.st_size);
156 		errno = saverr;
157 		return (NULL);
158 	}
159 
160 	rl = (_RuneLocale *)data;
161 	rl->__variable = rl + 1;
162 
163 	memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
164 	memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
165 
166 	rl->__variable_len = frl->variable_len;
167 	rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
168 	rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
169 	rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
170 
171 	for (x = 0; x < _CACHED_RUNES; ++x) {
172 		rl->__runetype[x] = frl->runetype[x];
173 		rl->__maplower[x] = frl->maplower[x];
174 		rl->__mapupper[x] = frl->mapupper[x];
175 	}
176 
177 	rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
178 	rl->__variable = rl->__runetype_ext.__ranges +
179 	    rl->__runetype_ext.__nranges;
180 
181 	rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
182 	rl->__variable = rl->__maplower_ext.__ranges +
183 	    rl->__maplower_ext.__nranges;
184 
185 	rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
186 	rl->__variable = rl->__mapupper_ext.__ranges +
187 	    rl->__mapupper_ext.__nranges;
188 
189 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
190 	frr = runetype_ext_ranges;
191 	rr = rl->__runetype_ext.__ranges;
192 	for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
193 		uint32_t *types;
194 
195 		rr[x].__min = frr[x].min;
196 		rr[x].__max = frr[x].max;
197 		rr[x].__map = frr[x].map;
198 		if (rr[x].__map == 0) {
199 			int len = rr[x].__max - rr[x].__min + 1;
200 			types = variable;
201 			variable = types + len;
202 			rr[x].__types = rl->__variable;
203 			rl->__variable = rr[x].__types + len;
204 			while (len-- > 0)
205 				rr[x].__types[len] = types[len];
206 		} else
207 			rr[x].__types = NULL;
208 	}
209 
210 	frr = maplower_ext_ranges;
211 	rr = rl->__maplower_ext.__ranges;
212 	for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
213 		rr[x].__min = frr[x].min;
214 		rr[x].__max = frr[x].max;
215 		rr[x].__map = frr[x].map;
216 	}
217 
218 	frr = mapupper_ext_ranges;
219 	rr = rl->__mapupper_ext.__ranges;
220 	for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
221 		rr[x].__min = frr[x].min;
222 		rr[x].__max = frr[x].max;
223 		rr[x].__map = frr[x].map;
224 	}
225 
226 	memcpy(rl->__variable, variable, rl->__variable_len);
227 	munmap(fdata, sb.st_size);
228 
229 	/*
230 	 * Go out and zero pointers that should be zero.
231 	 */
232 	if (!rl->__variable_len)
233 		rl->__variable = NULL;
234 
235 	if (!rl->__runetype_ext.__nranges)
236 		rl->__runetype_ext.__ranges = NULL;
237 
238 	if (!rl->__maplower_ext.__nranges)
239 		rl->__maplower_ext.__ranges = NULL;
240 
241 	if (!rl->__mapupper_ext.__nranges)
242 		rl->__mapupper_ext.__ranges = NULL;
243 
244 	return (rl);
245 
246 invalid:
247 	munmap(fdata, sb.st_size);
248 	errno = EINVAL;
249 	return (NULL);
250 }
251