1 /* simple-gettext.c - a simplified version of gettext.
2 * Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 /* This is a simplified version of gettext written by Ulrich Drepper.
22 * It is used for the Win32 version of GnuPG becuase all the overhead
23 * of gettext is not needed and we have to do some special Win32 stuff.
24 * I decided that this is far easier than to tweak gettext for the special
25 * cases (I tried it but it is a lot of code). wk 15.09.99
29 #ifdef USE_SIMPLE_GETTEXT
31 #error This file can only be used with MinGW32
39 #include <sys/types.h>
44 typedef unsigned int u32; /* That is fine with MingW32 */
46 typedef unsigned long ulong;
48 /* The magic number of the GNU message catalog format. */
49 #define MAGIC 0x950412de
50 #define MAGIC_SWAPPED 0xde120495
52 /* Revision number of the currently used .mo (binary) file format. */
53 #define MO_REVISION_NUMBER 0
56 /* Header for binary .mo file format. */
59 /* The magic number. */
61 /* The revision number of the file format. */
63 /* The number of strings pairs. */
65 /* Offset of table with start offsets of original strings. */
67 /* Offset of table with start offsets of translation strings. */
69 /* Size of hashing table. */
71 /* Offset of first hashing entry. */
77 /* Length of addressed string. */
79 /* Offset of string in file. */
91 struct string_desc *orig_tab;
92 struct string_desc *trans_tab;
98 static struct loaded_domain *the_domain;
100 static __inline__ u32
103 return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
106 #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
109 /* We assume to have `unsigned long int' value with at least 32 bits. */
110 #define HASHWORDBITS 32
112 /* The so called `hashpjw' function by P.J. Weinberger
113 [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
114 1986, 1987 Bell Telephone Laboratories, Inc.] */
116 static __inline__ ulong
117 hash_string( const char *str_param )
119 unsigned long int hval, g;
120 const char *str = str_param;
126 hval += (unsigned long int) *str++;
127 g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
130 hval ^= g >> (HASHWORDBITS - 8);
138 static struct loaded_domain *
139 load_domain( const char *filename )
144 struct mo_file_header *data = NULL;
145 struct loaded_domain *domain = NULL;
149 fp = g_fopen( filename, "rb" );
151 return NULL; /* can't open the file */
152 /* we must know about the size of the file */
153 if( fstat( fileno(fp ), &st )
154 || (size = (size_t)st.st_size) != st.st_size
155 || size < sizeof (struct mo_file_header) ) {
160 data = malloc( size );
163 return NULL; /* out of memory */
167 read_ptr = (char *) data;
169 long int nb = fread( read_ptr, 1, to_read, fp );
173 return NULL; /* read error */
177 } while( to_read > 0 );
180 /* Using the magic number we can test whether it really is a message
182 if( data->magic != MAGIC && data->magic != MAGIC_SWAPPED ) {
183 /* The magic number is wrong: not a message catalog file. */
188 domain = calloc( 1, sizeof *domain );
193 domain->data = (char *) data;
194 domain->must_swap = data->magic != MAGIC;
196 /* Fill in the information about the available tables. */
197 switch( SWAPIT(domain->must_swap, data->revision) ) {
199 domain->nstrings = SWAPIT(domain->must_swap, data->nstrings);
200 domain->orig_tab = (struct string_desc *)
201 ((char *) data + SWAPIT(domain->must_swap, data->orig_tab_offset));
202 domain->trans_tab = (struct string_desc *)
203 ((char *) data + SWAPIT(domain->must_swap, data->trans_tab_offset));
204 domain->hash_size = SWAPIT(domain->must_swap, data->hash_tab_size);
205 domain->hash_tab = (u32 *)
206 ((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset));
209 default: /* This is an invalid revision. */
215 /* allocate an array to keep track of code page mappings */
216 domain->mapped = calloc( 1, domain->nstrings );
217 if( !domain->mapped ) {
228 * Set the file used for translations. Pass a NULL to disable
229 * translation. A new filename may be set at anytime.
230 * WARNING: After changing the filename you shoudl not access any data
231 * retrieved by gettext().
234 set_gettext_file( const char *filename )
236 struct loaded_domain *domain = NULL;
238 if( filename && *filename ) {
239 if( filename[0] == '/'
240 #ifdef HAVE_DRIVE_LETTERS
241 || ( isalpha(filename[0])
242 && filename[1] == ':'
243 && (filename[2] == '/' || filename[2] == '\\') )
246 /* absolute path - use it as is */
247 domain = load_domain( filename );
249 else { /* relative path - append ".mo" and get dir from the environment */
253 dir = read_w32_registry_string( NULL,
254 "Control Panel\\Mingw32\\NLS",
256 if( dir && (buf=malloc(strlen(dir)+strlen(filename)+1+3+1)) ) {
257 strcpy(stpcpy(stpcpy(stpcpy( buf, dir),"\\"), filename),".mo");
258 domain = load_domain( buf );
267 free( the_domain->data );
268 free( the_domain->mapped );
278 get_string( struct loaded_domain *domain, u32 idx )
280 char *p = domain->data + SWAPIT(domain->must_swap,
281 domain->trans_tab[idx].offset);
283 /* status of domain->mapped[idx] is ignored.
284 * not sure about the consequences.
285 * perhaps mapped can entirely be removed?
288 /* we assume, strings are already correctly
292 return (const char*)p;
298 gettext( const char *msgid )
300 struct loaded_domain *domain;
304 if( !(domain = the_domain) ) {
308 /* Locate the MSGID and its translation. */
309 if( domain->hash_size > 2 && domain->hash_tab ) {
310 /* Use the hashing table. */
311 u32 len = strlen (msgid);
312 u32 hash_val = hash_string (msgid);
313 u32 idx = hash_val % domain->hash_size;
314 u32 incr = 1 + (hash_val % (domain->hash_size - 2));
315 u32 nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
317 if ( !nstr ) /* Hash table entry is empty. */
320 if( SWAPIT(domain->must_swap,
321 domain->orig_tab[nstr - 1].length) == len
323 domain->data + SWAPIT(domain->must_swap,
324 domain->orig_tab[nstr - 1].offset)) )
325 return get_string( domain, nstr - 1 );
328 if (idx >= domain->hash_size - incr)
329 idx -= domain->hash_size - incr;
333 nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
335 goto not_found; /* Hash table entry is empty. */
337 if ( SWAPIT(domain->must_swap,
338 domain->orig_tab[nstr - 1].length) == len
340 domain->data + SWAPIT(domain->must_swap,
341 domain->orig_tab[nstr - 1].offset)))
342 return get_string( domain, nstr-1 );
347 /* Now we try the default method: binary search in the sorted
348 array of messages. */
350 top = domain->nstrings;
351 while( bottom < top ) {
354 act = (bottom + top) / 2;
355 cmp_val = strcmp(msgid, domain->data
356 + SWAPIT(domain->must_swap,
357 domain->orig_tab[act].offset));
360 else if (cmp_val > 0)
363 return get_string( domain, act );
371 unsigned int cp1, cp2;
373 cp1 = GetConsoleCP();
374 cp2 = GetConsoleOutputCP();
376 log_info("InputCP=%u OutputCP=%u\n", cp1, cp2 );
378 if( !SetConsoleOutputCP( 1252 ) )
379 log_info("SetConsoleOutputCP failed: %d\n", (int)GetLastError() );
381 cp1 = GetConsoleCP();
382 cp2 = GetConsoleOutputCP();
383 log_info("InputCP=%u OutputCP=%u after switch1\n", cp1, cp2 );
386 #endif /* USE_SIMPLE_GETTEXT */