1 /* simple-gettext.c - a simplified version of gettext.
2 * Copyright (C) 1995, 1996, 1997, 1999,
3 * 2005 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 /* This is a simplified version of gettext written by Ulrich Drepper.
24 * It is used for the Win32 version of GnuPG beucase all the overhead
25 * of gettext is not needed and we have to do some special Win32 stuff.
26 * I decided that this is far easier than to tweak gettext for the special
27 * cases (I tried it but it is a lot of code). wk 15.09.99
31 #ifdef USE_SIMPLE_GETTEXT
32 #if !defined (_WIN32) && !defined (__CYGWIN32__)
33 #error This file can only be used under Windows or Cygwin32
41 #include <sys/types.h>
47 /* The magic number of the GNU message catalog format. */
48 #define MAGIC 0x950412de
49 #define MAGIC_SWAPPED 0xde120495
51 /* Revision number of the currently used .mo (binary) file format. */
52 #define MO_REVISION_NUMBER 0
55 /* Header for binary .mo file format. */
58 /* The magic number. */
60 /* The revision number of the file format. */
62 /* The number of strings pairs. */
64 /* Offset of table with start offsets of original strings. */
66 /* Offset of table with start offsets of translation strings. */
68 /* Size of hashing table. */
70 /* Offset of first hashing entry. */
76 /* Length of addressed string. */
78 /* Offset of string in file. */
83 struct overflow_space_s
85 struct overflow_space_s *next;
95 char *mapped; /* 0 = not yet mapped, 1 = mapped,
98 struct overflow_space_s *overflow_space;
99 struct string_desc *orig_tab;
100 struct string_desc *trans_tab;
106 static struct loaded_domain *the_domain;
108 static __inline__ u32
111 return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
114 #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
117 /* We assume to have `unsigned long int' value with at least 32 bits. */
118 #define HASHWORDBITS 32
120 /* The so called `hashpjw' function by P.J. Weinberger
121 [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
122 1986, 1987 Bell Telephone Laboratories, Inc.] */
124 static __inline__ ulong
125 hash_string( const char *str_param )
127 unsigned long int hval, g;
128 const char *str = str_param;
134 hval += (unsigned long int) *str++;
135 g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
138 hval ^= g >> (HASHWORDBITS - 8);
146 static struct loaded_domain *
147 load_domain( const char *filename )
152 struct mo_file_header *data = NULL;
153 struct loaded_domain *domain = NULL;
157 fp = fopen( filename, "rb" );
159 return NULL; /* can't open the file */
160 /* we must know about the size of the file */
161 if( fstat( fileno(fp ), &st )
162 || (size = (size_t)st.st_size) != st.st_size
163 || size < sizeof (struct mo_file_header) ) {
168 data = malloc( size );
171 return NULL; /* out of memory */
175 read_ptr = (char *) data;
177 long int nb = fread( read_ptr, 1, to_read, fp );
181 return NULL; /* read error */
185 } while( to_read > 0 );
188 /* Using the magic number we can test whether it really is a message
190 if( data->magic != MAGIC && data->magic != MAGIC_SWAPPED ) {
191 /* The magic number is wrong: not a message catalog file. */
196 domain = calloc( 1, sizeof *domain );
201 domain->data = (char *) data;
202 domain->must_swap = data->magic != MAGIC;
204 /* Fill in the information about the available tables. */
205 switch( SWAPIT(domain->must_swap, data->revision) ) {
207 domain->nstrings = SWAPIT(domain->must_swap, data->nstrings);
208 domain->orig_tab = (struct string_desc *)
209 ((char *) data + SWAPIT(domain->must_swap, data->orig_tab_offset));
210 domain->trans_tab = (struct string_desc *)
211 ((char *) data + SWAPIT(domain->must_swap, data->trans_tab_offset));
212 domain->hash_size = SWAPIT(domain->must_swap, data->hash_tab_size);
213 domain->hash_tab = (u32 *)
214 ((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset));
217 default: /* This is an invalid revision. */
223 /* Allocate an array to keep track of code page mappings. */
224 domain->mapped = calloc( 1, domain->nstrings );
225 if( !domain->mapped ) {
236 * Set the file used for translations. Pass a NULL to disable
237 * translation. A new filename may be set at anytime. If REGKEY is
238 * not NULL, the function tries to selected the language the registry
239 * key "Lang" below that key. WARNING: After changing the filename you
240 * should not access any data retrieved by gettext().
243 set_gettext_file ( const char *filename, const char *regkey )
245 struct loaded_domain *domain = NULL;
247 if( filename && *filename ) {
248 if( filename[0] == '/'
249 #ifdef HAVE_DRIVE_LETTERS
250 || ( isalpha(filename[0])
251 && filename[1] == ':'
252 && (filename[2] == '/' || filename[2] == '\\') )
255 /* absolute path - use it as is */
256 domain = load_domain( filename );
258 else if (regkey) { /* Standard. */
259 char *instdir, *langid, *fname;
262 instdir = read_w32_registry_string ("HKEY_LOCAL_MACHINE",
264 "Install Directory");
267 langid = read_w32_registry_string (NULL, /* HKCU then HKLM */
274 /* Strip stuff after a dot in case the user tried to enter
275 * the entire locale synatcs as usual for POSIX. */
276 p = strchr (langid, '.');
280 /* Build the key: "<instdir>/<domain>.nls/<langid>.mo" We
281 use a directory below the installation directory with
282 the domain included in case the software has been
283 insalled with other software altogether at the same
285 fname = malloc (strlen (instdir) + 1 + strlen (filename) + 5
286 + strlen (langid) + 3 + 1);
292 strcpy (stpcpy (stpcpy (stpcpy (stpcpy ( stpcpy (fname,
293 instdir),"\\"), filename), ".nls\\"), langid), ".mo");
297 /* Better make sure that we don't mix forward and
298 backward slashes. It seems that some Windoze
299 versions don't accept this. */
300 for (p=fname; *p; p++) {
304 domain = load_domain (fname);
313 struct overflow_space_s *os, *os2;
314 free( the_domain->data );
315 free( the_domain->mapped );
316 for (os=the_domain->overflow_space; os; os = os2) {
329 get_string( struct loaded_domain *domain, u32 idx )
331 struct overflow_space_s *os;
334 p = domain->data + SWAPIT(domain->must_swap, domain->trans_tab[idx].offset);
335 if (!domain->mapped[idx])
340 domain->mapped[idx] = 1;
343 buf = utf8_to_native (p, plen, -1);
344 buflen = strlen (buf);
349 /* There is not enough space for the translation - store it
350 in the overflow_space else and mark that in the mapped
351 array. Because we expect that this won't happen too
352 often, we use a simple linked list. */
353 os = malloc (sizeof *os + buflen);
358 os->next = domain->overflow_space;
359 domain->overflow_space = os;
363 p = "ERROR in GETTEXT MALLOC";
367 else if (domain->mapped[idx] == 2)
368 { /* We need to get the string from the overflow_space. */
369 for (os=domain->overflow_space; os; os = os->next)
371 return (const char*)os->d;
372 p = "ERROR in GETTEXT\n";
374 return (const char*)p;
380 gettext( const char *msgid )
382 struct loaded_domain *domain;
386 if( !(domain = the_domain) )
389 /* Locate the MSGID and its translation. */
390 if( domain->hash_size > 2 && domain->hash_tab ) {
391 /* Use the hashing table. */
392 u32 len = strlen (msgid);
393 u32 hash_val = hash_string (msgid);
394 u32 idx = hash_val % domain->hash_size;
395 u32 incr = 1 + (hash_val % (domain->hash_size - 2));
396 u32 nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
398 if ( !nstr ) /* Hash table entry is empty. */
401 if( SWAPIT(domain->must_swap,
402 domain->orig_tab[nstr - 1].length) == len
404 domain->data + SWAPIT(domain->must_swap,
405 domain->orig_tab[nstr - 1].offset)) )
406 return get_string( domain, nstr - 1 );
409 if (idx >= domain->hash_size - incr)
410 idx -= domain->hash_size - incr;
414 nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
416 goto not_found; /* Hash table entry is empty. */
418 if ( SWAPIT(domain->must_swap,
419 domain->orig_tab[nstr - 1].length) == len
421 domain->data + SWAPIT(domain->must_swap,
422 domain->orig_tab[nstr - 1].offset)))
423 return get_string( domain, nstr-1 );
428 /* Now we try the default method: binary search in the sorted
429 array of messages. */
431 top = domain->nstrings;
432 while( bottom < top ) {
435 act = (bottom + top) / 2;
436 cmp_val = strcmp(msgid, domain->data
437 + SWAPIT(domain->must_swap,
438 domain->orig_tab[act].offset));
441 else if (cmp_val > 0)
444 return get_string( domain, act );
452 unsigned int cp1, cp2;
454 cp1 = GetConsoleCP();
455 cp2 = GetConsoleOutputCP();
457 log_info("InputCP=%u OutputCP=%u\n", cp1, cp2 );
459 if( !SetConsoleOutputCP( 1252 ) )
460 log_info("SetConsoleOutputCP failed: %s\n", w32_strerror (0));
462 cp1 = GetConsoleCP();
463 cp2 = GetConsoleOutputCP();
464 log_info("InputCP=%u OutputCP=%u after switch1\n", cp1, cp2 );
467 #endif /* USE_SIMPLE_GETTEXT */