/[winpt]/trunk/Src/wptUTF8.cpp
ViewVC logotype

Diff of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 181 by twoaday, Tue Mar 14 11:01:22 2006 UTC
# Line 1  Line 1 
1  /* wptUTF8.cpp - UTF8 conversation  /* wptUTF8.cpp - UTF8 conversation
2   *      Copyright (C) 1994, 1998-2001 Free Software Foundation, Inc.   *      Copyright (C) 1994, 1998-2001 Free Software Foundation, Inc.
3   *      Copyright (C) 2002, 2004 Timo Schulz   *      Copyright (C) 2002, 2004, 2005 Timo Schulz
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
8   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
9   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
21    
22    #ifdef HAVE_CONFIG_H
23    #include <config.h>
24    #endif
25    
26  #include <windows.h>  #include <windows.h>
27  #include <stdlib.h>  #include <stdlib.h>
28  #include <stdio.h>  #include <stdio.h>
29  #include <string.h>  #include <string.h>
30  #include <ctype.h>  #include <ctype.h>
31    
32  #include "wptTypes.h"  #include "wptTypes.h"
33  #include "wptErrors.h"  #include "wptErrors.h"
34    
 static u16 koi8_unicode[128] = {  
     0x2500,0x2502,0x250c,0x2510,0x2514,0x2518,0x251c,0x2524,  
     0x252c,0x2534,0x253c,0x2580,0x2584,0x2588,0x258c,0x2590,  
     0x2591,0x2592,0x2593,0x2320,0x25a0,0x2219,0x221a,0x2248,  
     0x2264,0x2265,0x00a0,0x2321,0x00b0,0x00b2,0x00b7,0x00f7,  
     0x2550,0x2551,0x2552,0x0451,0x2553,0x2554,0x2555,0x2556,  
     0x2557,0x2558,0x2559,0x255a,0x255b,0x255c,0x255d,0x255e,  
     0x255f,0x2560,0x2561,0x0401,0x2562,0x2563,0x2564,0x2565,  
     0x2566,0x2567,0x2568,0x2569,0x256a,0x256b,0x256c,0x00a9,  
     0x044e,0x0430,0x0431,0x0446,0x0434,0x0435,0x0444,0x0433,  
     0x0445,0x0438,0x0439,0x043a,0x043b,0x043c,0x043d,0x043e,  
     0x043f,0x044f,0x0440,0x0441,0x0442,0x0443,0x0436,0x0432,  
     0x044c,0x044b,0x0437,0x0448,0x044d,0x0449,0x0447,0x044a,  
     0x042e,0x0410,0x0411,0x0426,0x0414,0x0415,0x0424,0x0413,  
     0x0425,0x0418,0x0419,0x041a,0x041b,0x041c,0x041d,0x041e,  
     0x041f,0x042f,0x0420,0x0421,0x0422,0x0423,0x0416,0x0412,  
     0x042c,0x042b,0x0417,0x0428,0x042d,0x0429,0x0427,0x042a  
 };  
35    
36  static u16 latin2_unicode[128] = {  static WORD latin2_unicode[128] = {
37      0x0080,0x0081,0x0082,0x0083,0x0084,0x0085,0x0086,0x0087,      0x0080,0x0081,0x0082,0x0083,0x0084,0x0085,0x0086,0x0087,
38      0x0088,0x0089,0x008A,0x008B,0x008C,0x008D,0x008E,0x008F,      0x0088,0x0089,0x008A,0x008B,0x008C,0x008D,0x008E,0x008F,
39      0x0090,0x0091,0x0092,0x0093,0x0094,0x0095,0x0096,0x0097,      0x0090,0x0091,0x0092,0x0093,0x0094,0x0095,0x0096,0x0097,
# Line 68  static u16 latin2_unicode[128] = { Line 54  static u16 latin2_unicode[128] = {
54    
55    
56  static const char *active_charset_name = "iso-8859-1";  static const char *active_charset_name = "iso-8859-1";
57  static u16 *active_charset = NULL;  static WORD *active_charset = NULL;
58  static int no_translation = 0;  static int no_translation = 0;
59    
60  static int  
61  ascii_strcasecmp( const char *a, const char *b )  static int
62  {  ascii_strcasecmp( const char *a, const char *b )
63      if( a == b )  {
64          return 0;      if( a == b )
65            return 0;
66      for (; *a && *b; a++, b++) {  
67          if (*a != *b && toupper(*a) != toupper(*b))      for (; *a && *b; a++, b++) {
68              break;          if (*a != *b && toupper(*a) != toupper(*b))
69      }              break;
70            }
71      return *a == *b? 0 : (toupper (*a) - toupper (*b));      
72  } /* ascii_strcasecmp */      return *a == *b? 0 : (toupper (*a) - toupper (*b));
73    }
74    
75  int  int
76  set_native_charset( const char *newset )  set_native_charset( const char *newset )
77  {  {
78    if( !ascii_strcasecmp( newset, "iso-8859-1" ) ) {    if( !ascii_strcasecmp( newset, "iso-8859-1" ) ) {
79        active_charset_name = "iso-8859-1";        active_charset_name = "iso-8859-1";
80        no_translation = 0;        no_translation = 0;
81        active_charset = NULL;        active_charset = NULL;
82    }    }
83    else if( !ascii_strcasecmp( newset, "iso-8859-2" ) ) {    else if( !ascii_strcasecmp( newset, "iso-8859-2" ) ) {
84        active_charset_name = "iso-8859-2";        active_charset_name = "iso-8859-2";
       no_translation = 0;  
       active_charset = latin2_unicode;  
     }  
   else if( !ascii_strcasecmp( newset, "koi8-r" ) ) {  
       active_charset_name = "koi8-r";  
85        no_translation = 0;        no_translation = 0;
86        active_charset = koi8_unicode;        active_charset = latin2_unicode;
87    }    }
88    else if( !ascii_strcasecmp (newset, "utf8" )    else if( !ascii_strcasecmp (newset, "utf8" )
89             || !ascii_strcasecmp(newset, "utf-8") ) {             || !ascii_strcasecmp(newset, "utf-8") ) {
# Line 109  set_native_charset( const char *newset ) Line 91  set_native_charset( const char *newset )
91        no_translation = 1;        no_translation = 1;
92        active_charset = NULL;        active_charset = NULL;
93    }    }
94    else    else
95        return WPTERR_GENERAL;        return WPTERR_GENERAL;
96    
97    return 0;    return 0;
98  } /* set_native_charset */  }
99    
100  const char*  const char*
101  get_native_charset( void )  get_native_charset( void )
102  {        {      
103      return active_charset_name;      return active_charset_name;
104  } /* get_native_charset */  }
105    
106    
107  /****************  /****************
108   * Convert string, which is in native encoding to UTF8 and return the   * Convert string, which is in native encoding to UTF8 and return the
# Line 135  native_to_utf8( const char *string ) Line 118  native_to_utf8( const char *string )
118    
119    if (no_translation)        if (no_translation)    
120        buffer = strdup( string );        buffer = strdup( string );
121    else if( active_charset ) {          else if( active_charset ) {      
122        for(s=(byte*)string; *s; s++ ) {        for(s=(byte*)string; *s; s++ ) {
123            length++;            length++;
124            if( *s & 0x80 )            if( *s & 0x80 )
125                length += 2; /* we may need 3 bytes */                length += 2; /* we may need 3 bytes */
126        }        }
127        buffer = (char *)malloc( length + 1 );        buffer = (char *)malloc( length + 1 );
128        for(p=(byte *)buffer, s=(byte *)string; *s; s++ ) {        for(p=(byte *)buffer, s=(byte *)string; *s; s++ ) {
129            if( *s & 0x80 ) {                        if( *s & 0x80 ) {            
130                u16 val = active_charset[ *s & 0x7f ];                WORD val = active_charset[ *s & 0x7f ];
131                if( val < 0x0800 ) {                                if( val < 0x0800 ) {                
132                    *p++ = 0xc0 | ( (val >> 6) & 0x1f );                    *p++ = 0xc0 | ( (val >> 6) & 0x1f );
133                    *p++ = 0x80 | (  val & 0x3f );                    *p++ = 0x80 | (  val & 0x3f );
# Line 154  native_to_utf8( const char *string ) Line 137  native_to_utf8( const char *string )
137                    *p++ = 0x80 | ( (val >>  6) & 0x3f );                    *p++ = 0x80 | ( (val >>  6) & 0x3f );
138                    *p++ = 0x80 | (  val & 0x3f );                    *p++ = 0x80 | (  val & 0x3f );
139                }                }
140            }                      }          
141            else            else
142                *p++ = *s;                *p++ = *s;
143        }        }
144        *p = 0;        *p = 0;
145    }    }
146    else {    else {
147        for(s=(byte*)string; *s; s++ ) {        for(s=(byte*)string; *s; s++ ) {
148            length++;            length++;
149            if( *s & 0x80 )            if( *s & 0x80 )
150                length++;                length++;
151        }        }
152        buffer = (char*)malloc( length + 1 );        buffer = (char*)malloc( length + 1 );
# Line 172  native_to_utf8( const char *string ) Line 155  native_to_utf8( const char *string )
155                *p++ = 0xc0 | ((*s >> 6) & 3);                *p++ = 0xc0 | ((*s >> 6) & 3);
156                *p++ = 0x80 | ( *s & 0x3f );                *p++ = 0x80 | ( *s & 0x3f );
157            }            }
158            else            else
159                *p++ = *s;                *p++ = *s;
160        }        }
161        *p = 0;        *p = 0;
162    }    }
163    
164    return buffer;    return buffer;
165  } /* native_to_utf8 */  } /* native_to_utf8 */
166    
167  /****************  /****************
168   * Convert string, which is in UTF8 to native encoding.  illegal   * Convert string, which is in UTF8 to native encoding.  illegal
169   * encodings by some "\xnn" and quote all control characters. A   * encodings by some "\xnn" and quote all control characters. A
170   * character with value DELIM will always be quoted, it must be a   * character with value DELIM will always be quoted, it must be a
171   * vanilla ASCII character.     * vanilla ASCII character.  
172    */    */
173  char *  char *
174  utf8_to_native( const char *string, size_t length, int delim )  utf8_to_native( const char *string, size_t length, int delim )
175  {  {
176      int nleft;      int nleft;
177      int i;      int i;
178      byte encbuf[8];      byte encbuf[8];
179      int encidx;      int encidx;
180      const byte *s;      const byte *s;
181      size_t n;      size_t n;
182      byte *buffer = NULL, *p = NULL;      byte *buffer = NULL, *p = NULL;
183      unsigned long val = 0;      unsigned long val = 0;
184      size_t slen;      size_t slen;
185      int resync = 0;      int resync = 0;
186            
187      /* 1. pass (p==NULL): count the extended utf-8 characters */      /* 1. pass (p==NULL): count the extended utf-8 characters */
188      /* 2. pass (p!=NULL): create string */      /* 2. pass (p!=NULL): create string */
189      for( ;; ) {      for( ;; ) {
190          for( slen=length, nleft=encidx=0, n=0, s=(byte*)string; slen; s++, slen-- ) {          for( slen=length, nleft=encidx=0, n=0, s=(byte*)string; slen; s++, slen-- ) {
191              if( resync ) {              if( resync ) {
192                  if( !(*s < 128 || (*s >= 0xc0 && *s <= 0xfd)) ) {                  if( !(*s < 128 || (*s >= 0xc0 && *s <= 0xfd)) ) {
193                      /* still invalid */                      /* still invalid */
194                      if( p ) {                      if( p ) {
195                          sprintf((char*)p, "\\x%02x", *s );                          sprintf((char*)p, "\\x%02x", *s );
196                          p += 4;                          p += 4;
197                      }                      }
198                      n += 4;                      n += 4;
199                      continue;                      continue;
200                  }                  }
201                  resync = 0;                  resync = 0;
202              }              }
203              if( !nleft ) {              if( !nleft ) {
204                  if( !(*s & 0x80) ) { /* plain ascii */                  if( !(*s & 0x80) ) { /* plain ascii */
205                      if( *s < 0x20 || *s == 0x7f || *s == delim) {                      if( *s < 0x20 || *s == 0x7f || *s == delim) {
206                          n++;                          n++;
207                          if( p )                          if( p )
208                              *p++ = '\\';                              *p++ = '\\';
209                          switch( *s ) {                          switch( *s ) {
210                          case '\n': n++; if( p ) *p++ = 'n'; break;                          case '\n': n++; if( p ) *p++ = 'n'; break;
211                          case '\r': n++; if( p ) *p++ = 'r'; break;                          case '\r': n++; if( p ) *p++ = 'r'; break;
212                          case '\f': n++; if( p ) *p++ = 'f'; break;                          case '\f': n++; if( p ) *p++ = 'f'; break;
213                          case '\v': n++; if( p ) *p++ = 'v'; break;                          case '\v': n++; if( p ) *p++ = 'v'; break;
214                          case '\b': n++; if( p ) *p++ = 'b'; break;                          case '\b': n++; if( p ) *p++ = 'b'; break;
215                          case     0 : n++; if( p ) *p++ = '0'; break;                          case     0 : n++; if( p ) *p++ = '0'; break;
216                          default:                          default:
217                              n += 3;                              n += 3;
218                              if ( p ) {                              if ( p ) {
219                                  sprintf( (char*)p, "x%02x", *s );                                  sprintf( (char*)p, "x%02x", *s );
220                                  p += 3;                                  p += 3;
221                              }                              }
222                              break;                              break;
223                          }                          }
224                      }                      }
225                      else {                      else {
226                          if( p ) *p++ = *s;                          if( p ) *p++ = *s;
227                          n++;                          n++;
228                      }                      }
229                  }                  }
230                  else if( (*s & 0xe0) == 0xc0 ) { /* 110x xxxx */                  else if( (*s & 0xe0) == 0xc0 ) { /* 110x xxxx */
231                      val = *s & 0x1f;                      val = *s & 0x1f;
232                      nleft = 1;                      nleft = 1;
233                      encidx = 0;                      encidx = 0;
234                      encbuf[encidx++] = *s;                      encbuf[encidx++] = *s;
235                  }                  }
236                  else if( (*s & 0xf0) == 0xe0 ) { /* 1110 xxxx */                  else if( (*s & 0xf0) == 0xe0 ) { /* 1110 xxxx */
237                      val = *s & 0x0f;                      val = *s & 0x0f;
238                      nleft = 2;                      nleft = 2;
239                      encidx = 0;                      encidx = 0;
240                      encbuf[encidx++] = *s;                      encbuf[encidx++] = *s;
241                  }                  }
242                  else if( (*s & 0xf8) == 0xf0 ) { /* 1111 0xxx */                  else if( (*s & 0xf8) == 0xf0 ) { /* 1111 0xxx */
243                      val = *s & 0x07;                      val = *s & 0x07;
244                      nleft = 3;                      nleft = 3;
245                      encidx = 0;                      encidx = 0;
246                      encbuf[encidx++] = *s;                      encbuf[encidx++] = *s;
247                  }                  }
248                  else if( (*s & 0xfc) == 0xf8 ) { /* 1111 10xx */                  else if( (*s & 0xfc) == 0xf8 ) { /* 1111 10xx */
249                      val = *s & 0x03;                      val = *s & 0x03;
250                      nleft = 4;                      nleft = 4;
251                      encidx = 0;                      encidx = 0;
252                      encbuf[encidx++] = *s;                      encbuf[encidx++] = *s;
253                  }                  }
254                  else if( (*s & 0xfe) == 0xfc ) { /* 1111 110x */                  else if( (*s & 0xfe) == 0xfc ) { /* 1111 110x */
255                      val = *s & 0x01;                      val = *s & 0x01;
256                      nleft = 5;                      nleft = 5;
257                      encidx = 0;                      encidx = 0;
258                      encbuf[encidx++] = *s;                      encbuf[encidx++] = *s;
259                  }                  }
260                  else {  /* invalid encoding: print as \xnn */                  else {  /* invalid encoding: print as \xnn */
261                      if( p ) {                      if( p ) {
262                          sprintf((char*)p, "\\x%02x", *s );                          sprintf((char*)p, "\\x%02x", *s );
263                          p += 4;                          p += 4;
264                      }                      }
265                      n += 4;                      n += 4;
266                      resync = 1;                      resync = 1;
267                  }                  }
268              }              }
269              else if( *s < 0x80 || *s >= 0xc0 ) { /* invalid */              else if( *s < 0x80 || *s >= 0xc0 ) { /* invalid */
270                  if( p ) {                  if( p ) {
271                      for(i=0; i < encidx; i++ ) {                      for(i=0; i < encidx; i++ ) {
272                          sprintf((char*)p, "\\x%02x", encbuf[i] );                          sprintf((char*)p, "\\x%02x", encbuf[i] );
273                          p += 4;                          p += 4;
274                      }                      }
275                      sprintf((char*)p, "\\x%02x", *s );                      sprintf((char*)p, "\\x%02x", *s );
276                      p += 4;                      p += 4;
277                  }                  }
278                  n += 4 + 4*encidx;                  n += 4 + 4*encidx;
279                  nleft = 0;                  nleft = 0;
280                  encidx = 0;                  encidx = 0;
281                  resync = 1;                  resync = 1;
282              }              }
283              else {              else {
284                  encbuf[encidx++] = *s;                  encbuf[encidx++] = *s;
285                  val <<= 6;                  val <<= 6;
286                  val |= *s & 0x3f;                  val |= *s & 0x3f;
287                  if( !--nleft ) { /* ready */                  if( !--nleft ) { /* ready */
288                      if (no_translation) {                      if (no_translation) {
289                          if( p ) {                          if( p ) {
290                              for(i=0; i < encidx; i++ )                              for(i=0; i < encidx; i++ )
291                                  *p++ = encbuf[i];                                  *p++ = encbuf[i];
292                          }                          }
293                          n += encidx;                          n += encidx;
294                          encidx = 0;                          encidx = 0;
295                      }                      }
296                      else if( active_charset ) { /* table lookup */                      else if( active_charset ) { /* table lookup */
297                          for(i=0; i < 128; i++ ) {                          for(i=0; i < 128; i++ ) {
298                              if( active_charset[i] == val )                              if( active_charset[i] == val )
299                                  break;                                  break;
300                          }                          }
301                          if( i < 128 ) { /* we can print this one */                          if( i < 128 ) { /* we can print this one */
302                              if( p ) *p++ = i+128;                              if( p ) *p++ = i+128;
303                              n++;                              n++;
304                          }                          }
305                          else { /* we do not have a translation: print utf8 */                          else { /* we do not have a translation: print utf8 */
306                              if( p ) {                              if( p ) {
307                                  for(i=0; i < encidx; i++ ) {                                  for(i=0; i < encidx; i++ ) {
308                                      sprintf((char*)p, "\\x%02x", encbuf[i] );                                      sprintf((char*)p, "\\x%02x", encbuf[i] );
309                                      p += 4;                                      p += 4;
310                                  }                                  }
311                              }                              }
312                              n += encidx*4;                              n += encidx*4;
313                              encidx = 0;                              encidx = 0;
314                          }                          }
315                      }                      }
316                      else { /* native set */                      else { /* native set */
317                          if( val >= 0x80 && val < 256 ) {                          if( val >= 0x80 && val < 256 ) {
318                              n++;    /* we can simply print this character */                              n++;    /* we can simply print this character */
319                              if( p ) *p++ = val;                              if( p ) *p++ = val;
320                          }                          }
321                          else { /* we do not have a translation: print utf8 */                          else { /* we do not have a translation: print utf8 */
322                              if( p ) {                              if( p ) {
323                                  for(i=0; i < encidx; i++ ) {                                  for(i=0; i < encidx; i++ ) {
324                                      sprintf((char*)p, "\\x%02x", encbuf[i] );                                      sprintf((char*)p, "\\x%02x", encbuf[i] );
325                                      p += 4;                                      p += 4;
326                                  }                                  }
327                              }                              }
328                              n += encidx*4;                              n += encidx*4;
329                              encidx = 0;                              encidx = 0;
330                          }                          }
331                      }                      }
332                  }                  }
333                                    
334              }              }
335          }          }
336          if( !buffer ) { /* allocate the buffer after the first pass */          if( !buffer ) { /* allocate the buffer after the first pass */
337              buffer = p = (byte *)malloc( n + 1 );              buffer = p = (byte *)malloc( n + 1 );
338          }          }
339          else {          else {
340              *p = 0; /* make a string */              *p = 0; /* make a string */
341              return (char*)buffer;              return (char*)buffer;
342          }          }
343      }      }
344    }
345    
346    
347    static void
348    conv_charset (byte *string, size_t size, int what)
349    {
350        int i;
351        
352        if( what == 0 ) {
353            for( i = 0; i < size; i++, string++ ) {
354                switch( *string ) {
355                case 0xa0: *string = 0xff; break;  /* nobreakspace */
356                case 0xa1: *string = 0xad; break;  /* exclamdown */
357                case 0xa2: *string = 0xbd; break;  /* cent */
358                case 0xa3: *string = 0x9c; break;  /* sterling */
359                case 0xa4: *string = 0xcf; break;  /* currency */
360                case 0xa5: *string = 0xbe; break;  /* yen */
361                case 0xa6: *string = 0xdd; break;  /* brokenbar */
362                case 0xa7: *string = 0xf5; break;  /* section */
363                case 0xa8: *string = 0xf9; break;  /* diaeresis */
364                case 0xa9: *string = 0xb8; break;  /* copyright */
365                case 0xaa: *string = 0xa6; break;  /* ordfeminine */
366                case 0xab: *string = 0xae; break;  /* guillemotleft */
367                case 0xac: *string = 0xaa; break;  /* notsign */
368                case 0xad: *string = 0xf0; break;  /* hyphen */
369                case 0xae: *string = 0xa9; break;  /* registered */
370                case 0xaf: *string = 0xee; break;  /* macron */
371                case 0xb0: *string = 0xf8; break;  /* degree */
372                case 0xb1: *string = 0xf1; break;  /* plusminus */
373                case 0xb2: *string = 0xfd; break;  /* twosuperior */
374                case 0xb3: *string = 0xfc; break;  /* threesuperior */
375                case 0xb4: *string = 0xef; break;  /* acute */
376                case 0xb5: *string = 0xe6; break;  /* mu */
377                case 0xb6: *string = 0xf4; break;  /* paragraph */
378                case 0xb7: *string = 0xfa; break;  /* periodcentered */
379                case 0xb8: *string = 0xf7; break;  /* cedilla */
380                case 0xb9: *string = 0xfb; break;  /* onesuperior */
381                case 0xba: *string = 0xa7; break;  /* masculine */
382                case 0xbb: *string = 0xaf; break;  /* guillemotright */
383                case 0xbc: *string = 0xac; break;  /* onequarter */
384                case 0xbd: *string = 0xab; break;  /* onehalf */
385                case 0xbe: *string = 0xf3; break;  /* threequarters */
386                case 0xbf: *string = 0xa8; break;  /* questiondown */
387                case 0xc0: *string = 0xb7; break;  /* Agrave */
388                case 0xc1: *string = 0xb5; break;  /* Aacute */
389                case 0xc2: *string = 0xb6; break;  /* Acircumflex */
390                case 0xc3: *string = 0xc7; break;  /* Atilde */
391                case 0xc4: *string = 0x8e; break;  /* Adiaeresis */
392                case 0xc5: *string = 0x8f; break;  /* Aring */
393                case 0xc6: *string = 0x92; break;  /* AE */
394                case 0xc7: *string = 0x80; break;  /* Ccedilla */
395                case 0xc8: *string = 0xd4; break;  /* Egrave */
396                case 0xc9: *string = 0x90; break;  /* Eacute */
397                case 0xca: *string = 0xd2; break;  /* Ecircumflex */
398                case 0xcb: *string = 0xd3; break;  /* Ediaeresis */
399                case 0xcc: *string = 0xde; break;  /* Igrave */
400                case 0xcd: *string = 0xd6; break;  /* Iacute */
401                case 0xce: *string = 0xd7; break;  /* Icircumflex */
402                case 0xcf: *string = 0xd8; break;  /* Idiaeresis */
403                case 0xd0: *string = 0xd1; break;  /* Eth */
404                case 0xd1: *string = 0xa5; break;  /* Ntilde */
405                case 0xd2: *string = 0xe3; break;  /* Ograve */
406                case 0xd3: *string = 0xe0; break;  /* Oacute */
407                case 0xd4: *string = 0xe2; break;  /* Ocircumflex */
408                case 0xd5: *string = 0xe5; break;  /* Otilde */
409                case 0xd6: *string = 0x99; break;  /* Odiaeresis */
410                case 0xd7: *string = 0x9e; break;  /* multiply */
411                case 0xd8: *string = 0x9d; break;  /* Ooblique */
412                case 0xd9: *string = 0xeb; break;  /* Ugrave */
413                case 0xda: *string = 0xe9; break;  /* Uacute */
414                case 0xdb: *string = 0xea; break;  /* Ucircumflex */
415                case 0xdc: *string = 0x9a; break;  /* Udiaeresis */
416                case 0xdd: *string = 0xed; break;  /* Yacute */
417                case 0xde: *string = 0xe8; break;  /* Thorn */
418                case 0xdf: *string = 0xe1; break;  /* ssharp */
419                case 0xe0: *string = 0x85; break;  /* agrave */
420                case 0xe1: *string = 0xa0; break;  /* aacute */
421                case 0xe2: *string = 0x83; break;  /* acircumflex */
422                case 0xe3: *string = 0xc6; break;  /* atilde */
423                case 0xe4: *string = 0x84; break;  /* adiaeresis */
424                case 0xe5: *string = 0x86; break;  /* aring */
425                case 0xe6: *string = 0x91; break;  /* ae */
426                case 0xe7: *string = 0x87; break;  /* ccedilla */
427                case 0xe8: *string = 0x8a; break;  /* egrave */
428                case 0xe9: *string = 0x82; break;  /* eacute */
429                case 0xea: *string = 0x88; break;  /* ecircumflex */
430                case 0xeb: *string = 0x89; break;  /* ediaeresis */
431                case 0xec: *string = 0x8d; break;  /* igrave */
432                case 0xed: *string = 0xa1; break;  /* iacute */
433                case 0xee: *string = 0x8c; break;  /* icircumflex */
434                case 0xef: *string = 0x8b; break;  /* idiaeresis */
435                case 0xf0: *string = 0xd0; break;  /* eth */
436                case 0xf1: *string = 0xa4; break;  /* ntilde */
437                case 0xf2: *string = 0x95; break;  /* ograve */
438                case 0xf3: *string = 0xa2; break;  /* oacute */
439                case 0xf4: *string = 0x93; break;  /* ocircumflex */
440                case 0xf5: *string = 0xe4; break;  /* otilde */
441                case 0xf6: *string = 0x94; break;  /* odiaeresis */
442                case 0xf7: *string = 0xf6; break;  /* division */
443                case 0xf8: *string = 0x9b; break;  /* oslash */
444                case 0xf9: *string = 0x97; break;  /* ugrave */
445                case 0xfa: *string = 0xa3; break;  /* uacute */
446                case 0xfb: *string = 0x96; break;  /* ucircumflex */
447                case 0xfc: *string = 0x81; break;  /* udiaeresis */
448                case 0xfd: *string = 0xec; break;  /* yacute */
449                case 0xfe: *string = 0xe7; break;  /* thorn */
450                case 0xff: *string = 0x98; break;  /* ydiaeresis */
451                default  :  break;
452                }
453            }
454        }
455        else {
456            for( i = 0; i < size; i++, string++ ) {
457                switch( *string ) {
458                case  0xff: *string = 0xa0; break;
459                case  0xad: *string = 0xa1; break;
460                case  0xbd: *string = 0xa2; break;
461                case  0x9c: *string = 0xa3; break;
462                case  0xcf: *string = 0xa4; break;
463                case  0xbe: *string = 0xa5; break;
464                case  0xdd: *string = 0xa6; break;
465                case  0xf5: *string = 0xa7; break;
466                case  0xf9: *string = 0xa8; break;
467                case  0xb8: *string = 0xa9; break;
468                case  0xa6: *string = 0xaa; break;
469                case  0xae: *string = 0xab; break;
470                case  0xaa: *string = 0xac; break;
471                case  0xf0: *string = 0xad; break;
472                case  0xa9: *string = 0xae; break;
473                case  0xee: *string = 0xaf; break;
474                case  0xf8: *string = 0xb0; break;
475                case  0xf1: *string = 0xb1; break;
476                case  0xfd: *string = 0xb2; break;
477                case  0xfc: *string = 0xb3; break;
478                case  0xef: *string = 0xb4; break;
479                case  0xe6: *string = 0xb5; break;
480                case  0xf4: *string = 0xb6; break;
481                case  0xfa: *string = 0xb7; break;
482                case  0xf7: *string = 0xb8; break;
483                case  0xfb: *string = 0xb9; break;
484                case  0xa7: *string = 0xba; break;
485                case  0xaf: *string = 0xbb; break;
486                case  0xac: *string = 0xbc; break;
487                case  0xab: *string = 0xbd; break;
488                case  0xf3: *string = 0xbe; break;
489                case  0xa8: *string = 0xbf; break;
490                case  0xb7: *string = 0xc0; break;
491                case  0xb5: *string = 0xc1; break;
492                case  0xb6: *string = 0xc2; break;
493                case  0xc7: *string = 0xc3; break;
494                case  0x8e: *string = 0xc4; break;
495                case  0x8f: *string = 0xc5; break;
496                case  0x92: *string = 0xc6; break;
497                case  0x80: *string = 0xc7; break;
498                case  0xd4: *string = 0xc8; break;
499                case  0x90: *string = 0xc9; break;
500                case  0xd2: *string = 0xca; break;
501                case  0xd3: *string = 0xcb; break;
502                case  0xde: *string = 0xcc; break;
503                case  0xd6: *string = 0xcd; break;
504                case  0xd7: *string = 0xce; break;
505                case  0xd8: *string = 0xcf; break;
506                case  0xd1: *string = 0xd0; break;
507                case  0xa5: *string = 0xd1; break;
508                case  0xe3: *string = 0xd2; break;
509                case  0xe0: *string = 0xd3; break;
510                case  0xe2: *string = 0xd4; break;
511                case  0xe5: *string = 0xd5; break;
512                case  0x99: *string = 0xd6; break;
513                case  0x9e: *string = 0xd7; break;
514                case  0x9d: *string = 0xd8; break;
515                case  0xeb: *string = 0xd9; break;
516                case  0xe9: *string = 0xda; break;
517                case  0xea: *string = 0xdb; break;
518                case  0x9a: *string = 0xdc; break;
519                case  0xed: *string = 0xdd; break;
520                case  0xe8: *string = 0xde; break;
521                case  0xe1: *string = 0xdf; break;
522                case  0x85: *string = 0xe0; break;
523                case  0xa0: *string = 0xe1; break;
524                case  0x83: *string = 0xe2; break;
525                case  0xc6: *string = 0xe3; break;
526                case  0x84: *string = 0xe4; break;
527                case  0x86: *string = 0xe5; break;
528                case  0x91: *string = 0xe6; break;
529                case  0x87: *string = 0xe7; break;
530                case  0x8a: *string = 0xe8; break;
531                case  0x82: *string = 0xe9; break;
532                case  0x88: *string = 0xea; break;
533                case  0x89: *string = 0xeb; break;
534                case  0x8d: *string = 0xec; break;
535                case  0xa1: *string = 0xed; break;
536                case  0x8c: *string = 0xee; break;
537                case  0x8b: *string = 0xef; break;
538                case  0xd0: *string = 0xf0; break;
539                case  0xa4: *string = 0xf1; break;
540                case  0x95: *string = 0xf2; break;
541                case  0xa2: *string = 0xf3; break;
542                case  0x93: *string = 0xf4; break;
543                case  0xe4: *string = 0xf5; break;
544                case  0x94: *string = 0xf6; break;
545                case  0xf6: *string = 0xf7; break;
546                case  0x9b: *string = 0xf8; break;
547                case  0x97: *string = 0xf9; break;
548                case  0xa3: *string = 0xfa; break;
549                case  0x96: *string = 0xfb; break;
550                case  0x81: *string = 0xfc; break;
551                case  0xec: *string = 0xfd; break;
552                case  0xe7: *string = 0xfe; break;
553                case  0x98: *string = 0xff; break;
554                default  :  break;
555                }
556            }
557        }
558    } /* conv_charset */
559    
560    
561    /* XXX: the conv_charset() call fails when the user-id was created
562            with iso-8859-1 but it is assumed that CP850 (gpg console) is used. */
563    
564    char*
565    utf8_to_wincp (const char * s, size_t len)
566    {
567        char *decs;
568        decs = utf8_to_native (s, len, 0);
569        conv_charset ((byte *)decs, strlen (decs), 1);
570        return decs;
571    }
572    
573    
574    char*
575    wincp_to_utf8 (const char * s, size_t len)
576    {
577        char * encs;
578        conv_charset ((byte *)s, len, 0);
579        encs = native_to_utf8 (s);
580        return encs;
581    }
582    
583    
584    int
585    is_8bit_string (const char * str)
586    {
587        size_t i;
588    
589        for (i = 0; i < strlen (str); i++) {
590            if (str[i] & 0x80)
591                return -1;
592        }
593        return 0;
594  }  }
   
   
 static void  
 conv_charset (byte *string, size_t size, int what)  
 {  
     int i;  
       
     if( what == 0 ) {  
         for( i = 0; i < size; i++, string++ ) {  
             switch( *string ) {  
             case 0xa0: *string = 0xff; break;  /* nobreakspace */  
             case 0xa1: *string = 0xad; break;  /* exclamdown */  
             case 0xa2: *string = 0xbd; break;  /* cent */  
             case 0xa3: *string = 0x9c; break;  /* sterling */  
             case 0xa4: *string = 0xcf; break;  /* currency */  
             case 0xa5: *string = 0xbe; break;  /* yen */  
             case 0xa6: *string = 0xdd; break;  /* brokenbar */  
             case 0xa7: *string = 0xf5; break;  /* section */  
             case 0xa8: *string = 0xf9; break;  /* diaeresis */  
             case 0xa9: *string = 0xb8; break;  /* copyright */  
             case 0xaa: *string = 0xa6; break;  /* ordfeminine */  
             case 0xab: *string = 0xae; break;  /* guillemotleft */  
             case 0xac: *string = 0xaa; break;  /* notsign */  
             case 0xad: *string = 0xf0; break;  /* hyphen */  
             case 0xae: *string = 0xa9; break;  /* registered */  
             case 0xaf: *string = 0xee; break;  /* macron */  
             case 0xb0: *string = 0xf8; break;  /* degree */  
             case 0xb1: *string = 0xf1; break;  /* plusminus */  
             case 0xb2: *string = 0xfd; break;  /* twosuperior */  
             case 0xb3: *string = 0xfc; break;  /* threesuperior */  
             case 0xb4: *string = 0xef; break;  /* acute */  
             case 0xb5: *string = 0xe6; break;  /* mu */  
             case 0xb6: *string = 0xf4; break;  /* paragraph */  
             case 0xb7: *string = 0xfa; break;  /* periodcentered */  
             case 0xb8: *string = 0xf7; break;  /* cedilla */  
             case 0xb9: *string = 0xfb; break;  /* onesuperior */  
             case 0xba: *string = 0xa7; break;  /* masculine */  
             case 0xbb: *string = 0xaf; break;  /* guillemotright */  
             case 0xbc: *string = 0xac; break;  /* onequarter */  
             case 0xbd: *string = 0xab; break;  /* onehalf */  
             case 0xbe: *string = 0xf3; break;  /* threequarters */  
             case 0xbf: *string = 0xa8; break;  /* questiondown */  
             case 0xc0: *string = 0xb7; break;  /* Agrave */  
             case 0xc1: *string = 0xb5; break;  /* Aacute */  
             case 0xc2: *string = 0xb6; break;  /* Acircumflex */  
             case 0xc3: *string = 0xc7; break;  /* Atilde */  
             case 0xc4: *string = 0x8e; break;  /* Adiaeresis */  
             case 0xc5: *string = 0x8f; break;  /* Aring */  
             case 0xc6: *string = 0x92; break;  /* AE */  
             case 0xc7: *string = 0x80; break;  /* Ccedilla */  
             case 0xc8: *string = 0xd4; break;  /* Egrave */  
             case 0xc9: *string = 0x90; break;  /* Eacute */  
             case 0xca: *string = 0xd2; break;  /* Ecircumflex */  
             case 0xcb: *string = 0xd3; break;  /* Ediaeresis */  
             case 0xcc: *string = 0xde; break;  /* Igrave */  
             case 0xcd: *string = 0xd6; break;  /* Iacute */  
             case 0xce: *string = 0xd7; break;  /* Icircumflex */  
             case 0xcf: *string = 0xd8; break;  /* Idiaeresis */  
             case 0xd0: *string = 0xd1; break;  /* Eth */  
             case 0xd1: *string = 0xa5; break;  /* Ntilde */  
             case 0xd2: *string = 0xe3; break;  /* Ograve */  
             case 0xd3: *string = 0xe0; break;  /* Oacute */  
             case 0xd4: *string = 0xe2; break;  /* Ocircumflex */  
             case 0xd5: *string = 0xe5; break;  /* Otilde */  
             case 0xd6: *string = 0x99; break;  /* Odiaeresis */  
             case 0xd7: *string = 0x9e; break;  /* multiply */  
             case 0xd8: *string = 0x9d; break;  /* Ooblique */  
             case 0xd9: *string = 0xeb; break;  /* Ugrave */  
             case 0xda: *string = 0xe9; break;  /* Uacute */  
             case 0xdb: *string = 0xea; break;  /* Ucircumflex */  
             case 0xdc: *string = 0x9a; break;  /* Udiaeresis */  
             case 0xdd: *string = 0xed; break;  /* Yacute */  
             case 0xde: *string = 0xe8; break;  /* Thorn */  
             case 0xdf: *string = 0xe1; break;  /* ssharp */  
             case 0xe0: *string = 0x85; break;  /* agrave */  
             case 0xe1: *string = 0xa0; break;  /* aacute */  
             case 0xe2: *string = 0x83; break;  /* acircumflex */  
             case 0xe3: *string = 0xc6; break;  /* atilde */  
             case 0xe4: *string = 0x84; break;  /* adiaeresis */  
             case 0xe5: *string = 0x86; break;  /* aring */  
             case 0xe6: *string = 0x91; break;  /* ae */  
             case 0xe7: *string = 0x87; break;  /* ccedilla */  
             case 0xe8: *string = 0x8a; break;  /* egrave */  
             case 0xe9: *string = 0x82; break;  /* eacute */  
             case 0xea: *string = 0x88; break;  /* ecircumflex */  
             case 0xeb: *string = 0x89; break;  /* ediaeresis */  
             case 0xec: *string = 0x8d; break;  /* igrave */  
             case 0xed: *string = 0xa1; break;  /* iacute */  
             case 0xee: *string = 0x8c; break;  /* icircumflex */  
             case 0xef: *string = 0x8b; break;  /* idiaeresis */  
             case 0xf0: *string = 0xd0; break;  /* eth */  
             case 0xf1: *string = 0xa4; break;  /* ntilde */  
             case 0xf2: *string = 0x95; break;  /* ograve */  
             case 0xf3: *string = 0xa2; break;  /* oacute */  
             case 0xf4: *string = 0x93; break;  /* ocircumflex */  
             case 0xf5: *string = 0xe4; break;  /* otilde */  
             case 0xf6: *string = 0x94; break;  /* odiaeresis */  
             case 0xf7: *string = 0xf6; break;  /* division */  
             case 0xf8: *string = 0x9b; break;  /* oslash */  
             case 0xf9: *string = 0x97; break;  /* ugrave */  
             case 0xfa: *string = 0xa3; break;  /* uacute */  
             case 0xfb: *string = 0x96; break;  /* ucircumflex */  
             case 0xfc: *string = 0x81; break;  /* udiaeresis */  
             case 0xfd: *string = 0xec; break;  /* yacute */  
             case 0xfe: *string = 0xe7; break;  /* thorn */  
             case 0xff: *string = 0x98; break;  /* ydiaeresis */  
             default  :  break;  
             }  
         }  
     }  
     else {  
         for( i = 0; i < size; i++, string++ ) {  
             switch( *string ) {  
             case  0xff: *string = 0xa0; break;  
             case  0xad: *string = 0xa1; break;  
             case  0xbd: *string = 0xa2; break;  
             case  0x9c: *string = 0xa3; break;  
             case  0xcf: *string = 0xa4; break;  
             case  0xbe: *string = 0xa5; break;  
             case  0xdd: *string = 0xa6; break;  
             case  0xf5: *string = 0xa7; break;  
             case  0xf9: *string = 0xa8; break;  
             case  0xb8: *string = 0xa9; break;  
             case  0xa6: *string = 0xaa; break;  
             case  0xae: *string = 0xab; break;  
             case  0xaa: *string = 0xac; break;  
             case  0xf0: *string = 0xad; break;  
             case  0xa9: *string = 0xae; break;  
             case  0xee: *string = 0xaf; break;  
             case  0xf8: *string = 0xb0; break;  
             case  0xf1: *string = 0xb1; break;  
             case  0xfd: *string = 0xb2; break;  
             case  0xfc: *string = 0xb3; break;  
             case  0xef: *string = 0xb4; break;  
             case  0xe6: *string = 0xb5; break;  
             case  0xf4: *string = 0xb6; break;  
             case  0xfa: *string = 0xb7; break;  
             case  0xf7: *string = 0xb8; break;  
             case  0xfb: *string = 0xb9; break;  
             case  0xa7: *string = 0xba; break;  
             case  0xaf: *string = 0xbb; break;  
             case  0xac: *string = 0xbc; break;  
             case  0xab: *string = 0xbd; break;  
             case  0xf3: *string = 0xbe; break;  
             case  0xa8: *string = 0xbf; break;  
             case  0xb7: *string = 0xc0; break;  
             case  0xb5: *string = 0xc1; break;  
             case  0xb6: *string = 0xc2; break;  
             case  0xc7: *string = 0xc3; break;  
             case  0x8e: *string = 0xc4; break;  
             case  0x8f: *string = 0xc5; break;  
             case  0x92: *string = 0xc6; break;  
             case  0x80: *string = 0xc7; break;  
             case  0xd4: *string = 0xc8; break;  
             case  0x90: *string = 0xc9; break;  
             case  0xd2: *string = 0xca; break;  
             case  0xd3: *string = 0xcb; break;  
             case  0xde: *string = 0xcc; break;  
             case  0xd6: *string = 0xcd; break;  
             case  0xd7: *string = 0xce; break;  
             case  0xd8: *string = 0xcf; break;  
             case  0xd1: *string = 0xd0; break;  
             case  0xa5: *string = 0xd1; break;  
             case  0xe3: *string = 0xd2; break;  
             case  0xe0: *string = 0xd3; break;  
             case  0xe2: *string = 0xd4; break;  
             case  0xe5: *string = 0xd5; break;  
             case  0x99: *string = 0xd6; break;  
             case  0x9e: *string = 0xd7; break;  
             case  0x9d: *string = 0xd8; break;  
             case  0xeb: *string = 0xd9; break;  
             case  0xe9: *string = 0xda; break;  
             case  0xea: *string = 0xdb; break;  
             case  0x9a: *string = 0xdc; break;  
             case  0xed: *string = 0xdd; break;  
             case  0xe8: *string = 0xde; break;  
             case  0xe1: *string = 0xdf; break;  
             case  0x85: *string = 0xe0; break;  
             case  0xa0: *string = 0xe1; break;  
             case  0x83: *string = 0xe2; break;  
             case  0xc6: *string = 0xe3; break;  
             case  0x84: *string = 0xe4; break;  
             case  0x86: *string = 0xe5; break;  
             case  0x91: *string = 0xe6; break;  
             case  0x87: *string = 0xe7; break;  
             case  0x8a: *string = 0xe8; break;  
             case  0x82: *string = 0xe9; break;  
             case  0x88: *string = 0xea; break;  
             case  0x89: *string = 0xeb; break;  
             case  0x8d: *string = 0xec; break;  
             case  0xa1: *string = 0xed; break;  
             case  0x8c: *string = 0xee; break;  
             case  0x8b: *string = 0xef; break;  
             case  0xd0: *string = 0xf0; break;  
             case  0xa4: *string = 0xf1; break;  
             case  0x95: *string = 0xf2; break;  
             case  0xa2: *string = 0xf3; break;  
             case  0x93: *string = 0xf4; break;  
             case  0xe4: *string = 0xf5; break;  
             case  0x94: *string = 0xf6; break;  
             case  0xf6: *string = 0xf7; break;  
             case  0x9b: *string = 0xf8; break;  
             case  0x97: *string = 0xf9; break;  
             case  0xa3: *string = 0xfa; break;  
             case  0x96: *string = 0xfb; break;  
             case  0x81: *string = 0xfc; break;  
             case  0xec: *string = 0xfd; break;  
             case  0xe7: *string = 0xfe; break;  
             case  0x98: *string = 0xff; break;  
             default  :  break;  
             }  
         }  
     }  
 } /* conv_charset */  
   
   
 char *  
 utf8_to_wincp (const char * s, size_t len)  
 {  
     char * decs;  
     decs = utf8_to_native (s, len, 0);  
     conv_charset ((byte *)decs, strlen (decs), 1);  
     return decs;  
 }  
   
   
 char *  
 wincp_to_utf8 (const char * s, size_t len)  
 {  
     char * encs;  
     conv_charset ((byte *)s, len, 0);  
     encs = native_to_utf8 (s);  
     return encs;  
 }  
   
   
 int  
 is_8bit_string (const char * str)  
 {  
     size_t i;  
   
     for (i = 0; i < strlen (str); i++) {  
         if (str[i] & 0x80)  
             return -1;  
     }  
     return 0;  
 } /* is_8bit_string */  

Legend:
Removed from v.2  
changed lines
  Added in v.181

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26