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