Guest User

Untitled

a guest
Apr 9th, 2021
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX_BUF 1024
  6.  
  7. uint8_t base64_tbl[] = {
  8. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  10. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
  11. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  12. 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  13. 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
  14. 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  15. 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  16. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  17. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  20. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  21. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  24.  
  25. void base64_gen_table() {
  26. uint8_t tbl[256];
  27. int i, s;
  28.  
  29. memset(tbl, 0xff, 256);
  30.  
  31. for (i = 0, s = 'A'; s <= 'Z'; ++i, ++s) {
  32. tbl[s] = i;
  33. }
  34.  
  35. for (s = 'a'; s <= 'z'; ++i, ++s) {
  36. tbl[s] = i;
  37. }
  38.  
  39. for (s = '0'; s <= '9'; ++i, ++s) {
  40. tbl[s] = i;
  41. }
  42.  
  43. tbl['+'] = i++;
  44. tbl['/'] = i;
  45.  
  46. for (i = 0; i < 256; ++i) {
  47. if (i % 0x10 == 0) {
  48. printf("\n");
  49. }
  50. printf("0x%02x,", tbl[i]);
  51. }
  52. }
  53.  
  54. int base64_decode(const uint8_t *src, size_t nsrc, uint8_t *dst, int *err) {
  55. const uint8_t *p;
  56. int nsrc_rem, ndst, e;
  57. uint8_t i0, i1, i2, i3;
  58.  
  59. p = src;
  60. nsrc_rem = nsrc % 4;
  61. nsrc = nsrc & -4;
  62. ndst = 0;
  63. e = 0;
  64.  
  65. while (nsrc) {
  66. i0 = base64_tbl[*p++];
  67. i1 = base64_tbl[*p++];
  68. i2 = base64_tbl[*p++];
  69. i3 = base64_tbl[*p++];
  70. if ((i0 | i1 | i3 | i3) & 0x80) {
  71. /* encoded text corruption */
  72. ++e;
  73. goto next;
  74. }
  75.  
  76. dst[ndst] = i0 << 2 | i1 >> 4;
  77. dst[ndst + 1] = i1 << 4 | i2 >> 2;
  78. dst[ndst + 2] = i2 << 6 | i3;
  79.  
  80. ndst += 3;
  81.  
  82. next:
  83. nsrc -= 4;
  84. }
  85.  
  86. if (nsrc_rem == 3) {
  87. i0 = base64_tbl[*p++];
  88. i1 = base64_tbl[*p++];
  89. i2 = base64_tbl[*p++];
  90. if (!(i0 & i1 & i2 & 0x80)) {
  91. dst[ndst] = i0 << 2 | i1 >> 4;
  92. dst[ndst + 1] = i1 << 4 | i2 >> 2;
  93. dst[ndst + 2] = i2 << 6;
  94. ndst += 3;
  95. } else {
  96. ++e;
  97. }
  98. } else if (nsrc_rem == 2) {
  99. i0 = base64_tbl[*p++];
  100. i1 = base64_tbl[*p++];
  101. if (!(i0 & i1 & 0x80)) {
  102. dst[ndst] = i0 << 2 | i1 >> 4;
  103. dst[ndst + 1] = i1 << 4;
  104. ndst += 2;
  105. } else {
  106. ++e;
  107. }
  108. } else if (nsrc_rem == 1) {
  109. i0 = base64_tbl[*p++];
  110. if (!(i0 & 0x80)) {
  111. dst[ndst] = i0 << 2;
  112. ndst += 2;
  113. } else {
  114. ++e;
  115. }
  116. }
  117.  
  118. *err = e;
  119. return ndst;
  120. }
  121.  
  122. int main(int argc, char **argv) {
  123. uint8_t buf[MAX_BUF];
  124. int len, err, i, hex;
  125. char *in;
  126.  
  127. if (argc < 2) {
  128. return 1;
  129. }
  130.  
  131. if (!strcmp(argv[1], "-g")) {
  132. base64_gen_table();
  133. return 0;
  134. }
  135.  
  136. hex = 0;
  137. in = argv[1];
  138.  
  139. if (argc > 2 && !strcmp(argv[1], "-hex")) {
  140. hex = 1;
  141. in = argv[2];
  142. }
  143.  
  144. len = base64_decode((uint8_t *)in, strlen(in), buf, &err);
  145.  
  146. printf("len: %d, err: %d\n", len, err);
  147.  
  148. for (i = 0; i < len; ++i) {
  149. if (hex) {
  150. printf("%02x ", buf[i]);
  151. } else {
  152. printf("%c", (char)buf[i]);
  153. }
  154. }
  155.  
  156. return 0;
  157. }
  158.  
RAW Paste Data