summaryrefslogtreecommitdiff
path: root/src/Base64.cc
blob: e0b3aa86dd8a1a4bb85266be6a2843df675e3ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* <!-- copyright */
/*
 * aria2 - a simple utility for downloading files faster
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/* copyright --> */
#include "Base64.h"

string Base64::part_encode(const string& subplain)
{
  static char base64_table[64] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/',
  };

  int shift = 2;
  char carry = 0;
  bool ignore_flag = false;
  string crypted;

  for(unsigned int index = 0; index < subplain.size(); ++index) {
    if(ignore_flag) {
      crypted += '=';
    } else {
      char cur = subplain.at(index) >> shift | carry;
      if(subplain.at(index) == 0) ignore_flag = true;
      carry = (subplain.at(index) << (6-shift)) & 0x3F;
      shift += 2;
      crypted += base64_table[(unsigned int)cur];
    }
  }
  if(subplain.at(subplain.size()-1) == 0) {
    crypted += '=';
  } else {
    char cur = subplain.at(subplain.size()-1) & 0x3F;
    crypted += base64_table[(unsigned int)cur];
  }

  return crypted;
}

string Base64::encode(string plain)
{
  int remainder = plain.size() % 3;
  if( remainder ) remainder = 3-remainder;
  for(int i = 0; i < remainder; ++i) plain += (char)0;
  string crypted;
  int start_pos = 0;
  for(unsigned int index = 0; plain.size() > index; index += 3) {
    string subplain = plain.substr(start_pos, 3);
    string subcrypted = part_encode(subplain);
    start_pos += 3;
    crypted += subcrypted;
  }
  return crypted;
}

char Base64::getValue(char ch)
{
  char retch;

  if(ch >= 'A' && ch <= 'Z') {
    retch = ch-'A';
  } else if(ch >= 'a' && ch <= 'z') {
    retch = ch-'a'+26;
  } else if(ch >= '0' && ch <= '9') {
    retch = ch-'0'+52;
  } else if(ch == '+') {
    retch = 62;
  } else if(ch == '/') {
    retch = 63;
  } else {
    retch = 0;
  }
  return retch;
}

string Base64::part_decode(const string& subCrypted)
{
  int shift = 2;
  string plain;

  for(unsigned int index = 0; index < subCrypted.size()-1; ++index) {
    if(subCrypted.at(index) == '=') break;
    char cur = getValue(subCrypted.at(index)) << shift;
    char carry = getValue(subCrypted.at(index+1)) >> (6-shift);
    shift += 2;
    plain += cur | carry;
  }

  return plain;
}

string Base64::decode(string crypted)
{
  string plain;
  int sIndex = 0;
  for(int index = 0; crypted.size() > (unsigned int)index; index +=4) {
    string subCrypted = crypted.substr(sIndex, 4);
    string subPlain = part_decode(subCrypted);
    sIndex += 4;
    plain += subPlain;
  }
  return plain;
}