xref: /illumos-gate/usr/src/lib/libc/port/locale/rune.c (revision 2d08521b)
1 /*
2  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Paul Borman at Krystal Technologies.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include "lint.h"
36 #include "file64.h"
37 #include <errno.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/mman.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 
47 #include "libc.h"
48 #include "runetype.h"
49 #include "runefile.h"
50 
51 _RuneLocale *
_Read_RuneMagi(const char * fname)52 _Read_RuneMagi(const char *fname)
53 {
54 	char *fdata, *data;
55 	void *lastp;
56 	_FileRuneLocale *frl;
57 	_RuneLocale *rl;
58 	_FileRuneEntry *frr;
59 	_RuneEntry *rr;
60 	struct stat sb;
61 	int x, saverr;
62 	void *variable;
63 	_FileRuneEntry *runetype_ext_ranges;
64 	_FileRuneEntry *maplower_ext_ranges;
65 	_FileRuneEntry *mapupper_ext_ranges;
66 	int runetype_ext_len = 0;
67 	int fd;
68 
69 	if ((fd = open(fname, O_RDONLY)) < 0) {
70 		errno = EINVAL;
71 		return (NULL);
72 	}
73 
74 	if (fstat(fd, &sb) < 0) {
75 		(void) close(fd);
76 		errno = EINVAL;
77 		return (NULL);
78 	}
79 
80 	if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
81 		(void) close(fd);
82 		errno = EINVAL;
83 		return (NULL);
84 	}
85 
86 
87 	fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
88 	(void) close(fd);
89 	if (fdata == NULL) {
90 		errno = EINVAL;
91 		return (NULL);
92 	}
93 
94 	frl = (_FileRuneLocale *)(void *)fdata;
95 	lastp = fdata + sb.st_size;
96 
97 	variable = frl + 1;
98 
99 	if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
100 		goto invalid;
101 	}
102 
103 	runetype_ext_ranges = (_FileRuneEntry *)variable;
104 	variable = runetype_ext_ranges + frl->runetype_ext_nranges;
105 	if (variable > lastp) {
106 		goto invalid;
107 	}
108 
109 	maplower_ext_ranges = (_FileRuneEntry *)variable;
110 	variable = maplower_ext_ranges + frl->maplower_ext_nranges;
111 	if (variable > lastp) {
112 		goto invalid;
113 	}
114 
115 	mapupper_ext_ranges = (_FileRuneEntry *)variable;
116 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
117 	if (variable > lastp) {
118 		goto invalid;
119 	}
120 
121 	frr = runetype_ext_ranges;
122 	for (x = 0; x < frl->runetype_ext_nranges; ++x) {
123 		uint32_t *types;
124 
125 		if (frr[x].map == 0) {
126 			int len = frr[x].max - frr[x].min + 1;
127 			types = variable;
128 			variable = types + len;
129 			runetype_ext_len += len;
130 			if (variable > lastp) {
131 				goto invalid;
132 			}
133 		}
134 	}
135 
136 	if ((char *)variable + frl->variable_len > (char *)lastp) {
137 		goto invalid;
138 	}
139 
140 	/*
141 	 * Convert from disk format to host format.
142 	 */
143 	data = libc_malloc(sizeof (_RuneLocale) +
144 	    (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
145 	    frl->mapupper_ext_nranges) * sizeof (_RuneEntry) +
146 	    runetype_ext_len * sizeof (*rr->__types) +
147 	    frl->variable_len);
148 	if (data == NULL) {
149 		saverr = errno;
150 		(void) munmap(fdata, sb.st_size);
151 		errno = saverr;
152 		return (NULL);
153 	}
154 
155 	rl = (_RuneLocale *)(void *)data;
156 	rl->__variable = rl + 1;
157 
158 	(void) memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof (rl->__magic));
159 	(void) memcpy(rl->__encoding, frl->encoding, sizeof (rl->__encoding));
160 
161 	rl->__variable_len = frl->variable_len;
162 	rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
163 	rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
164 	rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
165 
166 	for (x = 0; x < _CACHED_RUNES; ++x) {
167 		rl->__runetype[x] = frl->runetype[x];
168 		rl->__maplower[x] = frl->maplower[x];
169 		rl->__mapupper[x] = frl->mapupper[x];
170 	}
171 
172 	rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
173 	rl->__variable = rl->__runetype_ext.__ranges +
174 	    rl->__runetype_ext.__nranges;
175 
176 	rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
177 	rl->__variable = rl->__maplower_ext.__ranges +
178 	    rl->__maplower_ext.__nranges;
179 
180 	rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
181 	rl->__variable = rl->__mapupper_ext.__ranges +
182 	    rl->__mapupper_ext.__nranges;
183 
184 	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
185 	frr = runetype_ext_ranges;
186 	rr = rl->__runetype_ext.__ranges;
187 	for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
188 		uint32_t *types;
189 
190 		rr[x].__min = frr[x].min;
191 		rr[x].__max = frr[x].max;
192 		rr[x].__map = frr[x].map;
193 		if (rr[x].__map == 0) {
194 			int len = rr[x].__max - rr[x].__min + 1;
195 			types = variable;
196 			variable = types + len;
197 			rr[x].__types = rl->__variable;
198 			rl->__variable = rr[x].__types + len;
199 			while (len-- > 0)
200 				rr[x].__types[len] = types[len];
201 		} else
202 			rr[x].__types = NULL;
203 	}
204 
205 	frr = maplower_ext_ranges;
206 	rr = rl->__maplower_ext.__ranges;
207 	for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
208 		rr[x].__min = frr[x].min;
209 		rr[x].__max = frr[x].max;
210 		rr[x].__map = frr[x].map;
211 	}
212 
213 	frr = mapupper_ext_ranges;
214 	rr = rl->__mapupper_ext.__ranges;
215 	for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
216 		rr[x].__min = frr[x].min;
217 		rr[x].__max = frr[x].max;
218 		rr[x].__map = frr[x].map;
219 	}
220 
221 	(void) memcpy(rl->__variable, variable, rl->__variable_len);
222 	(void) munmap(fdata, sb.st_size);
223 
224 	/*
225 	 * Go out and zero pointers that should be zero.
226 	 */
227 	if (!rl->__variable_len)
228 		rl->__variable = NULL;
229 
230 	if (!rl->__runetype_ext.__nranges)
231 		rl->__runetype_ext.__ranges = NULL;
232 
233 	if (!rl->__maplower_ext.__nranges)
234 		rl->__maplower_ext.__ranges = NULL;
235 
236 	if (!rl->__mapupper_ext.__nranges)
237 		rl->__mapupper_ext.__ranges = NULL;
238 
239 	return (rl);
240 
241 invalid:
242 	(void) munmap(fdata, sb.st_size);
243 	errno = EINVAL;
244 	return (NULL);
245 }
246