gettext.c revision 1.19 1 1.19 tshiozak /* $NetBSD: gettext.c,v 1.19 2004/09/23 16:44:26 tshiozak Exp $ */
2 1.1 itojun
3 1.1 itojun /*-
4 1.9 minoura * Copyright (c) 2000, 2001 Citrus Project,
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun *
16 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 itojun * SUCH DAMAGE.
27 1.10 yamt *
28 1.10 yamt * $Citrus: xpg4dl/FreeBSD/lib/libintl/gettext.c,v 1.31 2001/09/27 15:18:45 yamt Exp $
29 1.1 itojun */
30 1.1 itojun
31 1.1 itojun #include <sys/cdefs.h>
32 1.19 tshiozak __RCSID("$NetBSD: gettext.c,v 1.19 2004/09/23 16:44:26 tshiozak Exp $");
33 1.1 itojun
34 1.1 itojun #include <sys/param.h>
35 1.1 itojun #include <sys/stat.h>
36 1.1 itojun #include <sys/mman.h>
37 1.1 itojun #include <sys/uio.h>
38 1.1 itojun
39 1.19 tshiozak #include <assert.h>
40 1.1 itojun #include <fcntl.h>
41 1.1 itojun #include <stdio.h>
42 1.1 itojun #include <stdlib.h>
43 1.1 itojun #include <unistd.h>
44 1.1 itojun #include <string.h>
45 1.1 itojun #if 0
46 1.1 itojun #include <util.h>
47 1.1 itojun #endif
48 1.1 itojun #include <libintl.h>
49 1.1 itojun #include <locale.h>
50 1.1 itojun #include "libintl_local.h"
51 1.1 itojun #include "pathnames.h"
52 1.1 itojun
53 1.1 itojun static const char *lookup_category __P((int));
54 1.1 itojun static const char *split_locale __P((const char *));
55 1.1 itojun static const char *lookup_mofile __P((char *, size_t, const char *,
56 1.10 yamt const char *, const char *, const char *, struct domainbinding *));
57 1.1 itojun static u_int32_t flip __P((u_int32_t, u_int32_t));
58 1.9 minoura static int validate __P((void *, struct mohandle *));
59 1.9 minoura static int mapit __P((const char *, struct domainbinding *));
60 1.9 minoura static int unmapit __P((struct domainbinding *));
61 1.9 minoura static const char *lookup_hash __P((const char *, struct domainbinding *));
62 1.9 minoura static const char *lookup_bsearch __P((const char *, struct domainbinding *));
63 1.9 minoura static const char *lookup __P((const char *, struct domainbinding *));
64 1.18 yamt static const char *get_lang_env __P((const char *));
65 1.1 itojun
66 1.1 itojun /*
67 1.1 itojun * shortcut functions. the main implementation resides in dcngettext().
68 1.1 itojun */
69 1.1 itojun char *
70 1.1 itojun gettext(msgid)
71 1.1 itojun const char *msgid;
72 1.1 itojun {
73 1.1 itojun
74 1.1 itojun return dcngettext(NULL, msgid, NULL, 1UL, LC_MESSAGES);
75 1.1 itojun }
76 1.1 itojun
77 1.1 itojun char *
78 1.1 itojun dgettext(domainname, msgid)
79 1.1 itojun const char *domainname;
80 1.1 itojun const char *msgid;
81 1.1 itojun {
82 1.1 itojun
83 1.1 itojun return dcngettext(domainname, msgid, NULL, 1UL, LC_MESSAGES);
84 1.1 itojun }
85 1.1 itojun
86 1.1 itojun char *
87 1.1 itojun dcgettext(domainname, msgid, category)
88 1.1 itojun const char *domainname;
89 1.1 itojun const char *msgid;
90 1.1 itojun int category;
91 1.1 itojun {
92 1.1 itojun
93 1.1 itojun return dcngettext(domainname, msgid, NULL, 1UL, category);
94 1.1 itojun }
95 1.1 itojun
96 1.1 itojun char *
97 1.1 itojun ngettext(msgid1, msgid2, n)
98 1.1 itojun const char *msgid1;
99 1.1 itojun const char *msgid2;
100 1.1 itojun unsigned long int n;
101 1.1 itojun {
102 1.1 itojun
103 1.1 itojun return dcngettext(NULL, msgid1, msgid2, n, LC_MESSAGES);
104 1.1 itojun }
105 1.1 itojun
106 1.1 itojun char *
107 1.1 itojun dngettext(domainname, msgid1, msgid2, n)
108 1.1 itojun const char *domainname;
109 1.1 itojun const char *msgid1;
110 1.1 itojun const char *msgid2;
111 1.1 itojun unsigned long int n;
112 1.1 itojun {
113 1.1 itojun
114 1.1 itojun return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
115 1.1 itojun }
116 1.1 itojun
117 1.1 itojun /*
118 1.1 itojun * dcngettext() -
119 1.1 itojun * lookup internationalized message on database locale/category/domainname
120 1.1 itojun * (like ja_JP.eucJP/LC_MESSAGES/domainname).
121 1.1 itojun * if n equals to 1, internationalized message will be looked up for msgid1.
122 1.1 itojun * otherwise, message will be looked up for msgid2.
123 1.1 itojun * if the lookup fails, the function will return msgid1 or msgid2 as is.
124 1.1 itojun *
125 1.1 itojun * Even though the return type is "char *", caller should not rewrite the
126 1.1 itojun * region pointed to by the return value (should be "const char *", but can't
127 1.1 itojun * change it for compatibility with other implementations).
128 1.1 itojun *
129 1.1 itojun * by default (if domainname == NULL), domainname is taken from the value set
130 1.1 itojun * by textdomain(). usually name of the application (like "ls") is used as
131 1.1 itojun * domainname. category is usually LC_MESSAGES.
132 1.1 itojun *
133 1.1 itojun * the code reads in *.mo files generated by GNU gettext. *.mo is a host-
134 1.1 itojun * endian encoded file. both endians are supported here, as the files are in
135 1.1 itojun * /usr/share/locale! (or we should move those files into /usr/libdata)
136 1.1 itojun */
137 1.1 itojun
138 1.1 itojun static const char *
139 1.1 itojun lookup_category(category)
140 1.1 itojun int category;
141 1.1 itojun {
142 1.1 itojun
143 1.1 itojun switch (category) {
144 1.1 itojun case LC_COLLATE: return "LC_COLLATE";
145 1.1 itojun case LC_CTYPE: return "LC_CTYPE";
146 1.1 itojun case LC_MONETARY: return "LC_MONETARY";
147 1.1 itojun case LC_NUMERIC: return "LC_NUMERIC";
148 1.1 itojun case LC_TIME: return "LC_TIME";
149 1.1 itojun case LC_MESSAGES: return "LC_MESSAGES";
150 1.1 itojun }
151 1.1 itojun return NULL;
152 1.1 itojun }
153 1.1 itojun
154 1.1 itojun /*
155 1.1 itojun * XPG syntax: language[_territory[.codeset]][@modifier]
156 1.1 itojun * XXX boundary check on "result" is lacking
157 1.1 itojun */
158 1.1 itojun static const char *
159 1.1 itojun split_locale(lname)
160 1.1 itojun const char *lname;
161 1.1 itojun {
162 1.1 itojun char buf[BUFSIZ], tmp[BUFSIZ];
163 1.1 itojun char *l, *t, *c, *m;
164 1.1 itojun static char result[BUFSIZ];
165 1.1 itojun
166 1.1 itojun memset(result, 0, sizeof(result));
167 1.1 itojun
168 1.1 itojun if (strlen(lname) + 1 > sizeof(buf)) {
169 1.1 itojun fail:
170 1.1 itojun return lname;
171 1.1 itojun }
172 1.1 itojun
173 1.1 itojun strlcpy(buf, lname, sizeof(buf));
174 1.1 itojun m = strrchr(buf, '@');
175 1.1 itojun if (m)
176 1.1 itojun *m++ = '\0';
177 1.1 itojun c = strrchr(buf, '.');
178 1.1 itojun if (c)
179 1.1 itojun *c++ = '\0';
180 1.1 itojun t = strrchr(buf, '_');
181 1.1 itojun if (t)
182 1.1 itojun *t++ = '\0';
183 1.1 itojun l = buf;
184 1.1 itojun if (strlen(l) == 0)
185 1.1 itojun goto fail;
186 1.1 itojun if (c && !t)
187 1.1 itojun goto fail;
188 1.1 itojun
189 1.1 itojun if (m) {
190 1.1 itojun if (t) {
191 1.1 itojun if (c) {
192 1.1 itojun snprintf(tmp, sizeof(tmp), "%s_%s.%s@%s",
193 1.19 tshiozak l, t, c, m);
194 1.1 itojun strlcat(result, tmp, sizeof(result));
195 1.1 itojun strlcat(result, ":", sizeof(result));
196 1.1 itojun }
197 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s_%s@%s", l, t, m);
198 1.1 itojun strlcat(result, tmp, sizeof(result));
199 1.1 itojun strlcat(result, ":", sizeof(result));
200 1.1 itojun }
201 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s@%s", l, m);
202 1.1 itojun strlcat(result, tmp, sizeof(result));
203 1.1 itojun strlcat(result, ":", sizeof(result));
204 1.1 itojun }
205 1.1 itojun if (t) {
206 1.1 itojun if (c) {
207 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s_%s.%s", l, t, c);
208 1.1 itojun strlcat(result, tmp, sizeof(result));
209 1.1 itojun strlcat(result, ":", sizeof(result));
210 1.1 itojun }
211 1.19 tshiozak snprintf(tmp, sizeof(tmp), "%s_%s", l, t);
212 1.1 itojun strlcat(result, tmp, sizeof(result));
213 1.1 itojun strlcat(result, ":", sizeof(result));
214 1.1 itojun }
215 1.1 itojun strlcat(result, l, sizeof(result));
216 1.1 itojun
217 1.1 itojun return result;
218 1.1 itojun }
219 1.1 itojun
220 1.1 itojun static const char *
221 1.9 minoura lookup_mofile(buf, len, dir, lpath, category, domainname, db)
222 1.1 itojun char *buf;
223 1.1 itojun size_t len;
224 1.1 itojun const char *dir;
225 1.10 yamt const char *lpath; /* list of locales to be tried */
226 1.1 itojun const char *category;
227 1.1 itojun const char *domainname;
228 1.9 minoura struct domainbinding *db;
229 1.1 itojun {
230 1.1 itojun struct stat st;
231 1.1 itojun char *p, *q;
232 1.10 yamt char lpath_tmp[BUFSIZ];
233 1.1 itojun
234 1.10 yamt strlcpy(lpath_tmp, lpath, sizeof(lpath_tmp));
235 1.10 yamt q = lpath_tmp;
236 1.9 minoura /* CONSTCOND */
237 1.1 itojun while (1) {
238 1.1 itojun p = strsep(&q, ":");
239 1.1 itojun if (!p)
240 1.1 itojun break;
241 1.1 itojun if (!*p)
242 1.1 itojun continue;
243 1.1 itojun
244 1.1 itojun /* don't mess with default locales */
245 1.1 itojun if (strcmp(p, "C") == 0 || strcmp(p, "POSIX") == 0)
246 1.1 itojun return NULL;
247 1.1 itojun
248 1.1 itojun /* validate pathname */
249 1.1 itojun if (strchr(p, '/') || strchr(category, '/'))
250 1.1 itojun continue;
251 1.1 itojun #if 1 /*?*/
252 1.1 itojun if (strchr(domainname, '/'))
253 1.1 itojun continue;
254 1.1 itojun #endif
255 1.1 itojun
256 1.1 itojun snprintf(buf, len, "%s/%s/%s/%s.mo", dir, p,
257 1.1 itojun category, domainname);
258 1.1 itojun if (stat(buf, &st) < 0)
259 1.1 itojun continue;
260 1.1 itojun if ((st.st_mode & S_IFMT) != S_IFREG)
261 1.1 itojun continue;
262 1.1 itojun
263 1.9 minoura if (mapit(buf, db) == 0)
264 1.1 itojun return buf;
265 1.1 itojun }
266 1.1 itojun
267 1.1 itojun return NULL;
268 1.1 itojun }
269 1.1 itojun
270 1.1 itojun static u_int32_t
271 1.1 itojun flip(v, magic)
272 1.1 itojun u_int32_t v;
273 1.1 itojun u_int32_t magic;
274 1.1 itojun {
275 1.1 itojun
276 1.1 itojun if (magic == MO_MAGIC)
277 1.1 itojun return v;
278 1.1 itojun else if (magic == MO_MAGIC_SWAPPED) {
279 1.1 itojun v = ((v >> 24) & 0xff) | ((v >> 8) & 0xff00) |
280 1.1 itojun ((v << 8) & 0xff0000) | ((v << 24) & 0xff000000);
281 1.1 itojun return v;
282 1.1 itojun } else {
283 1.1 itojun abort();
284 1.1 itojun /*NOTREACHED*/
285 1.1 itojun }
286 1.1 itojun }
287 1.1 itojun
288 1.1 itojun static int
289 1.9 minoura validate(arg, mohandle)
290 1.1 itojun void *arg;
291 1.9 minoura struct mohandle *mohandle;
292 1.1 itojun {
293 1.1 itojun char *p;
294 1.1 itojun
295 1.1 itojun p = (char *)arg;
296 1.9 minoura if (p < (char *)mohandle->addr ||
297 1.9 minoura p > (char *)mohandle->addr + mohandle->len)
298 1.1 itojun return 0;
299 1.1 itojun else
300 1.1 itojun return 1;
301 1.1 itojun }
302 1.1 itojun
303 1.1 itojun int
304 1.9 minoura mapit(path, db)
305 1.1 itojun const char *path;
306 1.9 minoura struct domainbinding *db;
307 1.1 itojun {
308 1.1 itojun int fd;
309 1.1 itojun struct stat st;
310 1.1 itojun char *base;
311 1.1 itojun u_int32_t magic, revision;
312 1.1 itojun struct moentry *otable, *ttable;
313 1.19 tshiozak const u_int32_t *htable;
314 1.1 itojun struct moentry_h *p;
315 1.1 itojun struct mo *mo;
316 1.1 itojun size_t l;
317 1.1 itojun int i;
318 1.1 itojun char *v;
319 1.9 minoura struct mohandle *mohandle = &db->mohandle;
320 1.1 itojun
321 1.9 minoura if (mohandle->addr && mohandle->addr != MAP_FAILED &&
322 1.9 minoura mohandle->mo.mo_magic)
323 1.1 itojun return 0; /*already opened*/
324 1.1 itojun
325 1.9 minoura unmapit(db);
326 1.1 itojun
327 1.1 itojun #if 0
328 1.1 itojun if (secure_path(path) != 0)
329 1.1 itojun goto fail;
330 1.1 itojun #endif
331 1.1 itojun if (stat(path, &st) < 0)
332 1.1 itojun goto fail;
333 1.1 itojun if ((st.st_mode & S_IFMT) != S_IFREG || st.st_size > GETTEXT_MMAP_MAX)
334 1.1 itojun goto fail;
335 1.1 itojun fd = open(path, O_RDONLY);
336 1.1 itojun if (fd < 0)
337 1.1 itojun goto fail;
338 1.2 itojun if (read(fd, &magic, sizeof(magic)) != sizeof(magic) ||
339 1.1 itojun (magic != MO_MAGIC && magic != MO_MAGIC_SWAPPED)) {
340 1.1 itojun close(fd);
341 1.1 itojun goto fail;
342 1.1 itojun }
343 1.19 tshiozak if (read(fd, &revision, sizeof(revision)) != sizeof(revision)) {
344 1.19 tshiozak close(fd);
345 1.19 tshiozak goto fail;
346 1.19 tshiozak }
347 1.19 tshiozak switch (flip(revision, magic)) {
348 1.19 tshiozak case MO_MAKE_REV(0, 0):
349 1.19 tshiozak #if 0
350 1.19 tshiozak case MO_MAKE_REV(0, 1):
351 1.19 tshiozak case MO_MAKE_REV(1, 1):
352 1.19 tshiozak #endif
353 1.19 tshiozak break;
354 1.19 tshiozak default:
355 1.1 itojun close(fd);
356 1.1 itojun goto fail;
357 1.1 itojun }
358 1.9 minoura mohandle->addr = mmap(NULL, (size_t)st.st_size, PROT_READ,
359 1.4 itojun MAP_FILE | MAP_SHARED, fd, (off_t)0);
360 1.9 minoura if (!mohandle->addr || mohandle->addr == MAP_FAILED) {
361 1.1 itojun close(fd);
362 1.1 itojun goto fail;
363 1.1 itojun }
364 1.1 itojun close(fd);
365 1.9 minoura mohandle->len = (size_t)st.st_size;
366 1.1 itojun
367 1.9 minoura base = mohandle->addr;
368 1.9 minoura mo = (struct mo *)mohandle->addr;
369 1.1 itojun
370 1.1 itojun /* flip endian. do not flip magic number! */
371 1.9 minoura mohandle->mo.mo_magic = mo->mo_magic;
372 1.9 minoura mohandle->mo.mo_revision = flip(mo->mo_revision, magic);
373 1.9 minoura mohandle->mo.mo_nstring = flip(mo->mo_nstring, magic);
374 1.19 tshiozak mohandle->mo.mo_hsize = flip(mo->mo_hsize, magic);
375 1.1 itojun
376 1.1 itojun /* validate otable/ttable */
377 1.19 tshiozak /* LINTED: ignore the alignment problem. */
378 1.1 itojun otable = (struct moentry *)(base + flip(mo->mo_otable, magic));
379 1.19 tshiozak /* LINTED: ignore the alignment problem. */
380 1.1 itojun ttable = (struct moentry *)(base + flip(mo->mo_ttable, magic));
381 1.9 minoura if (!validate(otable, mohandle) ||
382 1.9 minoura !validate(&otable[mohandle->mo.mo_nstring], mohandle)) {
383 1.9 minoura unmapit(db);
384 1.1 itojun goto fail;
385 1.1 itojun }
386 1.9 minoura if (!validate(ttable, mohandle) ||
387 1.9 minoura !validate(&ttable[mohandle->mo.mo_nstring], mohandle)) {
388 1.9 minoura unmapit(db);
389 1.1 itojun goto fail;
390 1.1 itojun }
391 1.1 itojun
392 1.1 itojun /* allocate [ot]table, and convert to normal pointer representation. */
393 1.9 minoura l = sizeof(struct moentry_h) * mohandle->mo.mo_nstring;
394 1.9 minoura mohandle->mo.mo_otable = (struct moentry_h *)malloc(l);
395 1.9 minoura if (!mohandle->mo.mo_otable) {
396 1.9 minoura unmapit(db);
397 1.1 itojun goto fail;
398 1.1 itojun }
399 1.9 minoura mohandle->mo.mo_ttable = (struct moentry_h *)malloc(l);
400 1.9 minoura if (!mohandle->mo.mo_ttable) {
401 1.9 minoura unmapit(db);
402 1.1 itojun goto fail;
403 1.1 itojun }
404 1.9 minoura p = mohandle->mo.mo_otable;
405 1.9 minoura for (i = 0; i < mohandle->mo.mo_nstring; i++) {
406 1.1 itojun p[i].len = flip(otable[i].len, magic);
407 1.1 itojun p[i].off = base + flip(otable[i].off, magic);
408 1.1 itojun
409 1.9 minoura if (!validate(p[i].off, mohandle) ||
410 1.9 minoura !validate(p[i].off + p[i].len + 1, mohandle)) {
411 1.9 minoura unmapit(db);
412 1.1 itojun goto fail;
413 1.1 itojun }
414 1.1 itojun }
415 1.9 minoura p = mohandle->mo.mo_ttable;
416 1.9 minoura for (i = 0; i < mohandle->mo.mo_nstring; i++) {
417 1.1 itojun p[i].len = flip(ttable[i].len, magic);
418 1.1 itojun p[i].off = base + flip(ttable[i].off, magic);
419 1.1 itojun
420 1.9 minoura if (!validate(p[i].off, mohandle) ||
421 1.9 minoura !validate(p[i].off + p[i].len + 1, mohandle)) {
422 1.9 minoura unmapit(db);
423 1.1 itojun goto fail;
424 1.1 itojun }
425 1.1 itojun }
426 1.19 tshiozak /* allocate htable, and convert it to the host order. */
427 1.19 tshiozak if (mohandle->mo.mo_hsize > 2) {
428 1.19 tshiozak l = sizeof(u_int32_t) * mohandle->mo.mo_hsize;
429 1.19 tshiozak mohandle->mo.mo_htable = (u_int32_t *)malloc(l);
430 1.19 tshiozak if (!mohandle->mo.mo_htable) {
431 1.19 tshiozak unmapit(db);
432 1.19 tshiozak goto fail;
433 1.19 tshiozak }
434 1.19 tshiozak /* LINTED: ignore the alignment problem. */
435 1.19 tshiozak htable = (const u_int32_t *)(base+flip(mo->mo_hoffset, magic));
436 1.19 tshiozak for (i=0; i < mohandle->mo.mo_hsize; i++) {
437 1.19 tshiozak mohandle->mo.mo_htable[i] = flip(htable[i], magic);
438 1.19 tshiozak if (mohandle->mo.mo_htable[i] >=
439 1.19 tshiozak mohandle->mo.mo_nstring+1) {
440 1.19 tshiozak /* illegal string number. */
441 1.19 tshiozak unmapit(db);
442 1.19 tshiozak goto fail;
443 1.19 tshiozak }
444 1.19 tshiozak }
445 1.19 tshiozak }
446 1.1 itojun /* grab MIME-header and charset field */
447 1.9 minoura mohandle->mo.mo_header = lookup("", db);
448 1.9 minoura if (mohandle->mo.mo_header)
449 1.9 minoura v = strstr(mohandle->mo.mo_header, "charset=");
450 1.1 itojun else
451 1.1 itojun v = NULL;
452 1.1 itojun if (v) {
453 1.9 minoura mohandle->mo.mo_charset = strdup(v + 8);
454 1.9 minoura if (!mohandle->mo.mo_charset)
455 1.6 itojun goto fail;
456 1.9 minoura v = strchr(mohandle->mo.mo_charset, '\n');
457 1.1 itojun if (v)
458 1.1 itojun *v = '\0';
459 1.1 itojun }
460 1.1 itojun
461 1.1 itojun /*
462 1.1 itojun * XXX check charset, reject it if we are unable to support the charset
463 1.1 itojun * with the current locale.
464 1.1 itojun * for example, if we are using euc-jp locale and we are looking at
465 1.1 itojun * *.mo file encoded by euc-kr (charset=euc-kr), we should reject
466 1.1 itojun * the *.mo file as we cannot support it.
467 1.1 itojun */
468 1.1 itojun
469 1.1 itojun return 0;
470 1.1 itojun
471 1.1 itojun fail:
472 1.1 itojun return -1;
473 1.1 itojun }
474 1.1 itojun
475 1.1 itojun static int
476 1.9 minoura unmapit(db)
477 1.9 minoura struct domainbinding *db;
478 1.1 itojun {
479 1.9 minoura struct mohandle *mohandle = &db->mohandle;
480 1.1 itojun
481 1.1 itojun /* unmap if there's already mapped region */
482 1.9 minoura if (mohandle->addr && mohandle->addr != MAP_FAILED)
483 1.9 minoura munmap(mohandle->addr, mohandle->len);
484 1.9 minoura mohandle->addr = NULL;
485 1.9 minoura if (mohandle->mo.mo_otable)
486 1.9 minoura free(mohandle->mo.mo_otable);
487 1.9 minoura if (mohandle->mo.mo_ttable)
488 1.9 minoura free(mohandle->mo.mo_ttable);
489 1.9 minoura if (mohandle->mo.mo_charset)
490 1.9 minoura free(mohandle->mo.mo_charset);
491 1.19 tshiozak if (mohandle->mo.mo_htable)
492 1.19 tshiozak free(mohandle->mo.mo_htable);
493 1.9 minoura memset(&mohandle->mo, 0, sizeof(mohandle->mo));
494 1.1 itojun return 0;
495 1.1 itojun }
496 1.1 itojun
497 1.19 tshiozak /*
498 1.19 tshiozak * calculate the step value if the hash value is conflicted.
499 1.19 tshiozak */
500 1.19 tshiozak static __inline u_int32_t
501 1.19 tshiozak calc_collision_step(u_int32_t hashval, u_int32_t hashsize)
502 1.19 tshiozak {
503 1.19 tshiozak _DIAGASSERT(hashsize>2);
504 1.19 tshiozak return (hashval % (hashsize - 2)) + 1;
505 1.19 tshiozak }
506 1.19 tshiozak
507 1.19 tshiozak /*
508 1.19 tshiozak * calculate the next index while conflicting.
509 1.19 tshiozak */
510 1.19 tshiozak static __inline u_int32_t
511 1.19 tshiozak calc_next_index(u_int32_t curidx, u_int32_t hashsize, u_int32_t step)
512 1.19 tshiozak {
513 1.19 tshiozak return curidx+step - (curidx >= hashsize-step ? hashsize : 0);
514 1.19 tshiozak }
515 1.19 tshiozak
516 1.9 minoura /* ARGSUSED */
517 1.1 itojun static const char *
518 1.9 minoura lookup_hash(msgid, db)
519 1.1 itojun const char *msgid;
520 1.9 minoura struct domainbinding *db;
521 1.1 itojun {
522 1.19 tshiozak struct mohandle *mohandle = &db->mohandle;
523 1.19 tshiozak u_int32_t idx, hashval, step, strno;
524 1.19 tshiozak size_t len;
525 1.19 tshiozak
526 1.19 tshiozak if (mohandle->mo.mo_hsize <= 2 || mohandle->mo.mo_htable == NULL)
527 1.19 tshiozak return NULL;
528 1.1 itojun
529 1.19 tshiozak hashval = __intl_string_hash(msgid);
530 1.19 tshiozak step = calc_collision_step(hashval, mohandle->mo.mo_hsize);
531 1.19 tshiozak idx = hashval % mohandle->mo.mo_hsize;
532 1.19 tshiozak len = strlen(msgid);
533 1.19 tshiozak while (/*CONSTCOND*/1) {
534 1.19 tshiozak strno = mohandle->mo.mo_htable[idx];
535 1.19 tshiozak if (strno == 0) {
536 1.19 tshiozak /* unexpected miss */
537 1.19 tshiozak return NULL;
538 1.19 tshiozak }
539 1.19 tshiozak strno--;
540 1.19 tshiozak if (len <= mohandle->mo.mo_otable[strno].len &&
541 1.19 tshiozak !strcmp(msgid, mohandle->mo.mo_otable[strno].off)) {
542 1.19 tshiozak /* hit */
543 1.19 tshiozak return mohandle->mo.mo_ttable[strno].off;
544 1.19 tshiozak }
545 1.19 tshiozak idx = calc_next_index(idx, mohandle->mo.mo_hsize, step);
546 1.19 tshiozak }
547 1.19 tshiozak /*NOTREACHED*/
548 1.1 itojun }
549 1.1 itojun
550 1.1 itojun static const char *
551 1.9 minoura lookup_bsearch(msgid, db)
552 1.1 itojun const char *msgid;
553 1.9 minoura struct domainbinding *db;
554 1.1 itojun {
555 1.1 itojun int top, bottom, middle, omiddle;
556 1.1 itojun int n;
557 1.9 minoura struct mohandle *mohandle = &db->mohandle;
558 1.1 itojun
559 1.1 itojun top = 0;
560 1.9 minoura bottom = mohandle->mo.mo_nstring;
561 1.1 itojun omiddle = -1;
562 1.9 minoura /* CONSTCOND */
563 1.1 itojun while (1) {
564 1.1 itojun if (top > bottom)
565 1.4 itojun break;
566 1.1 itojun middle = (top + bottom) / 2;
567 1.1 itojun /* avoid possible infinite loop, when the data is not sorted */
568 1.1 itojun if (omiddle == middle)
569 1.4 itojun break;
570 1.9 minoura if (middle < 0 || middle >= mohandle->mo.mo_nstring)
571 1.4 itojun break;
572 1.1 itojun
573 1.9 minoura n = strcmp(msgid, mohandle->mo.mo_otable[middle].off);
574 1.1 itojun if (n == 0)
575 1.9 minoura return (const char *)mohandle->mo.mo_ttable[middle].off;
576 1.1 itojun else if (n < 0)
577 1.1 itojun bottom = middle;
578 1.1 itojun else
579 1.1 itojun top = middle;
580 1.1 itojun omiddle = middle;
581 1.1 itojun }
582 1.1 itojun
583 1.1 itojun return NULL;
584 1.1 itojun }
585 1.1 itojun
586 1.1 itojun static const char *
587 1.9 minoura lookup(msgid, db)
588 1.1 itojun const char *msgid;
589 1.9 minoura struct domainbinding *db;
590 1.1 itojun {
591 1.1 itojun const char *v;
592 1.1 itojun
593 1.9 minoura v = lookup_hash(msgid, db);
594 1.1 itojun if (v)
595 1.1 itojun return v;
596 1.1 itojun
597 1.9 minoura return lookup_bsearch(msgid, db);
598 1.1 itojun }
599 1.1 itojun
600 1.16 itojun static const char *
601 1.16 itojun get_lang_env(const char *category_name)
602 1.10 yamt {
603 1.10 yamt const char *lang;
604 1.10 yamt
605 1.10 yamt /* 1. see LANGUAGE variable first. */
606 1.10 yamt lang = getenv("LANGUAGE");
607 1.10 yamt if (lang)
608 1.10 yamt return lang;
609 1.10 yamt
610 1.10 yamt /* 2. if LANGUAGE isn't set, see LC_ALL, LC_xxx, LANG. */
611 1.13 yamt lang = getenv("LC_ALL");
612 1.10 yamt if (!lang)
613 1.13 yamt lang = getenv(category_name);
614 1.10 yamt if (!lang)
615 1.10 yamt lang = getenv("LANG");
616 1.10 yamt
617 1.10 yamt if (!lang)
618 1.10 yamt return 0; /* error */
619 1.10 yamt
620 1.10 yamt return split_locale(lang);
621 1.10 yamt }
622 1.10 yamt
623 1.1 itojun char *
624 1.1 itojun dcngettext(domainname, msgid1, msgid2, n, category)
625 1.1 itojun const char *domainname;
626 1.1 itojun const char *msgid1;
627 1.1 itojun const char *msgid2;
628 1.1 itojun unsigned long int n;
629 1.1 itojun int category;
630 1.1 itojun {
631 1.1 itojun const char *msgid;
632 1.1 itojun char path[PATH_MAX];
633 1.10 yamt const char *lpath;
634 1.1 itojun static char olpath[PATH_MAX];
635 1.6 itojun const char *cname = NULL;
636 1.1 itojun const char *v;
637 1.6 itojun static char *ocname = NULL;
638 1.6 itojun static char *odomainname = NULL;
639 1.5 itojun struct domainbinding *db;
640 1.1 itojun
641 1.1 itojun msgid = (n == 1) ? msgid1 : msgid2;
642 1.8 minoura if (msgid == NULL)
643 1.8 minoura return NULL;
644 1.1 itojun
645 1.1 itojun if (!domainname)
646 1.9 minoura domainname = __current_domainname;
647 1.1 itojun cname = lookup_category(category);
648 1.1 itojun if (!domainname || !cname)
649 1.1 itojun goto fail;
650 1.1 itojun
651 1.10 yamt lpath = get_lang_env(cname);
652 1.10 yamt if (!lpath)
653 1.1 itojun goto fail;
654 1.19 tshiozak
655 1.9 minoura for (db = __bindings; db; db = db->next)
656 1.5 itojun if (strcmp(db->domainname, domainname) == 0)
657 1.5 itojun break;
658 1.9 minoura if (!db) {
659 1.9 minoura if (!bindtextdomain(domainname, _PATH_TEXTDOMAIN))
660 1.9 minoura goto fail;
661 1.9 minoura db = __bindings;
662 1.11 yamt }
663 1.11 yamt
664 1.11 yamt /* resolve relative path */
665 1.11 yamt /* XXX not necessary? */
666 1.11 yamt if (db->path[0] != '/') {
667 1.11 yamt char buf[PATH_MAX];
668 1.11 yamt
669 1.11 yamt if (getcwd(buf, sizeof(buf)) == 0)
670 1.11 yamt goto fail;
671 1.11 yamt if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
672 1.11 yamt goto fail;
673 1.11 yamt if (strlcat(buf, db->path, sizeof(buf)) >= sizeof(buf))
674 1.11 yamt goto fail;
675 1.15 itojun strlcpy(db->path, buf, sizeof(db->path));
676 1.9 minoura }
677 1.5 itojun
678 1.1 itojun /* don't bother looking it up if the values are the same */
679 1.5 itojun if (odomainname && strcmp(domainname, odomainname) == 0 &&
680 1.9 minoura ocname && strcmp(cname, ocname) == 0 && strcmp(lpath, olpath) == 0 &&
681 1.9 minoura db->mohandle.mo.mo_magic)
682 1.1 itojun goto found;
683 1.1 itojun
684 1.1 itojun /* try to find appropriate file, from $LANGUAGE */
685 1.5 itojun if (lookup_mofile(path, sizeof(path), db->path, lpath, cname,
686 1.9 minoura domainname, db) == NULL)
687 1.3 itojun goto fail;
688 1.5 itojun
689 1.5 itojun if (odomainname)
690 1.5 itojun free(odomainname);
691 1.5 itojun if (ocname)
692 1.5 itojun free(ocname);
693 1.6 itojun odomainname = strdup(domainname);
694 1.5 itojun ocname = strdup(cname);
695 1.6 itojun if (!odomainname || !ocname) {
696 1.6 itojun if (odomainname)
697 1.6 itojun free(odomainname);
698 1.6 itojun if (ocname)
699 1.6 itojun free(ocname);
700 1.6 itojun odomainname = ocname = NULL;
701 1.6 itojun }
702 1.10 yamt else
703 1.10 yamt strlcpy(olpath, lpath, sizeof(olpath));
704 1.1 itojun
705 1.1 itojun found:
706 1.9 minoura v = lookup(msgid, db);
707 1.1 itojun if (v) {
708 1.1 itojun /*
709 1.18 yamt * convert the translated message's encoding.
710 1.18 yamt *
711 1.18 yamt * special case:
712 1.18 yamt * a result of gettext("") shouldn't need any conversion.
713 1.1 itojun */
714 1.18 yamt if (msgid[0])
715 1.18 yamt v = __gettext_iconv(v, db);
716 1.1 itojun
717 1.1 itojun /*
718 1.1 itojun * Given the amount of printf-format security issues, it may
719 1.1 itojun * be a good idea to validate if the original msgid and the
720 1.1 itojun * translated message format string carry the same printf-like
721 1.1 itojun * format identifiers.
722 1.1 itojun */
723 1.1 itojun
724 1.1 itojun msgid = v;
725 1.1 itojun }
726 1.1 itojun
727 1.1 itojun fail:
728 1.19 tshiozak return (char *)__UNCONST(msgid);
729 1.1 itojun }
730