/[winpt]/trunk/Src/wptNLS.c
ViewVC logotype

Annotation of /trunk/Src/wptNLS.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 76 - (hide annotations)
Thu Nov 10 14:07:06 2005 UTC (19 years, 3 months ago) by twoaday
File MIME type: text/plain
File size: 9840 byte(s)
2005-11-09  Timo Schulz  <ts@g10code.com>
 
        * wptNLS.c (get_gettext_langid): Map user language id to
        printable gettext language strings.
        * wptRegistry.cpp (get_reg_entry_mo): Support for the
        gettext environment.
        * wptFileManager.cpp (fm_encrypt): Close all files before
        the wipe procedure is called.
        * wptVerlist.cpp (verlist_add_sig): Handle the case the
        issuer is not valid but the signature is.
        (verlist_add_sig_log): Likewise. Thanks to the folks on
        winpt-users.


1 werner 36 /* wptNLS.cpp - W32 Native Language Support
2     * Copyright (C) 2001, 2002, 2003 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     * WinPT is free software; you can redistribute it and/or
9     * modify it under the terms of the GNU 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     * WinPT 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     * You should have received a copy of the GNU General Public License
19     * along with WinPT; if not, write to the Free Software Foundation,
20     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21     */
22    
23     #ifdef HAVE_CONFIG_H
24     #include <config.h>
25     #endif
26    
27     #include <stdio.h>
28     #include <stdio.h>
29     #include <string.h>
30     #include <ctype.h>
31     #include <errno.h>
32     #include <sys/types.h>
33     #include <sys/stat.h>
34     #include <windows.h>
35    
36     #include "wptTypes.h"
37     #include "wptNLS.h"
38    
39     /* Missing W32 functions. */
40     static char *
41 twoaday 69 w32_stpcpy (char *a,const char *b)
42 werner 36 {
43 twoaday 69 while (*b)
44 werner 36 *a++ = *b++;
45     *a = 0;
46     return (char*)a;
47     }
48    
49 twoaday 69
50 werner 36 /* The magic number of the GNU message catalog format. */
51     #define MAGIC 0x950412de
52     #define MAGIC_SWAPPED 0xde120495
53    
54     /* Revision number of the currently used .mo (binary) file format. */
55     #define MO_REVISION_NUMBER 0
56    
57    
58     /* Header for binary .mo file format. */
59     struct mo_file_header {
60     u32 magic; /* The magic number. */
61     u32 revision; /* The revision number of the file format. */
62     u32 nstrings; /* The number of strings pairs. */
63     u32 orig_tab_offset; /* Offset of table with start offsets of original
64     strings. */
65     u32 trans_tab_offset; /* Offset of table with start offsets of translation
66     strings. */
67     u32 hash_tab_size; /* Size of hashing table. */
68     u32 hash_tab_offset; /* Offset of first hashing entry. */
69     };
70    
71     struct string_desc {
72     u32 length; /* Length of addressed string. */
73     u32 offset; /* Offset of string in file. */
74     };
75    
76     struct loaded_domain {
77     char *data;
78     int must_swap;
79     u32 nstrings;
80     char *mapped;
81     struct string_desc *orig_tab;
82     struct string_desc *trans_tab;
83     u32 hash_size;
84     u32 *hash_tab;
85     };
86    
87     static struct loaded_domain *the_domain;
88    
89     static u32
90     do_swap_u32( u32 i )
91     {
92     return (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | (i >> 24);
93     }
94    
95     #define SWAPIT(flag, data) ((flag) ? do_swap_u32(data) : (data) )
96    
97    
98     /* We assume to have `unsigned long int' value with at least 32 bits. */
99     #define HASHWORDBITS 32
100    
101     /* The so called `hashpjw' function by P.J. Weinberger
102     [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
103     1986, 1987 Bell Telephone Laboratories, Inc.] */
104    
105     static u32
106     hash_string( const char *str_param )
107     {
108     unsigned long int hval, g;
109     const char *str = str_param;
110    
111     hval = 0;
112     while (*str != '\0') {
113     hval <<= 4;
114     hval += (unsigned long int) *str++;
115     g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
116     if (g != 0) {
117     hval ^= g >> (HASHWORDBITS - 8);
118     hval ^= g;
119     }
120     }
121     return hval;
122     } /* hash_string */
123    
124    
125     static struct loaded_domain *
126     load_domain( const char *filename )
127     {
128     FILE *fp;
129     size_t size;
130     struct stat st;
131     struct mo_file_header *data = NULL;
132     struct loaded_domain *domain = NULL;
133     size_t to_read;
134     char *read_ptr;
135    
136     fp = fopen( filename, "rb" );
137     if( !fp )
138     return NULL; /* can't open the file */
139     /* we must know about the size of the file */
140     if( fstat( fileno(fp ), &st )
141     || (size = (size_t)st.st_size) != st.st_size
142     || size < sizeof (struct mo_file_header) ) {
143     fclose( fp );
144     return NULL;
145     }
146    
147     data = (struct mo_file_header *) malloc( size );
148     if( !data ) {
149     fclose( fp );
150     return NULL; /* out of memory */
151     }
152    
153     to_read = size;
154     read_ptr = (char *) data;
155     do {
156     long int nb = fread( read_ptr, 1, to_read, fp );
157     if( nb < to_read ) {
158     fclose (fp);
159     free(data);
160     return NULL; /* read error */
161    
162     }
163     read_ptr += nb;
164     to_read -= nb;
165     } while( to_read > 0 );
166     fclose (fp);
167    
168     /* Using the magic number we can test whether it really is a message
169     * catalog file. */
170     if( data->magic != MAGIC && data->magic != MAGIC_SWAPPED ) {
171     /* The magic number is wrong: not a message catalog file. */
172     free( data );
173     return NULL;
174     }
175    
176     domain = (struct loaded_domain *)calloc( 1, sizeof *domain );
177     if( !domain ) {
178     free( data );
179     return NULL;
180     }
181     domain->data = (char *) data;
182     domain->must_swap = data->magic != MAGIC;
183    
184     /* Fill in the information about the available tables. */
185     switch( SWAPIT(domain->must_swap, data->revision) ) {
186     case 0:
187     domain->nstrings = SWAPIT(domain->must_swap, data->nstrings);
188     domain->orig_tab = (struct string_desc *)
189     ((char *) data + SWAPIT(domain->must_swap, data->orig_tab_offset));
190     domain->trans_tab = (struct string_desc *)
191     ((char *) data + SWAPIT(domain->must_swap, data->trans_tab_offset));
192     domain->hash_size = SWAPIT(domain->must_swap, data->hash_tab_size);
193     domain->hash_tab = (u32 *)
194     ((char *) data + SWAPIT(domain->must_swap, data->hash_tab_offset));
195     break;
196    
197     default: /* This is an invalid revision. */
198     free( data );
199     free( domain );
200     return NULL;
201     }
202    
203     /* allocate an array to keep track of code page mappings */
204     domain->mapped = (char *)calloc( 1, domain->nstrings );
205     if( !domain->mapped ) {
206     free( data );
207     free( domain );
208     return NULL;
209     }
210    
211     return domain;
212     } /* load_domain */
213    
214    
215     /****************
216     * Set the file used for translations. Pass a NULL to disable
217     * translation. A new filename may be set at anytime.
218     * WARNING: After changing the filename you shoudl not access any data
219     * retrieved by gettext().
220     */
221     int
222     set_gettext_file( const char *filename, const char *nls_dir )
223     {
224     struct loaded_domain *domain = NULL;
225    
226     if( filename && *filename ) {
227     if( filename[0] == '/'
228     || ( isalpha(filename[0])
229     && filename[1] == ':'
230     && (filename[2] == '/' || filename[2] == '\\') )
231     ) {
232     /* absolute path - use it as is */
233     domain = load_domain( filename );
234     }
235     else { /* relative path - append ".mo" and get dir from the environment */
236     char *buf = NULL;
237     char *dir;
238    
239     dir = strdup( nls_dir );
240     if( dir && (buf= (char *)malloc(strlen(dir)+strlen(filename)+1+3+1)) ) {
241 twoaday 69 strcpy(w32_stpcpy(w32_stpcpy(w32_stpcpy( buf, dir),"/"), filename),".mo");
242 werner 36 domain = load_domain( buf );
243     free(buf);
244     }
245     free(dir);
246     }
247     if( !domain )
248     return -1;
249     }
250    
251     if( the_domain ) {
252     free( the_domain->data );
253     free( the_domain->mapped );
254     free( the_domain );
255     the_domain = NULL;
256     }
257     the_domain = domain;
258     return 0;
259     } /* set_gettext_file */
260    
261    
262     static const char*
263     get_string( struct loaded_domain *domain, u32 idx )
264     {
265     char *p = domain->data + SWAPIT(domain->must_swap,
266     domain->trans_tab[idx].offset);
267     if( !domain->mapped[idx] ) {
268     domain->mapped[idx] = 1;
269     }
270     return (const char*)p;
271     } /* get_string */
272    
273 werner 48
274 werner 36 const char *
275     gettext( const char *msgid )
276     {
277     struct loaded_domain *domain;
278     size_t act = 0;
279     size_t top, bottom;
280    
281     if( !(domain = the_domain) )
282     goto not_found;
283    
284     /* Locate the MSGID and its translation. */
285     if( domain->hash_size > 2 && domain->hash_tab ) {
286     /* Use the hashing table. */
287     u32 len = strlen (msgid);
288     u32 hash_val = hash_string (msgid);
289     u32 idx = hash_val % domain->hash_size;
290     u32 incr = 1 + (hash_val % (domain->hash_size - 2));
291     u32 nstr = SWAPIT (domain->must_swap, domain->hash_tab[idx]);
292    
293     if ( !nstr ) /* Hash table entry is empty. */
294     goto not_found;
295    
296    
297     if( SWAPIT(domain->must_swap,
298     domain->orig_tab[nstr - 1].length) == len
299     && !strcmp( msgid,
300     domain->data + SWAPIT(domain->must_swap,
301     domain->orig_tab[nstr - 1].offset)) )
302     return get_string( domain, nstr - 1 );
303     for(;;) {
304     if (idx >= domain->hash_size - incr)
305     idx -= domain->hash_size - incr;
306     else
307     idx += incr;
308     nstr = SWAPIT(domain->must_swap, domain->hash_tab[idx]);
309     if( !nstr )
310     goto not_found; /* Hash table entry is empty. */
311    
312     if ( SWAPIT(domain->must_swap,
313     domain->orig_tab[nstr - 1].length) == len
314     && !strcmp (msgid,
315     domain->data + SWAPIT(domain->must_swap,
316     domain->orig_tab[nstr - 1].offset)))
317    
318     return get_string( domain, nstr-1 );
319     } /* NOTREACHED */
320     }
321    
322     /* Now we try the default method: binary search in the sorted
323     array of messages. */
324     bottom = 0;
325     top = domain->nstrings;
326     while( bottom < top ) {
327     int cmp_val;
328    
329     act = (bottom + top) / 2;
330     cmp_val = strcmp(msgid, domain->data + SWAPIT(domain->must_swap,
331     domain->orig_tab[act].offset));
332     if (cmp_val < 0)
333     top = act;
334     else if (cmp_val > 0)
335     bottom = act + 1;
336     else
337     return get_string( domain, act );
338     }
339    
340     not_found:
341     return msgid;
342     } /* gettext */
343    
344 twoaday 76 /* Map the user specific language ID to a
345     gettext conform language string.
346     Example: LANG_GERMAN -> "de" */
347     const char*
348     get_gettext_langid (void)
349     {
350     struct {
351     const char *id;
352     int langid;
353     } lang_table[] = {
354     {"de", LANG_GERMAN},
355     {NULL, 0},
356     };
357     LANGID lang;
358     int i;
359    
360     lang = GetUserDefaultLangID ();
361    
362     for (i=0; lang_table[i].id; i++) {
363     if (PRIMARYLANGID (lang) == lang_table[i].langid)
364     return lang_table[i].id;
365     }
366     return NULL;
367     }
368 werner 36
369 twoaday 76
370 werner 36 /* XXX: this has to be placed in a .c file but not here */
371     #include <shlobj.h>
372    
373     void _SHFree (void *p)
374     {
375     IMalloc *pm;
376     SHGetMalloc(&pm);
377     if (pm) {
378     pm->lpVtbl->Free(pm,p);
379     pm->lpVtbl->Release(pm);
380     }
381     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26