Advertisement
Guest User

Untitled

a guest
Jun 11th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. static int decode_matrix[128] =
  4.     { -1,-1,-1,-1,-1,-1,-1,-1 // A..Z = 0..25
  5.     , -1,-1,-1,-1,-1,-1,-1,-1 // a..z = 26..51
  6.     , -1,-1,-1,-1,-1,-1,-1,-1 // 0..9 = 52..61
  7.     , -1,-1,-1,-1,-1,-1,-1,-1 // +,/ = 62,63
  8.     , -1,-1,-1,-1,-1,-1,-1,-1
  9.     , -1,-1,-1,62,-1,-1,-1,63
  10.     , 52,53,54,55,56,57,58,59
  11.     , 60,61,-1,-1,-1,-1,-1,-1
  12.     , -1, 0, 1, 2, 3, 4, 5, 6
  13.     ,  7, 8, 9,10,11,12,13,14
  14.     , 15,16,17,18,19,20,21,22
  15.     , 23,24,25,-1,-1,-1,-1,-1
  16.     , -1,26,27,28,29,30,31,32
  17.     , 33,34,35,36,37,38,39,40
  18.     , 41,42,43,44,45,46,47,48
  19.     , 49,50,51,-1,-1,-1,-1,-1 };
  20.  
  21. static int decode_base64_char (char c)
  22. {
  23.     if ((unsigned char) c < 128) {
  24.         return decode_matrix[c];
  25.     } else {
  26.         return -1;
  27.     }
  28. }
  29.  
  30. static char* write_64 (int data, char* out, int* bit_pos)
  31. {
  32.     switch (*bit_pos) {
  33.     case 6: // ======xx xxxx----
  34.         *out &= 0xfc;
  35.         *out |= data >> 4;
  36.         *(out + 1) = data << 4;
  37.         *bit_pos = 4;
  38.         return out + 1;
  39.     case 4: // ====xxxx xx------
  40.         *out &= 0xf0;
  41.         *out |= data >> 2;
  42.         *(out + 1) = data << 6;
  43.         *bit_pos = 2;
  44.         return out + 1;
  45.     case 2: // ==xxxxxx --------
  46.         *out &= 0xc0;
  47.         *out |= data;
  48.         *bit_pos = 0;
  49.         return out + 1;
  50.     case 0: // xxxxxx-- --------
  51.         *out = data << 2;
  52.         *bit_pos = 6;
  53.         return out;
  54.     default: return out;
  55.     }
  56. }
  57.  
  58. static char* finish_64 (char* out, int bit_pos)
  59. {
  60.     switch (bit_pos) {
  61.     case 6: // ======00
  62.         *out &= 0xfc;
  63.         return out + 1;
  64.         break;
  65.     case 4: // ====0000
  66.         *out &= 0xf0;
  67.         return out + 1;
  68.         break;
  69.     case 2: // ==000000
  70.         *out &= 0xc0;
  71.         return out + 1;
  72.         break;
  73.     case 0: // --------
  74.     default:
  75.         return out;
  76.     }
  77. }
  78.  
  79. char* decode_base64 (char* output, const char* input)
  80. {
  81.     int decoded;
  82.     int bit_pos = 0;
  83.     for (;;) {
  84.         if (*input == '=' || *input == '\0') {
  85.             break;
  86.         }
  87.  
  88.         decoded = decode_base64_char(*input++);
  89.         if (decoded == -1) {
  90.             return NULL;
  91.         }
  92.         output = write_64(decoded, output, &bit_pos);
  93.     }
  94.     output = finish_64(output, bit_pos);
  95.     *output = '\0';
  96.     return output;
  97. }
  98.  
  99. int main (int argc, char** argv)
  100. {
  101.     char* input;
  102.     char* output;
  103.     if (argc > 1) {
  104.         input = argv[1];
  105.     } else {
  106.         input = "aGVsbG8sIHdvcmxkIQ==";
  107.     }
  108.  
  109.     output = malloc(strlen(input) / 8 * 6 + 2);
  110.  
  111.     if (decode_base64(output, input)) {
  112.         printf("%s\n", output);
  113.     } else {
  114.         printf("invalid base64\n");
  115.     }
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement