Home | History | Annotate | Line # | Download | only in libintl
gettext.c revision 1.28
      1  1.28      yamt /*	$NetBSD: gettext.c,v 1.28 2012/07/30 23:04:42 yamt 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.28      yamt __RCSID("$NetBSD: gettext.c,v 1.28 2012/07/30 23:04:42 yamt 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.28      yamt 	/*
    219  1.28      yamt 	 * LANGUAGE is a colon separated list of locale names.
    220  1.28      yamt 	 */
    221  1.28      yamt 
    222  1.10      yamt 	strlcpy(lpath_tmp, lpath, sizeof(lpath_tmp));
    223  1.10      yamt 	q = lpath_tmp;
    224   1.9   minoura 	/* CONSTCOND */
    225   1.1    itojun 	while (1) {
    226   1.1    itojun 		p = strsep(&q, ":");
    227   1.1    itojun 		if (!p)
    228   1.1    itojun 			break;
    229   1.1    itojun 		if (!*p)
    230   1.1    itojun 			continue;
    231   1.1    itojun 
    232   1.1    itojun 		/* don't mess with default locales */
    233   1.1    itojun 		if (strcmp(p, "C") == 0 || strcmp(p, "POSIX") == 0)
    234   1.1    itojun 			return NULL;
    235   1.1    itojun 
    236   1.1    itojun 		/* validate pathname */
    237   1.1    itojun 		if (strchr(p, '/') || strchr(category, '/'))
    238   1.1    itojun 			continue;
    239   1.1    itojun #if 1	/*?*/
    240   1.1    itojun 		if (strchr(domainname, '/'))
    241   1.1    itojun 			continue;
    242   1.1    itojun #endif
    243   1.1    itojun 
    244   1.1    itojun 		snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
    245   1.1    itojun 		    category, domainname);
    246   1.1    itojun 		if (stat(buf, &st) < 0)
    247   1.1    itojun 			continue;
    248   1.1    itojun 		if ((st.st_mode & S_IFMT) != S_IFREG)
    249   1.1    itojun 			continue;
    250   1.1    itojun 
    251   1.9   minoura 		if (mapit(buf, db) == 0)
    252   1.1    itojun 			return buf;
    253   1.1    itojun 	}
    254   1.1    itojun 
    255   1.1    itojun 	return NULL;
    256   1.1    itojun }
    257   1.1    itojun 
    258  1.25  junyoung static uint32_t
    259  1.25  junyoung flip(uint32_t v, uint32_t magic)
    260   1.1    itojun {
    261   1.1    itojun 
    262   1.1    itojun 	if (magic == MO_MAGIC)
    263   1.1    itojun 		return v;
    264   1.1    itojun 	else if (magic == MO_MAGIC_SWAPPED) {
    265   1.1    itojun 		v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00) |
    266   1.1    itojun 		    ((v << 8) & 0xff0000) | ((v << 24) & 0xff000000);
    267   1.1    itojun 		return v;
    268   1.1    itojun 	} else {
    269   1.1    itojun 		abort();
    270   1.1    itojun 		/*NOTREACHED*/
    271   1.1    itojun 	}
    272   1.1    itojun }
    273   1.1    itojun 
    274   1.1    itojun static int
    275  1.25  junyoung validate(void *arg, struct mohandle *mohandle)
    276   1.1    itojun {
    277   1.1    itojun 	char *p;
    278   1.1    itojun 
    279   1.1    itojun 	p = (char *)arg;
    280   1.9   minoura 	if (p < (char *)mohandle->addr ||
    281   1.9   minoura 	    p > (char *)mohandle->addr + mohandle->len)
    282   1.1    itojun 		return 0;
    283   1.1    itojun 	else
    284   1.1    itojun 		return 1;
    285   1.1    itojun }
    286   1.1    itojun 
    287  1.20  tshiozak /*
    288  1.20  tshiozak  * calculate the step value if the hash value is conflicted.
    289  1.20  tshiozak  */
    290  1.25  junyoung static __inline uint32_t
    291  1.25  junyoung calc_collision_step(uint32_t hashval, uint32_t hashsize)
    292  1.20  tshiozak {
    293  1.20  tshiozak 	_DIAGASSERT(hashsize>2);
    294  1.20  tshiozak 	return (hashval % (hashsize - 2)) + 1;
    295  1.20  tshiozak }
    296  1.20  tshiozak 
    297  1.20  tshiozak /*
    298  1.20  tshiozak  * calculate the next index while conflicting.
    299  1.20  tshiozak  */
    300  1.25  junyoung static __inline uint32_t
    301  1.25  junyoung calc_next_index(uint32_t curidx, uint32_t hashsize, uint32_t step)
    302  1.20  tshiozak {
    303  1.20  tshiozak 	return curidx+step - (curidx >= hashsize-step ? hashsize : 0);
    304  1.20  tshiozak }
    305  1.20  tshiozak 
    306  1.20  tshiozak static int
    307  1.25  junyoung get_sysdep_string_table(struct mosysdepstr_h **table_h, uint32_t *ofstable,
    308  1.25  junyoung 			uint32_t nstrings, uint32_t magic, char *base)
    309  1.20  tshiozak {
    310  1.27      matt 	unsigned int i;
    311  1.27      matt 	int j, count;
    312  1.20  tshiozak 	size_t l;
    313  1.20  tshiozak 	struct mosysdepstr *table;
    314  1.20  tshiozak 
    315  1.20  tshiozak 	for (i=0; i<nstrings; i++) {
    316  1.20  tshiozak 		/* get mosysdepstr record */
    317  1.20  tshiozak 		/* LINTED: ignore the alignment problem. */
    318  1.20  tshiozak 		table = (struct mosysdepstr *)(base + flip(ofstable[i], magic));
    319  1.20  tshiozak 		/* count number of segments */
    320  1.20  tshiozak 		count = 0;
    321  1.20  tshiozak 		while (flip(table->segs[count++].ref, magic) != MO_LASTSEG)
    322  1.20  tshiozak 			;
    323  1.20  tshiozak 		/* get table */
    324  1.20  tshiozak 		l = sizeof(struct mosysdepstr_h) +
    325  1.20  tshiozak 		    sizeof(struct mosysdepsegentry_h) * (count-1);
    326  1.20  tshiozak 		table_h[i] = (struct mosysdepstr_h *)malloc(l);
    327  1.20  tshiozak 		if (!table_h[i])
    328  1.20  tshiozak 			return -1;
    329  1.20  tshiozak 		memset(table_h[i], 0, l);
    330  1.20  tshiozak 		table_h[i]->off = (const char *)(base + flip(table->off, magic));
    331  1.20  tshiozak 		for (j=0; j<count; j++) {
    332  1.20  tshiozak 			table_h[i]->segs[j].len =
    333  1.20  tshiozak 			    flip(table->segs[j].len, magic);
    334  1.20  tshiozak 			table_h[i]->segs[j].ref =
    335  1.20  tshiozak 			    flip(table->segs[j].ref, magic);
    336  1.20  tshiozak 		}
    337  1.20  tshiozak 		/* LINTED: ignore the alignment problem. */
    338  1.20  tshiozak 		table = (struct mosysdepstr *)&table->segs[count];
    339  1.20  tshiozak 	}
    340  1.20  tshiozak 	return 0;
    341  1.20  tshiozak }
    342  1.20  tshiozak 
    343  1.20  tshiozak static int
    344  1.20  tshiozak expand_sysdep(struct mohandle *mohandle, struct mosysdepstr_h *str)
    345  1.20  tshiozak {
    346  1.20  tshiozak 	int i;
    347  1.20  tshiozak 	const char *src;
    348  1.20  tshiozak 	char *dst;
    349  1.20  tshiozak 
    350  1.20  tshiozak 	/* check whether already expanded */
    351  1.20  tshiozak 	if (str->expanded)
    352  1.20  tshiozak 		return 0;
    353  1.20  tshiozak 
    354  1.20  tshiozak 	/* calc total length */
    355  1.20  tshiozak 	str->expanded_len = 1;
    356  1.20  tshiozak 	for (i=0; /*CONSTCOND*/1; i++) {
    357  1.20  tshiozak 		str->expanded_len += str->segs[i].len;
    358  1.20  tshiozak 		if (str->segs[i].ref == MO_LASTSEG)
    359  1.20  tshiozak 			break;
    360  1.20  tshiozak 		str->expanded_len +=
    361  1.20  tshiozak 		    mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len;
    362  1.20  tshiozak 	}
    363  1.20  tshiozak 	/* expand */
    364  1.20  tshiozak 	str->expanded = malloc(str->expanded_len);
    365  1.20  tshiozak 	if (!str->expanded)
    366  1.20  tshiozak 		return -1;
    367  1.20  tshiozak 	src = str->off;
    368  1.20  tshiozak 	dst = str->expanded;
    369  1.20  tshiozak 	for (i=0; /*CONSTCOND*/1; i++) {
    370  1.20  tshiozak 		memcpy(dst, src, str->segs[i].len);
    371  1.20  tshiozak 		src += str->segs[i].len;
    372  1.20  tshiozak 		dst += str->segs[i].len;
    373  1.20  tshiozak 		if (str->segs[i].ref == MO_LASTSEG)
    374  1.20  tshiozak 			break;
    375  1.20  tshiozak 		memcpy(dst, mohandle->mo.mo_sysdep_segs[str->segs[i].ref].str,
    376  1.20  tshiozak 		       mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len);
    377  1.20  tshiozak 		dst += mohandle->mo.mo_sysdep_segs[str->segs[i].ref].len;
    378  1.20  tshiozak 	}
    379  1.20  tshiozak 	*dst = '\0';
    380  1.20  tshiozak 
    381  1.20  tshiozak 	return 0;
    382  1.20  tshiozak }
    383  1.20  tshiozak 
    384  1.20  tshiozak static void
    385  1.25  junyoung insert_to_hash(uint32_t *htable, uint32_t hsize, const char *str, uint32_t ref)
    386  1.20  tshiozak {
    387  1.25  junyoung 	uint32_t hashval, idx, step;
    388  1.20  tshiozak 
    389  1.20  tshiozak 	hashval = __intl_string_hash(str);
    390  1.20  tshiozak 	step = calc_collision_step(hashval, hsize);
    391  1.20  tshiozak 	idx = hashval % hsize;
    392  1.20  tshiozak 
    393  1.20  tshiozak 	while (htable[idx])
    394  1.20  tshiozak 		idx = calc_next_index(idx, hsize, step);
    395  1.20  tshiozak 
    396  1.20  tshiozak 	htable[idx] = ref;
    397  1.20  tshiozak }
    398  1.20  tshiozak 
    399  1.20  tshiozak static int
    400  1.20  tshiozak setup_sysdep_stuffs(struct mo *mo, struct mohandle *mohandle, char *base)
    401  1.20  tshiozak {
    402  1.25  junyoung 	uint32_t magic;
    403  1.20  tshiozak 	struct moentry *stable;
    404  1.20  tshiozak 	size_t l;
    405  1.27      matt 	unsigned int i;
    406  1.20  tshiozak 	char *v;
    407  1.25  junyoung 	uint32_t *ofstable;
    408  1.20  tshiozak 
    409  1.20  tshiozak 	magic = mo->mo_magic;
    410  1.20  tshiozak 
    411  1.20  tshiozak 	mohandle->mo.mo_sysdep_nsegs = flip(mo->mo_sysdep_nsegs, magic);
    412  1.20  tshiozak 	mohandle->mo.mo_sysdep_nstring = flip(mo->mo_sysdep_nstring, magic);
    413  1.20  tshiozak 
    414  1.20  tshiozak 	if (mohandle->mo.mo_sysdep_nstring == 0)
    415  1.20  tshiozak 		return 0;
    416  1.20  tshiozak 
    417  1.20  tshiozak 	/* check hash size */
    418  1.20  tshiozak 	if (mohandle->mo.mo_hsize <= 2 ||
    419  1.20  tshiozak 	    mohandle->mo.mo_hsize <
    420  1.20  tshiozak 	    (mohandle->mo.mo_nstring + mohandle->mo.mo_sysdep_nstring))
    421  1.20  tshiozak 		return -1;
    422  1.20  tshiozak 
    423  1.20  tshiozak 	/* get sysdep segments */
    424  1.21      yamt 	l = sizeof(struct mosysdepsegs_h) * mohandle->mo.mo_sysdep_nsegs;
    425  1.20  tshiozak 	mohandle->mo.mo_sysdep_segs = (struct mosysdepsegs_h *)malloc(l);
    426  1.20  tshiozak 	if (!mohandle->mo.mo_sysdep_segs)
    427  1.20  tshiozak 		return -1;
    428  1.20  tshiozak 	/* LINTED: ignore the alignment problem. */
    429  1.20  tshiozak 	stable = (struct moentry *)(base + flip(mo->mo_sysdep_segoff, magic));
    430  1.20  tshiozak 	for (i=0; i<mohandle->mo.mo_sysdep_nsegs; i++) {
    431  1.20  tshiozak 		v = base + flip(stable[i].off, magic);
    432  1.20  tshiozak 		mohandle->mo.mo_sysdep_segs[i].str =
    433  1.20  tshiozak 		    __intl_sysdep_get_string_by_tag(
    434  1.20  tshiozak 			    v,
    435  1.20  tshiozak 			    &mohandle->mo.mo_sysdep_segs[i].len);
    436  1.20  tshiozak 	}
    437  1.20  tshiozak 
    438  1.20  tshiozak 	/* get sysdep string table */
    439  1.20  tshiozak 	mohandle->mo.mo_sysdep_otable =
    440  1.20  tshiozak 	    (struct mosysdepstr_h **)calloc(mohandle->mo.mo_sysdep_nstring,
    441  1.20  tshiozak 					    sizeof(struct mosysdepstr_h *));
    442  1.20  tshiozak 	if (!mohandle->mo.mo_sysdep_otable)
    443  1.20  tshiozak 		return -1;
    444  1.20  tshiozak 	/* LINTED: ignore the alignment problem. */
    445  1.25  junyoung 	ofstable = (uint32_t *)(base + flip(mo->mo_sysdep_otable, magic));
    446  1.20  tshiozak 	if (get_sysdep_string_table(mohandle->mo.mo_sysdep_otable, ofstable,
    447  1.20  tshiozak 				    mohandle->mo.mo_sysdep_nstring, magic,
    448  1.20  tshiozak 				    base))
    449  1.20  tshiozak 		return -1;
    450  1.20  tshiozak 	mohandle->mo.mo_sysdep_ttable =
    451  1.20  tshiozak 	    (struct mosysdepstr_h **)calloc(mohandle->mo.mo_sysdep_nstring,
    452  1.20  tshiozak 					    sizeof(struct mosysdepstr_h *));
    453  1.20  tshiozak 	if (!mohandle->mo.mo_sysdep_ttable)
    454  1.20  tshiozak 		return -1;
    455  1.20  tshiozak 	/* LINTED: ignore the alignment problem. */
    456  1.25  junyoung 	ofstable = (uint32_t *)(base + flip(mo->mo_sysdep_ttable, magic));
    457  1.20  tshiozak 	if (get_sysdep_string_table(mohandle->mo.mo_sysdep_ttable, ofstable,
    458  1.20  tshiozak 				    mohandle->mo.mo_sysdep_nstring, magic,
    459  1.20  tshiozak 				    base))
    460  1.20  tshiozak 		return -1;
    461  1.20  tshiozak 
    462  1.20  tshiozak 	/* update hash */
    463  1.20  tshiozak 	for (i=0; i<mohandle->mo.mo_sysdep_nstring; i++) {
    464  1.20  tshiozak 		if (expand_sysdep(mohandle, mohandle->mo.mo_sysdep_otable[i]))
    465  1.20  tshiozak 			return -1;
    466  1.20  tshiozak 		insert_to_hash(mohandle->mo.mo_htable,
    467  1.20  tshiozak 			       mohandle->mo.mo_hsize,
    468  1.20  tshiozak 			       mohandle->mo.mo_sysdep_otable[i]->expanded,
    469  1.20  tshiozak 			       (i+1) | MO_HASH_SYSDEP_MASK);
    470  1.20  tshiozak 	}
    471  1.20  tshiozak 
    472  1.20  tshiozak 	return 0;
    473  1.20  tshiozak }
    474  1.20  tshiozak 
    475   1.1    itojun int
    476  1.25  junyoung mapit(const char *path, struct domainbinding *db)
    477   1.1    itojun {
    478   1.1    itojun 	int fd;
    479   1.1    itojun 	struct stat st;
    480   1.1    itojun 	char *base;
    481  1.25  junyoung 	uint32_t magic, revision, flags = 0;
    482   1.1    itojun 	struct moentry *otable, *ttable;
    483  1.25  junyoung 	const uint32_t *htable;
    484   1.1    itojun 	struct moentry_h *p;
    485   1.1    itojun 	struct mo *mo;
    486  1.22  tshiozak 	size_t l, headerlen;
    487  1.27      matt 	unsigned int i;
    488   1.1    itojun 	char *v;
    489   1.9   minoura 	struct mohandle *mohandle = &db->mohandle;
    490   1.1    itojun 
    491   1.9   minoura 	if (mohandle->addr && mohandle->addr != MAP_FAILED &&
    492   1.9   minoura 	    mohandle->mo.mo_magic)
    493   1.1    itojun 		return 0;	/*already opened*/
    494   1.1    itojun 
    495   1.9   minoura 	unmapit(db);
    496   1.1    itojun 
    497   1.1    itojun #if 0
    498   1.1    itojun 	if (secure_path(path) != 0)
    499   1.1    itojun 		goto fail;
    500   1.1    itojun #endif
    501   1.1    itojun 	if (stat(path, &st) < 0)
    502   1.1    itojun 		goto fail;
    503   1.1    itojun 	if ((st.st_mode & S_IFMT) != S_IFREG || st.st_size > GETTEXT_MMAP_MAX)
    504   1.1    itojun 		goto fail;
    505   1.1    itojun 	fd = open(path, O_RDONLY);
    506   1.1    itojun 	if (fd < 0)
    507   1.1    itojun 		goto fail;
    508   1.2    itojun 	if (read(fd, &magic, sizeof(magic)) != sizeof(magic) ||
    509   1.1    itojun 	    (magic != MO_MAGIC && magic != MO_MAGIC_SWAPPED)) {
    510   1.1    itojun 		close(fd);
    511   1.1    itojun 		goto fail;
    512   1.1    itojun 	}
    513  1.19  tshiozak 	if (read(fd, &revision, sizeof(revision)) != sizeof(revision)) {
    514  1.19  tshiozak 		close(fd);
    515  1.19  tshiozak 		goto fail;
    516  1.19  tshiozak 	}
    517  1.19  tshiozak 	switch (flip(revision, magic)) {
    518  1.19  tshiozak 	case MO_MAKE_REV(0, 0):
    519  1.20  tshiozak 		break;
    520  1.19  tshiozak 	case MO_MAKE_REV(0, 1):
    521  1.19  tshiozak 	case MO_MAKE_REV(1, 1):
    522  1.20  tshiozak 		flags |= MO_F_SYSDEP;
    523  1.19  tshiozak 		break;
    524  1.19  tshiozak 	default:
    525   1.1    itojun 		close(fd);
    526   1.1    itojun 		goto fail;
    527   1.1    itojun 	}
    528   1.9   minoura 	mohandle->addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
    529   1.4    itojun 	    MAP_FILE | MAP_SHARED, fd, (off_t)0);
    530   1.9   minoura 	if (!mohandle->addr || mohandle->addr == MAP_FAILED) {
    531   1.1    itojun 		close(fd);
    532   1.1    itojun 		goto fail;
    533   1.1    itojun 	}
    534   1.1    itojun 	close(fd);
    535   1.9   minoura 	mohandle->len = (size_t)st.st_size;
    536   1.1    itojun 
    537   1.9   minoura 	base = mohandle->addr;
    538   1.9   minoura 	mo = (struct mo *)mohandle->addr;
    539   1.1    itojun 
    540   1.1    itojun 	/* flip endian.  do not flip magic number! */
    541   1.9   minoura 	mohandle->mo.mo_magic = mo->mo_magic;
    542   1.9   minoura 	mohandle->mo.mo_revision = flip(mo->mo_revision, magic);
    543   1.9   minoura 	mohandle->mo.mo_nstring = flip(mo->mo_nstring, magic);
    544  1.19  tshiozak 	mohandle->mo.mo_hsize = flip(mo->mo_hsize, magic);
    545  1.20  tshiozak 	mohandle->mo.mo_flags = flags;
    546   1.1    itojun 
    547   1.1    itojun 	/* validate otable/ttable */
    548  1.19  tshiozak 	/* LINTED: ignore the alignment problem. */
    549   1.1    itojun 	otable = (struct moentry *)(base + flip(mo->mo_otable, magic));
    550  1.19  tshiozak 	/* LINTED: ignore the alignment problem. */
    551   1.1    itojun 	ttable = (struct moentry *)(base + flip(mo->mo_ttable, magic));
    552   1.9   minoura 	if (!validate(otable, mohandle) ||
    553   1.9   minoura 	    !validate(&otable[mohandle->mo.mo_nstring], mohandle)) {
    554   1.9   minoura 		unmapit(db);
    555   1.1    itojun 		goto fail;
    556   1.1    itojun 	}
    557   1.9   minoura 	if (!validate(ttable, mohandle) ||
    558   1.9   minoura 	    !validate(&ttable[mohandle->mo.mo_nstring], mohandle)) {
    559   1.9   minoura 		unmapit(db);
    560   1.1    itojun 		goto fail;
    561   1.1    itojun 	}
    562   1.1    itojun 
    563   1.1    itojun 	/* allocate [ot]table, and convert to normal pointer representation. */
    564   1.9   minoura 	l = sizeof(struct moentry_h) * mohandle->mo.mo_nstring;
    565   1.9   minoura 	mohandle->mo.mo_otable = (struct moentry_h *)malloc(l);
    566   1.9   minoura 	if (!mohandle->mo.mo_otable) {
    567   1.9   minoura 		unmapit(db);
    568   1.1    itojun 		goto fail;
    569   1.1    itojun 	}
    570   1.9   minoura 	mohandle->mo.mo_ttable = (struct moentry_h *)malloc(l);
    571   1.9   minoura 	if (!mohandle->mo.mo_ttable) {
    572   1.9   minoura 		unmapit(db);
    573   1.1    itojun 		goto fail;
    574   1.1    itojun 	}
    575   1.9   minoura 	p = mohandle->mo.mo_otable;
    576   1.9   minoura 	for (i = 0; i < mohandle->mo.mo_nstring; i++) {
    577   1.1    itojun 		p[i].len = flip(otable[i].len, magic);
    578   1.1    itojun 		p[i].off = base + flip(otable[i].off, magic);
    579   1.1    itojun 
    580   1.9   minoura 		if (!validate(p[i].off, mohandle) ||
    581   1.9   minoura 		    !validate(p[i].off + p[i].len + 1, mohandle)) {
    582   1.9   minoura 			unmapit(db);
    583   1.1    itojun 			goto fail;
    584   1.1    itojun 		}
    585   1.1    itojun 	}
    586   1.9   minoura 	p = mohandle->mo.mo_ttable;
    587   1.9   minoura 	for (i = 0; i < mohandle->mo.mo_nstring; i++) {
    588   1.1    itojun 		p[i].len = flip(ttable[i].len, magic);
    589   1.1    itojun 		p[i].off = base + flip(ttable[i].off, magic);
    590   1.1    itojun 
    591   1.9   minoura 		if (!validate(p[i].off, mohandle) ||
    592   1.9   minoura 		    !validate(p[i].off + p[i].len + 1, mohandle)) {
    593   1.9   minoura 			unmapit(db);
    594   1.1    itojun 			goto fail;
    595   1.1    itojun 		}
    596   1.1    itojun 	}
    597  1.19  tshiozak 	/* allocate htable, and convert it to the host order. */
    598  1.19  tshiozak 	if (mohandle->mo.mo_hsize > 2) {
    599  1.25  junyoung 		l = sizeof(uint32_t) * mohandle->mo.mo_hsize;
    600  1.25  junyoung 		mohandle->mo.mo_htable = (uint32_t *)malloc(l);
    601  1.19  tshiozak 		if (!mohandle->mo.mo_htable) {
    602  1.19  tshiozak 			unmapit(db);
    603  1.19  tshiozak 			goto fail;
    604  1.19  tshiozak 		}
    605  1.19  tshiozak 		/* LINTED: ignore the alignment problem. */
    606  1.25  junyoung 		htable = (const uint32_t *)(base+flip(mo->mo_hoffset, magic));
    607  1.19  tshiozak 		for (i=0; i < mohandle->mo.mo_hsize; i++) {
    608  1.19  tshiozak 			mohandle->mo.mo_htable[i] = flip(htable[i], magic);
    609  1.19  tshiozak 			if (mohandle->mo.mo_htable[i] >=
    610  1.19  tshiozak 			    mohandle->mo.mo_nstring+1) {
    611  1.19  tshiozak 				/* illegal string number. */
    612  1.19  tshiozak 				unmapit(db);
    613  1.19  tshiozak 				goto fail;
    614  1.19  tshiozak 			}
    615  1.19  tshiozak 		}
    616  1.19  tshiozak 	}
    617   1.1    itojun 	/* grab MIME-header and charset field */
    618  1.22  tshiozak 	mohandle->mo.mo_header = lookup("", db, &headerlen);
    619   1.9   minoura 	if (mohandle->mo.mo_header)
    620   1.9   minoura 		v = strstr(mohandle->mo.mo_header, "charset=");
    621   1.1    itojun 	else
    622   1.1    itojun 		v = NULL;
    623   1.1    itojun 	if (v) {
    624   1.9   minoura 		mohandle->mo.mo_charset = strdup(v + 8);
    625   1.9   minoura 		if (!mohandle->mo.mo_charset)
    626   1.6    itojun 			goto fail;
    627   1.9   minoura 		v = strchr(mohandle->mo.mo_charset, '\n');
    628   1.1    itojun 		if (v)
    629   1.1    itojun 			*v = '\0';
    630   1.1    itojun 	}
    631  1.26   tnozaki 	if (!mohandle->mo.mo_header ||
    632  1.26   tnozaki 	    _gettext_parse_plural(&mohandle->mo.mo_plural,
    633  1.22  tshiozak 				  &mohandle->mo.mo_nplurals,
    634  1.22  tshiozak 				  mohandle->mo.mo_header, headerlen))
    635  1.22  tshiozak 		mohandle->mo.mo_plural = NULL;
    636   1.1    itojun 
    637   1.1    itojun 	/*
    638   1.1    itojun 	 * XXX check charset, reject it if we are unable to support the charset
    639   1.1    itojun 	 * with the current locale.
    640   1.1    itojun 	 * for example, if we are using euc-jp locale and we are looking at
    641   1.1    itojun 	 * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
    642   1.1    itojun 	 * the *.mo file as we cannot support it.
    643   1.1    itojun 	 */
    644   1.1    itojun 
    645  1.20  tshiozak 	/* system dependent string support */
    646  1.20  tshiozak 	if ((mohandle->mo.mo_flags & MO_F_SYSDEP) != 0) {
    647  1.20  tshiozak 		if (setup_sysdep_stuffs(mo, mohandle, base)) {
    648  1.20  tshiozak 			unmapit(db);
    649  1.20  tshiozak 			goto fail;
    650  1.20  tshiozak 		}
    651  1.20  tshiozak 	}
    652  1.20  tshiozak 
    653   1.1    itojun 	return 0;
    654   1.1    itojun 
    655   1.1    itojun fail:
    656   1.1    itojun 	return -1;
    657   1.1    itojun }
    658   1.1    itojun 
    659  1.20  tshiozak static void
    660  1.25  junyoung free_sysdep_table(struct mosysdepstr_h **table, uint32_t nstring)
    661  1.20  tshiozak {
    662  1.25  junyoung 	uint32_t i;
    663  1.20  tshiozak 
    664  1.20  tshiozak 	for (i=0; i<nstring; i++) {
    665  1.20  tshiozak 		if (table[i]) {
    666  1.20  tshiozak 			if (table[i]->expanded)
    667  1.20  tshiozak 				free(table[i]->expanded);
    668  1.20  tshiozak 			free(table[i]);
    669  1.20  tshiozak 		}
    670  1.20  tshiozak 	}
    671  1.20  tshiozak 	free(table);
    672  1.20  tshiozak }
    673  1.20  tshiozak 
    674   1.1    itojun static int
    675  1.25  junyoung unmapit(struct domainbinding *db)
    676   1.1    itojun {
    677   1.9   minoura 	struct mohandle *mohandle = &db->mohandle;
    678   1.1    itojun 
    679   1.1    itojun 	/* unmap if there's already mapped region */
    680   1.9   minoura 	if (mohandle->addr && mohandle->addr != MAP_FAILED)
    681   1.9   minoura 		munmap(mohandle->addr, mohandle->len);
    682   1.9   minoura 	mohandle->addr = NULL;
    683   1.9   minoura 	if (mohandle->mo.mo_otable)
    684   1.9   minoura 		free(mohandle->mo.mo_otable);
    685   1.9   minoura 	if (mohandle->mo.mo_ttable)
    686   1.9   minoura 		free(mohandle->mo.mo_ttable);
    687   1.9   minoura 	if (mohandle->mo.mo_charset)
    688   1.9   minoura 		free(mohandle->mo.mo_charset);
    689  1.19  tshiozak 	if (mohandle->mo.mo_htable)
    690  1.19  tshiozak 		free(mohandle->mo.mo_htable);
    691  1.20  tshiozak 	if (mohandle->mo.mo_sysdep_segs)
    692  1.20  tshiozak 		free(mohandle->mo.mo_sysdep_segs);
    693  1.20  tshiozak 	if (mohandle->mo.mo_sysdep_otable) {
    694  1.20  tshiozak 		free_sysdep_table(mohandle->mo.mo_sysdep_otable,
    695  1.20  tshiozak 				  mohandle->mo.mo_sysdep_nstring);
    696  1.20  tshiozak 	}
    697  1.20  tshiozak 	if (mohandle->mo.mo_sysdep_ttable) {
    698  1.20  tshiozak 		free_sysdep_table(mohandle->mo.mo_sysdep_ttable,
    699  1.20  tshiozak 				  mohandle->mo.mo_sysdep_nstring);
    700  1.20  tshiozak 	}
    701  1.22  tshiozak 	if (mohandle->mo.mo_plural)
    702  1.22  tshiozak 		_gettext_free_plural(mohandle->mo.mo_plural);
    703   1.9   minoura 	memset(&mohandle->mo, 0, sizeof(mohandle->mo));
    704   1.1    itojun 	return 0;
    705   1.1    itojun }
    706   1.1    itojun 
    707   1.9   minoura /* ARGSUSED */
    708   1.1    itojun static const char *
    709  1.25  junyoung lookup_hash(const char *msgid, struct domainbinding *db, size_t *rlen)
    710   1.1    itojun {
    711  1.19  tshiozak 	struct mohandle *mohandle = &db->mohandle;
    712  1.25  junyoung 	uint32_t idx, hashval, step, strno;
    713  1.19  tshiozak 	size_t len;
    714  1.20  tshiozak 	struct mosysdepstr_h *sysdep_otable, *sysdep_ttable;
    715  1.19  tshiozak 
    716  1.19  tshiozak 	if (mohandle->mo.mo_hsize <= 2 || mohandle->mo.mo_htable == NULL)
    717  1.19  tshiozak 		return NULL;
    718   1.1    itojun 
    719  1.19  tshiozak 	hashval = __intl_string_hash(msgid);
    720  1.19  tshiozak 	step = calc_collision_step(hashval, mohandle->mo.mo_hsize);
    721  1.19  tshiozak 	idx = hashval % mohandle->mo.mo_hsize;
    722  1.19  tshiozak 	len = strlen(msgid);
    723  1.19  tshiozak 	while (/*CONSTCOND*/1) {
    724  1.19  tshiozak 		strno = mohandle->mo.mo_htable[idx];
    725  1.19  tshiozak 		if (strno == 0) {
    726  1.19  tshiozak 			/* unexpected miss */
    727  1.19  tshiozak 			return NULL;
    728  1.19  tshiozak 		}
    729  1.19  tshiozak 		strno--;
    730  1.20  tshiozak 		if ((strno & MO_HASH_SYSDEP_MASK) == 0) {
    731  1.20  tshiozak 			/* system independent strings */
    732  1.20  tshiozak 			if (len <= mohandle->mo.mo_otable[strno].len &&
    733  1.20  tshiozak 			    !strcmp(msgid, mohandle->mo.mo_otable[strno].off)) {
    734  1.20  tshiozak 				/* hit */
    735  1.22  tshiozak 				if (rlen)
    736  1.22  tshiozak 					*rlen =
    737  1.22  tshiozak 					    mohandle->mo.mo_ttable[strno].len;
    738  1.20  tshiozak 				return mohandle->mo.mo_ttable[strno].off;
    739  1.20  tshiozak 			}
    740  1.20  tshiozak 		} else {
    741  1.20  tshiozak 			/* system dependent strings */
    742  1.20  tshiozak 			strno &= ~MO_HASH_SYSDEP_MASK;
    743  1.20  tshiozak 			sysdep_otable = mohandle->mo.mo_sysdep_otable[strno];
    744  1.20  tshiozak 			sysdep_ttable = mohandle->mo.mo_sysdep_ttable[strno];
    745  1.20  tshiozak 			if (len <= sysdep_otable->expanded_len &&
    746  1.20  tshiozak 			    !strcmp(msgid, sysdep_otable->expanded)) {
    747  1.20  tshiozak 				/* hit */
    748  1.20  tshiozak 				if (expand_sysdep(mohandle, sysdep_ttable))
    749  1.20  tshiozak 					/* memory exhausted */
    750  1.20  tshiozak 					return NULL;
    751  1.22  tshiozak 				if (rlen)
    752  1.22  tshiozak 					*rlen = sysdep_ttable->expanded_len;
    753  1.20  tshiozak 				return sysdep_ttable->expanded;
    754  1.20  tshiozak 			}
    755  1.19  tshiozak 		}
    756  1.19  tshiozak 		idx = calc_next_index(idx, mohandle->mo.mo_hsize, step);
    757  1.19  tshiozak 	}
    758  1.19  tshiozak 	/*NOTREACHED*/
    759   1.1    itojun }
    760   1.1    itojun 
    761   1.1    itojun static const char *
    762  1.25  junyoung lookup_bsearch(const char *msgid, struct domainbinding *db, size_t *rlen)
    763   1.1    itojun {
    764   1.1    itojun 	int top, bottom, middle, omiddle;
    765   1.1    itojun 	int n;
    766   1.9   minoura 	struct mohandle *mohandle = &db->mohandle;
    767   1.1    itojun 
    768   1.1    itojun 	top = 0;
    769   1.9   minoura 	bottom = mohandle->mo.mo_nstring;
    770   1.1    itojun 	omiddle = -1;
    771   1.9   minoura 	/* CONSTCOND */
    772   1.1    itojun 	while (1) {
    773   1.1    itojun 		if (top > bottom)
    774   1.4    itojun 			break;
    775   1.1    itojun 		middle = (top + bottom) / 2;
    776   1.1    itojun 		/* avoid possible infinite loop, when the data is not sorted */
    777   1.1    itojun 		if (omiddle == middle)
    778   1.4    itojun 			break;
    779  1.27      matt 		if ((size_t)middle >= mohandle->mo.mo_nstring)
    780   1.4    itojun 			break;
    781   1.1    itojun 
    782   1.9   minoura 		n = strcmp(msgid, mohandle->mo.mo_otable[middle].off);
    783  1.22  tshiozak 		if (n == 0) {
    784  1.22  tshiozak 			if (rlen)
    785  1.22  tshiozak 				*rlen = mohandle->mo.mo_ttable[middle].len;
    786   1.9   minoura 			return (const char *)mohandle->mo.mo_ttable[middle].off;
    787  1.22  tshiozak 		}
    788   1.1    itojun 		else if (n < 0)
    789   1.1    itojun 			bottom = middle;
    790   1.1    itojun 		else
    791   1.1    itojun 			top = middle;
    792   1.1    itojun 		omiddle = middle;
    793   1.1    itojun 	}
    794   1.1    itojun 
    795   1.1    itojun 	return NULL;
    796   1.1    itojun }
    797   1.1    itojun 
    798   1.1    itojun static const char *
    799  1.25  junyoung lookup(const char *msgid, struct domainbinding *db, size_t *rlen)
    800   1.1    itojun {
    801   1.1    itojun 	const char *v;
    802   1.1    itojun 
    803  1.22  tshiozak 	v = lookup_hash(msgid, db, rlen);
    804   1.1    itojun 	if (v)
    805   1.1    itojun 		return v;
    806   1.1    itojun 
    807  1.22  tshiozak 	return lookup_bsearch(msgid, db, rlen);
    808   1.1    itojun }
    809   1.1    itojun 
    810  1.16    itojun static const char *
    811  1.16    itojun get_lang_env(const char *category_name)
    812  1.10      yamt {
    813  1.10      yamt 	const char *lang;
    814  1.10      yamt 
    815  1.28      yamt 	/*
    816  1.28      yamt 	 * 1. see LANGUAGE variable first.
    817  1.28      yamt 	 *
    818  1.28      yamt 	 * LANGUAGE is a GNU extension.
    819  1.28      yamt 	 * It's a colon separated list of locale names.
    820  1.28      yamt 	 */
    821  1.10      yamt 	lang = getenv("LANGUAGE");
    822  1.10      yamt 	if (lang)
    823  1.10      yamt 		return lang;
    824  1.10      yamt 
    825  1.28      yamt 	/*
    826  1.28      yamt 	 * 2. if LANGUAGE isn't set, see LC_ALL, LC_xxx, LANG.
    827  1.28      yamt 	 *
    828  1.28      yamt 	 * It's essentially setlocale(LC_xxx, NULL).
    829  1.28      yamt 	 */
    830  1.13      yamt 	lang = getenv("LC_ALL");
    831  1.10      yamt 	if (!lang)
    832  1.13      yamt 		lang = getenv(category_name);
    833  1.10      yamt 	if (!lang)
    834  1.10      yamt 		lang = getenv("LANG");
    835  1.10      yamt 
    836  1.10      yamt 	if (!lang)
    837  1.10      yamt 		return 0; /* error */
    838  1.10      yamt 
    839  1.10      yamt 	return split_locale(lang);
    840  1.10      yamt }
    841  1.10      yamt 
    842  1.22  tshiozak static const char *
    843  1.22  tshiozak get_indexed_string(const char *str, size_t len, unsigned long idx)
    844  1.22  tshiozak {
    845  1.22  tshiozak 	while (idx > 0) {
    846  1.22  tshiozak 		if (len <= 1)
    847  1.22  tshiozak 			return str;
    848  1.22  tshiozak 		if (*str == '\0')
    849  1.22  tshiozak 			idx--;
    850  1.22  tshiozak 		if (len > 0) {
    851  1.22  tshiozak 			str++;
    852  1.22  tshiozak 			len--;
    853  1.22  tshiozak 		}
    854  1.22  tshiozak 	}
    855  1.22  tshiozak 	return str;
    856  1.22  tshiozak }
    857  1.22  tshiozak 
    858  1.23      yamt #define	_NGETTEXT_DEFAULT(msgid1, msgid2, n)	\
    859  1.23      yamt 	((char *)__UNCONST((n) == 1 ? (msgid1) : (msgid2)))
    860  1.23      yamt 
    861   1.1    itojun char *
    862  1.25  junyoung dcngettext(const char *domainname, const char *msgid1, const char *msgid2,
    863  1.25  junyoung 	   unsigned long int n, int category)
    864   1.1    itojun {
    865   1.1    itojun 	const char *msgid;
    866   1.1    itojun 	char path[PATH_MAX];
    867  1.10      yamt 	const char *lpath;
    868   1.1    itojun 	static char olpath[PATH_MAX];
    869   1.6    itojun 	const char *cname = NULL;
    870   1.1    itojun 	const char *v;
    871   1.6    itojun 	static char *ocname = NULL;
    872   1.6    itojun 	static char *odomainname = NULL;
    873   1.5    itojun 	struct domainbinding *db;
    874  1.24     lukem 	unsigned long plural_index = 0;
    875  1.22  tshiozak 	size_t len;
    876   1.1    itojun 
    877   1.1    itojun 	if (!domainname)
    878   1.9   minoura 		domainname = __current_domainname;
    879   1.1    itojun 	cname = lookup_category(category);
    880   1.1    itojun 	if (!domainname || !cname)
    881   1.1    itojun 		goto fail;
    882   1.1    itojun 
    883  1.10      yamt 	lpath = get_lang_env(cname);
    884  1.10      yamt 	if (!lpath)
    885   1.1    itojun 		goto fail;
    886  1.19  tshiozak 
    887   1.9   minoura 	for (db = __bindings; db; db = db->next)
    888   1.5    itojun 		if (strcmp(db->domainname, domainname) == 0)
    889   1.5    itojun 			break;
    890   1.9   minoura 	if (!db) {
    891   1.9   minoura 		if (!bindtextdomain(domainname, _PATH_TEXTDOMAIN))
    892   1.9   minoura 			goto fail;
    893   1.9   minoura 		db = __bindings;
    894  1.11      yamt 	}
    895  1.11      yamt 
    896  1.11      yamt 	/* resolve relative path */
    897  1.11      yamt 	/* XXX not necessary? */
    898  1.11      yamt 	if (db->path[0] != '/') {
    899  1.11      yamt 		char buf[PATH_MAX];
    900  1.11      yamt 
    901  1.11      yamt 		if (getcwd(buf, sizeof(buf)) == 0)
    902  1.11      yamt 			goto fail;
    903  1.11      yamt 		if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
    904  1.11      yamt 			goto fail;
    905  1.11      yamt 		if (strlcat(buf, db->path, sizeof(buf)) >= sizeof(buf))
    906  1.11      yamt 			goto fail;
    907  1.15    itojun 		strlcpy(db->path, buf, sizeof(db->path));
    908   1.9   minoura 	}
    909   1.5    itojun 
    910   1.1    itojun 	/* don't bother looking it up if the values are the same */
    911   1.5    itojun 	if (odomainname && strcmp(domainname, odomainname) == 0 &&
    912   1.9   minoura 	    ocname && strcmp(cname, ocname) == 0 && strcmp(lpath, olpath) == 0 &&
    913   1.9   minoura 	    db->mohandle.mo.mo_magic)
    914   1.1    itojun 		goto found;
    915   1.1    itojun 
    916   1.1    itojun 	/* try to find appropriate file, from $LANGUAGE */
    917   1.5    itojun 	if (lookup_mofile(path, sizeof(path), db->path, lpath, cname,
    918   1.9   minoura 	    domainname, db) == NULL)
    919   1.3    itojun 		goto fail;
    920   1.5    itojun 
    921   1.5    itojun 	if (odomainname)
    922   1.5    itojun 		free(odomainname);
    923   1.5    itojun 	if (ocname)
    924   1.5    itojun 		free(ocname);
    925   1.6    itojun 	odomainname = strdup(domainname);
    926   1.5    itojun 	ocname = strdup(cname);
    927   1.6    itojun 	if (!odomainname || !ocname) {
    928   1.6    itojun 		if (odomainname)
    929   1.6    itojun 			free(odomainname);
    930   1.6    itojun 		if (ocname)
    931   1.6    itojun 			free(ocname);
    932   1.6    itojun 		odomainname = ocname = NULL;
    933   1.6    itojun 	}
    934  1.10      yamt 	else
    935  1.10      yamt 		strlcpy(olpath, lpath, sizeof(olpath));
    936   1.1    itojun 
    937   1.1    itojun found:
    938  1.22  tshiozak 	if (db->mohandle.mo.mo_plural) {
    939  1.22  tshiozak 		plural_index =
    940  1.22  tshiozak 		    _gettext_calculate_plural(db->mohandle.mo.mo_plural, n);
    941  1.22  tshiozak 		if (plural_index >= db->mohandle.mo.mo_nplurals)
    942  1.22  tshiozak 			plural_index = 0;
    943  1.22  tshiozak 		msgid = msgid1;
    944  1.22  tshiozak 	} else
    945  1.23      yamt 		msgid = _NGETTEXT_DEFAULT(msgid1, msgid2, n);
    946  1.22  tshiozak 
    947  1.22  tshiozak 	if (msgid == NULL)
    948  1.22  tshiozak 		return NULL;
    949  1.22  tshiozak 
    950  1.22  tshiozak 	v = lookup(msgid, db, &len);
    951   1.1    itojun 	if (v) {
    952  1.22  tshiozak 		if (db->mohandle.mo.mo_plural)
    953  1.22  tshiozak 			v = get_indexed_string(v, len, plural_index);
    954   1.1    itojun 		/*
    955  1.18      yamt 		 * convert the translated message's encoding.
    956  1.18      yamt 		 *
    957  1.18      yamt 		 * special case:
    958  1.18      yamt 		 *	a result of gettext("") shouldn't need any conversion.
    959   1.1    itojun 		 */
    960  1.18      yamt 		if (msgid[0])
    961  1.18      yamt 			v = __gettext_iconv(v, db);
    962   1.1    itojun 
    963   1.1    itojun 		/*
    964   1.1    itojun 		 * Given the amount of printf-format security issues, it may
    965   1.1    itojun 		 * be a good idea to validate if the original msgid and the
    966   1.1    itojun 		 * translated message format string carry the same printf-like
    967   1.1    itojun 		 * format identifiers.
    968   1.1    itojun 		 */
    969   1.1    itojun 
    970   1.1    itojun 		msgid = v;
    971   1.1    itojun 	}
    972   1.1    itojun 
    973  1.23      yamt 	return (char *)__UNCONST(msgid);
    974  1.23      yamt 
    975   1.1    itojun fail:
    976  1.23      yamt 	return _NGETTEXT_DEFAULT(msgid1, msgid2, n);
    977   1.1    itojun }
    978