/[gpgoe]/trunk/src/OENLS.c
ViewVC logotype

Contents of /trunk/src/OENLS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations)
Tue Dec 13 10:40:30 2011 UTC (13 years, 4 months ago) by twoaday
File MIME type: text/plain
File size: 8654 byte(s)
Commit code for backup purposes.


1 /* wptNLS.c - W32 Native Language Support
2 * Copyright (C) 2001, 2002, 2003, 2006 Timo Schulz
3 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
4 *
5 * This code based on the simple-gettext.c code from the GnuPG
6 * by Ulrich Drepper.
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <windows.h>
27
28 #include "gpgme.h"
29 #include "GPGOE.h"
30
31
32 /* The magic number of the GNU message catalog format. */
33 #define MAGIC 0x950412de
34 #define MAGIC_SWAPPED 0xde120495
35
36 /* Revision number of the currently used .mo (binary) file format. */
37 #define MO_REVISION_NUMBER 0
38
39 #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
40
41
42 /* We assume to have `unsigned long int' value with at least 32 bits. */
43 #define HASHWORDBITS 32
44
45 /* The so called `hashpjw' function by P.J. Weinberger
46 [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
47 1986, 1987 Bell Telephone Laboratories, Inc.] */
48
49 /* Header for binary .mo file format. */
50 struct mo_file_header {
51 DWORD magic; /* The magic number. */
52 DWORD revision; /* The revision number of the file format. */
53 DWORD nstrings; /* The number of strings pairs. */
54 DWORD orig_tab_offset; /* Offset of table with start offsets of original
55 strings. */
56 DWORD trans_tab_offset; /* Offset of table with start offsets of translation
57 strings. */
58 DWORD hash_tab_size; /* Size of hashing table. */
59 DWORD hash_tab_offset; /* Offset of first hashing entry. */
60 };
61
62 struct string_desc {
63 DWORD length; /* Length of addressed string. */
64 DWORD offset; /* Offset of string in file. */
65 };
66
67 struct loaded_domain {
68 char *data;
69 int must_swap;
70 DWORD nstrings;
71 char *mapped;
72 struct string_desc *orig_tab;
73 struct string_desc *trans_tab;
74 DWORD hash_size;
75 DWORD *hash_tab;
76 };
77
78
79 /* The current domain. */
80 static struct loaded_domain *the_domain;
81
82
83 static DWORD
84 do_swap_u32 (DWORD i)
85 {
86 return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
87 }
88
89
90 DWORD
91 hash_string (const char *str_param)
92 {
93 unsigned long int hval, g;
94 const char *str = str_param;
95
96 hval = 0;
97 while (*str != '\0') {
98 hval <<= 4;
99 hval += (unsigned long int) *str++;
100 g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
101 if (g != 0) {
102 hval ^= g >> (HASHWORDBITS - 8);
103 hval ^= g;
104 }
105 }
106 return hval;
107 }
108
109
110 static struct loaded_domain *
111 load_domain( const char *filename )
112 {
113 FILE *fp;
114 size_t size;
115 struct stat st;
116 struct mo_file_header *data = NULL;
117 struct loaded_domain *domain = NULL;
118 size_t to_read;
119 char *read_ptr;
120
121 fp = fopen( filename, "rb" );
122 if( !fp )
123 return NULL; /* can't open the file */
124 /* we must know about the size of the file */
125 if( fstat( fileno(fp ), &st )
126 || (size = (size_t)st.st_size) != (size_t)st.st_size
127 || size < sizeof (struct mo_file_header) ) {
128 fclose( fp );
129 return NULL;
130 }
131
132 data = (struct mo_file_header *) xcalloc (1, size);
133 if( !data ) {
134 fclose( fp );
135 return NULL; /* out of memory */
136 }
137
138 to_read = size;
139 read_ptr = (char *) data;
140 do {
141 size_t nb = fread( read_ptr, 1, to_read, fp );
142 if( nb < to_read ) {
143 fclose (fp);
144 free(data);
145 return NULL; /* read error */
146
147 }
148 read_ptr += nb;
149 to_read -= nb;
150 } while( to_read > 0 );
151 fclose (fp);
152
153 /* Using the magic number we can test whether it really is a message
154 * catalog file. */
155 if( data->magic != MAGIC && data->magic != MAGIC_SWAPPED ) {
156 /* The magic number is wrong: not a message catalog file. */
157 free( data );
158 return NULL;
159 }
160
161 domain = (struct loaded_domain *)xcalloc( 1, sizeof *domain );
162 if( !domain ) {
163 free( data );
164 return NULL;
165 }
166 domain->data = (char *) data;
167 domain->must_swap = data->magic != MAGIC;
168
169 /* Fill in the information about the available tables. */
170 switch( SWAPIT(domain->must_swap, data->revision) ) {
171 case 0:
172 domain->nstrings = SWAPIT(domain->must_swap, data->nstrings);
173 domain->orig_tab = (struct string_desc *)
174 ((char *) data + SWAPIT(domain->must_swap, data->orig_tab_offset));
175 domain->trans_tab = (struct string_desc *)
176 ((char *) data + SWAPIT(domain->must_swap, data->trans_tab_offset));
177 domain->hash_size = SWAPIT(domain->must_swap, data->hash_tab_size);
178 domain->hash_tab = (DWORD *)
179 ((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset));
180 break;
181
182 default: /* This is an invalid revision. */
183 free( data );
184 free( domain );
185 return NULL;
186 }
187
188 /* allocate an array to keep track of code page mappings */
189 domain->mapped = (char *)xcalloc( 1, domain->nstrings );
190 if( !domain->mapped ) {
191 free( data );
192 free( domain );
193 return NULL;
194 }
195
196 return domain;
197 } /* load_domain */
198
199
200 static const char*
201 get_string( struct loaded_domain *domain, DWORD idx )
202 {
203 char *p = domain->data + SWAPIT(domain->must_swap,
204 domain->trans_tab[idx].offset);
205 if( !domain->mapped[idx] ) {
206 domain->mapped[idx] = 1;
207 }
208 return (const char*)p;
209 }
210
211
212 const char *
213 gettext( const char *msgid )
214 {
215 struct loaded_domain *domain;
216 size_t act = 0;
217 size_t top, bottom;
218
219 if( !(domain = the_domain) )
220 goto not_found;
221
222 /* Locate the MSGID and its translation. */
223 if( domain->hash_size > 2 && domain->hash_tab ) {
224 /* Use the hashing table. */
225 DWORD len = strlen (msgid);
226 DWORD hash_val = hash_string (msgid);
227 DWORD idx = hash_val % domain->hash_size;
228 DWORD incr = 1 + (hash_val % (domain->hash_size - 2));
229 DWORD nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
230
231 if ( !nstr ) /* Hash table entry is empty. */
232 goto not_found;
233
234
235 if( SWAPIT(domain->must_swap,
236 domain->orig_tab[nstr - 1].length) == len
237 && !strcmp( msgid,
238 domain->data + SWAPIT(domain->must_swap,
239 domain->orig_tab[nstr - 1].offset)) )
240 return get_string( domain, nstr - 1 );
241 for(;;) {
242 if (idx >= domain->hash_size - incr)
243 idx -= domain->hash_size - incr;
244 else
245 idx += incr;
246 nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
247 if( !nstr )
248 goto not_found; /* Hash table entry is empty. */
249
250 if ( SWAPIT(domain->must_swap,
251 domain->orig_tab[nstr - 1].length) == len
252 && !strcmp (msgid,
253 domain->data + SWAPIT(domain->must_swap,
254 domain->orig_tab[nstr - 1].offset)))
255
256 return get_string( domain, nstr-1 );
257 } /* NOTREACHED */
258 }
259
260 /* Now we try the default method: binary search in the sorted
261 array of messages. */
262 bottom = 0;
263 top = domain->nstrings;
264 while( bottom < top ) {
265 int cmp_val;
266
267 act = (bottom + top) / 2;
268 cmp_val = strcmp(msgid, domain->data + SWAPIT(domain->must_swap,
269 domain->orig_tab[act].offset));
270 if (cmp_val < 0)
271 top = act;
272 else if (cmp_val > 0)
273 bottom = act + 1;
274 else
275 return get_string( domain, act );
276 }
277
278 not_found:
279 return msgid;
280 }
281
282
283 /* Set the file used for translations. Pass a NULL to disable translation.
284 A new filename may be set at anytime. */
285 int
286 set_gettext_file (const char *filename)
287 {
288 struct loaded_domain *domain = NULL;
289
290 if (!filename)
291 return 0;
292
293 domain = load_domain (filename);
294 if (the_domain) {
295 free (the_domain->data);
296 free (the_domain->mapped);
297 free (the_domain);
298 the_domain = NULL;
299 }
300 the_domain = domain;
301 return 0;
302 }
303
304
305 int
306 read_gettext_entry (char *path, DWORD pathlen)
307 {
308 HKEY hkey;
309 LONG err;
310 DWORD type = 0;
311
312 err = RegOpenKeyEx (HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", 0,
313 KEY_READ, &hkey);
314 if (err != ERROR_SUCCESS)
315 return -1;
316 err = RegQueryValueEx (hkey, "MODir", NULL, &type, (BYTE*)path, &pathlen);
317 RegCloseKey (hkey);
318 if (err != ERROR_SUCCESS)
319 return -1;
320 return 0;
321 }
322
323
324 int
325 setup_gettext (void)
326 {
327 struct stat st;
328 char modir[MAX_PATH+1+32];
329
330 memset (modir, 0, sizeof (modir));
331 if (read_gettext_entry (modir, MAX_PATH-1))
332 return -1;
333 strcat (modir, "\\gpgoe.mo");
334 if (stat (modir, &st))
335 return -1;
336 set_gettext_file (modir);
337 return 0;
338 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26