| 1 | /* |
| 2 | ** Copyright 2000-2003 Double Precision, Inc. |
| 3 | ** See COPYING for distribution information. |
| 4 | ** |
| 5 | ** $Id: iso8859.c,v 1.3 2003/03/07 00:47:31 mrsam Exp $ |
| 6 | */ |
| 7 | |
| 8 | #include "unicode_config.h" |
| 9 | #include "unicode.h" |
| 10 | #include <string.h> |
| 11 | #include <stdlib.h> |
| 12 | |
| 13 | /* ISO8859 charsets all share the same functions */ |
| 14 | |
| 15 | unicode_char *unicode_iso8859_c2u(const char *str, int *err, |
| 16 | const unicode_char *table) |
| 17 | { |
| 18 | size_t l=strlen(str); |
| 19 | unicode_char *p=(unicode_char *)malloc((l+1) * sizeof(unicode_char)); |
| 20 | |
| 21 | if (err) |
| 22 | *err= -1; |
| 23 | |
| 24 | if (!p) |
| 25 | return (0); |
| 26 | |
| 27 | for (l=0; str[l]; l++) |
| 28 | { |
| 29 | unicode_char c=(int)(unsigned char)str[l]; |
| 30 | |
| 31 | c= c < 128 ? c:table[c & 0x7F]; |
| 32 | |
| 33 | if (!c) |
| 34 | { |
| 35 | if (err) |
| 36 | { |
| 37 | *err=l; |
| 38 | free(p); |
| 39 | return (0); |
| 40 | } |
| 41 | c=(int)(unsigned char)str[l]; |
| 42 | } |
| 43 | p[l]=c; |
| 44 | } |
| 45 | p[l]=0; |
| 46 | return (p); |
| 47 | } |
| 48 | |
| 49 | char *unicode_iso8859_u2c(const unicode_char *uc, int *errflag, |
| 50 | const unicode_char *tab) |
| 51 | { |
| 52 | size_t l; |
| 53 | char *p; |
| 54 | |
| 55 | for (l=0; uc[l]; l++) |
| 56 | ; |
| 57 | |
| 58 | if (errflag) *errflag= -1; |
| 59 | p=malloc(l+1); |
| 60 | if (!p) |
| 61 | return (0); |
| 62 | |
| 63 | for (l=0; uc[l]; l++) |
| 64 | { |
| 65 | int c; |
| 66 | unicode_char ucc=uc[l]; |
| 67 | |
| 68 | /* First, guess */ |
| 69 | |
| 70 | if ((ucc & 0x7F) == ucc) |
| 71 | c=(unsigned char)ucc; |
| 72 | else if (tab[ ucc & 0x7F ] == uc[l]) |
| 73 | c=(int)(ucc & 0x7F) | 0x80; |
| 74 | else |
| 75 | { |
| 76 | for (c=0; c<128; c++) |
| 77 | if (tab[c] == uc[l]) |
| 78 | break; |
| 79 | if (c >= 128) |
| 80 | { |
| 81 | if (errflag) |
| 82 | { |
| 83 | *errflag=l; |
| 84 | free(p); |
| 85 | return (0); |
| 86 | } |
| 87 | c=uc[l]; |
| 88 | } |
| 89 | c |= 0x80; |
| 90 | } |
| 91 | if (c == 0) |
| 92 | c=255; |
| 93 | p[l]=(char)c; |
| 94 | } |
| 95 | p[l]=0; |
| 96 | return (p); |
| 97 | } |