Base64.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. static char base64_table[64] = {
  37. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  38. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  39. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  40. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  41. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  42. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  43. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  44. '4', '5', '6', '7', '8', '9', '+', '/',
  45. };
  46. void Base64::part_encode(const unsigned char* sub, int subLength,
  47. unsigned char* buf)
  48. {
  49. int shift = 2;
  50. unsigned char carry = 0;
  51. int index;
  52. for(index = 0; index < subLength; index++) {
  53. unsigned char cur = sub[index] >> shift | carry;
  54. carry = (sub[index] << (6-shift)) & 0x3f;
  55. shift += 2;
  56. buf[index] = base64_table[(unsigned int)cur];
  57. }
  58. if(subLength == 1) {
  59. buf[index] = base64_table[(unsigned int)carry];
  60. buf[index+1] = buf[index+2] = '=';
  61. } else if(subLength == 2) {
  62. buf[index] = base64_table[(unsigned int)carry];
  63. buf[index+1] = '=';
  64. } else {
  65. unsigned char cur = sub[subLength-1] & 0x3f;
  66. buf[index] = base64_table[(unsigned int)cur];
  67. }
  68. }
  69. string Base64::encode(const string& plainSrc)
  70. {
  71. unsigned char* result = 0;
  72. int resultLength = 0;
  73. encode((const unsigned char*)plainSrc.c_str(), plainSrc.size(),
  74. result, resultLength);
  75. string encoded(&result[0], &result[resultLength]);
  76. delete [] result;
  77. return encoded;
  78. }
  79. void Base64::encode(const unsigned char* src, int srcLength,
  80. unsigned char*& result, int& resultLength) {
  81. resultLength = (srcLength+(srcLength%3 == 0 ? 0 : 3-srcLength%3))/3*4;
  82. result = new unsigned char[resultLength];
  83. unsigned char* tail = result;
  84. for(int index = 0; srcLength > index; index += 3) {
  85. unsigned char temp[4];
  86. part_encode(&src[index],
  87. srcLength >= index+3 ? 3 : srcLength-index,
  88. temp);
  89. memcpy(tail, temp, sizeof(temp));
  90. tail += sizeof(temp);
  91. }
  92. }
  93. char Base64::getValue(char ch)
  94. {
  95. char retch;
  96. if(ch >= 'A' && ch <= 'Z') {
  97. retch = ch-'A';
  98. } else if(ch >= 'a' && ch <= 'z') {
  99. retch = ch-'a'+26;
  100. } else if(ch >= '0' && ch <= '9') {
  101. retch = ch-'0'+52;
  102. } else if(ch == '+') {
  103. retch = 62;
  104. } else if(ch == '/') {
  105. retch = 63;
  106. } else {
  107. retch = 0;
  108. }
  109. return retch;
  110. }
  111. string Base64::part_decode(const string& subCrypted)
  112. {
  113. int shift = 2;
  114. string plain;
  115. for(unsigned int index = 0; index < subCrypted.size()-1; ++index) {
  116. if(subCrypted.at(index) == '=') break;
  117. char cur = getValue(subCrypted.at(index)) << shift;
  118. char carry = getValue(subCrypted.at(index+1)) >> (6-shift);
  119. shift += 2;
  120. plain += cur | carry;
  121. }
  122. return plain;
  123. }
  124. string Base64::decode(const string& crypted)
  125. {
  126. string plain;
  127. int sIndex = 0;
  128. for(int index = 0; crypted.size() > (unsigned int)index; index +=4) {
  129. string subCrypted = crypted.substr(sIndex, 4);
  130. string subPlain = part_decode(subCrypted);
  131. sIndex += 4;
  132. plain += subPlain;
  133. }
  134. return plain;
  135. }