xref: /netbsd-src/lib/libintl/gettext_iconv.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: gettext_iconv.c,v 1.7 2004/08/02 13:38:21 tshiozak 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 	/*
131 	 * don't convert message if *.mo doesn't specify codeset.
132 	 */
133 	if (fromcode == NULL)
134 		return origmsg;
135 
136 	tocode = db->codeset;
137 	if (tocode == NULL) {
138 		/*
139 		 * codeset isn't specified explicitly by
140 		 * bind_textdomain_codeset().
141 		 * use current locale(LC_CTYPE)'s codeset.
142 		 *
143 		 * XXX maybe wrong; it can mismatch with
144 		 * environment variable setting.
145 		 */
146 		tocode = nl_langinfo(CODESET);
147 	}
148 
149 	/*
150 	 * shortcut if possible.
151 	 * XXX should handle aliases
152 	 */
153 	if (!strcasecmp(tocode, fromcode))
154 		return origmsg;
155 
156 	/* XXX LOCK */
157 
158 	/* XXX should detect change of tocode and purge caches? */
159 
160 	/*
161 	 * see if we have already converted this message.
162 	 */
163 	cache = cache_find(origmsg, db);
164 	if (cache) {
165 		result = cache->c_resultmsg;
166 		goto out;
167 	}
168 
169 	origlen = strlen(origmsg) + 1;
170 again:
171 	cd = iconv_open(tocode, fromcode);
172 	if (cd == (iconv_t)-1) {
173 		result = origmsg;
174 		goto out;
175 	}
176 
177 	src = origmsg;
178 	srclen = origlen;
179 	dst = buffer;
180 	dstlen = bufferlen;
181 	nvalid = iconv(cd, &src, &srclen, &dst, &dstlen);
182 	iconv_close(cd);
183 
184 	if (nvalid == (size_t)-1) {
185 		/*
186 		 * try to allocate a new buffer.
187 		 *
188 		 * just give up if GETTEXT_ICONV_MALLOC_CHUNK was not enough.
189 		 */
190 		if (errno == E2BIG &&
191 		    bufferlen != GETTEXT_ICONV_MALLOC_CHUNK) {
192 			buffer = malloc(GETTEXT_ICONV_MALLOC_CHUNK);
193 			if (buffer) {
194 				bufferlen = GETTEXT_ICONV_MALLOC_CHUNK;
195 				goto again;
196 			}
197 		}
198 
199 		result = origmsg;
200 	} else if (cache_enter(origmsg, buffer)) {
201 		/*
202 		 * failed to enter cache.  give up.
203 		 */
204 		result = origmsg;
205 	} else {
206 		size_t resultlen = dst - buffer;
207 
208 		result = buffer;
209 		bufferlen -= resultlen;
210 		buffer += resultlen;
211 	}
212 
213 out:
214 	/* XXX UNLOCK */
215 	errno = savederrno;
216 
217 	return result;
218 }
219