| 1 | /* |
| 2 | ** Copyright 2001-2003 Double Precision, Inc. |
| 3 | ** See COPYING for distribution information. |
| 4 | ** |
| 5 | ** $Id: windows874u.c,v 1.2 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 | char *unicode_windows874_u2c(const unicode_char *uc, int *errflag, |
| 14 | const unicode_char *tab) |
| 15 | { |
| 16 | size_t l; |
| 17 | char *p; |
| 18 | |
| 19 | for (l=0; uc[l]; l++) |
| 20 | ; |
| 21 | |
| 22 | if (errflag) *errflag= -1; |
| 23 | p=malloc(l+1); |
| 24 | if (!p) |
| 25 | return (0); |
| 26 | |
| 27 | for (l=0; uc[l]; l++) |
| 28 | { |
| 29 | int c; |
| 30 | unicode_char ucc=uc[l]; |
| 31 | |
| 32 | /* First, guess */ |
| 33 | |
| 34 | if ((ucc & 0x7F) == ucc) |
| 35 | c=(unsigned char)ucc; |
| 36 | else if (tab[ ucc & 0x7F ] == ucc) |
| 37 | c=(int)(ucc & 0x7F) | 0x80; |
| 38 | else if (ucc > 0x0E00 && ucc < 0x0E60 && |
| 39 | tab[ (ucc + 0x20) & 0x7F] == ucc) |
| 40 | c=(int)( (ucc + 0x20) & 0x7F) | 0x80; |
| 41 | else |
| 42 | { |
| 43 | for (c=0; c<128; c++) |
| 44 | if (tab[c] == uc[l]) |
| 45 | break; |
| 46 | if (c >= 128) |
| 47 | { |
| 48 | if (errflag) |
| 49 | { |
| 50 | *errflag=l; |
| 51 | free(p); |
| 52 | return (0); |
| 53 | } |
| 54 | c=uc[l]; |
| 55 | } |
| 56 | c |= 0x80; |
| 57 | } |
| 58 | if (c == 0) |
| 59 | c=255; |
| 60 | p[l]=(char)c; |
| 61 | } |
| 62 | p[l]=0; |
| 63 | return (p); |
| 64 | } |