xref: /netbsd/lib/libc/locale/rune.c (revision bf9ec67e)
1 /*	$NetBSD: rune.c,v 1.15 2002/03/18 11:34:40 yamt Exp $	*/
2 
3 /*-
4  * Copyright (c)1999 Citrus Project,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Paul Borman at Krystal Technologies.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  */
64 
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
67 #if 0
68 static char sccsid[] = "@(#)rune.c	8.1 (Berkeley) 6/4/93";
69 #else
70 __RCSID("$NetBSD: rune.c,v 1.15 2002/03/18 11:34:40 yamt Exp $");
71 #endif
72 #endif /* LIBC_SCCS and not lint */
73 
74 #include <assert.h>
75 #include <stdio.h>
76 #include <string.h>
77 #include <stdlib.h>
78 #include <errno.h>
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 #include <citrus/citrus_module.h>
82 #include <citrus/citrus_ctype.h>
83 #include "rune.h"
84 #include "rune_local.h"
85 
86 static int readrange __P((_RuneLocale *, _RuneRange *, _FileRuneRange *, void *, FILE *));
87 static void _freeentry __P((_RuneRange *));
88 
89 static int
90 readrange(_RuneLocale *rl, _RuneRange *rr, _FileRuneRange *frr, void *lastp,
91 	FILE *fp)
92 {
93 	int i;
94 	_RuneEntry *re;
95 	_FileRuneEntry fre;
96 
97 	_DIAGASSERT(rl != NULL);
98 	_DIAGASSERT(rr != NULL);
99 	_DIAGASSERT(frr != NULL);
100 	_DIAGASSERT(lastp != NULL);
101 	_DIAGASSERT(fp != NULL);
102 
103 	re = (_RuneEntry *)rl->rl_variable;
104 
105 	rr->rr_nranges = ntohl(frr->frr_nranges);
106 	if (rr->rr_nranges == 0) {
107 		rr->rr_rune_ranges = NULL;
108 		return 0;
109 	}
110 
111 	rr->rr_rune_ranges = re;
112 	for (i = 0; i < rr->rr_nranges; i++) {
113 		if (fread(&fre, sizeof(fre), 1, fp) != 1)
114 			return -1;
115 
116 		re->re_min = ntohl((u_int32_t)fre.fre_min);
117 		re->re_max = ntohl((u_int32_t)fre.fre_max);
118 		re->re_map = ntohl((u_int32_t)fre.fre_map);
119 		re++;
120 
121 		if ((void *)re > lastp)
122 			return -1;
123 	}
124 	rl->rl_variable = re;
125 	return 0;
126 }
127 
128 static int
129 readentry(_RuneRange *rr, FILE *fp)
130 {
131 	_RuneEntry *re;
132 	size_t l, i, j;
133 	int error;
134 
135 	_DIAGASSERT(rr != NULL);
136 	_DIAGASSERT(fp != NULL);
137 
138 	re = rr->rr_rune_ranges;
139 	for (i = 0; i < rr->rr_nranges; i++) {
140 		if (re[i].re_map != 0) {
141 			re[i].re_rune_types = NULL;
142 			continue;
143 		}
144 
145 		l = re[i].re_max - re[i].re_min + 1;
146 		re[i].re_rune_types = malloc(l * sizeof(_RuneType));
147 		if (!re[i].re_rune_types) {
148 			error = ENOMEM;
149 			goto fail;
150 		}
151 		memset(re[i].re_rune_types, 0, l * sizeof(_RuneType));
152 
153 		if (fread(re[i].re_rune_types, sizeof(_RuneType), l, fp) != l)
154 			goto fail2;
155 
156 		for (j = 0; j < l; j++)
157 			re[i].re_rune_types[j] = ntohl(re[i].re_rune_types[j]);
158 	}
159 	return 0;
160 
161 fail:
162 	for (j = 0; j < i; j++) {
163 		free(re[j].re_rune_types);
164 		re[j].re_rune_types = NULL;
165 	}
166 	return error;
167 fail2:
168 	for (j = 0; j <= i; j++) {
169 		free(re[j].re_rune_types);
170 		re[j].re_rune_types = NULL;
171 	}
172 	return errno;
173 }
174 
175 /* XXX: temporary implementation */
176 static void
177 find_codeset(_RuneLocale *rl)
178 {
179 	char *top, *codeset, *tail;
180 
181 	rl->rl_codeset = NULL;
182 	if (!(top=strstr(rl->rl_variable, _RUNE_CODESET)))
183 		return;
184 	tail = strpbrk(top, " \t");
185 	codeset = top + sizeof(_RUNE_CODESET)-1;
186 	if (tail) {
187 		*top = *tail;
188 		*tail = '\0';
189 		rl->rl_codeset = strdup(codeset);
190 		strcpy(top+1, tail+1);
191 
192 	} else {
193 		*top='\0';
194 		rl->rl_codeset = strdup(codeset);
195 	}
196 }
197 
198 void
199 _freeentry(_RuneRange *rr)
200 {
201 	_RuneEntry *re;
202 	int i;
203 
204 	_DIAGASSERT(rr != NULL);
205 
206 	re = rr->rr_rune_ranges;
207 	for (i = 0; i < rr->rr_nranges; i++) {
208 		if (re[i].re_rune_types)
209 			free(re[i].re_rune_types);
210 		re[i].re_rune_types = NULL;
211 	}
212 }
213 
214 _RuneLocale *
215 _Read_RuneMagi(fp)
216 	FILE *fp;
217 {
218 	/* file */
219 	_FileRuneLocale frl;
220 	/* host data */
221 	char *hostdata;
222 	size_t hostdatalen;
223 	void *lastp;
224 	_RuneLocale *rl;
225 	struct stat sb;
226 	int x;
227 
228 	_DIAGASSERT(fp != NULL);
229 
230 	if (fstat(fileno(fp), &sb) < 0)
231 		return NULL;
232 
233 	if (sb.st_size < sizeof(_RuneLocale))
234 		return NULL;
235 	/* XXX more validation? */
236 
237 	/* Someone might have read the magic number once already */
238 	rewind(fp);
239 
240 	if (fread(&frl, sizeof(frl), 1, fp) != 1)
241 		return NULL;
242 	if (memcmp(frl.frl_magic, _RUNE_MAGIC_1, sizeof(frl.frl_magic)))
243 		return NULL;
244 
245 	hostdatalen = sizeof(*rl) + ntohl((u_int32_t)frl.frl_variable_len) +
246 	    ntohl(frl.frl_runetype_ext.frr_nranges) * sizeof(_RuneEntry) +
247 	    ntohl(frl.frl_maplower_ext.frr_nranges) * sizeof(_RuneEntry) +
248 	    ntohl(frl.frl_mapupper_ext.frr_nranges) * sizeof(_RuneEntry);
249 
250 	if ((hostdata = malloc(hostdatalen)) == NULL)
251 		return NULL;
252 	memset(hostdata, 0, hostdatalen);
253 	lastp = hostdata + hostdatalen;
254 
255 	rl = (_RuneLocale *)(void *)hostdata;
256 	rl->rl_variable = rl + 1;
257 
258 	memcpy(rl->rl_magic, frl.frl_magic, sizeof(rl->rl_magic));
259 	memcpy(rl->rl_encoding, frl.frl_encoding, sizeof(rl->rl_encoding));
260 
261 	rl->rl_invalid_rune = ntohl((u_int32_t)frl.frl_invalid_rune);
262 	rl->rl_variable_len = ntohl((u_int32_t)frl.frl_variable_len);
263 
264 	for (x = 0; x < _CACHED_RUNES; ++x) {
265 		rl->rl_runetype[x] = ntohl(frl.frl_runetype[x]);
266 
267 		/* XXX assumes rune_t = u_int32_t */
268 		rl->rl_maplower[x] = ntohl((u_int32_t)frl.frl_maplower[x]);
269 		rl->rl_mapupper[x] = ntohl((u_int32_t)frl.frl_mapupper[x]);
270 	}
271 
272 	if (readrange(rl, &rl->rl_runetype_ext, &frl.frl_runetype_ext, lastp, fp))
273 	{
274 		free(hostdata);
275 		return NULL;
276 	}
277 	if (readrange(rl, &rl->rl_maplower_ext, &frl.frl_maplower_ext, lastp, fp))
278 	{
279 		free(hostdata);
280 		return NULL;
281 	}
282 	if (readrange(rl, &rl->rl_mapupper_ext, &frl.frl_mapupper_ext, lastp, fp))
283 	{
284 		free(hostdata);
285 		return NULL;
286 	}
287 
288 	if (readentry(&rl->rl_runetype_ext, fp) != 0) {
289 		free(hostdata);
290 		return NULL;
291 	}
292 
293 	if ((u_int8_t *)rl->rl_variable + rl->rl_variable_len >
294 	    (u_int8_t *)lastp) {
295 		_freeentry(&rl->rl_runetype_ext);
296 		free(hostdata);
297 		return NULL;
298 	}
299 	if (rl->rl_variable_len == 0)
300 		rl->rl_variable = NULL;
301 	else if (fread(rl->rl_variable, rl->rl_variable_len, 1, fp) != 1) {
302 		_freeentry(&rl->rl_runetype_ext);
303 		free(hostdata);
304 		return NULL;
305 	}
306 	find_codeset(rl);
307 
308 	/* error if we have junk at the tail */
309 	if (ftell(fp) != sb.st_size) {
310 		_freeentry(&rl->rl_runetype_ext);
311 		free(hostdata);
312 		return NULL;
313 	}
314 
315 	return(rl);
316 }
317 
318 void
319 _NukeRune(rl)
320 	_RuneLocale *rl;
321 {
322 
323 	_DIAGASSERT(rl != NULL);
324 
325 	if (rl != &_DefaultRuneLocale) {
326 		_freeentry(&rl->rl_runetype_ext);
327 		if (rl->rl_codeset)
328 			free(rl->rl_codeset);
329 		if (rl->rl_citrus_ctype)
330 			_citrus_ctype_close(rl->rl_citrus_ctype);
331 		free(rl);
332 	}
333 }
334 
335 /*
336  * read in old LC_CTYPE declaration file, convert into runelocale info
337  */
338 #define _CTYPE_PRIVATE
339 #include <limits.h>
340 #include <ctype.h>
341 
342 _RuneLocale *
343 _Read_CTypeAsRune(fp)
344 	FILE *fp;
345 {
346 	char id[sizeof(_CTYPE_ID) - 1];
347 	u_int32_t i, len;
348 	u_int8_t *new_ctype = NULL;
349 	int16_t *new_toupper = NULL, *new_tolower = NULL;
350 	/* host data */
351 	char *hostdata = NULL;
352 	size_t hostdatalen;
353 	_RuneLocale *rl;
354 	struct stat sb;
355 	int x;
356 
357 	_DIAGASSERT(fp != NULL);
358 
359 	if (fstat(fileno(fp), &sb) < 0)
360 		return NULL;
361 
362 	if (sb.st_size < sizeof(id))
363 		return NULL;
364 	/* XXX more validation? */
365 
366 	/* Someone might have read the magic number once already */
367 	rewind(fp);
368 
369 	if (fread(id, sizeof(id), 1, fp) != 1)
370 		goto bad;
371 	if (memcmp(id, _CTYPE_ID, sizeof(id)) != 0)
372 		goto bad;
373 
374 	if (fread(&i, sizeof(u_int32_t), 1, fp) != 1)
375 		goto bad;
376 	if ((i = ntohl(i)) != _CTYPE_REV)
377 		goto bad;
378 
379 	if (fread(&len, sizeof(u_int32_t), 1, fp) != 1)
380 		goto bad;
381 	if ((len = ntohl(len)) != _CTYPE_NUM_CHARS)
382 		goto bad;
383 
384 	if ((new_ctype = malloc(sizeof(u_int8_t) * (1 + len))) == NULL ||
385 	    (new_toupper = malloc(sizeof(int16_t) * (1 + len))) == NULL ||
386 	    (new_tolower = malloc(sizeof(int16_t) * (1 + len))) == NULL)
387 		goto bad;
388 	new_ctype[0] = 0;
389 	if (fread(&new_ctype[1], sizeof(u_int8_t), len, fp) != len)
390 		goto bad;
391 	new_toupper[0] = EOF;
392 	if (fread(&new_toupper[1], sizeof(int16_t), len, fp) != len)
393 		goto bad;
394 	new_tolower[0] = EOF;
395 	if (fread(&new_tolower[1], sizeof(int16_t), len, fp) != len)
396 		goto bad;
397 
398 	hostdatalen = sizeof(*rl);
399 
400 	if ((hostdata = malloc(hostdatalen)) == NULL)
401 		goto bad;
402 	memset(hostdata, 0, hostdatalen);
403 	rl = (_RuneLocale *)(void *)hostdata;
404 	rl->rl_variable = NULL;
405 
406 	memcpy(rl->rl_magic, _RUNE_MAGIC_1, sizeof(rl->rl_magic));
407 	memcpy(rl->rl_encoding, "NONE", 4);
408 
409 	rl->rl_invalid_rune = _DefaultRuneLocale.rl_invalid_rune;	/*XXX*/
410 	rl->rl_variable_len = 0;
411 
412 	for (x = 0; x < _CACHED_RUNES; ++x) {
413 		if (x > len)
414 			continue;
415 
416 		/*
417 		 * TWEAKS!
418 		 * - old locale file declarations do not have proper _B
419 		 *   in many cases.
420 		 * - isprint() declaration in ctype.h incorrectly uses _B.
421 		 *   _B means "isprint but !isgraph", not "isblank" with the
422 		 *   declaration.
423 		 * - _X and _CTYPE_X have negligible difference in meaning.
424 		 * - we don't set digit value, fearing that it would be
425 		 *   too much of hardcoding.  we may need to revisit it.
426 		 */
427 
428 		if (new_ctype[1 + x] & _U)
429 			rl->rl_runetype[x] |= _CTYPE_U;
430 		if (new_ctype[1 + x] & _L)
431 			rl->rl_runetype[x] |= _CTYPE_L;
432 		if (new_ctype[1 + x] & _N)
433 			rl->rl_runetype[x] |= _CTYPE_D;
434 		if (new_ctype[1 + x] & _S)
435 			rl->rl_runetype[x] |= _CTYPE_S;
436 		if (new_ctype[1 + x] & _P)
437 			rl->rl_runetype[x] |= _CTYPE_P;
438 		if (new_ctype[1 + x] & _C)
439 			rl->rl_runetype[x] |= _CTYPE_C;
440 		/* derived flag bits, duplicate of ctype.h */
441 		if (new_ctype[1 + x] & (_U | _L))
442 			rl->rl_runetype[x] |= _CTYPE_A;
443 		if (new_ctype[1 + x] & (_N | _X))
444 			rl->rl_runetype[x] |= _CTYPE_X;
445 		if (new_ctype[1 + x] & (_P|_U|_L|_N))
446 			rl->rl_runetype[x] |= _CTYPE_G;
447 		/* we don't really trust _B in the file.  see above. */
448 		if (new_ctype[1 + x] & _B)
449 			rl->rl_runetype[x] |= _CTYPE_B;
450 		if ((new_ctype[1 + x] & (_P|_U|_L|_N|_B)) || x == ' ')
451 			rl->rl_runetype[x] |= (_CTYPE_R | _CTYPE_SW1);
452 		if (x == ' ' || x == '\t')
453 			rl->rl_runetype[x] |= _CTYPE_B;
454 
455 		/* XXX may fail on non-8bit encoding only */
456 		rl->rl_mapupper[x] = ntohs(new_toupper[1 + x]);
457 		rl->rl_maplower[x] = ntohs(new_tolower[1 + x]);
458 	}
459 
460 	/*
461 	 * __runetable_to_netbsd_ctype() will be called from
462 	 * setlocale.c:loadlocale(), and fill old ctype table.
463 	 */
464 
465 	free(new_ctype);
466 	free(new_toupper);
467 	free(new_tolower);
468 	return(rl);
469 
470 bad:
471 	if (new_ctype)
472 		free(new_ctype);
473 	if (new_toupper)
474 		free(new_toupper);
475 	if (new_tolower)
476 		free(new_tolower);
477 	if (hostdata)
478 		free(hostdata);
479 	return NULL;
480 }
481