Base64.cc 3.7 KB

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