Home | History | Annotate | Line # | Download | only in libintl
gettext_iconv.c revision 1.1
      1  1.1  yamt /*	$NetBSD: gettext_iconv.c,v 1.1 2004/01/18 08:40:40 yamt 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.1  yamt static const struct cache *
     57  1.1  yamt cache_find(const char *msg, struct domainbinding *db)
     58  1.1  yamt {
     59  1.1  yamt 	struct cache key;
     60  1.1  yamt 	struct cache **c;
     61  1.1  yamt 
     62  1.1  yamt 	key.c_origmsg = msg;
     63  1.1  yamt 	c = tfind(&key, &cacheroot, cache_cmp);
     64  1.1  yamt 
     65  1.1  yamt 	return c ? *c : NULL;
     66  1.1  yamt }
     67  1.1  yamt 
     68  1.1  yamt static int
     69  1.1  yamt cache_enter(const char *origmsg, const char *resultmsg)
     70  1.1  yamt {
     71  1.1  yamt 	struct cache *c;
     72  1.1  yamt 
     73  1.1  yamt 	c = malloc(sizeof(*c));
     74  1.1  yamt 	if (c == NULL)
     75  1.1  yamt 		return -1;
     76  1.1  yamt 
     77  1.1  yamt 	c->c_origmsg = origmsg;
     78  1.1  yamt 	c->c_resultmsg = resultmsg;
     79  1.1  yamt 
     80  1.1  yamt 	if (tsearch(c, &cacheroot, cache_cmp) == NULL) {
     81  1.1  yamt 		free(c);
     82  1.1  yamt 		return -1;
     83  1.1  yamt 	}
     84  1.1  yamt 
     85  1.1  yamt 	return 0;
     86  1.1  yamt }
     87  1.1  yamt 
     88  1.1  yamt static int
     89  1.1  yamt cache_cmp(const void *va, const void *vb)
     90  1.1  yamt {
     91  1.1  yamt 	const struct cache *a = va;
     92  1.1  yamt 	const struct cache *b = vb;
     93  1.1  yamt 	int result;
     94  1.1  yamt 
     95  1.1  yamt 	result = a->c_origmsg - b->c_origmsg;
     96  1.1  yamt 
     97  1.1  yamt 	return result;
     98  1.1  yamt }
     99  1.1  yamt 
    100  1.1  yamt #define	GETTEXT_ICONV_MALLOC_CHUNK	(16*1024)
    101  1.1  yamt 
    102  1.1  yamt const char *
    103  1.1  yamt __gettext_iconv(const char *origmsg, struct domainbinding *db)
    104  1.1  yamt {
    105  1.1  yamt 	const char *tocode;
    106  1.1  yamt 	const char *fromcode = db->mohandle.mo.mo_charset;
    107  1.1  yamt 	const struct cache *cache;
    108  1.1  yamt 	const char *result;
    109  1.1  yamt 	iconv_t cd;
    110  1.1  yamt 	const char *src;
    111  1.1  yamt 	char *dst;
    112  1.1  yamt 	size_t origlen;
    113  1.1  yamt 	size_t srclen;
    114  1.1  yamt 	size_t dstlen;
    115  1.1  yamt 	size_t nvalid;
    116  1.1  yamt 	int savederrno = errno;
    117  1.1  yamt 
    118  1.1  yamt 	static char *buffer;
    119  1.1  yamt 	static size_t bufferlen;
    120  1.1  yamt 
    121  1.1  yamt 	tocode = db->codeset;
    122  1.1  yamt 	if (tocode == NULL) {
    123  1.1  yamt 		/*
    124  1.1  yamt 		 * convert to current LC_MESSAGE's codeset.
    125  1.1  yamt 		 *
    126  1.1  yamt 		 * XXX maybe wrong; it can mismatch with
    127  1.1  yamt 		 * environment variable setting.
    128  1.1  yamt 		 */
    129  1.1  yamt 		tocode = nl_langinfo(CODESET);
    130  1.1  yamt 	}
    131  1.1  yamt 
    132  1.1  yamt 	/*
    133  1.1  yamt 	 * shortcut if possible.
    134  1.1  yamt 	 * XXX should handle aliases
    135  1.1  yamt 	 */
    136  1.1  yamt 	if (!strcasecmp(tocode, fromcode))
    137  1.1  yamt 		return origmsg;
    138  1.1  yamt 
    139  1.1  yamt 	/* XXX LOCK */
    140  1.1  yamt 
    141  1.1  yamt 	/* XXX should detect change of tocode and purge caches? */
    142  1.1  yamt 
    143  1.1  yamt 	/*
    144  1.1  yamt 	 * see if we have already converted this message.
    145  1.1  yamt 	 */
    146  1.1  yamt 	cache = cache_find(origmsg, db);
    147  1.1  yamt 	if (cache) {
    148  1.1  yamt 		result = cache->c_resultmsg;
    149  1.1  yamt 		goto out;
    150  1.1  yamt 	}
    151  1.1  yamt 
    152  1.1  yamt 	origlen = strlen(origmsg) + 1;
    153  1.1  yamt again:
    154  1.1  yamt 	cd = iconv_open(tocode, fromcode);
    155  1.1  yamt 	if (cd == (iconv_t)-1) {
    156  1.1  yamt 		result = origmsg;
    157  1.1  yamt 		goto out;
    158  1.1  yamt 	}
    159  1.1  yamt 
    160  1.1  yamt 	src = origmsg;
    161  1.1  yamt 	srclen = origlen;
    162  1.1  yamt 	dst = buffer;
    163  1.1  yamt 	dstlen = bufferlen;
    164  1.1  yamt 	nvalid = iconv(cd, &src, &srclen, &dst, &dstlen);
    165  1.1  yamt 	iconv_close(cd);
    166  1.1  yamt 
    167  1.1  yamt 	if (nvalid == (size_t)-1) {
    168  1.1  yamt 		/*
    169  1.1  yamt 		 * try to allocate a new buffer.
    170  1.1  yamt 		 *
    171  1.1  yamt 		 * just give up if GETTEXT_ICONV_MALLOC_CHUNK was not enough.
    172  1.1  yamt 		 */
    173  1.1  yamt 		if (errno == E2BIG &&
    174  1.1  yamt 		    bufferlen != GETTEXT_ICONV_MALLOC_CHUNK) {
    175  1.1  yamt 			buffer = malloc(GETTEXT_ICONV_MALLOC_CHUNK);
    176  1.1  yamt 			if (buffer) {
    177  1.1  yamt 				bufferlen = GETTEXT_ICONV_MALLOC_CHUNK;
    178  1.1  yamt 				goto again;
    179  1.1  yamt 			}
    180  1.1  yamt 		}
    181  1.1  yamt 
    182  1.1  yamt 		result = origmsg;
    183  1.1  yamt 	} else if (cache_enter(origmsg, buffer)) {
    184  1.1  yamt 		/*
    185  1.1  yamt 		 * failed to enter cache.  give up.
    186  1.1  yamt 		 */
    187  1.1  yamt 		result = origmsg;
    188  1.1  yamt 	} else {
    189  1.1  yamt 		size_t resultlen = dst - buffer;
    190  1.1  yamt 
    191  1.1  yamt 		result = buffer;
    192  1.1  yamt 		bufferlen -= resultlen;
    193  1.1  yamt 		buffer += resultlen;
    194  1.1  yamt 	}
    195  1.1  yamt 
    196  1.1  yamt out:
    197  1.1  yamt 	/* XXX UNLOCK */
    198  1.1  yamt 	errno = savederrno;
    199  1.1  yamt 
    200  1.1  yamt 	return result;
    201  1.1  yamt }
    202