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