base64.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /* base64.c: base64 encoding/decoding
  21. *
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "base64.h"
  26. #include "src/compat/compat.h"
  27. static const char base64[65] = ".\\0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  28. static const char base64r[256] = {
  29. 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,
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0,
  33. 0, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  34. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 0, 1, 0, 0, 0,
  35. 0, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  36. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0,
  37. 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,
  39. 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,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. };
  46. static char base64to[256] =
  47. {
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  52. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 62, 0, 63, 0, 0, 0, 26, 27, 28,
  53. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  54. 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  61. };
  62. int base64_to_int(char *buf)
  63. {
  64. int i = 0;
  65. while (*buf) {
  66. i = i << 6;
  67. i += base64to[(int) *buf];
  68. buf++;
  69. }
  70. return i;
  71. }
  72. static const char tobase64array[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]";
  73. char *int_to_base64(unsigned int val)
  74. {
  75. static char buf_base64[12] = "";
  76. buf_base64[11] = 0;
  77. if (!val) {
  78. buf_base64[10] = 'A';
  79. return buf_base64 + 10;
  80. }
  81. int i = 11;
  82. while (val) {
  83. i--;
  84. buf_base64[i] = tobase64array[val & 0x3f];
  85. val = val >> 6;
  86. }
  87. return buf_base64 + i;
  88. }
  89. char *
  90. b64enc(const unsigned char *data, size_t len)
  91. {
  92. char *dest = (char *) my_calloc(1, (len << 2) / 3 + 4 + 1);
  93. b64enc_buf(data, len, dest);
  94. return (dest);
  95. }
  96. void
  97. b64enc_buf(const unsigned char *data, size_t len, char *dest)
  98. {
  99. #define DB(x) ((unsigned char) (x + i < len ? data[x + i] : 0))
  100. register size_t t, i;
  101. for (i = 0, t = 0; i < len; i += 3, t += 4) {
  102. dest[t] = base64[DB(0) >> 2];
  103. dest[t + 1] = base64[((DB(0) & 3) << 4) | (DB(1) >> 4)];
  104. dest[t + 2] = base64[((DB(1) & 0x0F) << 2) | (DB(2) >> 6)];
  105. dest[t + 3] = base64[(DB(2) & 0x3F)];
  106. }
  107. #undef DB
  108. dest[t] = 0;
  109. }
  110. char *
  111. b64dec(const unsigned char *data, size_t *len)
  112. {
  113. char *dest = (char *) my_calloc(1, ((*len * 3) >> 2) + 6 + 1);
  114. b64dec_buf(data, len, dest);
  115. return (dest);
  116. }
  117. void
  118. b64dec_buf(const unsigned char *data, size_t *len, char *dest)
  119. {
  120. #define DB(x) ((unsigned char) (x + i < *len ? base64r[(unsigned char) data[x + i]] : 0))
  121. register size_t t, i;
  122. for (i = 0, t = 0; i < *len; i += 4, t += 3) {
  123. dest[t] = (DB(0) << 2) + (DB(1) >> 4);
  124. dest[t + 1] = ((DB(1) & 0x0F) << 4) + (DB(2) >> 2);
  125. dest[t + 2] = ((DB(2) & 3) << 6) + DB(3);
  126. };
  127. #undef DB
  128. t += 3;
  129. t -= (t % 4);
  130. dest[t] = 0;
  131. *len = t;
  132. }