xref: /netbsd-src/lib/libintl/gettext.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: gettext.c,v 1.27 2012/03/21 10:10:36 matt 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.27 2012/03/21 10:10:36 matt 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 	unsigned int i;
307 	int j, 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 	unsigned 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 	unsigned 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 (!mohandle->mo.mo_header ||
628 	    _gettext_parse_plural(&mohandle->mo.mo_plural,
629 				  &mohandle->mo.mo_nplurals,
630 				  mohandle->mo.mo_header, headerlen))
631 		mohandle->mo.mo_plural = NULL;
632 
633 	/*
634 	 * XXX check charset, reject it if we are unable to support the charset
635 	 * with the current locale.
636 	 * for example, if we are using euc-jp locale and we are looking at
637 	 * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
638 	 * the *.mo file as we cannot support it.
639 	 */
640 
641 	/* system dependent string support */
642 	if ((mohandle->mo.mo_flags & MO_F_SYSDEP) != 0) {
643 		if (setup_sysdep_stuffs(mo, mohandle, base)) {
644 			unmapit(db);
645 			goto fail;
646 		}
647 	}
648 
649 	return 0;
650 
651 fail:
652 	return -1;
653 }
654 
655 static void
656 free_sysdep_table(struct mosysdepstr_h **table, uint32_t nstring)
657 {
658 	uint32_t i;
659 
660 	for (i=0; i<nstring; i++) {
661 		if (table[i]) {
662 			if (table[i]->expanded)
663 				free(table[i]->expanded);
664 			free(table[i]);
665 		}
666 	}
667 	free(table);
668 }
669 
670 static int
671 unmapit(struct domainbinding *db)
672 {
673 	struct mohandle *mohandle = &db->mohandle;
674 
675 	/* unmap if there's already mapped region */
676 	if (mohandle->addr && mohandle->addr != MAP_FAILED)
677 		munmap(mohandle->addr, mohandle->len);
678 	mohandle->addr = NULL;
679 	if (mohandle->mo.mo_otable)
680 		free(mohandle->mo.mo_otable);
681 	if (mohandle->mo.mo_ttable)
682 		free(mohandle->mo.mo_ttable);
683 	if (mohandle->mo.mo_charset)
684 		free(mohandle->mo.mo_charset);
685 	if (mohandle->mo.mo_htable)
686 		free(mohandle->mo.mo_htable);
687 	if (mohandle->mo.mo_sysdep_segs)
688 		free(mohandle->mo.mo_sysdep_segs);
689 	if (mohandle->mo.mo_sysdep_otable) {
690 		free_sysdep_table(mohandle->mo.mo_sysdep_otable,
691 				  mohandle->mo.mo_sysdep_nstring);
692 	}
693 	if (mohandle->mo.mo_sysdep_ttable) {
694 		free_sysdep_table(mohandle->mo.mo_sysdep_ttable,
695 				  mohandle->mo.mo_sysdep_nstring);
696 	}
697 	if (mohandle->mo.mo_plural)
698 		_gettext_free_plural(mohandle->mo.mo_plural);
699 	memset(&mohandle->mo, 0, sizeof(mohandle->mo));
700 	return 0;
701 }
702 
703 /* ARGSUSED */
704 static const char *
705 lookup_hash(const char *msgid, struct domainbinding *db, size_t *rlen)
706 {
707 	struct mohandle *mohandle = &db->mohandle;
708 	uint32_t idx, hashval, step, strno;
709 	size_t len;
710 	struct mosysdepstr_h *sysdep_otable, *sysdep_ttable;
711 
712 	if (mohandle->mo.mo_hsize <= 2 || mohandle->mo.mo_htable == NULL)
713 		return NULL;
714 
715 	hashval = __intl_string_hash(msgid);
716 	step = calc_collision_step(hashval, mohandle->mo.mo_hsize);
717 	idx = hashval % mohandle->mo.mo_hsize;
718 	len = strlen(msgid);
719 	while (/*CONSTCOND*/1) {
720 		strno = mohandle->mo.mo_htable[idx];
721 		if (strno == 0) {
722 			/* unexpected miss */
723 			return NULL;
724 		}
725 		strno--;
726 		if ((strno & MO_HASH_SYSDEP_MASK) == 0) {
727 			/* system independent strings */
728 			if (len <= mohandle->mo.mo_otable[strno].len &&
729 			    !strcmp(msgid, mohandle->mo.mo_otable[strno].off)) {
730 				/* hit */
731 				if (rlen)
732 					*rlen =
733 					    mohandle->mo.mo_ttable[strno].len;
734 				return mohandle->mo.mo_ttable[strno].off;
735 			}
736 		} else {
737 			/* system dependent strings */
738 			strno &= ~MO_HASH_SYSDEP_MASK;
739 			sysdep_otable = mohandle->mo.mo_sysdep_otable[strno];
740 			sysdep_ttable = mohandle->mo.mo_sysdep_ttable[strno];
741 			if (len <= sysdep_otable->expanded_len &&
742 			    !strcmp(msgid, sysdep_otable->expanded)) {
743 				/* hit */
744 				if (expand_sysdep(mohandle, sysdep_ttable))
745 					/* memory exhausted */
746 					return NULL;
747 				if (rlen)
748 					*rlen = sysdep_ttable->expanded_len;
749 				return sysdep_ttable->expanded;
750 			}
751 		}
752 		idx = calc_next_index(idx, mohandle->mo.mo_hsize, step);
753 	}
754 	/*NOTREACHED*/
755 }
756 
757 static const char *
758 lookup_bsearch(const char *msgid, struct domainbinding *db, size_t *rlen)
759 {
760 	int top, bottom, middle, omiddle;
761 	int n;
762 	struct mohandle *mohandle = &db->mohandle;
763 
764 	top = 0;
765 	bottom = mohandle->mo.mo_nstring;
766 	omiddle = -1;
767 	/* CONSTCOND */
768 	while (1) {
769 		if (top > bottom)
770 			break;
771 		middle = (top + bottom) / 2;
772 		/* avoid possible infinite loop, when the data is not sorted */
773 		if (omiddle == middle)
774 			break;
775 		if ((size_t)middle >= mohandle->mo.mo_nstring)
776 			break;
777 
778 		n = strcmp(msgid, mohandle->mo.mo_otable[middle].off);
779 		if (n == 0) {
780 			if (rlen)
781 				*rlen = mohandle->mo.mo_ttable[middle].len;
782 			return (const char *)mohandle->mo.mo_ttable[middle].off;
783 		}
784 		else if (n < 0)
785 			bottom = middle;
786 		else
787 			top = middle;
788 		omiddle = middle;
789 	}
790 
791 	return NULL;
792 }
793 
794 static const char *
795 lookup(const char *msgid, struct domainbinding *db, size_t *rlen)
796 {
797 	const char *v;
798 
799 	v = lookup_hash(msgid, db, rlen);
800 	if (v)
801 		return v;
802 
803 	return lookup_bsearch(msgid, db, rlen);
804 }
805 
806 static const char *
807 get_lang_env(const char *category_name)
808 {
809 	const char *lang;
810 
811 	/* 1. see LANGUAGE variable first. */
812 	lang = getenv("LANGUAGE");
813 	if (lang)
814 		return lang;
815 
816 	/* 2. if LANGUAGE isn't set, see LC_ALL, LC_xxx, LANG. */
817 	lang = getenv("LC_ALL");
818 	if (!lang)
819 		lang = getenv(category_name);
820 	if (!lang)
821 		lang = getenv("LANG");
822 
823 	if (!lang)
824 		return 0; /* error */
825 
826 	return split_locale(lang);
827 }
828 
829 static const char *
830 get_indexed_string(const char *str, size_t len, unsigned long idx)
831 {
832 	while (idx > 0) {
833 		if (len <= 1)
834 			return str;
835 		if (*str == '\0')
836 			idx--;
837 		if (len > 0) {
838 			str++;
839 			len--;
840 		}
841 	}
842 	return str;
843 }
844 
845 #define	_NGETTEXT_DEFAULT(msgid1, msgid2, n)	\
846 	((char *)__UNCONST((n) == 1 ? (msgid1) : (msgid2)))
847 
848 char *
849 dcngettext(const char *domainname, const char *msgid1, const char *msgid2,
850 	   unsigned long int n, int category)
851 {
852 	const char *msgid;
853 	char path[PATH_MAX];
854 	const char *lpath;
855 	static char olpath[PATH_MAX];
856 	const char *cname = NULL;
857 	const char *v;
858 	static char *ocname = NULL;
859 	static char *odomainname = NULL;
860 	struct domainbinding *db;
861 	unsigned long plural_index = 0;
862 	size_t len;
863 
864 	if (!domainname)
865 		domainname = __current_domainname;
866 	cname = lookup_category(category);
867 	if (!domainname || !cname)
868 		goto fail;
869 
870 	lpath = get_lang_env(cname);
871 	if (!lpath)
872 		goto fail;
873 
874 	for (db = __bindings; db; db = db->next)
875 		if (strcmp(db->domainname, domainname) == 0)
876 			break;
877 	if (!db) {
878 		if (!bindtextdomain(domainname, _PATH_TEXTDOMAIN))
879 			goto fail;
880 		db = __bindings;
881 	}
882 
883 	/* resolve relative path */
884 	/* XXX not necessary? */
885 	if (db->path[0] != '/') {
886 		char buf[PATH_MAX];
887 
888 		if (getcwd(buf, sizeof(buf)) == 0)
889 			goto fail;
890 		if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
891 			goto fail;
892 		if (strlcat(buf, db->path, sizeof(buf)) >= sizeof(buf))
893 			goto fail;
894 		strlcpy(db->path, buf, sizeof(db->path));
895 	}
896 
897 	/* don't bother looking it up if the values are the same */
898 	if (odomainname && strcmp(domainname, odomainname) == 0 &&
899 	    ocname && strcmp(cname, ocname) == 0 && strcmp(lpath, olpath) == 0 &&
900 	    db->mohandle.mo.mo_magic)
901 		goto found;
902 
903 	/* try to find appropriate file, from $LANGUAGE */
904 	if (lookup_mofile(path, sizeof(path), db->path, lpath, cname,
905 	    domainname, db) == NULL)
906 		goto fail;
907 
908 	if (odomainname)
909 		free(odomainname);
910 	if (ocname)
911 		free(ocname);
912 	odomainname = strdup(domainname);
913 	ocname = strdup(cname);
914 	if (!odomainname || !ocname) {
915 		if (odomainname)
916 			free(odomainname);
917 		if (ocname)
918 			free(ocname);
919 		odomainname = ocname = NULL;
920 	}
921 	else
922 		strlcpy(olpath, lpath, sizeof(olpath));
923 
924 found:
925 	if (db->mohandle.mo.mo_plural) {
926 		plural_index =
927 		    _gettext_calculate_plural(db->mohandle.mo.mo_plural, n);
928 		if (plural_index >= db->mohandle.mo.mo_nplurals)
929 			plural_index = 0;
930 		msgid = msgid1;
931 	} else
932 		msgid = _NGETTEXT_DEFAULT(msgid1, msgid2, n);
933 
934 	if (msgid == NULL)
935 		return NULL;
936 
937 	v = lookup(msgid, db, &len);
938 	if (v) {
939 		if (db->mohandle.mo.mo_plural)
940 			v = get_indexed_string(v, len, plural_index);
941 		/*
942 		 * convert the translated message's encoding.
943 		 *
944 		 * special case:
945 		 *	a result of gettext("") shouldn't need any conversion.
946 		 */
947 		if (msgid[0])
948 			v = __gettext_iconv(v, db);
949 
950 		/*
951 		 * Given the amount of printf-format security issues, it may
952 		 * be a good idea to validate if the original msgid and the
953 		 * translated message format string carry the same printf-like
954 		 * format identifiers.
955 		 */
956 
957 		msgid = v;
958 	}
959 
960 	return (char *)__UNCONST(msgid);
961 
962 fail:
963 	return _NGETTEXT_DEFAULT(msgid1, msgid2, n);
964 }
965