Subversion Repositories Kolibri OS

Rev

Rev 7005 | Rev 7044 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //#include <string.h>
  2. //#include <stdio.h>
  3. typedef unsigned int size_t;
  4. #define NULL ((void*)0)
  5.  
  6. typedef int conv_t;
  7. typedef unsigned int ucs4_t;
  8.  
  9. typedef int iconv_t;
  10.  
  11.  
  12. /* Return code if invalid input after a shift sequence of n bytes was read.
  13.    (xxx_mbtowc) */
  14. #define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
  15. /* Return code if invalid. (xxx_mbtowc) */
  16. #define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
  17. /* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
  18. #define RET_TOOFEW(n)       (-2-2*(n))
  19.  
  20. /* Return code if invalid. (xxx_wctomb) */
  21. #define RET_ILUNI      -1
  22. /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
  23. #define RET_TOOSMALL   -2
  24.  
  25.  
  26. #define CP866 0
  27. #define CP1251 1
  28. #define CP1252 2
  29. #define KOI8_RU 3
  30. #define ISO8859_5 4
  31. #define UTF_8 5
  32.  
  33.  
  34. int strcmp (const char* a, const char* b)
  35. {
  36.         return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
  37. }
  38.  
  39.  
  40.  
  41.  
  42. #include "cp866.h"
  43. #include "cp1251.h"
  44. #include "cp1252.h"
  45. #include "koi8_ru.h"
  46. #include "iso8859_5.h"
  47. #include "utf8.h"
  48.  
  49.  
  50. int encoding(char *what) {
  51.  
  52.         /* Ignore //TRANSLIT or //IGNORE for now. */
  53.         int i;
  54.         for(i = 0; i < strlen(what); i++) {
  55.           if(what[i] == '/') {
  56.                 what[i] = '\0';
  57.                 break;
  58.           }
  59.         }
  60.  
  61.         if (!strcmp(what,"CP866")) return CP866;
  62.         if (!strcmp(what,"CP1251")) return CP1251;
  63.         if (!strcmp(what,"windows-1252")) return CP1252;
  64.         if (!strcmp(what,"CP1252")) return CP1252;
  65.         if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
  66.         if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
  67.         if (!strcmp(what,"UTF-8")) return UTF_8;
  68.         return -1;
  69. }
  70.  
  71.  
  72. iconv_t iconv_open(const char *tocode, const char *fromcode) {
  73.         int to, from;
  74.         if ((to=encoding(tocode))==-1) return -1;
  75.         if ((from=encoding(fromcode))==-1) return -1;
  76.         to=to<<16&0xFFFF0000;
  77.         from=from&0xFFFF;
  78.         return to+from;
  79. }
  80.  
  81. int iconv_close(iconv_t icd)
  82. {
  83.   return 0;
  84. }
  85.  
  86. size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
  87.         char **outbuf, size_t *outbytesleft)
  88. {
  89.         int n, to, from, count1,count2;
  90.         int pwc, converted,written;
  91.         int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
  92.         int (*wctomb)(conv_t, ucs4_t *, const unsigned char *, int);
  93.        
  94.         char *str;
  95.         str=*outbuf;
  96.        
  97.         from=cd>>16;
  98.         to=cd&0xFFFF;
  99.                        
  100.         switch (from)
  101.         {
  102.                 case CP866: mbtowc=cp866_mbtowc; break;
  103.                 case CP1251: mbtowc=cp1251_mbtowc; break;
  104.                 case CP1252: mbtowc=cp1252_mbtowc; break;
  105.                 case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
  106.                 case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
  107.                 case UTF_8: mbtowc=utf8_mbtowc; break;
  108.                 default: return -2;
  109.         }
  110.                
  111.         switch (to)
  112.         {
  113.                 case CP866: wctomb=cp866_wctomb; break;
  114.                 case CP1251: wctomb=cp1251_wctomb; break;
  115.                 case CP1252: wctomb=cp1252_wctomb; break;
  116.                 case ISO8859_5: wctomb=iso8859_5_wctomb; break;
  117.                 case KOI8_RU: wctomb=koi8_ru_wctomb; break;
  118.                 case UTF_8: wctomb=utf8_wctomb; break;
  119.                 default: return -3;
  120.         }
  121.  
  122.     if(from == to) {
  123.       int oc=0,ic=0;
  124.  
  125.       while(*inbytesleft > 0 && *outbytesleft > 0) {
  126.         str[oc]=(*inbuf)[ic];
  127.         ++ic;
  128.         ++oc;
  129.         (*inbytesleft)--;
  130.         (*outbytesleft)--;
  131.         (*outbuf)++;
  132.       }
  133.  
  134.       return 0;
  135.     }
  136.        
  137.         count1=0;
  138.         count2=0;
  139.                
  140.         while ( *inbytesleft>0 && *outbytesleft>1)
  141.         {
  142.                 n=1;
  143.                
  144.                 do {
  145.                 //converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
  146.                 //      printf("%d\n",n);
  147.                 converted= (mbtowc)(0,&pwc,((*inbuf)+count1),n);
  148.                
  149.                 n++;
  150.                 }       while (converted==RET_TOOFEW(0));
  151.                
  152.                 if (converted<0) return -10;
  153.                 //written=  (cp866_wctomb)(0,str+count2,pwc,1);
  154.                 written=  (wctomb)(0,str+count2,pwc,1);
  155.                 if (written<0) written=0;//return -11;
  156.                                
  157.                 //printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
  158.                
  159.                 (*inbytesleft)-=converted;
  160.                 (*outbytesleft)-=written;
  161.                 count1+=converted;
  162.                 count2+=written;
  163.         }
  164.         *(str+count2)='\0';
  165.                        
  166.         if (*inbytesleft>0 && *outbytesleft==0) return -12;
  167.         return 0;
  168. }
  169.  
  170.  
  171. /*
  172. int main()
  173. {
  174.         char *s;// ="вертолет";
  175.         char *z;
  176.         //unsigned int pwc;
  177.         iconv_t cd;
  178.         int in, out;
  179.        
  180.                 FILE *infile;
  181.                 char *fname = "file.txt";
  182.            
  183.                 infile = fopen(fname,"r");
  184.        
  185.         fseek(infile, 0, SEEK_END);
  186.         size_t file_size = ftell(infile);
  187.         rewind(infile);
  188.  
  189.         //printf ("LOL\n");
  190.  
  191.         char *buffer = (char*)malloc(file_size * sizeof(char));
  192.         if (buffer == NULL)
  193.         {
  194.                 fclose(infile);
  195.                 printf("Error allocating %d bytes.\n", file_size * sizeof(char));
  196.                 return -1;
  197.         }
  198.         size_t bytes_read = fread(buffer, sizeof(char), file_size, infile);
  199.         if (bytes_read != file_size)
  200.         {
  201.                 printf("Have read only %d bytes of %d.\n", bytes_read, file_size);
  202.                 free(buffer);
  203.                 fclose(infile);
  204.                 return -1;
  205.         }
  206.        
  207.         in=strlen(buffer);
  208.         z=malloc(in+1);
  209.        
  210.         out=in+1;
  211.         cd=iconv_open("CP1251","CP866");
  212. //      printf("%x\n",cd);
  213.         int t;
  214.         t=iconv(cd, &buffer, &in, &z, &out);
  215.         printf("\nResult: %d", t);
  216.         puts(z);
  217.         //for (;s<s+strlen(s);s++) {cp866_mbtowc (0,  &pwc, s, 1);printf("%c=%u\n",*s,pwc);}
  218. }
  219. */
  220.  
  221.  
  222. typedef struct
  223. {
  224.         char *name;
  225.         void *f;
  226. } export_t;
  227.  
  228. char szStart[]           = "START";
  229. char szVersion[]         = "version";
  230. char sziconv_open[]    = "iconv_open";
  231. char sziconv[]   = "iconv";
  232.  
  233. export_t EXPORTS[] __asm__("EXPORTS") =
  234. {
  235.         { szStart,       (void*)0x0 },
  236.         { szVersion,     (void*)0x00010001 },
  237.         { sziconv_open,  iconv_open    },
  238.         { sziconv,       iconv   },
  239.         { NULL,          NULL },
  240. };
  241.