xref: /freebsd/lib/libc/locale/rune.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
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 #include "namespace.h"
38 #include <arpa/inet.h>
39 #include <errno.h>
40 #include <runetype.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 <sys/mman.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include "un-namespace.h"
50 
51 #include "runefile.h"
52 
53 _RuneLocale *
54 _Read_RuneMagi(const char *fname)
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 	int fd;
70 
71 	if ((fd = _open(fname, O_RDONLY | O_CLOEXEC)) < 0) {
72 		errno = EINVAL;
73 		return (NULL);
74 	}
75 
76 	if (_fstat(fd, &sb) < 0) {
77 		(void) _close(fd);
78 		errno = EINVAL;
79 		return (NULL);
80 	}
81 
82 	if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
83 		(void) _close(fd);
84 		errno = EINVAL;
85 		return (NULL);
86 	}
87 
88 
89 	fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
90 	(void) _close(fd);
91 	if (fdata == MAP_FAILED) {
92 		errno = EINVAL;
93 		return (NULL);
94 	}
95 
96 	frl = (_FileRuneLocale *)(void *)fdata;
97 	lastp = fdata + sb.st_size;
98 
99 	variable = frl + 1;
100 
101 	if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
102 		goto invalid;
103 	}
104 
105 	runetype_ext_ranges = (_FileRuneEntry *)variable;
106 	variable = runetype_ext_ranges + frl->runetype_ext_nranges;
107 	if (variable > lastp) {
108 		goto invalid;
109 	}
110 
111 	maplower_ext_ranges = (_FileRuneEntry *)variable;
112 	variable = maplower_ext_ranges + frl->maplower_ext_nranges;
113 	if (variable > lastp) {
114 		goto invalid;
115 	}
116 
117 	mapupper_ext_ranges = (_FileRuneEntry *)variable;
118 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
119 	if (variable > lastp) {
120 		goto invalid;
121 	}
122 
123 	frr = runetype_ext_ranges;
124 	for (x = 0; x < frl->runetype_ext_nranges; ++x) {
125 		uint32_t *types;
126 
127 		if (frr[x].map == 0) {
128 			int len = frr[x].max - frr[x].min + 1;
129 			types = variable;
130 			variable = types + len;
131 			runetype_ext_len += len;
132 			if (variable > lastp) {
133 				goto invalid;
134 			}
135 		}
136 	}
137 
138 	if ((char *)variable + frl->variable_len > (char *)lastp) {
139 		goto invalid;
140 	}
141 
142 	/*
143 	 * Convert from disk format to host format.
144 	 */
145 	data = malloc(sizeof(_RuneLocale) +
146 	    (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
147 	    frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
148 	    runetype_ext_len * sizeof(*rr->__types) + frl->variable_len);
149 	if (data == NULL) {
150 		saverr = errno;
151 		munmap(fdata, sb.st_size);
152 		errno = saverr;
153 		return (NULL);
154 	}
155 
156 	rl = (_RuneLocale *)data;
157 	rl->__variable = rl + 1;
158 
159 	memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
160 	memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
161 
162 	rl->__variable_len = frl->variable_len;
163 	rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
164 	rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
165 	rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
166 
167 	for (x = 0; x < _CACHED_RUNES; ++x) {
168 		rl->__runetype[x] = frl->runetype[x];
169 		rl->__maplower[x] = frl->maplower[x];
170 		rl->__mapupper[x] = frl->mapupper[x];
171 	}
172 
173 	rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
174 	rl->__variable = rl->__runetype_ext.__ranges +
175 	    rl->__runetype_ext.__nranges;
176 
177 	rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
178 	rl->__variable = rl->__maplower_ext.__ranges +
179 	    rl->__maplower_ext.__nranges;
180 
181 	rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
182 	rl->__variable = rl->__mapupper_ext.__ranges +
183 	    rl->__mapupper_ext.__nranges;
184 
185 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
186 	frr = runetype_ext_ranges;
187 	rr = rl->__runetype_ext.__ranges;
188 	for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
189 		uint32_t *types;
190 
191 		rr[x].__min = frr[x].min;
192 		rr[x].__max = frr[x].max;
193 		rr[x].__map = frr[x].map;
194 		if (rr[x].__map == 0) {
195 			int len = rr[x].__max - rr[x].__min + 1;
196 			types = variable;
197 			variable = types + len;
198 			rr[x].__types = rl->__variable;
199 			rl->__variable = rr[x].__types + len;
200 			while (len-- > 0)
201 				rr[x].__types[len] = types[len];
202 		} else
203 			rr[x].__types = NULL;
204 	}
205 
206 	frr = maplower_ext_ranges;
207 	rr = rl->__maplower_ext.__ranges;
208 	for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
209 		rr[x].__min = frr[x].min;
210 		rr[x].__max = frr[x].max;
211 		rr[x].__map = frr[x].map;
212 	}
213 
214 	frr = mapupper_ext_ranges;
215 	rr = rl->__mapupper_ext.__ranges;
216 	for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
217 		rr[x].__min = frr[x].min;
218 		rr[x].__max = frr[x].max;
219 		rr[x].__map = frr[x].map;
220 	}
221 
222 	memcpy(rl->__variable, variable, rl->__variable_len);
223 	munmap(fdata, sb.st_size);
224 
225 	/*
226 	 * Go out and zero pointers that should be zero.
227 	 */
228 	if (!rl->__variable_len)
229 		rl->__variable = NULL;
230 
231 	if (!rl->__runetype_ext.__nranges)
232 		rl->__runetype_ext.__ranges = NULL;
233 
234 	if (!rl->__maplower_ext.__nranges)
235 		rl->__maplower_ext.__ranges = NULL;
236 
237 	if (!rl->__mapupper_ext.__nranges)
238 		rl->__mapupper_ext.__ranges = NULL;
239 
240 	return (rl);
241 
242 invalid:
243 	munmap(fdata, sb.st_size);
244 	errno = EINVAL;
245 	return (NULL);
246 }
247