base64.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* base64.c: base64 encoding/decoding
  2. *
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "base64.h"
  7. #include "src/compat/compat.h"
  8. static const char base64[65] = ".\\0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  9. static const char base64r[256] = {
  10. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0,
  14. 0, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  15. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 0, 1, 0, 0, 0,
  16. 0, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  17. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. };
  27. static char base64to[256] =
  28. {
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, 0, 0, 0, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
  32. 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  33. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 62, 0, 63, 0, 0, 0, 26, 27, 28,
  34. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  35. 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  37. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  42. };
  43. int base64_to_int(char *buf)
  44. {
  45. int i = 0;
  46. while (*buf) {
  47. i = i << 6;
  48. i += base64to[(int) *buf];
  49. buf++;
  50. }
  51. return i;
  52. }
  53. /* Thank you ircu :) */
  54. static char tobase64array[64] =
  55. {
  56. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  57. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  58. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  59. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  60. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  61. '[', ']'
  62. };
  63. char *int_to_base64(unsigned int val)
  64. {
  65. static char buf_base64[12] = "";
  66. buf_base64[11] = 0;
  67. if (!val) {
  68. buf_base64[10] = 'A';
  69. return buf_base64 + 10;
  70. }
  71. int i = 11;
  72. while (val) {
  73. i--;
  74. buf_base64[i] = tobase64array[val & 0x3f];
  75. val = val >> 6;
  76. }
  77. return buf_base64 + i;
  78. }
  79. char *
  80. b64enc(const unsigned char *data, size_t len)
  81. {
  82. char *dest = (char *) my_calloc(1, (len << 2) / 3 + 4 + 1);
  83. b64enc_buf(data, len, dest);
  84. return (dest);
  85. }
  86. void
  87. b64enc_buf(const unsigned char *data, size_t len, char *dest)
  88. {
  89. #define DB(x) ((unsigned char) (x + i < len ? data[x + i] : 0))
  90. register size_t t, i;
  91. for (i = 0, t = 0; i < len; i += 3, t += 4) {
  92. dest[t] = base64[DB(0) >> 2];
  93. dest[t + 1] = base64[((DB(0) & 3) << 4) | (DB(1) >> 4)];
  94. dest[t + 2] = base64[((DB(1) & 0x0F) << 2) | (DB(2) >> 6)];
  95. dest[t + 3] = base64[(DB(2) & 0x3F)];
  96. }
  97. #undef DB
  98. dest[t] = 0;
  99. }
  100. char *
  101. b64dec(const unsigned char *data, size_t *len)
  102. {
  103. char *dest = (char *) my_calloc(1, ((*len * 3) >> 2) + 6 + 1);
  104. b64dec_buf(data, len, dest);
  105. return (dest);
  106. }
  107. void
  108. b64dec_buf(const unsigned char *data, size_t *len, char *dest)
  109. {
  110. #define DB(x) ((unsigned char) (x + i < *len ? base64r[(unsigned char) data[x + i]] : 0))
  111. register size_t t, i;
  112. for (i = 0, t = 0; i < *len; i += 4, t += 3) {
  113. dest[t] = (DB(0) << 2) + (DB(1) >> 4);
  114. dest[t + 1] = ((DB(1) & 0x0F) << 4) + (DB(2) >> 2);
  115. dest[t + 2] = ((DB(2) & 3) << 6) + DB(3);
  116. };
  117. #undef DB
  118. t += 3;
  119. t -= (t % 4);
  120. dest[t] = 0;
  121. *len = t;
  122. }