Home | History | Annotate | Line # | Download | only in libintl
gettext.c revision 1.3
      1  1.3  itojun /*	$NetBSD: gettext.c,v 1.3 2000/10/31 11:08:18 itojun Exp $	*/
      2  1.1  itojun 
      3  1.1  itojun /*-
      4  1.1  itojun  * Copyright (c) 2000 Citrus Project,
      5  1.1  itojun  * All rights reserved.
      6  1.1  itojun  *
      7  1.1  itojun  * Redistribution and use in source and binary forms, with or without
      8  1.1  itojun  * modification, are permitted provided that the following conditions
      9  1.1  itojun  * are met:
     10  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     15  1.1  itojun  *
     16  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  itojun  * SUCH DAMAGE.
     27  1.1  itojun  */
     28  1.1  itojun 
     29  1.1  itojun #include <sys/cdefs.h>
     30  1.1  itojun #if defined(LIBC_SCCS) && !defined(lint)
     31  1.3  itojun __RCSID("$NetBSD: gettext.c,v 1.3 2000/10/31 11:08:18 itojun Exp $");
     32  1.1  itojun #endif /* LIBC_SCCS and not lint */
     33  1.1  itojun 
     34  1.1  itojun #include <sys/types.h>
     35  1.1  itojun #include <sys/param.h>
     36  1.1  itojun #include <sys/stat.h>
     37  1.1  itojun #include <sys/mman.h>
     38  1.1  itojun #include <sys/uio.h>
     39  1.1  itojun 
     40  1.1  itojun #include <fcntl.h>
     41  1.1  itojun #include <stdio.h>
     42  1.1  itojun #include <stdlib.h>
     43  1.1  itojun #include <unistd.h>
     44  1.1  itojun #include <string.h>
     45  1.1  itojun #if 0
     46  1.1  itojun #include <util.h>
     47  1.1  itojun #endif
     48  1.1  itojun #include <libintl.h>
     49  1.1  itojun #include <locale.h>
     50  1.1  itojun #include "libintl_local.h"
     51  1.1  itojun #include "pathnames.h"
     52  1.1  itojun 
     53  1.1  itojun static struct mohandle mohandle;
     54  1.1  itojun 
     55  1.1  itojun static const char *lookup_category __P((int));
     56  1.1  itojun static const char *split_locale __P((const char *));
     57  1.1  itojun static const char *lookup_mofile __P((char *, size_t, const char *,
     58  1.1  itojun 	char *, const char *, const char *));
     59  1.1  itojun static u_int32_t flip __P((u_int32_t, u_int32_t));
     60  1.1  itojun static int validate __P((void *));
     61  1.1  itojun static int mapit __P((const char *));
     62  1.1  itojun static int unmapit __P((void));
     63  1.1  itojun static const char *lookup_hash __P((const char *));
     64  1.1  itojun static const char *lookup_bsearch __P((const char *));
     65  1.1  itojun static const char *lookup __P((const char *));
     66  1.1  itojun 
     67  1.1  itojun /*
     68  1.1  itojun  * shortcut functions.  the main implementation resides in dcngettext().
     69  1.1  itojun  */
     70  1.1  itojun char *
     71  1.1  itojun gettext(msgid)
     72  1.1  itojun 	const char *msgid;
     73  1.1  itojun {
     74  1.1  itojun 
     75  1.1  itojun 	return dcngettext(NULL, msgid, NULL, 1UL, LC_MESSAGES);
     76  1.1  itojun }
     77  1.1  itojun 
     78  1.1  itojun char *
     79  1.1  itojun dgettext(domainname, msgid)
     80  1.1  itojun 	const char *domainname;
     81  1.1  itojun 	const char *msgid;
     82  1.1  itojun {
     83  1.1  itojun 
     84  1.1  itojun 	return dcngettext(domainname, msgid, NULL, 1UL, LC_MESSAGES);
     85  1.1  itojun }
     86  1.1  itojun 
     87  1.1  itojun char *
     88  1.1  itojun dcgettext(domainname, msgid, category)
     89  1.1  itojun 	const char *domainname;
     90  1.1  itojun 	const char *msgid;
     91  1.1  itojun 	int category;
     92  1.1  itojun {
     93  1.1  itojun 
     94  1.1  itojun 	return dcngettext(domainname, msgid, NULL, 1UL, category);
     95  1.1  itojun }
     96  1.1  itojun 
     97  1.1  itojun char *
     98  1.1  itojun ngettext(msgid1, msgid2, n)
     99  1.1  itojun 	const char *msgid1;
    100  1.1  itojun 	const char *msgid2;
    101  1.1  itojun 	unsigned long int n;
    102  1.1  itojun {
    103  1.1  itojun 
    104  1.1  itojun 	return dcngettext(NULL, msgid1, msgid2, n, LC_MESSAGES);
    105  1.1  itojun }
    106  1.1  itojun 
    107  1.1  itojun char *
    108  1.1  itojun dngettext(domainname, msgid1, msgid2, n)
    109  1.1  itojun 	const char *domainname;
    110  1.1  itojun 	const char *msgid1;
    111  1.1  itojun 	const char *msgid2;
    112  1.1  itojun 	unsigned long int n;
    113  1.1  itojun {
    114  1.1  itojun 
    115  1.1  itojun 	return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
    116  1.1  itojun }
    117  1.1  itojun 
    118  1.1  itojun /*
    119  1.1  itojun  * dcngettext() -
    120  1.1  itojun  * lookup internationalized message on database locale/category/domainname
    121  1.1  itojun  * (like ja_JP.eucJP/LC_MESSAGES/domainname).
    122  1.1  itojun  * if n equals to 1, internationalized message will be looked up for msgid1.
    123  1.1  itojun  * otherwise, message will be looked up for msgid2.
    124  1.1  itojun  * if the lookup fails, the function will return msgid1 or msgid2 as is.
    125  1.1  itojun  *
    126  1.1  itojun  * Even though the return type is "char *", caller should not rewrite the
    127  1.1  itojun  * region pointed to by the return value (should be "const char *", but can't
    128  1.1  itojun  * change it for compatibility with other implementations).
    129  1.1  itojun  *
    130  1.1  itojun  * by default (if domainname == NULL), domainname is taken from the value set
    131  1.1  itojun  * by textdomain().  usually name of the application (like "ls") is used as
    132  1.1  itojun  * domainname.  category is usually LC_MESSAGES.
    133  1.1  itojun  *
    134  1.1  itojun  * the code reads in *.mo files generated by GNU gettext.  *.mo is a host-
    135  1.1  itojun  * endian encoded file.  both endians are supported here, as the files are in
    136  1.1  itojun  * /usr/share/locale! (or we should move those files into /usr/libdata)
    137  1.1  itojun  */
    138  1.1  itojun 
    139  1.1  itojun static const char *
    140  1.1  itojun lookup_category(category)
    141  1.1  itojun 	int category;
    142  1.1  itojun {
    143  1.1  itojun 
    144  1.1  itojun 	switch (category) {
    145  1.1  itojun 	case LC_COLLATE:	return "LC_COLLATE";
    146  1.1  itojun 	case LC_CTYPE:		return "LC_CTYPE";
    147  1.1  itojun 	case LC_MONETARY:	return "LC_MONETARY";
    148  1.1  itojun 	case LC_NUMERIC:	return "LC_NUMERIC";
    149  1.1  itojun 	case LC_TIME:		return "LC_TIME";
    150  1.1  itojun 	case LC_MESSAGES:	return "LC_MESSAGES";
    151  1.1  itojun 	}
    152  1.1  itojun 	return NULL;
    153  1.1  itojun }
    154  1.1  itojun 
    155  1.1  itojun /*
    156  1.1  itojun  * XPG syntax: language[_territory[.codeset]][@modifier]
    157  1.1  itojun  * XXX boundary check on "result" is lacking
    158  1.1  itojun  */
    159  1.1  itojun static const char *
    160  1.1  itojun split_locale(lname)
    161  1.1  itojun 	const char *lname;
    162  1.1  itojun {
    163  1.1  itojun 	char buf[BUFSIZ], tmp[BUFSIZ];
    164  1.1  itojun 	char *l, *t, *c, *m;
    165  1.1  itojun 	static char result[BUFSIZ];
    166  1.1  itojun 
    167  1.1  itojun 	memset(result, 0, sizeof(result));
    168  1.1  itojun 
    169  1.1  itojun 	if (strlen(lname) + 1 > sizeof(buf)) {
    170  1.1  itojun fail:
    171  1.1  itojun 		return lname;
    172  1.1  itojun 	}
    173  1.1  itojun 
    174  1.1  itojun 	strlcpy(buf, lname, sizeof(buf));
    175  1.1  itojun 	m = strrchr(buf, '@');
    176  1.1  itojun 	if (m)
    177  1.1  itojun 		*m++ = '\0';
    178  1.1  itojun 	c = strrchr(buf, '.');
    179  1.1  itojun 	if (c)
    180  1.1  itojun 		*c++ = '\0';
    181  1.1  itojun 	t = strrchr(buf, '_');
    182  1.1  itojun 	if (t)
    183  1.1  itojun 		*t++ = '\0';
    184  1.1  itojun 	l = buf;
    185  1.1  itojun 	if (strlen(l) == 0)
    186  1.1  itojun 		goto fail;
    187  1.1  itojun 	if (c && !t)
    188  1.1  itojun 		goto fail;
    189  1.1  itojun 
    190  1.1  itojun 	if (m) {
    191  1.1  itojun 		if (t) {
    192  1.1  itojun 			if (c) {
    193  1.1  itojun 				snprintf(tmp, sizeof(tmp), "%s_%s.%s@%s",
    194  1.1  itojun 				    l, t, c, m);
    195  1.1  itojun 				strlcat(result, tmp, sizeof(result));
    196  1.1  itojun 				strlcat(result, ":", sizeof(result));
    197  1.1  itojun 			}
    198  1.1  itojun 			snprintf(tmp, sizeof(tmp), "%s_%s@%s", l, t, m);
    199  1.1  itojun 			strlcat(result, tmp, sizeof(result));
    200  1.1  itojun 			strlcat(result, ":", sizeof(result));
    201  1.1  itojun 		}
    202  1.1  itojun 		snprintf(tmp, sizeof(tmp), "%s@%s", l, m);
    203  1.1  itojun 		strlcat(result, tmp, sizeof(result));
    204  1.1  itojun 		strlcat(result, ":", sizeof(result));
    205  1.1  itojun 	}
    206  1.1  itojun 	if (t) {
    207  1.1  itojun 		if (c) {
    208  1.1  itojun 			snprintf(tmp, sizeof(tmp), "%s_%s.%s", l, t, c);
    209  1.1  itojun 			strlcat(result, tmp, sizeof(result));
    210  1.1  itojun 			strlcat(result, ":", sizeof(result));
    211  1.1  itojun 		}
    212  1.1  itojun 		strlcat(result, tmp, sizeof(result));
    213  1.1  itojun 		strlcat(result, ":", sizeof(result));
    214  1.1  itojun 	}
    215  1.1  itojun 	strlcat(result, l, sizeof(result));
    216  1.1  itojun 
    217  1.1  itojun 	return result;
    218  1.1  itojun }
    219  1.1  itojun 
    220  1.1  itojun static const char *
    221  1.1  itojun lookup_mofile(buf, len, dir, lpath, category, domainname)
    222  1.1  itojun 	char *buf;
    223  1.1  itojun 	size_t len;
    224  1.1  itojun 	const char *dir;
    225  1.1  itojun 	char *lpath;	/* list of locales to be tried */
    226  1.1  itojun 	const char *category;
    227  1.1  itojun 	const char *domainname;
    228  1.1  itojun {
    229  1.1  itojun 	struct stat st;
    230  1.1  itojun 	char *p, *q;
    231  1.1  itojun 
    232  1.1  itojun 	q = lpath;
    233  1.1  itojun 	while (1) {
    234  1.1  itojun 		p = strsep(&q, ":");
    235  1.1  itojun 		if (!p)
    236  1.1  itojun 			break;
    237  1.1  itojun 		if (!*p)
    238  1.1  itojun 			continue;
    239  1.1  itojun 
    240  1.1  itojun 		/* don't mess with default locales */
    241  1.1  itojun 		if (strcmp(p, "C") == 0 || strcmp(p, "POSIX") == 0)
    242  1.1  itojun 			return NULL;
    243  1.1  itojun 
    244  1.1  itojun 		/* validate pathname */
    245  1.1  itojun 		if (strchr(p, '/') || strchr(category, '/'))
    246  1.1  itojun 			continue;
    247  1.1  itojun #if 1	/*?*/
    248  1.1  itojun 		if (strchr(domainname, '/'))
    249  1.1  itojun 			continue;
    250  1.1  itojun #endif
    251  1.1  itojun 
    252  1.1  itojun 		snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
    253  1.1  itojun 		    category, domainname);
    254  1.1  itojun 		if (stat(buf, &st) < 0)
    255  1.1  itojun 			continue;
    256  1.1  itojun 		if ((st.st_mode & S_IFMT) != S_IFREG)
    257  1.1  itojun 			continue;
    258  1.1  itojun 
    259  1.1  itojun 		if (mapit(buf) == 0)
    260  1.1  itojun 			return buf;
    261  1.1  itojun 	}
    262  1.1  itojun 
    263  1.1  itojun 	return NULL;
    264  1.1  itojun }
    265  1.1  itojun 
    266  1.1  itojun static u_int32_t
    267  1.1  itojun flip(v, magic)
    268  1.1  itojun 	u_int32_t v;
    269  1.1  itojun 	u_int32_t magic;
    270  1.1  itojun {
    271  1.1  itojun 
    272  1.1  itojun 	if (magic == MO_MAGIC)
    273  1.1  itojun 		return v;
    274  1.1  itojun 	else if (magic == MO_MAGIC_SWAPPED) {
    275  1.1  itojun 		v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00) |
    276  1.1  itojun 		    ((v << 8) & 0xff0000) | ((v << 24) & 0xff000000);
    277  1.1  itojun 		return v;
    278  1.1  itojun 	} else {
    279  1.1  itojun 		abort();
    280  1.1  itojun 		/*NOTREACHED*/
    281  1.1  itojun 	}
    282  1.1  itojun }
    283  1.1  itojun 
    284  1.1  itojun static int
    285  1.1  itojun validate(arg)
    286  1.1  itojun 	void *arg;
    287  1.1  itojun {
    288  1.1  itojun 	char *p;
    289  1.1  itojun 
    290  1.1  itojun 	p = (char *)arg;
    291  1.1  itojun 	if (p < (char *)mohandle.addr ||
    292  1.1  itojun 	    p > (char *)mohandle.addr + mohandle.len)
    293  1.1  itojun 		return 0;
    294  1.1  itojun 	else
    295  1.1  itojun 		return 1;
    296  1.1  itojun }
    297  1.1  itojun 
    298  1.1  itojun int
    299  1.1  itojun mapit(path)
    300  1.1  itojun 	const char *path;
    301  1.1  itojun {
    302  1.1  itojun 	int fd;
    303  1.1  itojun 	struct stat st;
    304  1.1  itojun 	char *base;
    305  1.1  itojun 	u_int32_t magic, revision;
    306  1.1  itojun 	struct moentry *otable, *ttable;
    307  1.1  itojun 	struct moentry_h *p;
    308  1.1  itojun 	struct mo *mo;
    309  1.1  itojun 	size_t l;
    310  1.1  itojun 	int i;
    311  1.1  itojun 	char *v;
    312  1.1  itojun 
    313  1.1  itojun 	if (mohandle.addr && strcmp(path, mohandle.path) == 0)
    314  1.1  itojun 		return 0;	/*already opened*/
    315  1.1  itojun 
    316  1.1  itojun 	unmapit();
    317  1.1  itojun 
    318  1.1  itojun #if 0
    319  1.1  itojun 	if (secure_path(path) != 0)
    320  1.1  itojun 		goto fail;
    321  1.1  itojun #endif
    322  1.1  itojun 	if (stat(path, &st) < 0)
    323  1.1  itojun 		goto fail;
    324  1.1  itojun 	if ((st.st_mode & S_IFMT) != S_IFREG || st.st_size > GETTEXT_MMAP_MAX)
    325  1.1  itojun 		goto fail;
    326  1.1  itojun 	fd = open(path, O_RDONLY);
    327  1.1  itojun 	if (fd < 0)
    328  1.1  itojun 		goto fail;
    329  1.2  itojun 	if (read(fd, &magic, sizeof(magic)) != sizeof(magic) ||
    330  1.1  itojun 	    (magic != MO_MAGIC && magic != MO_MAGIC_SWAPPED)) {
    331  1.1  itojun 		close(fd);
    332  1.1  itojun 		goto fail;
    333  1.1  itojun 	}
    334  1.2  itojun 	if (read(fd, &revision, sizeof(revision)) != sizeof(revision) ||
    335  1.1  itojun 	    flip(revision, magic) != MO_REVISION) {
    336  1.1  itojun 		close(fd);
    337  1.1  itojun 		goto fail;
    338  1.1  itojun 	}
    339  1.1  itojun 	mohandle.addr = mmap(NULL, st.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
    340  1.1  itojun 	    fd, (off_t)0);
    341  1.1  itojun 	if (!mohandle.addr) {
    342  1.1  itojun 		close(fd);
    343  1.1  itojun 		goto fail;
    344  1.1  itojun 	}
    345  1.1  itojun 	close(fd);
    346  1.1  itojun 	mohandle.len = st.st_size;
    347  1.1  itojun 	strlcpy(mohandle.path, path, sizeof(mohandle.path));
    348  1.1  itojun 
    349  1.1  itojun 	base = mohandle.addr;
    350  1.1  itojun 	mo = (struct mo *)mohandle.addr;
    351  1.1  itojun 
    352  1.1  itojun 	/* flip endian.  do not flip magic number! */
    353  1.1  itojun 	mohandle.mo.mo_magic = mo->mo_magic;
    354  1.1  itojun 	mohandle.mo.mo_revision = flip(mo->mo_revision, magic);
    355  1.1  itojun 	mohandle.mo.mo_nstring = flip(mo->mo_nstring, magic);
    356  1.1  itojun 
    357  1.1  itojun 	/* validate otable/ttable */
    358  1.1  itojun 	otable = (struct moentry *)(base + flip(mo->mo_otable, magic));
    359  1.1  itojun 	ttable = (struct moentry *)(base + flip(mo->mo_ttable, magic));
    360  1.1  itojun 	if (!validate(otable) || !validate(&otable[mohandle.mo.mo_nstring])) {
    361  1.1  itojun 		unmapit();
    362  1.1  itojun 		goto fail;
    363  1.1  itojun 	}
    364  1.1  itojun 	if (!validate(ttable) || !validate(&ttable[mohandle.mo.mo_nstring])) {
    365  1.1  itojun 		unmapit();
    366  1.1  itojun 		goto fail;
    367  1.1  itojun 	}
    368  1.1  itojun 
    369  1.1  itojun 	/* allocate [ot]table, and convert to normal pointer representation. */
    370  1.1  itojun 	l = sizeof(struct moentry_h) * mohandle.mo.mo_nstring;
    371  1.1  itojun 	mohandle.mo.mo_otable = (struct moentry_h *)malloc(l);
    372  1.1  itojun 	if (!mohandle.mo.mo_otable) {
    373  1.1  itojun 		unmapit();
    374  1.1  itojun 		goto fail;
    375  1.1  itojun 	}
    376  1.1  itojun 	mohandle.mo.mo_ttable = (struct moentry_h *)malloc(l);
    377  1.1  itojun 	if (!mohandle.mo.mo_ttable) {
    378  1.1  itojun 		unmapit();
    379  1.1  itojun 		goto fail;
    380  1.1  itojun 	}
    381  1.1  itojun 	p = mohandle.mo.mo_otable;
    382  1.1  itojun 	for (i = 0; i < mohandle.mo.mo_nstring; i++) {
    383  1.1  itojun 		p[i].len = flip(otable[i].len, magic);
    384  1.1  itojun 		p[i].off = base + flip(otable[i].off, magic);
    385  1.1  itojun 
    386  1.1  itojun 		if (!validate(p[i].off) || !validate(p[i].off + p[i].len + 1)) {
    387  1.1  itojun 			unmapit();
    388  1.1  itojun 			goto fail;
    389  1.1  itojun 		}
    390  1.1  itojun 	}
    391  1.1  itojun 	p = mohandle.mo.mo_ttable;
    392  1.1  itojun 	for (i = 0; i < mohandle.mo.mo_nstring; i++) {
    393  1.1  itojun 		p[i].len = flip(ttable[i].len, magic);
    394  1.1  itojun 		p[i].off = base + flip(ttable[i].off, magic);
    395  1.1  itojun 
    396  1.1  itojun 		if (!validate(p[i].off) || !validate(p[i].off + p[i].len + 1)) {
    397  1.1  itojun 			unmapit();
    398  1.1  itojun 			goto fail;
    399  1.1  itojun 		}
    400  1.1  itojun 	}
    401  1.1  itojun 
    402  1.1  itojun 	/* grab MIME-header and charset field */
    403  1.1  itojun 	mohandle.mo.mo_header = lookup("");
    404  1.1  itojun 	if (mohandle.mo.mo_header)
    405  1.1  itojun 		v = strstr(mohandle.mo.mo_header, "charset=");
    406  1.1  itojun 	else
    407  1.1  itojun 		v = NULL;
    408  1.1  itojun 	if (v) {
    409  1.1  itojun 		mohandle.mo.mo_charset = strdup(v + 8);
    410  1.1  itojun 		v = strchr(mohandle.mo.mo_charset, '\n');
    411  1.1  itojun 		if (v)
    412  1.1  itojun 			*v = '\0';
    413  1.1  itojun 	}
    414  1.1  itojun 
    415  1.1  itojun 	/*
    416  1.1  itojun 	 * XXX check charset, reject it if we are unable to support the charset
    417  1.1  itojun 	 * with the current locale.
    418  1.1  itojun 	 * for example, if we are using euc-jp locale and we are looking at
    419  1.1  itojun 	 * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
    420  1.1  itojun 	 * the *.mo file as we cannot support it.
    421  1.1  itojun 	 */
    422  1.1  itojun 
    423  1.1  itojun 	return 0;
    424  1.1  itojun 
    425  1.1  itojun fail:
    426  1.1  itojun 	return -1;
    427  1.1  itojun }
    428  1.1  itojun 
    429  1.1  itojun static int
    430  1.1  itojun unmapit()
    431  1.1  itojun {
    432  1.1  itojun 
    433  1.1  itojun 	/* unmap if there's already mapped region */
    434  1.1  itojun 	if (mohandle.addr)
    435  1.1  itojun 		munmap(mohandle.addr, mohandle.len);
    436  1.1  itojun 	mohandle.addr = NULL;
    437  1.1  itojun 	mohandle.path[0] = '\0';
    438  1.1  itojun 	if (mohandle.mo.mo_otable)
    439  1.1  itojun 		free(mohandle.mo.mo_otable);
    440  1.1  itojun 	if (mohandle.mo.mo_ttable)
    441  1.1  itojun 		free(mohandle.mo.mo_ttable);
    442  1.1  itojun 	if (mohandle.mo.mo_charset)
    443  1.1  itojun 		free(mohandle.mo.mo_charset);
    444  1.1  itojun 	memset(&mohandle.mo, 0, sizeof(mohandle.mo));
    445  1.1  itojun 	return 0;
    446  1.1  itojun }
    447  1.1  itojun 
    448  1.1  itojun static const char *
    449  1.1  itojun lookup_hash(msgid)
    450  1.1  itojun 	const char *msgid;
    451  1.1  itojun {
    452  1.1  itojun 
    453  1.1  itojun 	/*
    454  1.1  itojun 	 * XXX should try a hashed lookup here, but to do so, we need to
    455  1.1  itojun 	 * look inside the GPL'ed *.c and re-implement...
    456  1.1  itojun 	 */
    457  1.1  itojun 	return NULL;
    458  1.1  itojun }
    459  1.1  itojun 
    460  1.1  itojun static const char *
    461  1.1  itojun lookup_bsearch(msgid)
    462  1.1  itojun 	const char *msgid;
    463  1.1  itojun {
    464  1.1  itojun 	size_t l;
    465  1.1  itojun 	int top, bottom, middle, omiddle;
    466  1.1  itojun 	int n;
    467  1.1  itojun 
    468  1.1  itojun 	l = strlen(msgid);
    469  1.1  itojun 
    470  1.1  itojun 	top = 0;
    471  1.1  itojun 	bottom = mohandle.mo.mo_nstring;
    472  1.1  itojun 	omiddle = -1;
    473  1.1  itojun 	while (1) {
    474  1.1  itojun 		if (top > bottom)
    475  1.1  itojun 			return NULL;
    476  1.1  itojun 		middle = (top + bottom) / 2;
    477  1.1  itojun 		/* avoid possible infinite loop, when the data is not sorted */
    478  1.1  itojun 		if (omiddle == middle)
    479  1.1  itojun 			return NULL;
    480  1.1  itojun 		if (middle < 0 || middle >= mohandle.mo.mo_nstring)
    481  1.1  itojun 			return NULL;
    482  1.1  itojun 
    483  1.1  itojun 		n = strcmp(msgid, mohandle.mo.mo_otable[middle].off);
    484  1.1  itojun 		if (n == 0)
    485  1.1  itojun 			return (const char *)mohandle.mo.mo_ttable[middle].off;
    486  1.1  itojun 		else if (n < 0)
    487  1.1  itojun 			bottom = middle;
    488  1.1  itojun 		else
    489  1.1  itojun 			top = middle;
    490  1.1  itojun 		omiddle = middle;
    491  1.1  itojun 	}
    492  1.1  itojun 
    493  1.1  itojun 	return NULL;
    494  1.1  itojun }
    495  1.1  itojun 
    496  1.1  itojun static const char *
    497  1.1  itojun lookup(msgid)
    498  1.1  itojun 	const char *msgid;
    499  1.1  itojun {
    500  1.1  itojun 	const char *v;
    501  1.1  itojun 
    502  1.1  itojun 	v = lookup_hash(msgid);
    503  1.1  itojun 	if (v)
    504  1.1  itojun 		return v;
    505  1.1  itojun 
    506  1.1  itojun 	return lookup_bsearch(msgid);
    507  1.1  itojun }
    508  1.1  itojun 
    509  1.1  itojun char *
    510  1.1  itojun dcngettext(domainname, msgid1, msgid2, n, category)
    511  1.1  itojun 	const char *domainname;
    512  1.1  itojun 	const char *msgid1;
    513  1.1  itojun 	const char *msgid2;
    514  1.1  itojun 	unsigned long int n;
    515  1.1  itojun 	int category;
    516  1.1  itojun {
    517  1.1  itojun 	const char *msgid;
    518  1.1  itojun 	char path[PATH_MAX];
    519  1.1  itojun 	static char lpath[PATH_MAX];
    520  1.1  itojun 	static char olpath[PATH_MAX];
    521  1.1  itojun 	const char *locale;
    522  1.1  itojun 	const char *language;
    523  1.1  itojun 	const char *cname;
    524  1.1  itojun 	const char *v;
    525  1.1  itojun 
    526  1.1  itojun 	msgid = (n == 1) ? msgid1 : msgid2;
    527  1.1  itojun 
    528  1.1  itojun 	if (!domainname)
    529  1.1  itojun 		domainname = __domainname;
    530  1.1  itojun 	cname = lookup_category(category);
    531  1.1  itojun 	if (!domainname || !cname)
    532  1.1  itojun 		goto fail;
    533  1.1  itojun 
    534  1.1  itojun 	language = getenv("LANGUAGE");
    535  1.1  itojun 	locale = setlocale(LC_MESSAGES, NULL);	/*XXX*/
    536  1.1  itojun 	if (locale)
    537  1.1  itojun 		locale = split_locale(locale);
    538  1.1  itojun 	if (language && locale) {
    539  1.1  itojun 		if (strlen(language) + strlen(locale) + 2 > sizeof(lpath))
    540  1.1  itojun 			goto fail;
    541  1.1  itojun 		snprintf(lpath, sizeof(lpath), "%s:%s", language, locale);
    542  1.1  itojun 	} else if (language) {
    543  1.1  itojun 		if (strlen(language) + 1 > sizeof(lpath))
    544  1.1  itojun 			goto fail;
    545  1.1  itojun 		strlcpy(lpath, language, sizeof(lpath));
    546  1.1  itojun 	} else if (locale) {
    547  1.1  itojun 		if (strlen(locale) + 1 > sizeof(lpath))
    548  1.1  itojun 			goto fail;
    549  1.1  itojun 		strlcpy(lpath, locale, sizeof(lpath));
    550  1.1  itojun 	} else
    551  1.1  itojun 		goto fail;
    552  1.1  itojun 
    553  1.1  itojun 	/* don't bother looking it up if the values are the same */
    554  1.1  itojun 	if (strcmp(lpath, olpath) == 0)
    555  1.1  itojun 		goto found;
    556  1.1  itojun 
    557  1.1  itojun 	strlcpy(olpath, lpath, sizeof(olpath));
    558  1.1  itojun 
    559  1.1  itojun 	/* try to find appropriate file, from $LANGUAGE */
    560  1.1  itojun 	if (lookup_mofile(path, sizeof(path), __domainpath, lpath, cname,
    561  1.3  itojun 	    domainname) == NULL)
    562  1.3  itojun 		goto fail;
    563  1.1  itojun 
    564  1.1  itojun found:
    565  1.1  itojun 	v = lookup(msgid);
    566  1.1  itojun 	if (v) {
    567  1.1  itojun 		/*
    568  1.1  itojun 		 * XXX call iconv() here, if translated text is encoded
    569  1.1  itojun 		 * differently from currently-selected encoding (locale).
    570  1.1  itojun 		 * look at Content-type header in *.mo file, in string obtained
    571  1.1  itojun 		 * by gettext("").
    572  1.1  itojun 		 */
    573  1.1  itojun 
    574  1.1  itojun 		/*
    575  1.1  itojun 		 * Given the amount of printf-format security issues, it may
    576  1.1  itojun 		 * be a good idea to validate if the original msgid and the
    577  1.1  itojun 		 * translated message format string carry the same printf-like
    578  1.1  itojun 		 * format identifiers.
    579  1.1  itojun 		 */
    580  1.1  itojun 
    581  1.1  itojun 		msgid = v;
    582  1.1  itojun 	}
    583  1.1  itojun 
    584  1.1  itojun fail:
    585  1.1  itojun 	/* LINTED const cast */
    586  1.1  itojun 	return (char *)msgid;
    587  1.1  itojun }
    588