Base64.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. namespace {
  38. const char CHAR_TABLE[] = {
  39. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  40. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  41. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  42. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  43. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  44. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  45. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  46. '4', '5', '6', '7', '8', '9', '+', '/',
  47. };
  48. } // namespace
  49. namespace {
  50. const int INDEX_TABLE[] = {
  51. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  52. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  53. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  54. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  55. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  56. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  57. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  58. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -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. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  65. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  66. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  67. };
  68. } // namespace
  69. void Base64::encode(unsigned char*& result, size_t& rlength,
  70. const unsigned char* src, size_t slength)
  71. {
  72. if(slength == 0) {
  73. rlength = 0;
  74. return;
  75. }
  76. size_t trituple = (slength+2)/3;
  77. int r = slength%3;
  78. rlength = trituple*4;
  79. result = new unsigned char[rlength];
  80. unsigned char* p = result;
  81. const unsigned char* s = src;
  82. const unsigned char* smax = s+slength-r;
  83. while(s != smax) {
  84. int n = *s++ << 16;
  85. n += *s++ << 8;
  86. n += *s++;
  87. *p++ = CHAR_TABLE[n >> 18];
  88. *p++ = CHAR_TABLE[n >> 12&0x3fu];
  89. *p++ = CHAR_TABLE[n >> 6&0x3fu];
  90. *p++ = CHAR_TABLE[n&0x3fu];
  91. }
  92. if(r == 2) {
  93. int n = *s++ << 16;
  94. n += *s++ << 8;
  95. *p++ = CHAR_TABLE[n >> 18];
  96. *p++ = CHAR_TABLE[n >> 12&0x3fu];
  97. *p++ = CHAR_TABLE[n >> 6&0x3fu];
  98. *p++ = '=';
  99. } else if(r == 1) {
  100. int n = *s++ << 16;
  101. *p++ = CHAR_TABLE[n >> 18];
  102. *p++ = CHAR_TABLE[n >> 12&0x3fu];
  103. *p++ = '=';
  104. *p++ = '=';
  105. }
  106. }
  107. void Base64::removeNonBase64Chars(unsigned char*& nsrc,
  108. size_t& nlength,
  109. const unsigned char* src,
  110. size_t slength)
  111. {
  112. unsigned char* temp = new unsigned char[slength];
  113. const unsigned char* end = src+slength;
  114. size_t n = 0;
  115. for(const unsigned char*s = src; s != end; ++s) {
  116. if(INDEX_TABLE[*s] != -1 || *s == '=') {
  117. *(temp+n++) = *s;
  118. }
  119. }
  120. nlength = n;
  121. nsrc = temp;
  122. }
  123. void Base64::decode(unsigned char*& result, size_t& rlength,
  124. const unsigned char* src, size_t slength)
  125. {
  126. if(slength == 0) {
  127. rlength = 0;
  128. return;
  129. }
  130. unsigned char* nsrc;
  131. size_t nlength;
  132. removeNonBase64Chars(nsrc, nlength, src, slength);
  133. if(nlength%4 != 0) {
  134. delete [] nsrc;
  135. rlength = 0;
  136. return;
  137. }
  138. size_t quadtuple = nlength/4;
  139. size_t len;
  140. if(nsrc[nlength-1] == '=') {
  141. if(nsrc[nlength-2] == '=') {
  142. len = nlength-2;
  143. } else {
  144. len = nlength-1;
  145. }
  146. } else {
  147. len = nlength;
  148. }
  149. rlength = len-quadtuple;
  150. result = new unsigned char[rlength];
  151. int r = len%4;
  152. unsigned char* p = result;
  153. const unsigned char* s = nsrc;
  154. const unsigned char* smax = s+len-r;
  155. while(s != smax) {
  156. int n = INDEX_TABLE[*s++] << 18;
  157. n += INDEX_TABLE[*s++] << 12;
  158. n += INDEX_TABLE[*s++] << 6;
  159. n += INDEX_TABLE[*s++];
  160. *p++ = n >> 16;
  161. *p++ = n >> 8&0xffu;
  162. *p++ = n&0xffu;
  163. }
  164. if(r == 2) {
  165. int n = INDEX_TABLE[*s++] << 18;
  166. n += INDEX_TABLE[*s++] << 12;
  167. *p++ = n >> 16;
  168. } else if(r == 3) {
  169. int n = INDEX_TABLE[*s++] << 18;
  170. n += INDEX_TABLE[*s++] << 12;
  171. n += INDEX_TABLE[*s++] << 6;
  172. *p++ = n >> 16;
  173. *p++ = n >> 8&0xffu;
  174. }
  175. delete [] nsrc;
  176. }
  177. std::string Base64::encode(const std::string& s)
  178. {
  179. unsigned char* buf = 0;
  180. size_t len;
  181. encode(buf, len, s.c_str(), s.size());
  182. std::string r(&buf[0], &buf[len]);
  183. delete [] buf;
  184. return r;
  185. }
  186. std::string Base64::decode(const std::string& s)
  187. {
  188. unsigned char* buf = 0;
  189. size_t len;
  190. decode(buf, len, s.c_str(), s.size());
  191. std::string r(&buf[0], &buf[len]);
  192. delete [] buf;
  193. return r;
  194. }
  195. } // namespace aria2