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 3 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, see <http://www.gnu.org/licenses/>.
20 /* This is a simplified version of gettext written by Ulrich Drepper.
21 * It is used for the Win32 version of GnuPG becuase all the overhead
22 * of gettext is not needed and we have to do some special Win32 stuff.
23 * I decided that this is far easier than to tweak gettext for the special
24 * cases (I tried it but it is a lot of code). wk 15.09.99
28 #ifdef USE_SIMPLE_GETTEXT
30 #error This file can only be used with MinGW32
38 #include <sys/types.h>
43 typedef unsigned int u32
; /* That is fine with MingW32 */
45 typedef unsigned long ulong
;
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. */
90 struct string_desc
*orig_tab
;
91 struct string_desc
*trans_tab
;
97 static struct loaded_domain
*the_domain
;
102 return (i
<< 24) | ((i
& 0xff00) << 8) | ((i
>> 8) & 0xff00) | (i
>> 24);
105 #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
108 /* We assume to have `unsigned long int' value with at least 32 bits. */
109 #define HASHWORDBITS 32
111 /* The so called `hashpjw' function by P.J. Weinberger
112 [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
113 1986, 1987 Bell Telephone Laboratories, Inc.] */
115 static __inline__ ulong
116 hash_string( const char *str_param
)
118 unsigned long int hval
, g
;
119 const char *str
= str_param
;
125 hval
+= (unsigned long int) *str
++;
126 g
= hval
& ((unsigned long int) 0xf << (HASHWORDBITS
- 4));
129 hval
^= g
>> (HASHWORDBITS
- 8);
137 static struct loaded_domain
*
138 load_domain( const char *filename
)
143 struct mo_file_header
*data
= NULL
;
144 struct loaded_domain
*domain
= NULL
;
148 fp
= g_fopen( filename
, "rb" );
150 return NULL
; /* can't open the file */
151 /* we must know about the size of the file */
152 if( fstat( fileno(fp
), &st
)
153 || (size
= (size_t)st
.st_size
) != st
.st_size
154 || size
< sizeof (struct mo_file_header
) ) {
159 data
= malloc( size
);
162 return NULL
; /* out of memory */
166 read_ptr
= (char *) data
;
168 long int nb
= fread( read_ptr
, 1, to_read
, fp
);
172 return NULL
; /* read error */
176 } while( to_read
> 0 );
179 /* Using the magic number we can test whether it really is a message
181 if( data
->magic
!= MAGIC
&& data
->magic
!= MAGIC_SWAPPED
) {
182 /* The magic number is wrong: not a message catalog file. */
187 domain
= calloc( 1, sizeof *domain
);
192 domain
->data
= (char *) data
;
193 domain
->must_swap
= data
->magic
!= MAGIC
;
195 /* Fill in the information about the available tables. */
196 switch( SWAPIT(domain
->must_swap
, data
->revision
) ) {
198 domain
->nstrings
= SWAPIT(domain
->must_swap
, data
->nstrings
);
199 domain
->orig_tab
= (struct string_desc
*)
200 ((char *) data
+ SWAPIT(domain
->must_swap
, data
->orig_tab_offset
));
201 domain
->trans_tab
= (struct string_desc
*)
202 ((char *) data
+ SWAPIT(domain
->must_swap
, data
->trans_tab_offset
));
203 domain
->hash_size
= SWAPIT(domain
->must_swap
, data
->hash_tab_size
);
204 domain
->hash_tab
= (u32
*)
205 ((char *) data
+ SWAPIT(domain
->must_swap
, data
->hash_tab_offset
));
208 default: /* This is an invalid revision. */
214 /* allocate an array to keep track of code page mappings */
215 domain
->mapped
= calloc( 1, domain
->nstrings
);
216 if( !domain
->mapped
) {
227 * Set the file used for translations. Pass a NULL to disable
228 * translation. A new filename may be set at anytime.
229 * WARNING: After changing the filename you shoudl not access any data
230 * retrieved by gettext().
233 set_gettext_file( const char *filename
)
235 struct loaded_domain
*domain
= NULL
;
237 if( filename
&& *filename
) {
238 if( filename
[0] == '/'
239 #ifdef HAVE_DRIVE_LETTERS
240 || ( isalpha(filename
[0])
241 && filename
[1] == ':'
242 && (filename
[2] == '/' || filename
[2] == '\\') )
245 /* absolute path - use it as is */
246 domain
= load_domain( filename
);
248 else { /* relative path - append ".mo" and get dir from the environment */
252 dir
= read_w32_registry_string( NULL
,
253 "Control Panel\\Mingw32\\NLS",
255 if( dir
&& (buf
=malloc(strlen(dir
)+strlen(filename
)+1+3+1)) ) {
256 strcpy(stpcpy(stpcpy(stpcpy( buf
, dir
),"\\"), filename
),".mo");
257 domain
= load_domain( buf
);
266 free( the_domain
->data
);
267 free( the_domain
->mapped
);
277 get_string( struct loaded_domain
*domain
, u32 idx
)
279 char *p
= domain
->data
+ SWAPIT(domain
->must_swap
,
280 domain
->trans_tab
[idx
].offset
);
282 /* status of domain->mapped[idx] is ignored.
283 * not sure about the consequences.
284 * perhaps mapped can entirely be removed?
287 /* we assume, strings are already correctly
291 return (const char*)p
;
297 gettext( const char *msgid
)
299 struct loaded_domain
*domain
;
303 if( !(domain
= the_domain
) ) {
307 /* Locate the MSGID and its translation. */
308 if( domain
->hash_size
> 2 && domain
->hash_tab
) {
309 /* Use the hashing table. */
310 u32 len
= strlen (msgid
);
311 u32 hash_val
= hash_string (msgid
);
312 u32 idx
= hash_val
% domain
->hash_size
;
313 u32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
314 u32 nstr
= SWAPIT (domain
->must_swap
, domain
->hash_tab
[idx
]);
316 if ( !nstr
) /* Hash table entry is empty. */
319 if( SWAPIT(domain
->must_swap
,
320 domain
->orig_tab
[nstr
- 1].length
) == len
322 domain
->data
+ SWAPIT(domain
->must_swap
,
323 domain
->orig_tab
[nstr
- 1].offset
)) )
324 return get_string( domain
, nstr
- 1 );
327 if (idx
>= domain
->hash_size
- incr
)
328 idx
-= domain
->hash_size
- incr
;
332 nstr
= SWAPIT(domain
->must_swap
, domain
->hash_tab
[idx
]);
334 goto not_found
; /* Hash table entry is empty. */
336 if ( SWAPIT(domain
->must_swap
,
337 domain
->orig_tab
[nstr
- 1].length
) == len
339 domain
->data
+ SWAPIT(domain
->must_swap
,
340 domain
->orig_tab
[nstr
- 1].offset
)))
341 return get_string( domain
, nstr
-1 );
346 /* Now we try the default method: binary search in the sorted
347 array of messages. */
349 top
= domain
->nstrings
;
350 while( bottom
< top
) {
353 act
= (bottom
+ top
) / 2;
354 cmp_val
= strcmp(msgid
, domain
->data
355 + SWAPIT(domain
->must_swap
,
356 domain
->orig_tab
[act
].offset
));
359 else if (cmp_val
> 0)
362 return get_string( domain
, act
);
369 #endif /* USE_SIMPLE_GETTEXT */