Home | History | Annotate | Line # | Download | only in libintl
      1  1.9     kamil /*	$NetBSD: gettext_iconv.c,v 1.9 2019/10/24 18:18:00 kamil Exp $	*/
      2  1.1      yamt 
      3  1.1      yamt /*-
      4  1.1      yamt  * Copyright (c) 2004 Citrus Project,
      5  1.1      yamt  * All rights reserved.
      6  1.1      yamt  *
      7  1.1      yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1      yamt  * modification, are permitted provided that the following conditions
      9  1.1      yamt  * are met:
     10  1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1      yamt  *
     16  1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1      yamt  * SUCH DAMAGE.
     27  1.1      yamt  *
     28  1.1      yamt  * $Citrus$
     29  1.1      yamt  */
     30  1.1      yamt 
     31  1.1      yamt 
     32  1.1      yamt #include <sys/types.h>
     33  1.1      yamt #include <sys/param.h>
     34  1.1      yamt 
     35  1.1      yamt #include <errno.h>
     36  1.1      yamt #include <iconv.h>
     37  1.1      yamt #include <libintl.h>
     38  1.1      yamt #include <langinfo.h>
     39  1.1      yamt #include <search.h>
     40  1.1      yamt #include <stdlib.h>
     41  1.1      yamt #include <string.h>
     42  1.1      yamt 
     43  1.1      yamt #include "libintl_local.h"
     44  1.1      yamt 
     45  1.1      yamt struct cache {
     46  1.1      yamt 	const char *c_origmsg;
     47  1.1      yamt 	const char *c_resultmsg;
     48  1.1      yamt };
     49  1.1      yamt 
     50  1.1      yamt static const struct cache *cache_find(const char *, struct domainbinding *);
     51  1.1      yamt static int cache_enter(const char *, const char *);
     52  1.1      yamt static int cache_cmp(const void *, const void *);
     53  1.1      yamt 
     54  1.1      yamt static void *cacheroot;
     55  1.1      yamt 
     56  1.3      yamt /* ARGSUSED1 */
     57  1.1      yamt static const struct cache *
     58  1.1      yamt cache_find(const char *msg, struct domainbinding *db)
     59  1.1      yamt {
     60  1.1      yamt 	struct cache key;
     61  1.1      yamt 	struct cache **c;
     62  1.1      yamt 
     63  1.1      yamt 	key.c_origmsg = msg;
     64  1.1      yamt 	c = tfind(&key, &cacheroot, cache_cmp);
     65  1.1      yamt 
     66  1.1      yamt 	return c ? *c : NULL;
     67  1.1      yamt }
     68  1.1      yamt 
     69  1.1      yamt static int
     70  1.1      yamt cache_enter(const char *origmsg, const char *resultmsg)
     71  1.1      yamt {
     72  1.1      yamt 	struct cache *c;
     73  1.1      yamt 
     74  1.1      yamt 	c = malloc(sizeof(*c));
     75  1.1      yamt 	if (c == NULL)
     76  1.1      yamt 		return -1;
     77  1.1      yamt 
     78  1.1      yamt 	c->c_origmsg = origmsg;
     79  1.1      yamt 	c->c_resultmsg = resultmsg;
     80  1.1      yamt 
     81  1.1      yamt 	if (tsearch(c, &cacheroot, cache_cmp) == NULL) {
     82  1.1      yamt 		free(c);
     83  1.1      yamt 		return -1;
     84  1.1      yamt 	}
     85  1.1      yamt 
     86  1.1      yamt 	return 0;
     87  1.1      yamt }
     88  1.1      yamt 
     89  1.1      yamt static int
     90  1.1      yamt cache_cmp(const void *va, const void *vb)
     91  1.1      yamt {
     92  1.1      yamt 	const struct cache *a = va;
     93  1.1      yamt 	const struct cache *b = vb;
     94  1.1      yamt 	int result;
     95  1.1      yamt 
     96  1.8      yamt 	if (a->c_origmsg > b->c_origmsg) {
     97  1.8      yamt 		result = 1;
     98  1.8      yamt 	} else if (a->c_origmsg < b->c_origmsg) {
     99  1.8      yamt 		result = -1;
    100  1.8      yamt 	} else {
    101  1.8      yamt 		result = 0;
    102  1.8      yamt 	}
    103  1.1      yamt 
    104  1.1      yamt 	return result;
    105  1.1      yamt }
    106  1.1      yamt 
    107  1.5  uebayasi #define	GETTEXT_ICONV_MALLOC_CHUNK	(16 * 1024)
    108  1.1      yamt 
    109  1.1      yamt const char *
    110  1.1      yamt __gettext_iconv(const char *origmsg, struct domainbinding *db)
    111  1.1      yamt {
    112  1.1      yamt 	const char *tocode;
    113  1.1      yamt 	const char *fromcode = db->mohandle.mo.mo_charset;
    114  1.1      yamt 	const struct cache *cache;
    115  1.1      yamt 	const char *result;
    116  1.1      yamt 	iconv_t cd;
    117  1.1      yamt 	const char *src;
    118  1.1      yamt 	char *dst;
    119  1.1      yamt 	size_t origlen;
    120  1.1      yamt 	size_t srclen;
    121  1.1      yamt 	size_t dstlen;
    122  1.1      yamt 	size_t nvalid;
    123  1.1      yamt 	int savederrno = errno;
    124  1.1      yamt 
    125  1.2      yamt 	/*
    126  1.2      yamt 	 * static buffer for converted texts.
    127  1.2      yamt 	 *
    128  1.2      yamt 	 * note:
    129  1.2      yamt 	 * we never free buffers once returned to callers.
    130  1.2      yamt 	 * because of interface design of gettext, we can't know
    131  1.2      yamt 	 * the lifetime of them.
    132  1.2      yamt 	 */
    133  1.1      yamt 	static char *buffer;
    134  1.1      yamt 	static size_t bufferlen;
    135  1.1      yamt 
    136  1.5  uebayasi 	/*
    137  1.5  uebayasi 	 * don't convert message if *.mo doesn't specify codeset.
    138  1.5  uebayasi 	 */
    139  1.5  uebayasi 	if (fromcode == NULL)
    140  1.5  uebayasi 		return origmsg;
    141  1.5  uebayasi 
    142  1.1      yamt 	tocode = db->codeset;
    143  1.1      yamt 	if (tocode == NULL) {
    144  1.1      yamt 		/*
    145  1.2      yamt 		 * codeset isn't specified explicitly by
    146  1.2      yamt 		 * bind_textdomain_codeset().
    147  1.4      yamt 		 * use current locale(LC_CTYPE)'s codeset.
    148  1.1      yamt 		 *
    149  1.1      yamt 		 * XXX maybe wrong; it can mismatch with
    150  1.1      yamt 		 * environment variable setting.
    151  1.1      yamt 		 */
    152  1.1      yamt 		tocode = nl_langinfo(CODESET);
    153  1.1      yamt 	}
    154  1.1      yamt 
    155  1.1      yamt 	/*
    156  1.1      yamt 	 * shortcut if possible.
    157  1.1      yamt 	 * XXX should handle aliases
    158  1.1      yamt 	 */
    159  1.1      yamt 	if (!strcasecmp(tocode, fromcode))
    160  1.1      yamt 		return origmsg;
    161  1.1      yamt 
    162  1.1      yamt 	/* XXX LOCK */
    163  1.1      yamt 
    164  1.1      yamt 	/* XXX should detect change of tocode and purge caches? */
    165  1.1      yamt 
    166  1.1      yamt 	/*
    167  1.1      yamt 	 * see if we have already converted this message.
    168  1.1      yamt 	 */
    169  1.1      yamt 	cache = cache_find(origmsg, db);
    170  1.1      yamt 	if (cache) {
    171  1.1      yamt 		result = cache->c_resultmsg;
    172  1.1      yamt 		goto out;
    173  1.1      yamt 	}
    174  1.1      yamt 
    175  1.1      yamt 	origlen = strlen(origmsg) + 1;
    176  1.1      yamt again:
    177  1.1      yamt 	cd = iconv_open(tocode, fromcode);
    178  1.1      yamt 	if (cd == (iconv_t)-1) {
    179  1.1      yamt 		result = origmsg;
    180  1.1      yamt 		goto out;
    181  1.1      yamt 	}
    182  1.1      yamt 
    183  1.1      yamt 	src = origmsg;
    184  1.1      yamt 	srclen = origlen;
    185  1.1      yamt 	dst = buffer;
    186  1.1      yamt 	dstlen = bufferlen;
    187  1.9     kamil 	nvalid = iconv(cd, __UNCONST(&src), &srclen, &dst, &dstlen);
    188  1.1      yamt 	iconv_close(cd);
    189  1.1      yamt 
    190  1.1      yamt 	if (nvalid == (size_t)-1) {
    191  1.1      yamt 		/*
    192  1.1      yamt 		 * try to allocate a new buffer.
    193  1.1      yamt 		 *
    194  1.1      yamt 		 * just give up if GETTEXT_ICONV_MALLOC_CHUNK was not enough.
    195  1.1      yamt 		 */
    196  1.1      yamt 		if (errno == E2BIG &&
    197  1.1      yamt 		    bufferlen != GETTEXT_ICONV_MALLOC_CHUNK) {
    198  1.1      yamt 			buffer = malloc(GETTEXT_ICONV_MALLOC_CHUNK);
    199  1.1      yamt 			if (buffer) {
    200  1.1      yamt 				bufferlen = GETTEXT_ICONV_MALLOC_CHUNK;
    201  1.1      yamt 				goto again;
    202  1.1      yamt 			}
    203  1.1      yamt 		}
    204  1.1      yamt 
    205  1.1      yamt 		result = origmsg;
    206  1.1      yamt 	} else if (cache_enter(origmsg, buffer)) {
    207  1.1      yamt 		/*
    208  1.1      yamt 		 * failed to enter cache.  give up.
    209  1.1      yamt 		 */
    210  1.1      yamt 		result = origmsg;
    211  1.1      yamt 	} else {
    212  1.1      yamt 		size_t resultlen = dst - buffer;
    213  1.1      yamt 
    214  1.1      yamt 		result = buffer;
    215  1.1      yamt 		bufferlen -= resultlen;
    216  1.1      yamt 		buffer += resultlen;
    217  1.1      yamt 	}
    218  1.1      yamt 
    219  1.1      yamt out:
    220  1.1      yamt 	/* XXX UNLOCK */
    221  1.1      yamt 	errno = savederrno;
    222  1.1      yamt 
    223  1.1      yamt 	return result;
    224  1.1      yamt }
    225