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