See ChangeLog: Mon Jan 24 13:04:28 CET 2000 Werner Koch
[gnupg.git] / util / strgutil.c
1 /* strgutil.c -  string utilities
2  *      Copyright (C) 1998 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "types.h"
26 #include "util.h"
27 #include "memory.h"
28
29
30 static ushort koi8_unicode[128] = {
31     0x2500,0x2502,0x250c,0x2510,0x2514,0x2518,0x251c,0x2524,
32     0x252c,0x2534,0x253c,0x2580,0x2584,0x2588,0x258c,0x2590,
33     0x2591,0x2592,0x2593,0x2320,0x25a0,0x2219,0x221a,0x2248,
34     0x2264,0x2265,0x00a0,0x2321,0x00b0,0x00b2,0x00b7,0x00f7,
35     0x2550,0x2551,0x2552,0x0451,0x2553,0x2554,0x2555,0x2556,
36     0x2557,0x2558,0x2559,0x255a,0x255b,0x255c,0x255d,0x255e,
37     0x255f,0x2560,0x2561,0x0401,0x2562,0x2563,0x2564,0x2565,
38     0x2566,0x2567,0x2568,0x2569,0x256a,0x256b,0x256c,0x00a9,
39     0x044e,0x0430,0x0431,0x0446,0x0434,0x0435,0x0444,0x0433,
40     0x0445,0x0438,0x0439,0x043a,0x043b,0x043c,0x043d,0x043e,
41     0x043f,0x044f,0x0440,0x0441,0x0442,0x0443,0x0436,0x0432,
42     0x044c,0x044b,0x0437,0x0448,0x044d,0x0449,0x0447,0x044a,
43     0x042e,0x0410,0x0411,0x0426,0x0414,0x0415,0x0424,0x0413,
44     0x0425,0x0418,0x0419,0x041a,0x041b,0x041c,0x041d,0x041e,
45     0x041f,0x042f,0x0420,0x0421,0x0422,0x0423,0x0416,0x0412,
46     0x042c,0x042b,0x0417,0x0428,0x042d,0x0429,0x0427,0x042a
47 };
48
49 static ushort latin2_unicode[128] = {
50     0x0080,0x0081,0x0082,0x0083,0x0084,0x0085,0x0086,0x0087,
51     0x0088,0x0089,0x008A,0x008B,0x008C,0x008D,0x008E,0x008F,
52     0x0090,0x0091,0x0092,0x0093,0x0094,0x0095,0x0096,0x0097,
53     0x0098,0x0099,0x009A,0x009B,0x009C,0x009D,0x009E,0x009F,
54     0x00A0,0x0104,0x02D8,0x0141,0x00A4,0x013D,0x015A,0x00A7,
55     0x00A8,0x0160,0x015E,0x0164,0x0179,0x00AD,0x017D,0x017B,
56     0x00B0,0x0105,0x02DB,0x0142,0x00B4,0x013E,0x015B,0x02C7,
57     0x00B8,0x0161,0x015F,0x0165,0x017A,0x02DD,0x017E,0x017C,
58     0x0154,0x00C1,0x00C2,0x0102,0x00C4,0x0139,0x0106,0x00C7,
59     0x010C,0x00C9,0x0118,0x00CB,0x011A,0x00CD,0x00CE,0x010E,
60     0x0110,0x0143,0x0147,0x00D3,0x00D4,0x0150,0x00D6,0x00D7,
61     0x0158,0x016E,0x00DA,0x0170,0x00DC,0x00DD,0x0162,0x00DF,
62     0x0155,0x00E1,0x00E2,0x0103,0x00E4,0x013A,0x0107,0x00E7,
63     0x010D,0x00E9,0x0119,0x00EB,0x011B,0x00ED,0x00EE,0x010F,
64     0x0111,0x0144,0x0148,0x00F3,0x00F4,0x0151,0x00F6,0x00F7,
65     0x0159,0x016F,0x00FA,0x0171,0x00FC,0x00FD,0x0163,0x02D9
66 };
67
68 static ushort ibm850_unicode[128] = {
69     0x00c7,0x00fc,0x00e9,0x00e2,0x00e4,0x00e0,0x00e5,0x00e7,
70     0x00ea,0x00eb,0x00e8,0x00ef,0x00ee,0x00ec,0x00c4,0x00c5,
71     0x00c9,0x00e6,0x00c6,0x00f4,0x00f6,0x00f2,0x00fb,0x00f9,
72     0x00ff,0x00d6,0x00dc,0x00f8,0x00a3,0x00d8,0x00d7,0x0192,
73     0x00e1,0x00ed,0x00f3,0x00fa,0x00f1,0x00d1,0x00aa,0x00ba,
74     0x00bf,0x00ae,0x00ac,0x00bd,0x00bc,0x00a1,0x00ab,0x00bb,
75     0x2591,0x2592,0x2593,0x2502,0x2524,0x00c1,0x00c2,0x00c0,
76     0x00a9,0x2563,0x2551,0x2557,0x255d,0x00a2,0x00a5,0x2510,
77     0x2514,0x2534,0x252c,0x251c,0x2500,0x253c,0x00e3,0x00c3,
78     0x255a,0x2554,0x2569,0x2566,0x2560,0x2550,0x256c,0x00a4,
79     0x00f0,0x00d0,0x00ca,0x00cb,0x00c8,0x0131,0x00cd,0x00ce,
80     0x00cf,0x2518,0x250c,0x2588,0x2584,0x00a6,0x00cc,0x2580,
81     0x00d3,0x00df,0x00d4,0x00d2,0x00f5,0x00d5,0x00b5,0x00fe,
82     0x00de,0x00da,0x00db,0x00d9,0x00fd,0x00dd,0x00af,0x00b4,
83     0x00ad,0x00b1,0x2017,0x00be,0x00b6,0x00a7,0x00f7,0x00b8,
84     0x00b0,0x00a8,0x00b7,0x00b9,0x00b3,0x00b2,0x25a0,0x00a0,
85 };
86
87 static int query_native_charset_done = 0;
88 static const char *active_charset_name = "iso-8859-1";
89 static ushort *active_charset = NULL;
90
91
92 void
93 free_strlist( STRLIST sl )
94 {
95     STRLIST sl2;
96
97     for(; sl; sl = sl2 ) {
98         sl2 = sl->next;
99         gcry_free(sl);
100     }
101 }
102
103
104 STRLIST
105 add_to_strlist( STRLIST *list, const char *string )
106 {
107     STRLIST sl;
108
109     sl = gcry_xmalloc( sizeof *sl + strlen(string));
110     sl->flags = 0;
111     strcpy(sl->d, string);
112     sl->next = *list;
113     *list = sl;
114     return sl;
115 }
116
117 /****************
118  * ame as add_to_strlist() but if is_utf8 is *not* set a conversion
119  * to UTF8 is done
120  */
121 STRLIST
122 add_to_strlist2( STRLIST *list, const char *string, int is_utf8 )
123 {
124     STRLIST sl;
125
126     if( is_utf8 )
127         sl = add_to_strlist( list, string );
128     else {
129         char *p = native_to_utf8( string );
130         sl = add_to_strlist( list, p );
131         gcry_free( p );
132     }
133     return sl;
134 }
135
136 STRLIST
137 append_to_strlist( STRLIST *list, const char *string )
138 {
139     STRLIST r, sl;
140
141     sl = gcry_xmalloc( sizeof *sl + strlen(string));
142     sl->flags = 0;
143     strcpy(sl->d, string);
144     sl->next = NULL;
145     if( !*list )
146         *list = sl;
147     else {
148         for( r = *list; r->next; r = r->next )
149             ;
150         r->next = sl;
151     }
152     return sl;
153 }
154
155 STRLIST
156 append_to_strlist2( STRLIST *list, const char *string, int is_utf8 )
157 {
158     STRLIST sl;
159
160     if( is_utf8 )
161         sl = append_to_strlist( list, string );
162     else {
163         char *p = native_to_utf8( string );
164         sl = append_to_strlist( list, p );
165         gcry_free( p );
166     }
167     return sl;
168 }
169
170
171 STRLIST
172 strlist_prev( STRLIST head, STRLIST node )
173 {
174     STRLIST n;
175
176     for(n=NULL; head && head != node; head = head->next )
177         n = head;
178     return n;
179 }
180
181 STRLIST
182 strlist_last( STRLIST node )
183 {
184     if( node )
185         for( ; node->next ; node = node->next )
186             ;
187     return node;
188 }
189
190
191
192 int
193 string_count_chr( const char *string, int c )
194 {
195     int count;
196     for(count=0; *string; string++ )
197         if( *string == c )
198             count++;
199     return count;
200 }
201
202
203 static const char*
204 query_native_charset(void)
205 {
206   #ifdef __MINGW32__
207     unsigned int cp;
208
209     cp = GetConsoleOutputCP();
210     if( cp != GetConsoleCP() ) {
211         /* The input cgarset is not equal to the output charset
212          * our system depends on it and therefore we will set
213          * same the same (this won't work on Windows 95) */
214         if( !SetConsoleCP( cp ) )
215             log_info("can't set Input-CP to Output-CP: %d\n",
216                                                     (int)GetLastError() );
217     }
218     /* we could read the registry, but this seems to be too much work */
219     switch( cp ) {
220       case 850:  return "ibm850";
221       case 437:  return "ibm437";
222       case 1252: return "iso-8859-1";
223       default:
224         log_info("unknown MS-Windows CodePage %u "
225                  "- trying to switch to Latin-1\n", cp );
226         /* try to set latin-1 */
227         if( !SetConsoleOutputCP( 1252 ) ) {
228             if( !SetConsoleCP( 1252 ) )
229                 return "iso-8859-1";
230             else /* back off */
231                 SetConsoleOutputCP( cp );
232         }
233         log_info("no information about MS-Windows CodePage %u\n", cp );
234         return NULL;
235     }
236   #else
237     return NULL; /* unknown */
238   #endif
239 }
240
241
242 const char*
243 get_native_charset()
244 {
245     if( !query_native_charset_done ) {
246         const char *s;
247
248         query_native_charset_done = 1;
249         s = query_native_charset();
250         if( s )
251             set_native_charset(s);
252     }
253
254     return active_charset_name;
255 }
256
257
258 int
259 set_native_charset( const char *newset )
260 {
261     query_native_charset_done = 1; /* don't do this when we want to set one*/
262     if( !stricmp( newset, "iso-8859-1" ) ) {
263         active_charset_name = "iso-8859-1";
264         active_charset = NULL;
265     }
266     else if( !stricmp( newset, "iso-8859-2" ) ) {
267         active_charset_name = "iso-8859-2";
268         active_charset = latin2_unicode;
269     }
270     else if( !stricmp( newset, "koi8-r" ) ) {
271         active_charset_name = "koi8-r";
272         active_charset = koi8_unicode;
273     }
274     else if( !stricmp( newset, "ibm850" ) || !stricmp( newset, "ibm437" ) ) {
275         active_charset_name = "ibm850";
276         active_charset = ibm850_unicode;
277     }
278     else
279         return G10ERR_GENERAL;
280     return 0;
281 }
282
283
284 /****************
285  * Convert string, which is in native encoding to UTF8 and return the
286  * new allocated UTF8 string.
287  */
288 char *
289 native_to_utf8( const char *string )
290 {
291     const byte *s;
292     char *buffer;
293     byte *p;
294     size_t length=0;
295
296     if( active_charset ) {
297         for(s=string; *s; s++ ) {
298             length++;
299             if( *s & 0x80 )
300                 length += 2; /* we may need 3 bytes */
301         }
302         buffer = gcry_xmalloc( length + 1 );
303         for(p=buffer, s=string; *s; s++ ) {
304             if( *s & 0x80 ) {
305                 ushort val = active_charset[ *s & 0x7f ];
306                 if( val < 0x0800 ) {
307                     *p++ = 0xc0 | ( (val >> 6) & 0x1f );
308                     *p++ = 0x80 | (  val & 0x3f );
309                 }
310                 else {
311                     *p++ = 0xe0 | ( (val >> 12) & 0x0f );
312                     *p++ = 0x80 | ( (val >>  6) & 0x3f );
313                     *p++ = 0x80 | (  val & 0x3f );
314                 }
315             }
316             else
317                 *p++ = *s;
318         }
319         *p = 0;
320     }
321     else {
322         for(s=string; *s; s++ ) {
323             length++;
324             if( *s & 0x80 )
325                 length++;
326         }
327         buffer = gcry_xmalloc( length + 1 );
328         for(p=buffer, s=string; *s; s++ ) {
329             if( *s & 0x80 ) {
330                 *p++ = 0xc0 | ((*s >> 6) & 3);
331                 *p++ = 0x80 | ( *s & 0x3f );
332             }
333             else
334                 *p++ = *s;
335         }
336         *p = 0;
337     }
338     return buffer;
339 }
340
341
342 /****************
343  * Convert string, which is in UTF8 to native encoding.
344  * illegal encodings by some "\xnn" and quote all control characters
345  */
346 char *
347 utf8_to_native( const char *string, size_t length )
348 {
349     int nleft;
350     int i;
351     byte encbuf[7];
352     int encidx;
353     const byte *s;
354     size_t n;
355     byte *buffer = NULL, *p = NULL;
356     unsigned long val = 0;
357     size_t slen;
358     int resync = 0;
359
360     /* 1. pass (p==NULL): count the extended utf-8 characters */
361     /* 2. pass (p!=NULL): create string */
362     for( ;; ) {
363         for( slen=length, nleft=encidx=0, n=0, s=string; slen; s++, slen-- ) {
364             if( resync ) {
365                 if( !(*s < 128 || (*s >= 0xc0 && *s <= 0xfd)) ) {
366                     /* still invalid */
367                     if( p ) {
368                         sprintf(p, "\\x%02x", *s );
369                         p += 4;
370                     }
371                     n += 4;
372                     continue;
373                 }
374                 resync = 0;
375             }
376             if( !nleft ) {
377                 if( !(*s & 0x80) ) { /* plain ascii */
378                     if( iscntrl( *s ) ) {
379                         n++;
380                         if( p )
381                             *p++ = '\\';
382                         switch( *s ) {
383                           case '\n': n++; if( p ) *p++ = 'n'; break;
384                           case '\r': n++; if( p ) *p++ = 'r'; break;
385                           case '\f': n++; if( p ) *p++ = 'f'; break;
386                           case '\v': n++; if( p ) *p++ = 'v'; break;
387                           case '\b': n++; if( p ) *p++ = 'b'; break;
388                           case   0 : n++; if( p ) *p++ = '0'; break;
389                           default: n += 3;
390                                    sprintf( p, "x%02x", *s );
391                                    p += 3;
392                                    break;
393                         }
394                     }
395                     else {
396                         if( p ) *p++ = *s;
397                         n++;
398                     }
399                 }
400                 else if( (*s & 0xe0) == 0xc0 ) { /* 110x xxxx */
401                     val = *s & 0x1f;
402                     nleft = 1;
403                     encbuf[encidx=0] = *s;
404                 }
405                 else if( (*s & 0xf0) == 0xe0 ) { /* 1110 xxxx */
406                     val = *s & 0x0f;
407                     nleft = 2;
408                     encbuf[encidx=0] = *s;
409                 }
410                 else if( (*s & 0xf8) == 0xf0 ) { /* 1111 0xxx */
411                     val = *s & 0x07;
412                     nleft = 3;
413                     encbuf[encidx=0] = *s;
414                 }
415                 else if( (*s & 0xfc) == 0xf8 ) { /* 1111 10xx */
416                     val = *s & 0x03;
417                     nleft = 4;
418                     encbuf[encidx=0] = *s;
419                 }
420                 else if( (*s & 0xfe) == 0xfc ) { /* 1111 110x */
421                     val = *s & 0x01;
422                     nleft = 5;
423                     encbuf[encidx=0] = *s;
424                 }
425                 else {  /* invalid encoding: print as \xnn */
426                     if( p ) {
427                         sprintf(p, "\\x%02x", *s );
428                         p += 4;
429                     }
430                     n += 4;
431                     resync = 1;
432                 }
433             }
434             else if( *s < 0x80 || *s >= 0xc0 ) { /* invalid */
435                 if( p ) {
436                     sprintf(p, "\\x%02x", *s );
437                     p += 4;
438                 }
439                 n += 4;
440                 nleft = 0;
441                 resync = 1;
442             }
443             else {
444                 encbuf[++encidx] = *s;
445                 val <<= 6;
446                 val |= *s & 0x3f;
447                 if( !--nleft ) { /* ready */
448                     if( active_charset ) { /* table lookup */
449                         for(i=0; i < 128; i++ ) {
450                             if( active_charset[i] == val )
451                                 break;
452                         }
453                         if( i < 128 ) { /* we can print this one */
454                             if( p ) *p++ = i+128;
455                             n++;
456                         }
457                         else { /* we do not have a translation: print utf8 */
458                             if( p ) {
459                                 for(i=0; i < encidx; i++ ) {
460                                     sprintf(p, "\\x%02x", encbuf[i] );
461                                     p += 4;
462                                 }
463                             }
464                             n += encidx*4;
465                         }
466                     }
467                     else { /* native set */
468                         if( val >= 0x80 && val < 256 ) {
469                             n++;    /* we can simply print this character */
470                             if( p ) *p++ = val;
471                         }
472                         else { /* we do not have a translation: print utf8 */
473                             if( p ) {
474                                 for(i=0; i < encidx; i++ ) {
475                                     sprintf(p, "\\x%02x", encbuf[i] );
476                                     p += 4;
477                                 }
478                             }
479                             n += encidx*4;
480                         }
481                     }
482
483                 }
484
485             }
486         }
487         if( !buffer ) { /* allocate the buffer after the first pass */
488             buffer = p = gcry_xmalloc( n + 1 );
489         }
490         else {
491             *p = 0; /* make a string */
492             return buffer;
493         }
494     }
495 }
496
497