gettext_iconv.c revision 1.2 1 /* $NetBSD: gettext_iconv.c,v 1.2 2004/01/18 08:53:09 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 /*
119 * static buffer for converted texts.
120 *
121 * note:
122 * we never free buffers once returned to callers.
123 * because of interface design of gettext, we can't know
124 * the lifetime of them.
125 */
126 static char *buffer;
127 static size_t bufferlen;
128
129 tocode = db->codeset;
130 if (tocode == NULL) {
131 /*
132 * codeset isn't specified explicitly by
133 * bind_textdomain_codeset().
134 * use current locale(LC_MESSAGE)'s codeset.
135 *
136 * XXX maybe wrong; it can mismatch with
137 * environment variable setting.
138 */
139 tocode = nl_langinfo(CODESET);
140 }
141
142 /*
143 * shortcut if possible.
144 * XXX should handle aliases
145 */
146 if (!strcasecmp(tocode, fromcode))
147 return origmsg;
148
149 /* XXX LOCK */
150
151 /* XXX should detect change of tocode and purge caches? */
152
153 /*
154 * see if we have already converted this message.
155 */
156 cache = cache_find(origmsg, db);
157 if (cache) {
158 result = cache->c_resultmsg;
159 goto out;
160 }
161
162 origlen = strlen(origmsg) + 1;
163 again:
164 cd = iconv_open(tocode, fromcode);
165 if (cd == (iconv_t)-1) {
166 result = origmsg;
167 goto out;
168 }
169
170 src = origmsg;
171 srclen = origlen;
172 dst = buffer;
173 dstlen = bufferlen;
174 nvalid = iconv(cd, &src, &srclen, &dst, &dstlen);
175 iconv_close(cd);
176
177 if (nvalid == (size_t)-1) {
178 /*
179 * try to allocate a new buffer.
180 *
181 * just give up if GETTEXT_ICONV_MALLOC_CHUNK was not enough.
182 */
183 if (errno == E2BIG &&
184 bufferlen != GETTEXT_ICONV_MALLOC_CHUNK) {
185 buffer = malloc(GETTEXT_ICONV_MALLOC_CHUNK);
186 if (buffer) {
187 bufferlen = GETTEXT_ICONV_MALLOC_CHUNK;
188 goto again;
189 }
190 }
191
192 result = origmsg;
193 } else if (cache_enter(origmsg, buffer)) {
194 /*
195 * failed to enter cache. give up.
196 */
197 result = origmsg;
198 } else {
199 size_t resultlen = dst - buffer;
200
201 result = buffer;
202 bufferlen -= resultlen;
203 buffer += resultlen;
204 }
205
206 out:
207 /* XXX UNLOCK */
208 errno = savederrno;
209
210 return result;
211 }
212