Base64.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. string Base64::part_encode(const string& subplain)
  24. {
  25. static char base64_table[64] = {
  26. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  27. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  28. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  29. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  30. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  31. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  32. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  33. '4', '5', '6', '7', '8', '9', '+', '/',
  34. };
  35. int shift = 2;
  36. char carry = 0;
  37. bool ignore_flag = false;
  38. string crypted;
  39. for(unsigned int index = 0; index < subplain.size(); ++index) {
  40. if(ignore_flag) {
  41. crypted += '=';
  42. } else {
  43. char cur = subplain.at(index) >> shift | carry;
  44. if(subplain.at(index) == 0) ignore_flag = true;
  45. carry = (subplain.at(index) << (6-shift)) & 0x3F;
  46. shift += 2;
  47. crypted += base64_table[(unsigned int)cur];
  48. }
  49. }
  50. if(subplain.at(subplain.size()-1) == 0) {
  51. crypted += '=';
  52. } else {
  53. char cur = subplain.at(subplain.size()-1) & 0x3F;
  54. crypted += base64_table[(unsigned int)cur];
  55. }
  56. return crypted;
  57. }
  58. string Base64::encode(string plain)
  59. {
  60. int remainder = plain.size() % 3;
  61. if( remainder ) remainder = 3-remainder;
  62. for(int i = 0; i < remainder; ++i) plain += (char)0;
  63. string crypted;
  64. int start_pos = 0;
  65. for(unsigned int index = 0; plain.size() > index; index += 3) {
  66. string subplain = plain.substr(start_pos, 3);
  67. string subcrypted = part_encode(subplain);
  68. start_pos += 3;
  69. crypted += subcrypted;
  70. }
  71. return crypted;
  72. }
  73. char Base64::getValue(char ch)
  74. {
  75. char retch;
  76. if(ch >= 'A' && ch <= 'Z') {
  77. retch = ch-'A';
  78. } else if(ch >= 'a' && ch <= 'z') {
  79. retch = ch-'a'+26;
  80. } else if(ch >= '0' && ch <= '9') {
  81. retch = ch-'0'+52;
  82. } else if(ch == '+') {
  83. retch = 62;
  84. } else if(ch == '/') {
  85. retch = 63;
  86. } else {
  87. retch = 0;
  88. }
  89. return retch;
  90. }
  91. string Base64::part_decode(const string& subCrypted)
  92. {
  93. int shift = 2;
  94. string plain;
  95. for(unsigned int index = 0; index < subCrypted.size()-1; ++index) {
  96. if(subCrypted.at(index) == '=') break;
  97. char cur = getValue(subCrypted.at(index)) << shift;
  98. char carry = getValue(subCrypted.at(index+1)) >> (6-shift);
  99. shift += 2;
  100. plain += cur | carry;
  101. }
  102. return plain;
  103. }
  104. string Base64::decode(string crypted)
  105. {
  106. string plain;
  107. int sIndex = 0;
  108. for(int index = 0; crypted.size() > (unsigned int)index; index +=4) {
  109. string subCrypted = crypted.substr(sIndex, 4);
  110. string subPlain = part_decode(subCrypted);
  111. sIndex += 4;
  112. plain += subPlain;
  113. }
  114. return plain;
  115. }