Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  7. static char *decoding_table = NULL;
  8. static int mod_table[] = {0, 2, 1};
  9.  
  10. size_t getFilesize ( char * inputName )
  11. {
  12. FILE * buff = fopen ( inputName, "rb" );
  13. fseek ( buff, 0, SEEK_END );
  14. size_t res = ftell ( buff );
  15. fclose ( buff );
  16. return res;
  17. }
  18.  
  19. void build_decoding_table() {
  20.  
  21. decoding_table = malloc(256);
  22.  
  23. for (int i = 0; i < 64; i++)
  24. decoding_table[(unsigned char) alphabet[i]] = i;
  25. }
  26.  
  27.  
  28. void base64_cleanup() {
  29. free(decoding_table);
  30. }
  31.  
  32. char *base64_encode(char *data,
  33. size_t input_length,
  34. size_t *output_length) {
  35.  
  36. *output_length = 4 * ((input_length + 2) / 3);
  37.  
  38. char *encoded_data = malloc(*output_length);
  39. if (encoded_data == NULL) return NULL;
  40.  
  41. for (int i = 0, j = 0; i < input_length;) {
  42.  
  43. uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
  44. uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
  45. uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
  46.  
  47. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  48.  
  49. encoded_data[j++] = alphabet[(triple >> 3 * 6) & 0x3F];
  50. encoded_data[j++] = alphabet[(triple >> 2 * 6) & 0x3F];
  51. encoded_data[j++] = alphabet[(triple >> 1 * 6) & 0x3F];
  52. encoded_data[j++] = alphabet[(triple >> 0 * 6) & 0x3F];
  53. }
  54.  
  55. for (int i = 0; i < mod_table[input_length % 3]; i++)
  56. encoded_data[*output_length - 1 - i] = '=';
  57.  
  58. return encoded_data;
  59. }
  60.  
  61. char *base64_decode(char *data,
  62. size_t input_length,
  63. size_t *output_length) {
  64.  
  65. if (decoding_table == NULL) build_decoding_table();
  66.  
  67. if (input_length % 4 != 0) return NULL;
  68.  
  69. *output_length = input_length / 4 * 3;
  70. if (data[input_length - 1] == '=') (*output_length)--;
  71. if (data[input_length - 2] == '=') (*output_length)--;
  72.  
  73. char *decoded_data = malloc(*output_length);
  74. if (decoded_data == NULL) return NULL;
  75.  
  76. for (int i = 0, j = 0; i < input_length;) {
  77.  
  78. uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  79. uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  80. uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  81. uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  82.  
  83. uint32_t triple = (sextet_a << 3 * 6)
  84. + (sextet_b << 2 * 6)
  85. + (sextet_c << 1 * 6)
  86. + (sextet_d << 0 * 6);
  87.  
  88. if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
  89. if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
  90. if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
  91. }
  92.  
  93. return decoded_data;
  94. }
  95.  
  96. int main ( int argc, char ** argv )
  97. {
  98. size_t input_length = getFilesize ( argv [ 1 ] );
  99. char * input = ( char * ) malloc ( input_length );
  100. FILE * in = fopen ( argv [ 1 ], "rb" );
  101. fread ( input, 1, input_length, in);
  102. size_t output_length = 0;
  103. char * encoded = base64_encode ( input, input_length, &output_length);
  104. char * decoded = base64_decode ( encoded, output_length, &input_length);
  105. FILE * out = fopen ( "out.out", "wb");
  106. fwrite ( decoded, sizeof (char), input_length, out);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement