Subversion Repositories Kolibri OS

Rev

Rev 7085 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. typedef unsigned int size_t;
  2. #define NULL ((void*)0)
  3.  
  4. typedef int conv_t;
  5. typedef unsigned int ucs4_t;
  6. typedef int iconv_t;
  7.  
  8. /* Return code if invalid input after a shift sequence of n bytes was read.
  9.    (xxx_mbtowc) */
  10. #define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
  11. /* Return code if invalid. (xxx_mbtowc) */
  12. #define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
  13. /* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
  14. #define RET_TOOFEW(n)       (-2-2*(n))
  15.  
  16. /* Return code if invalid. (xxx_wctomb) */
  17. #define RET_ILUNI      -1
  18. /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
  19. #define RET_TOOSMALL   -2
  20.  
  21. #define CP866 0
  22. #define CP1251 1
  23. #define CP1252 2
  24. #define KOI8_RU 3
  25. #define ISO8859_5 4
  26. #define UTF_8 5
  27.  
  28. int strcmp (const char* a, const char* b) {
  29.     return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
  30. }
  31.  
  32. #include "cp866.h"
  33. #include "cp1251.h"
  34. #include "cp1252.h"
  35. #include "koi8_ru.h"
  36. #include "iso8859_5.h"
  37. #include "utf8.h"
  38.  
  39. int encoding(const char *what) {
  40.     if (!strcmp(what,"CP866")) return CP866;
  41.     if (!strcmp(what,"CP1251")) return CP1251;
  42.     if (!strcmp(what,"CP1252")) return CP1252;
  43.     if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
  44.     if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
  45.     if (!strcmp(what,"UTF-8")) return UTF_8;
  46.     return -1;
  47. }
  48.  
  49. iconv_t iconv_open(const char *tocode, const char *fromcode) {
  50.     int to, from;
  51.     if ((to=encoding(tocode))==-1) return -1;
  52.     if ((from=encoding(fromcode))==-1) return -1;
  53.     to=to<<16&0xFFFF0000;
  54.     from=from&0xFFFF;
  55.     return to+from;
  56. }
  57.  
  58. size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) {
  59.     int n, to, from, count1,count2;
  60.     int pwc, converted,written;
  61.     int (*mbtowc)(conv_t, ucs4_t*, const unsigned char*, int);
  62.     int (*wctomb)(conv_t, unsigned char*, ucs4_t, int);
  63.  
  64.     char *str;
  65.     str=*outbuf;
  66.  
  67.     from=cd>>16;
  68.     to=cd&0xFFFF;
  69.  
  70.     switch (from)
  71.     {
  72.         case CP866: mbtowc=cp866_mbtowc; break;
  73.         case CP1251: mbtowc=cp1251_mbtowc; break;
  74.         case CP1252: mbtowc=cp1252_mbtowc; break;
  75.         case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
  76.         case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
  77.         case UTF_8: mbtowc=utf8_mbtowc; break;
  78.         default: return -2;
  79.     }
  80.  
  81.     switch (to)
  82.     {
  83.         case CP866: wctomb=cp866_wctomb; break;
  84.         case CP1251: wctomb=cp1251_wctomb; break;
  85.         case CP1252: wctomb=cp1252_wctomb; break;
  86.         case ISO8859_5: wctomb=iso8859_5_wctomb; break;
  87.         case KOI8_RU: wctomb=koi8_ru_wctomb; break;
  88.         case UTF_8: wctomb=utf8_wctomb; break;
  89.         default: return -3;
  90.     }
  91.  
  92.     count1=0;
  93.     count2=0;
  94.  
  95.     while ( *inbytesleft>0 && *outbytesleft>1)
  96.     {
  97.         n=1;
  98.  
  99.         do {
  100.         //converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
  101.         //    printf("%d\n",n);
  102.         converted = (mbtowc)(0,&pwc,((*inbuf)+count1),n);
  103.  
  104.         n++;
  105.         }    while (converted==RET_TOOFEW(0));
  106.  
  107.         if (converted<0) return -10;
  108.         //written=  (cp866_wctomb)(0,str+count2,pwc,1);
  109.         written = (wctomb)(0,str+count2,pwc,1);
  110.         if (written<0) written=0;//return -11;
  111.  
  112.         //printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
  113.  
  114.         (*inbytesleft)-=converted;
  115.         (*outbytesleft)-=written;
  116.         count1+=converted;
  117.         count2+=written;
  118.     }
  119.     *(str+count2)='\0';
  120.  
  121.     if (*inbytesleft>0 && *outbytesleft==0) return -12;
  122.     return 0;
  123. }
  124.  
  125. typedef struct {
  126.     char *name;
  127.     void *func;
  128. } export_t;
  129.  
  130. export_t EXPORTS[] = {
  131.     {"START",      (void*)0x0},
  132.     {"version",    (void*)0x00010001},
  133.     {"iconv_open", iconv_open},
  134.     {"iconv",      iconv},
  135.     NULL
  136. };
  137.