Base64.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Base64.h"
  36. namespace aria2 {
  37. static const char CHAR_TABLE[] = {
  38. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  39. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  40. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  41. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  42. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  43. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  44. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  45. '4', '5', '6', '7', '8', '9', '+', '/',
  46. };
  47. static const int INDEX_TABLE[] = {
  48. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  49. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  50. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  51. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  52. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  53. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  54. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  55. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
  56. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  57. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  58. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  59. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  60. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  61. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  62. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  64. };
  65. void Base64::encode(unsigned char*& result, size_t& rlength,
  66. const unsigned char* src, size_t slength)
  67. {
  68. if(slength == 0) {
  69. rlength = 0;
  70. return;
  71. }
  72. size_t trituple = (slength+2)/3;
  73. int r = slength%3;
  74. rlength = trituple*4;
  75. result = new unsigned char[rlength];
  76. unsigned char* p = result;
  77. const unsigned char* s = src;
  78. const unsigned char* smax = s+slength-r;
  79. while(s != smax) {
  80. int n = *s++ << 16;
  81. n += *s++ << 8;
  82. n += *s++;
  83. *p++ = CHAR_TABLE[n >> 18];
  84. *p++ = CHAR_TABLE[n >> 12&0x3f];
  85. *p++ = CHAR_TABLE[n >> 6&0x3f];
  86. *p++ = CHAR_TABLE[n&0x3f];
  87. }
  88. if(r == 2) {
  89. int n = *s++ << 16;
  90. n += *s++ << 8;
  91. *p++ = CHAR_TABLE[n >> 18];
  92. *p++ = CHAR_TABLE[n >> 12&0x3f];
  93. *p++ = CHAR_TABLE[n >> 6&0x3f];
  94. *p++ = '=';
  95. } else if(r == 1) {
  96. int n = *s++ << 16;
  97. *p++ = CHAR_TABLE[n >> 18];
  98. *p++ = CHAR_TABLE[n >> 12&0x3f];
  99. *p++ = '=';
  100. *p++ = '=';
  101. }
  102. }
  103. void Base64::removeNonBase64Chars(unsigned char*& nsrc,
  104. size_t& nlength,
  105. const unsigned char* src,
  106. size_t slength)
  107. {
  108. unsigned char* temp = new unsigned char[slength];
  109. const unsigned char* end = src+slength;
  110. size_t n = 0;
  111. for(const unsigned char*s = src; s != end; ++s) {
  112. if(INDEX_TABLE[*s] != -1 || *s == '=') {
  113. *(temp+n++) = *s;
  114. }
  115. }
  116. nlength = n;
  117. nsrc = temp;
  118. }
  119. void Base64::decode(unsigned char*& result, size_t& rlength,
  120. const unsigned char* src, size_t slength)
  121. {
  122. if(slength == 0) {
  123. rlength = 0;
  124. return;
  125. }
  126. unsigned char* nsrc;
  127. size_t nlength;
  128. removeNonBase64Chars(nsrc, nlength, src, slength);
  129. if(nlength%4 != 0) {
  130. delete [] nsrc;
  131. rlength = 0;
  132. return;
  133. }
  134. size_t quadtuple = nlength/4;
  135. size_t len;
  136. if(nsrc[nlength-1] == '=') {
  137. if(nsrc[nlength-2] == '=') {
  138. len = nlength-2;
  139. } else {
  140. len = nlength-1;
  141. }
  142. } else {
  143. len = nlength;
  144. }
  145. rlength = len-quadtuple;
  146. result = new unsigned char[rlength];
  147. int r = len%4;
  148. unsigned char* p = result;
  149. const unsigned char* s = nsrc;
  150. const unsigned char* smax = s+len-r;
  151. while(s != smax) {
  152. int n = INDEX_TABLE[*s++] << 18;
  153. n += INDEX_TABLE[*s++] << 12;
  154. n += INDEX_TABLE[*s++] << 6;
  155. n += INDEX_TABLE[*s++];
  156. *p++ = n >> 16;
  157. *p++ = n >> 8&0xff;
  158. *p++ = n&0xff;
  159. }
  160. if(r == 2) {
  161. int n = INDEX_TABLE[*s++] << 18;
  162. n += INDEX_TABLE[*s++] << 12;
  163. *p++ = n >> 16;
  164. } else if(r == 3) {
  165. int n = INDEX_TABLE[*s++] << 18;
  166. n += INDEX_TABLE[*s++] << 12;
  167. n += INDEX_TABLE[*s++] << 6;
  168. *p++ = n >> 16;
  169. *p++ = n >> 8&0xff;
  170. }
  171. delete [] nsrc;
  172. }
  173. std::string Base64::encode(const std::string& s)
  174. {
  175. unsigned char* buf = 0;
  176. size_t len;
  177. encode(buf, len, s.c_str(), s.size());
  178. std::string r(&buf[0], &buf[len]);
  179. delete [] buf;
  180. return r;
  181. }
  182. std::string Base64::decode(const std::string& s)
  183. {
  184. unsigned char* buf = 0;
  185. size_t len;
  186. decode(buf, len, s.c_str(), s.size());
  187. std::string r(&buf[0], &buf[len]);
  188. delete [] buf;
  189. return r;
  190. }
  191. } // namespace aria2