xref: /netbsd/lib/libintl/gettext.c (revision 6550d01e)
1 /*	$NetBSD: gettext.c,v 1.25 2007/09/25 08:19:09 junyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001 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  * $Citrus: xpg4dl/FreeBSD/lib/libintl/gettext.c,v 1.31 2001/09/27 15:18:45 yamt Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: gettext.c,v 1.25 2007/09/25 08:19:09 junyoung Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
37 #include <sys/uio.h>
38 
39 #include <assert.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #if 0
46 #include <util.h>
47 #endif
48 #include <libintl.h>
49 #include <locale.h>
50 #include "libintl_local.h"
51 #include "plural_parser.h"
52 #include "pathnames.h"
53 
54 static const char *lookup_category(int);
55 static const char *split_locale(const char *);
56 static const char *lookup_mofile(char *, size_t, const char *, const char *,
57 				 const char *, const char *,
58 				 struct domainbinding *);
59 static uint32_t flip(uint32_t, uint32_t);
60 static int validate(void *, struct mohandle *);
61 static int mapit(const char *, struct domainbinding *);
62 static int unmapit(struct domainbinding *);
63 static const char *lookup_hash(const char *, struct domainbinding *, size_t *);
64 static const char *lookup_bsearch(const char *, struct domainbinding *,
65 				  size_t *);
66 static const char *lookup(const char *, struct domainbinding *, size_t *);
67 static const char *get_lang_env(const char *);
68 
69 /*
70  * shortcut functions.  the main implementation resides in dcngettext().
71  */
72 char *
73 gettext(const char *msgid)
74 {
75 
76 	return dcngettext(NULL, msgid, NULL, 1UL, LC_MESSAGES);
77 }
78 
79 char *
80 dgettext(const char *domainname, const char *msgid)
81 {
82 
83 	return dcngettext(domainname, msgid, NULL, 1UL, LC_MESSAGES);
84 }
85 
86 char *
87 dcgettext(const char *domainname, const char *msgid, int category)
88 {
89 
90 	return dcngettext(domainname, msgid, NULL, 1UL, category);
91 }
92 
93 char *
94 ngettext(const char *msgid1, const char *msgid2, unsigned long int n)
95 {
96 
97 	return dcngettext(NULL, msgid1, msgid2, n, LC_MESSAGES);
98 }
99 
100 char *
101 dngettext(const char *domainname, const char *msgid1, const char *msgid2,
102 	  unsigned long int n)
103 {
104 
105 	return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
106 }
107 
108 /*
109  * dcngettext() -
110  * lookup internationalized message on database locale/category/domainname
111  * (like ja_JP.eucJP/LC_MESSAGES/domainname).
112  * if n equals to 1, internationalized message will be looked up for msgid1.
113  * otherwise, message will be looked up for msgid2.
114  * if the lookup fails, the function will return msgid1 or msgid2 as is.
115  *
116  * Even though the return type is "char *", caller should not rewrite the
117  * region pointed to by the return value (should be "const char *", but can't
118  * change it for compatibility with other implementations).
119  *
120  * by default (if domainname == NULL), domainname is taken from the value set
121  * by textdomain().  usually name of the application (like "ls") is used as
122  * domainname.  category is usually LC_MESSAGES.
123  *
124  * the code reads in *.mo files generated by GNU gettext.  *.mo is a host-
125  * endian encoded file.  both endians are supported here, as the files are in
126  * /usr/share/locale! (or we should move those files into /usr/libdata)
127  */
128 
129 static const char *
130 lookup_category(int category)
131 {
132 
133 	switch (category) {
134 	case LC_COLLATE:	return "LC_COLLATE";
135 	case LC_CTYPE:		return "LC_CTYPE";
136 	case LC_MONETARY:	return "LC_MONETARY";
137 	case LC_NUMERIC:	return "LC_NUMERIC";
138 	case LC_TIME:		return "LC_TIME";
139 	case LC_MESSAGES:	return "LC_MESSAGES";
140 	}
141 	return NULL;
142 }
143 
144 /*
145  * XPG syntax: language[_territory[.codeset]][@modifier]
146  * XXX boundary check on "result" is lacking
147  */
148 static const char *
149 split_locale(const char *lname)
150 {
151 	char buf[BUFSIZ], tmp[BUFSIZ];
152 	char *l, *t, *c, *m;
153 	static char result[BUFSIZ];
154 
155 	memset(result, 0, sizeof(result));
156 
157 	if (strlen(lname) + 1 > sizeof(buf)) {
158 fail:
159 		return lname;
160 	}
161 
162 	strlcpy(buf, lname, sizeof(buf));
163 	m = strrchr(buf, '@');
164 	if (m)
165 		*m++ = '\0';
166 	c = strrchr(buf, '.');
167 	if (c)
168 		*c++ = '\0';
169 	t = strrchr(buf, '_');
170 	if (t)
171 		*t++ = '\0';
172 	l = buf;
173 	if (strlen(l) == 0)
174 		goto fail;
175 	if (c && !t)
176 		goto fail;
177 
178 	if (m) {
179 		if (t) {
180 			if (c) {
181 				snprintf(tmp, sizeof(tmp), "%s_%s.%s@%s",
182 				    l, t, c, m);
183 				strlcat(result, tmp, sizeof(result));
184 				strlcat(result, ":", sizeof(result));
185 			}
186 			snprintf(tmp, sizeof(tmp), "%s_%s@%s", l, t, m);
187 			strlcat(result, tmp, sizeof(result));
188 			strlcat(result, ":", sizeof(result));
189 		}
190 		snprintf(tmp, sizeof(tmp), "%s@%s", l, m);
191 		strlcat(result, tmp, sizeof(result));
192 		strlcat(result, ":", sizeof(result));
193 	}
194 	if (t) {
195 		if (c) {
196 			snprintf(tmp, sizeof(tmp), "%s_%s.%s", l, t, c);
197 			strlcat(result, tmp, sizeof(result));
198 			strlcat(result, ":", sizeof(result));
199 		}
200 		snprintf(tmp, sizeof(tmp), "%s_%s", l, t);
201 		strlcat(result, tmp, sizeof(result));
202 		strlcat(result, ":", sizeof(result));
203 	}
204 	strlcat(result, l, sizeof(result));
205 
206 	return result;
207 }
208 
209 static const char *
210 lookup_mofile(char *buf, size_t len, const char *dir, const char *lpath,
211 	      const char *category, const char *domainname,
212 	      struct domainbinding *db)
213 {
214 	struct stat st;
215 	char *p, *q;
216 	char lpath_tmp[BUFSIZ];
217 
218 	strlcpy(lpath_tmp, lpath, sizeof(lpath_tmp));
219 	q = lpath_tmp;
220 	/* CONSTCOND */
221 	while (1) {
222 		p = strsep(&q, ":");
223 		if (!p)
224 			break;
225 		if (!*p)
226 			continue;
227 
228 		/* don't mess with default locales */
229 		if (strcmp(p, "C") == 0 || strcmp(p, "POSIX") == 0)
230 			return NULL;
231 
232 		/* validate pathname */
233 		if (strchr(p, '/') || strchr(category, '/'))
234 			continue;
235 #if 1	/*?*/
236 		if (strchr(domainname, '/'))
237 			continue;
238 #endif
239 
240 		snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
241 		    category, domainname);
242 		if (stat(buf, &st) < 0)
243 			continue;
244 		if ((st.st_mode & S_IFMT) != S_IFREG)
245 			continue;
246 
247 		if (mapit(buf, db) == 0)
248 			return buf;
249 	}
250 
251 	return NULL;
252 }
253 
254 static uint32_t
255 flip(uint32_t v, uint32_t magic)
256 {
257 
258 	if (magic == MO_MAGIC)
259 		return v;
260 	else if (magic == MO_MAGIC_SWAPPED) {
261 		v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00) |
262 		    ((v << 8) & 0xff0000) | ((v << 24) & 0xff000000);
263 		return v;
264 	} else {
265 		abort();
266 		/*NOTREACHED*/
267 	}
268 }
269 
270 static int
271 validate(void *arg, struct mohandle *mohandle)
272 {
273 	char *p;
274 
275 	p = (char *)arg;
276 	if (p < (char *)mohandle->addr ||
277 	    p > (char *)mohandle->addr + mohandle->len)
278 		return 0;
279 	else
280 		return 1;
281 }
282 
283 /*
284  * calculate the step value if the hash value is conflicted.
285  */
286 static __inline uint32_t
287 calc_collision_step(uint32_t hashval, uint32_t hashsize)
288 {
289 	_DIAGASSERT(hashsize>2);
290 	return (hashval % (hashsize - 2)) + 1;
291 }
292 
293 /*
294  * calculate the next index while conflicting.
295  */
296 static __inline uint32_t
297 calc_next_index(uint32_t curidx, uint32_t hashsize, uint32_t step)
298 {
299 	return curidx+step - (curidx >= hashsize-step ? hashsize : 0);
300 }
301 
302 static int
303 get_sysdep_string_table(struct mosysdepstr_h **table_h, uint32_t *ofstable,
304 			uint32_t nstrings, uint32_t magic, char *base)
305 {
306 	int i, j;
307 	int count;
308 	size_t l;
309 	struct mosysdepstr *table;
310 
311 	for (i=0; i<nstrings; i++) {
312 		/* get mosysdepstr record */
313 		/* LINTED: ignore the alignment problem. */
314 		table = (struct mosysdepstr *)(base + flip(ofstable[i], magic));
315 		/* count number of segments */
316 		count = 0;
317 		while (flip(table->segs[count++].ref, magic) != MO_LASTSEG)
318 			;
319 		/* get table */
320 		l = sizeof(struct mosysdepstr_h) +
321 		    sizeof(struct mosysdepsegentry_h) * (count-1);
322 		table_h[i] = (struct mosysdepstr_h *)malloc(l);
323 		if (!table_h[i])
324 			return -1;
325 		memset(table_h[i], 0, l);
326 		table_h[i]->off = (const char *)(base + flip(table->off, magic));
327 		for (j=0; j<count; j++) {
328 			table_h[i]->segs[j].len =
329 			    flip(table->segs[j].len, magic);
330 			table_h[i]->segs[j].ref =
331 			    flip(table->segs[j].ref, magic);
332 		}
333 		/* LINTED: ignore the alignment problem. */
334 		table = (struct mosysdepstr *)&table->segs[count];
335 	}
336 	return 0;
337 }
338 
339 static int
340 expand_sysdep(struct mohandle *mohandle, struct mosysdepstr_h *str)
341 {
342 	int i;
343 	const char *src;
344 	char *dst;
345 
346 	/* check whether already expanded */
347 	if (str->expanded)
348 		return 0;
349 
350 	/* calc total length */
351 	str->expanded_len = 1;
352 	for (i=0; /*CONSTCOND*/1; i++) {
353 		str->expanded_len += str->segs[i].len;
354 		if (str->segs[i].ref == MO_LASTSEG)
355 			break;
356 		str->expanded_len +=
357 		    mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len;
358 	}
359 	/* expand */
360 	str->expanded = malloc(str->expanded_len);
361 	if (!str->expanded)
362 		return -1;
363 	src = str->off;
364 	dst = str->expanded;
365 	for (i=0; /*CONSTCOND*/1; i++) {
366 		memcpy(dst, src, str->segs[i].len);
367 		src += str->segs[i].len;
368 		dst += str->segs[i].len;
369 		if (str->segs[i].ref == MO_LASTSEG)
370 			break;
371 		memcpy(dst, mohandle->mo.mo_sysdep_segs[str->segs[i].ref].str,
372 		       mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len);
373 		dst += mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len;
374 	}
375 	*dst = '\0';
376 
377 	return 0;
378 }
379 
380 static void
381 insert_to_hash(uint32_t *htable, uint32_t hsize, const char *str, uint32_t ref)
382 {
383 	uint32_t hashval, idx, step;
384 
385 	hashval = __intl_string_hash(str);
386 	step = calc_collision_step(hashval, hsize);
387 	idx = hashval % hsize;
388 
389 	while (htable[idx])
390 		idx = calc_next_index(idx, hsize, step);
391 
392 	htable[idx] = ref;
393 }
394 
395 static int
396 setup_sysdep_stuffs(struct mo *mo, struct mohandle *mohandle, char *base)
397 {
398 	uint32_t magic;
399 	struct moentry *stable;
400 	size_t l;
401 	int i;
402 	char *v;
403 	uint32_t *ofstable;
404 
405 	magic = mo->mo_magic;
406 
407 	mohandle->mo.mo_sysdep_nsegs = flip(mo->mo_sysdep_nsegs, magic);
408 	mohandle->mo.mo_sysdep_nstring = flip(mo->mo_sysdep_nstring, magic);
409 
410 	if (mohandle->mo.mo_sysdep_nstring == 0)
411 		return 0;
412 
413 	/* check hash size */
414 	if (mohandle->mo.mo_hsize <= 2 ||
415 	    mohandle->mo.mo_hsize <
416 	    (mohandle->mo.mo_nstring + mohandle->mo.mo_sysdep_nstring))
417 		return -1;
418 
419 	/* get sysdep segments */
420 	l = sizeof(struct mosysdepsegs_h) * mohandle->mo.mo_sysdep_nsegs;
421 	mohandle->mo.mo_sysdep_segs = (struct mosysdepsegs_h *)malloc(l);
422 	if (!mohandle->mo.mo_sysdep_segs)
423 		return -1;
424 	/* LINTED: ignore the alignment problem. */
425 	stable = (struct moentry *)(base + flip(mo->mo_sysdep_segoff, magic));
426 	for (i=0; i<mohandle->mo.mo_sysdep_nsegs; i++) {
427 		v = base + flip(stable[i].off, magic);
428 		mohandle->mo.mo_sysdep_segs[i].str =
429 		    __intl_sysdep_get_string_by_tag(
430 			    v,
431 			    &mohandle->mo.mo_sysdep_segs[i].len);
432 	}
433 
434 	/* get sysdep string table */
435 	mohandle->mo.mo_sysdep_otable =
436 	    (struct mosysdepstr_h **)calloc(mohandle->mo.mo_sysdep_nstring,
437 					    sizeof(struct mosysdepstr_h *));
438 	if (!mohandle->mo.mo_sysdep_otable)
439 		return -1;
440 	/* LINTED: ignore the alignment problem. */
441 	ofstable = (uint32_t *)(base + flip(mo->mo_sysdep_otable, magic));
442 	if (get_sysdep_string_table(mohandle->mo.mo_sysdep_otable, ofstable,
443 				    mohandle->mo.mo_sysdep_nstring, magic,
444 				    base))
445 		return -1;
446 	mohandle->mo.mo_sysdep_ttable =
447 	    (struct mosysdepstr_h **)calloc(mohandle->mo.mo_sysdep_nstring,
448 					    sizeof(struct mosysdepstr_h *));
449 	if (!mohandle->mo.mo_sysdep_ttable)
450 		return -1;
451 	/* LINTED: ignore the alignment problem. */
452 	ofstable = (uint32_t *)(base + flip(mo->mo_sysdep_ttable, magic));
453 	if (get_sysdep_string_table(mohandle->mo.mo_sysdep_ttable, ofstable,
454 				    mohandle->mo.mo_sysdep_nstring, magic,
455 				    base))
456 		return -1;
457 
458 	/* update hash */
459 	for (i=0; i<mohandle->mo.mo_sysdep_nstring; i++) {
460 		if (expand_sysdep(mohandle, mohandle->mo.mo_sysdep_otable[i]))
461 			return -1;
462 		insert_to_hash(mohandle->mo.mo_htable,
463 			       mohandle->mo.mo_hsize,
464 			       mohandle->mo.mo_sysdep_otable[i]->expanded,
465 			       (i+1) | MO_HASH_SYSDEP_MASK);
466 	}
467 
468 	return 0;
469 }
470 
471 int
472 mapit(const char *path, struct domainbinding *db)
473 {
474 	int fd;
475 	struct stat st;
476 	char *base;
477 	uint32_t magic, revision, flags = 0;
478 	struct moentry *otable, *ttable;
479 	const uint32_t *htable;
480 	struct moentry_h *p;
481 	struct mo *mo;
482 	size_t l, headerlen;
483 	int i;
484 	char *v;
485 	struct mohandle *mohandle = &db->mohandle;
486 
487 	if (mohandle->addr && mohandle->addr != MAP_FAILED &&
488 	    mohandle->mo.mo_magic)
489 		return 0;	/*already opened*/
490 
491 	unmapit(db);
492 
493 #if 0
494 	if (secure_path(path) != 0)
495 		goto fail;
496 #endif
497 	if (stat(path, &st) < 0)
498 		goto fail;
499 	if ((st.st_mode & S_IFMT) != S_IFREG || st.st_size > GETTEXT_MMAP_MAX)
500 		goto fail;
501 	fd = open(path, O_RDONLY);
502 	if (fd < 0)
503 		goto fail;
504 	if (read(fd, &magic, sizeof(magic)) != sizeof(magic) ||
505 	    (magic != MO_MAGIC && magic != MO_MAGIC_SWAPPED)) {
506 		close(fd);
507 		goto fail;
508 	}
509 	if (read(fd, &revision, sizeof(revision)) != sizeof(revision)) {
510 		close(fd);
511 		goto fail;
512 	}
513 	switch (flip(revision, magic)) {
514 	case MO_MAKE_REV(0, 0):
515 		break;
516 	case MO_MAKE_REV(0, 1):
517 	case MO_MAKE_REV(1, 1):
518 		flags |= MO_F_SYSDEP;
519 		break;
520 	default:
521 		close(fd);
522 		goto fail;
523 	}
524 	mohandle->addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
525 	    MAP_FILE | MAP_SHARED, fd, (off_t)0);
526 	if (!mohandle->addr || mohandle->addr == MAP_FAILED) {
527 		close(fd);
528 		goto fail;
529 	}
530 	close(fd);
531 	mohandle->len = (size_t)st.st_size;
532 
533 	base = mohandle->addr;
534 	mo = (struct mo *)mohandle->addr;
535 
536 	/* flip endian.  do not flip magic number! */
537 	mohandle->mo.mo_magic = mo->mo_magic;
538 	mohandle->mo.mo_revision = flip(mo->mo_revision, magic);
539 	mohandle->mo.mo_nstring = flip(mo->mo_nstring, magic);
540 	mohandle->mo.mo_hsize = flip(mo->mo_hsize, magic);
541 	mohandle->mo.mo_flags = flags;
542 
543 	/* validate otable/ttable */
544 	/* LINTED: ignore the alignment problem. */
545 	otable = (struct moentry *)(base + flip(mo->mo_otable, magic));
546 	/* LINTED: ignore the alignment problem. */
547 	ttable = (struct moentry *)(base + flip(mo->mo_ttable, magic));
548 	if (!validate(otable, mohandle) ||
549 	    !validate(&otable[mohandle->mo.mo_nstring], mohandle)) {
550 		unmapit(db);
551 		goto fail;
552 	}
553 	if (!validate(ttable, mohandle) ||
554 	    !validate(&ttable[mohandle->mo.mo_nstring], mohandle)) {
555 		unmapit(db);
556 		goto fail;
557 	}
558 
559 	/* allocate [ot]table, and convert to normal pointer representation. */
560 	l = sizeof(struct moentry_h) * mohandle->mo.mo_nstring;
561 	mohandle->mo.mo_otable = (struct moentry_h *)malloc(l);
562 	if (!mohandle->mo.mo_otable) {
563 		unmapit(db);
564 		goto fail;
565 	}
566 	mohandle->mo.mo_ttable = (struct moentry_h *)malloc(l);
567 	if (!mohandle->mo.mo_ttable) {
568 		unmapit(db);
569 		goto fail;
570 	}
571 	p = mohandle->mo.mo_otable;
572 	for (i = 0; i < mohandle->mo.mo_nstring; i++) {
573 		p[i].len = flip(otable[i].len, magic);
574 		p[i].off = base + flip(otable[i].off, magic);
575 
576 		if (!validate(p[i].off, mohandle) ||
577 		    !validate(p[i].off + p[i].len + 1, mohandle)) {
578 			unmapit(db);
579 			goto fail;
580 		}
581 	}
582 	p = mohandle->mo.mo_ttable;
583 	for (i = 0; i < mohandle->mo.mo_nstring; i++) {
584 		p[i].len = flip(ttable[i].len, magic);
585 		p[i].off = base + flip(ttable[i].off, magic);
586 
587 		if (!validate(p[i].off, mohandle) ||
588 		    !validate(p[i].off + p[i].len + 1, mohandle)) {
589 			unmapit(db);
590 			goto fail;
591 		}
592 	}
593 	/* allocate htable, and convert it to the host order. */
594 	if (mohandle->mo.mo_hsize > 2) {
595 		l = sizeof(uint32_t) * mohandle->mo.mo_hsize;
596 		mohandle->mo.mo_htable = (uint32_t *)malloc(l);
597 		if (!mohandle->mo.mo_htable) {
598 			unmapit(db);
599 			goto fail;
600 		}
601 		/* LINTED: ignore the alignment problem. */
602 		htable = (const uint32_t *)(base+flip(mo->mo_hoffset, magic));
603 		for (i=0; i < mohandle->mo.mo_hsize; i++) {
604 			mohandle->mo.mo_htable[i] = flip(htable[i], magic);
605 			if (mohandle->mo.mo_htable[i] >=
606 			    mohandle->mo.mo_nstring+1) {
607 				/* illegal string number. */
608 				unmapit(db);
609 				goto fail;
610 			}
611 		}
612 	}
613 	/* grab MIME-header and charset field */
614 	mohandle->mo.mo_header = lookup("", db, &headerlen);
615 	if (mohandle->mo.mo_header)
616 		v = strstr(mohandle->mo.mo_header, "charset=");
617 	else
618 		v = NULL;
619 	if (v) {
620 		mohandle->mo.mo_charset = strdup(v + 8);
621 		if (!mohandle->mo.mo_charset)
622 			goto fail;
623 		v = strchr(mohandle->mo.mo_charset, '\n');
624 		if (v)
625 			*v = '\0';
626 	}
627 	if (_gettext_parse_plural(&mohandle->mo.mo_plural,
628 				  &mohandle->mo.mo_nplurals,
629 				  mohandle->mo.mo_header, headerlen))
630 		mohandle->mo.mo_plural = NULL;
631 
632 	/*
633 	 * XXX check charset, reject it if we are unable to support the charset
634 	 * with the current locale.
635 	 * for example, if we are using euc-jp locale and we are looking at
636 	 * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
637 	 * the *.mo file as we cannot support it.
638 	 */
639 
640 	/* system dependent string support */
641 	if ((mohandle->mo.mo_flags & MO_F_SYSDEP) != 0) {
642 		if (setup_sysdep_stuffs(mo, mohandle, base)) {
643 			unmapit(db);
644 			goto fail;
645 		}
646 	}
647 
648 	return 0;
649 
650 fail:
651 	return -1;
652 }
653 
654 static void
655 free_sysdep_table(struct mosysdepstr_h **table, uint32_t nstring)
656 {
657 	uint32_t i;
658 
659 	for (i=0; i<nstring; i++) {
660 		if (table[i]) {
661 			if (table[i]->expanded)
662 				free(table[i]->expanded);
663 			free(table[i]);
664 		}
665 	}
666 	free(table);
667 }
668 
669 static int
670 unmapit(struct domainbinding *db)
671 {
672 	struct mohandle *mohandle = &db->mohandle;
673 
674 	/* unmap if there's already mapped region */
675 	if (mohandle->addr && mohandle->addr != MAP_FAILED)
676 		munmap(mohandle->addr, mohandle->len);
677 	mohandle->addr = NULL;
678 	if (mohandle->mo.mo_otable)
679 		free(mohandle->mo.mo_otable);
680 	if (mohandle->mo.mo_ttable)
681 		free(mohandle->mo.mo_ttable);
682 	if (mohandle->mo.mo_charset)
683 		free(mohandle->mo.mo_charset);
684 	if (mohandle->mo.mo_htable)
685 		free(mohandle->mo.mo_htable);
686 	if (mohandle->mo.mo_sysdep_segs)
687 		free(mohandle->mo.mo_sysdep_segs);
688 	if (mohandle->mo.mo_sysdep_otable) {
689 		free_sysdep_table(mohandle->mo.mo_sysdep_otable,
690 				  mohandle->mo.mo_sysdep_nstring);
691 	}
692 	if (mohandle->mo.mo_sysdep_ttable) {
693 		free_sysdep_table(mohandle->mo.mo_sysdep_ttable,
694 				  mohandle->mo.mo_sysdep_nstring);
695 	}
696 	if (mohandle->mo.mo_plural)
697 		_gettext_free_plural(mohandle->mo.mo_plural);
698 	memset(&mohandle->mo, 0, sizeof(mohandle->mo));
699 	return 0;
700 }
701 
702 /* ARGSUSED */
703 static const char *
704 lookup_hash(const char *msgid, struct domainbinding *db, size_t *rlen)
705 {
706 	struct mohandle *mohandle = &db->mohandle;
707 	uint32_t idx, hashval, step, strno;
708 	size_t len;
709 	struct mosysdepstr_h *sysdep_otable, *sysdep_ttable;
710 
711 	if (mohandle->mo.mo_hsize <= 2 || mohandle->mo.mo_htable == NULL)
712 		return NULL;
713 
714 	hashval = __intl_string_hash(msgid);
715 	step = calc_collision_step(hashval, mohandle->mo.mo_hsize);
716 	idx = hashval % mohandle->mo.mo_hsize;
717 	len = strlen(msgid);
718 	while (/*CONSTCOND*/1) {
719 		strno = mohandle->mo.mo_htable[idx];
720 		if (strno == 0) {
721 			/* unexpected miss */
722 			return NULL;
723 		}
724 		strno--;
725 		if ((strno & MO_HASH_SYSDEP_MASK) == 0) {
726 			/* system independent strings */
727 			if (len <= mohandle->mo.mo_otable[strno].len &&
728 			    !strcmp(msgid, mohandle->mo.mo_otable[strno].off)) {
729 				/* hit */
730 				if (rlen)
731 					*rlen =
732 					    mohandle->mo.mo_ttable[strno].len;
733 				return mohandle->mo.mo_ttable[strno].off;
734 			}
735 		} else {
736 			/* system dependent strings */
737 			strno &= ~MO_HASH_SYSDEP_MASK;
738 			sysdep_otable = mohandle->mo.mo_sysdep_otable[strno];
739 			sysdep_ttable = mohandle->mo.mo_sysdep_ttable[strno];
740 			if (len <= sysdep_otable->expanded_len &&
741 			    !strcmp(msgid, sysdep_otable->expanded)) {
742 				/* hit */
743 				if (expand_sysdep(mohandle, sysdep_ttable))
744 					/* memory exhausted */
745 					return NULL;
746 				if (rlen)
747 					*rlen = sysdep_ttable->expanded_len;
748 				return sysdep_ttable->expanded;
749 			}
750 		}
751 		idx = calc_next_index(idx, mohandle->mo.mo_hsize, step);
752 	}
753 	/*NOTREACHED*/
754 }
755 
756 static const char *
757 lookup_bsearch(const char *msgid, struct domainbinding *db, size_t *rlen)
758 {
759 	int top, bottom, middle, omiddle;
760 	int n;
761 	struct mohandle *mohandle = &db->mohandle;
762 
763 	top = 0;
764 	bottom = mohandle->mo.mo_nstring;
765 	omiddle = -1;
766 	/* CONSTCOND */
767 	while (1) {
768 		if (top > bottom)
769 			break;
770 		middle = (top + bottom) / 2;
771 		/* avoid possible infinite loop, when the data is not sorted */
772 		if (omiddle == middle)
773 			break;
774 		if (middle < 0 || middle >= mohandle->mo.mo_nstring)
775 			break;
776 
777 		n = strcmp(msgid, mohandle->mo.mo_otable[middle].off);
778 		if (n == 0) {
779 			if (rlen)
780 				*rlen = mohandle->mo.mo_ttable[middle].len;
781 			return (const char *)mohandle->mo.mo_ttable[middle].off;
782 		}
783 		else if (n < 0)
784 			bottom = middle;
785 		else
786 			top = middle;
787 		omiddle = middle;
788 	}
789 
790 	return NULL;
791 }
792 
793 static const char *
794 lookup(const char *msgid, struct domainbinding *db, size_t *rlen)
795 {
796 	const char *v;
797 
798 	v = lookup_hash(msgid, db, rlen);
799 	if (v)
800 		return v;
801 
802 	return lookup_bsearch(msgid, db, rlen);
803 }
804 
805 static const char *
806 get_lang_env(const char *category_name)
807 {
808 	const char *lang;
809 
810 	/* 1. see LANGUAGE variable first. */
811 	lang = getenv("LANGUAGE");
812 	if (lang)
813 		return lang;
814 
815 	/* 2. if LANGUAGE isn't set, see LC_ALL, LC_xxx, LANG. */
816 	lang = getenv("LC_ALL");
817 	if (!lang)
818 		lang = getenv(category_name);
819 	if (!lang)
820 		lang = getenv("LANG");
821 
822 	if (!lang)
823 		return 0; /* error */
824 
825 	return split_locale(lang);
826 }
827 
828 static const char *
829 get_indexed_string(const char *str, size_t len, unsigned long idx)
830 {
831 	while (idx > 0) {
832 		if (len <= 1)
833 			return str;
834 		if (*str == '\0')
835 			idx--;
836 		if (len > 0) {
837 			str++;
838 			len--;
839 		}
840 	}
841 	return str;
842 }
843 
844 #define	_NGETTEXT_DEFAULT(msgid1, msgid2, n)	\
845 	((char *)__UNCONST((n) == 1 ? (msgid1) : (msgid2)))
846 
847 char *
848 dcngettext(const char *domainname, const char *msgid1, const char *msgid2,
849 	   unsigned long int n, int category)
850 {
851 	const char *msgid;
852 	char path[PATH_MAX];
853 	const char *lpath;
854 	static char olpath[PATH_MAX];
855 	const char *cname = NULL;
856 	const char *v;
857 	static char *ocname = NULL;
858 	static char *odomainname = NULL;
859 	struct domainbinding *db;
860 	unsigned long plural_index = 0;
861 	size_t len;
862 
863 	if (!domainname)
864 		domainname = __current_domainname;
865 	cname = lookup_category(category);
866 	if (!domainname || !cname)
867 		goto fail;
868 
869 	lpath = get_lang_env(cname);
870 	if (!lpath)
871 		goto fail;
872 
873 	for (db = __bindings; db; db = db->next)
874 		if (strcmp(db->domainname, domainname) == 0)
875 			break;
876 	if (!db) {
877 		if (!bindtextdomain(domainname, _PATH_TEXTDOMAIN))
878 			goto fail;
879 		db = __bindings;
880 	}
881 
882 	/* resolve relative path */
883 	/* XXX not necessary? */
884 	if (db->path[0] != '/') {
885 		char buf[PATH_MAX];
886 
887 		if (getcwd(buf, sizeof(buf)) == 0)
888 			goto fail;
889 		if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
890 			goto fail;
891 		if (strlcat(buf, db->path, sizeof(buf)) >= sizeof(buf))
892 			goto fail;
893 		strlcpy(db->path, buf, sizeof(db->path));
894 	}
895 
896 	/* don't bother looking it up if the values are the same */
897 	if (odomainname && strcmp(domainname, odomainname) == 0 &&
898 	    ocname && strcmp(cname, ocname) == 0 && strcmp(lpath, olpath) == 0 &&
899 	    db->mohandle.mo.mo_magic)
900 		goto found;
901 
902 	/* try to find appropriate file, from $LANGUAGE */
903 	if (lookup_mofile(path, sizeof(path), db->path, lpath, cname,
904 	    domainname, db) == NULL)
905 		goto fail;
906 
907 	if (odomainname)
908 		free(odomainname);
909 	if (ocname)
910 		free(ocname);
911 	odomainname = strdup(domainname);
912 	ocname = strdup(cname);
913 	if (!odomainname || !ocname) {
914 		if (odomainname)
915 			free(odomainname);
916 		if (ocname)
917 			free(ocname);
918 		odomainname = ocname = NULL;
919 	}
920 	else
921 		strlcpy(olpath, lpath, sizeof(olpath));
922 
923 found:
924 	if (db->mohandle.mo.mo_plural) {
925 		plural_index =
926 		    _gettext_calculate_plural(db->mohandle.mo.mo_plural, n);
927 		if (plural_index >= db->mohandle.mo.mo_nplurals)
928 			plural_index = 0;
929 		msgid = msgid1;
930 	} else
931 		msgid = _NGETTEXT_DEFAULT(msgid1, msgid2, n);
932 
933 	if (msgid == NULL)
934 		return NULL;
935 
936 	v = lookup(msgid, db, &len);
937 	if (v) {
938 		if (db->mohandle.mo.mo_plural)
939 			v = get_indexed_string(v, len, plural_index);
940 		/*
941 		 * convert the translated message's encoding.
942 		 *
943 		 * special case:
944 		 *	a result of gettext("") shouldn't need any conversion.
945 		 */
946 		if (msgid[0])
947 			v = __gettext_iconv(v, db);
948 
949 		/*
950 		 * Given the amount of printf-format security issues, it may
951 		 * be a good idea to validate if the original msgid and the
952 		 * translated message format string carry the same printf-like
953 		 * format identifiers.
954 		 */
955 
956 		msgid = v;
957 	}
958 
959 	return (char *)__UNCONST(msgid);
960 
961 fail:
962 	return _NGETTEXT_DEFAULT(msgid1, msgid2, n);
963 }
964