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