Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include "base64.h"
  3.  
  4. ///===============
  5.  
  6. char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  7.  
  8. ///===============
  9.  
  10. char* strchr (const char *s, int c)
  11. {
  12.   do {
  13.     if (*s == c)
  14.       {
  15.         return (char*)s;
  16.       }
  17.   } while (*s++);
  18.   return (0);
  19. }
  20.  
  21. ///===============
  22.  
  23. int base64_encode(char inp[], char outp[], int len)
  24. {
  25. int i, j;
  26. unsigned char chr[3];
  27.  
  28. for (i = 0, j=-1; i < len; i+=3)
  29.         {
  30.         chr[0] = (unsigned char) inp[i];
  31.         if (i < len)
  32.                 {
  33.                 chr[1] = (unsigned char) inp[i+1];
  34.                 chr[2] = (unsigned char) inp[i+2];
  35.                 }
  36.  
  37.         outp[++j] = base64_table[ chr[0]>>2 ];
  38.         outp[++j] = base64_table[ ((chr[0] & 3) << 4) | (chr[1] >> 4) ];
  39.  
  40.         outp[++j] = base64_table[ ((chr[1] & 15) << 2) | (chr[2] >> 6) ];
  41.         outp[++j] = base64_table[ chr[2] & 63 ];
  42.         }
  43.  
  44. switch (len%3)
  45.     {
  46.     case 1:
  47.             outp[j-1] = outp[j] = '=';
  48.             break;
  49.  
  50.     case 2:
  51.             outp[j] = '=';
  52.             break;
  53.  
  54.     default:
  55.         break;
  56.  
  57.     };
  58.  
  59. outp[j+1] = '\0';
  60.  
  61. return j+1;
  62. }
  63.  
  64. ///===============
  65.  
  66. int base64_decode(char inp[], char outp[], int len)
  67. {
  68. int i, j, k;
  69. char chr[4];
  70.  
  71. for (i = 0, j=-1, k=0; i < len; i+=4)
  72.     {
  73.     chr[0] = strchr(base64_table, inp[i]) - base64_table;
  74.  
  75.     chr[1] = strchr(base64_table, inp[i+1]) - base64_table;
  76.  
  77.     if (inp[i+2] == '=')
  78.         {
  79.         chr[2] = 0;
  80.         k++;
  81.         }
  82.     else
  83.         chr[2] = strchr(base64_table, inp[i+2]) - base64_table;
  84.  
  85.     if (inp[i+3] == '=')
  86.         {
  87.         chr[3] = 0;
  88.         k++;
  89.         }
  90.     else
  91.         chr[3] = strchr(base64_table, inp[i+3]) - base64_table;
  92.  
  93.     outp[++j] = ((chr[0] << 2) | (chr[1] >> 4));
  94.     outp[++j] = ((chr[1] << 4) | (chr[2] >> 2));
  95.     outp[++j] = ((chr[2] & 0x03 )<< 6) | (chr[3] & 0x3f);
  96.     }
  97.  
  98. outp[j+1-k] = '\0';
  99.  
  100. return j+1-k;
  101. }
  102.  
  103. ///===============
  104.  
  105. #define NULL ((void*)0)
  106.  
  107. typedef struct
  108. {
  109. void    *name;
  110. void    *function;
  111. } export_t;
  112.  
  113. char szbase64_encode[]={"base64_encode"};
  114. char szbase64_decode[]={"base64_decode"};
  115.  
  116. export_t EXPORTS[] =
  117. {
  118. {szbase64_encode, (void*) base64_encode},
  119. {szbase64_decode, (void*) base64_decode},
  120. { NULL, NULL },
  121. };
  122.  
  123. ///===============
  124.