Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', '!',
  8. 'I', 'J', 'K', 'L', 'M', 'N', '@', 'P',
  9. 'Q', 'R', 'S', 'T', 'U', '#', 'W', 'X',
  10. 'Y', 'Z', 'a', 'b', '$', 'd', 'e', 'f',
  11. 'g', 'h', 'i', '%', 'k', 'l', 'm', 'n',
  12. 'o', 'p', '^', 'r', 's', 't', 'u', 'v',
  13. 'w', '&', 'y', 'z', '0', '1', '2', '3',
  14. '*', '5', '6', '7', '8', '9', '+', '/'};
  15. static char *decoding_table = NULL;
  16. static int mod_table[] = {0, 2, 1};
  17. void build_decoding_table();
  18.  
  19.  
  20. char *base64_encode(const unsigned char *data,
  21. size_t input_length,
  22. size_t *output_length) {
  23.  
  24. *output_length = 4 * ((input_length + 2) / 3);
  25.  
  26. char *encoded_data = malloc(*output_length);
  27. if (encoded_data == NULL) return NULL;
  28.  
  29. for (int i = 0, j = 0; i < input_length;) {
  30.  
  31. uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
  32. uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
  33. uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
  34.  
  35. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  36.  
  37. encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
  38. encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
  39. encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
  40. encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
  41. }
  42.  
  43. for (int i = 0; i < mod_table[input_length % 3]; i++)
  44. encoded_data[*output_length - 1 - i] = '=';
  45.  
  46. return encoded_data;
  47. }
  48.  
  49.  
  50. unsigned char *base64_decode(const char *data,
  51. size_t input_length,
  52. size_t *output_length) {
  53.  
  54. if (decoding_table == NULL) build_decoding_table();
  55.  
  56. if (input_length % 4 != 0) return NULL;
  57.  
  58. *output_length = input_length / 4 * 3;
  59. if (data[input_length - 1] == '=') (*output_length)--;
  60. if (data[input_length - 2] == '=') (*output_length)--;
  61.  
  62. unsigned char *decoded_data = malloc(*output_length);
  63. if (decoded_data == NULL) return NULL;
  64.  
  65. for (int i = 0, j = 0; i < input_length;) {
  66.  
  67. uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  68. uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  69. uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  70. uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
  71.  
  72. uint32_t triple = (sextet_a << 3 * 6)
  73. + (sextet_b << 2 * 6)
  74. + (sextet_c << 1 * 6)
  75. + (sextet_d << 0 * 6);
  76.  
  77. if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
  78. if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
  79. if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
  80. }
  81.  
  82. return decoded_data;
  83. }
  84.  
  85.  
  86. void build_decoding_table() {
  87.  
  88. decoding_table = malloc(256);
  89.  
  90. for (int i = 0; i < 64; i++)
  91. decoding_table[(unsigned char) encoding_table[i]] = i;
  92. }
  93.  
  94.  
  95. void base64_cleanup() {
  96. free(decoding_table);
  97. }
  98.  
  99. int main(int argc, char* argv[]) {
  100. // get file
  101. size_t oc;
  102. char *res;
  103. FILE * ifp = fopen(argv[1], "r");
  104. fseek(ifp, 0, SEEK_END);
  105. size_t bc = ftell(ifp);
  106. fseek(ifp, 0, SEEK_SET);
  107. unsigned char *dat = malloc(bc+1);
  108. fread(dat, bc, 1, ifp);
  109. fclose(ifp);
  110.  
  111. // encode
  112. res = base64_encode(dat, bc, &oc);
  113.  
  114. // write to file
  115. FILE * ofp = fopen(argv[2], "w");
  116. fputs(res, ofp);
  117. fclose(ofp);
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement