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