1 | /*
|
---|
2 | * base64.cpp - Base64 converting functions
|
---|
3 | * Copyright (C) 2003 Justin Karneges
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2.1 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include"base64.h"
|
---|
22 |
|
---|
23 | //! \class Base64 base64.h
|
---|
24 | //! \brief Base64 conversion functions.
|
---|
25 | //!
|
---|
26 | //! Converts Base64 data between arrays and strings.
|
---|
27 | //!
|
---|
28 | //! \code
|
---|
29 | //! #include "base64.h"
|
---|
30 | //!
|
---|
31 | //! ...
|
---|
32 | //!
|
---|
33 | //! // encode a block of data into base64
|
---|
34 | //! QByteArray block(1024);
|
---|
35 | //! QByteArray enc = Base64::encode(block);
|
---|
36 | //!
|
---|
37 | //! \endcode
|
---|
38 |
|
---|
39 | //!
|
---|
40 | //! Encodes array \a s and returns the result.
|
---|
41 | QByteArray Base64::encode(const QByteArray &s)
|
---|
42 | {
|
---|
43 | int i;
|
---|
44 | int len = s.size();
|
---|
45 | char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
---|
46 | int a, b, c;
|
---|
47 |
|
---|
48 | QByteArray p((len+2)/3*4);
|
---|
49 | int at = 0;
|
---|
50 | for( i = 0; i < len; i += 3 ) {
|
---|
51 | a = ((unsigned char)s[i] & 3) << 4;
|
---|
52 | if(i + 1 < len) {
|
---|
53 | a += (unsigned char)s[i + 1] >> 4;
|
---|
54 | b = ((unsigned char)s[i + 1] & 0xF) << 2;
|
---|
55 | if(i + 2 < len) {
|
---|
56 | b += (unsigned char)s[i + 2] >> 6;
|
---|
57 | c = (unsigned char)s[i + 2] & 0x3F;
|
---|
58 | }
|
---|
59 | else
|
---|
60 | c = 64;
|
---|
61 | }
|
---|
62 | else
|
---|
63 | b = c = 64;
|
---|
64 |
|
---|
65 | p[at++] = tbl[(unsigned char)s[i] >> 2];
|
---|
66 | p[at++] = tbl[a];
|
---|
67 | p[at++] = tbl[b];
|
---|
68 | p[at++] = tbl[c];
|
---|
69 | }
|
---|
70 | return p;
|
---|
71 | }
|
---|
72 |
|
---|
73 | //!
|
---|
74 | //! Decodes array \a s and returns the result.
|
---|
75 | QByteArray Base64::decode(const QByteArray &s)
|
---|
76 | {
|
---|
77 | // return value
|
---|
78 | QByteArray p;
|
---|
79 |
|
---|
80 | // -1 specifies invalid
|
---|
81 | // 64 specifies eof
|
---|
82 | // everything else specifies data
|
---|
83 |
|
---|
84 | char tbl[] = {
|
---|
85 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
86 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
87 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
---|
88 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,64,-1,-1,
|
---|
89 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
|
---|
90 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
|
---|
91 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
|
---|
92 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
|
---|
93 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
94 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
95 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
96 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
97 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
98 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
99 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
100 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
---|
101 | };
|
---|
102 |
|
---|
103 | // this should be a multiple of 4
|
---|
104 | int len = s.size();
|
---|
105 |
|
---|
106 | if(len % 4)
|
---|
107 | return p;
|
---|
108 |
|
---|
109 | p.resize(len / 4 * 3);
|
---|
110 |
|
---|
111 | int i;
|
---|
112 | int at = 0;
|
---|
113 |
|
---|
114 | int a, b, c, d;
|
---|
115 | c = d = 0;
|
---|
116 |
|
---|
117 | for( i = 0; i < len; i += 4 ) {
|
---|
118 | a = tbl[(int)s[i]];
|
---|
119 | b = tbl[(int)s[i + 1]];
|
---|
120 | c = tbl[(int)s[i + 2]];
|
---|
121 | d = tbl[(int)s[i + 3]];
|
---|
122 | if((a == 64 || b == 64) || (a < 0 || b < 0 || c < 0 || d < 0)) {
|
---|
123 | p.resize(0);
|
---|
124 | return p;
|
---|
125 | }
|
---|
126 | p[at++] = ((a & 0x3F) << 2) | ((b >> 4) & 0x03);
|
---|
127 | p[at++] = ((b & 0x0F) << 4) | ((c >> 2) & 0x0F);
|
---|
128 | p[at++] = ((c & 0x03) << 6) | ((d >> 0) & 0x3F);
|
---|
129 | }
|
---|
130 |
|
---|
131 | if(c & 64)
|
---|
132 | p.resize(at - 2);
|
---|
133 | else if(d & 64)
|
---|
134 | p.resize(at - 1);
|
---|
135 |
|
---|
136 | return p;
|
---|
137 | }
|
---|
138 |
|
---|
139 | //!
|
---|
140 | //! Encodes array \a a and returns the result as a string.
|
---|
141 | QString Base64::arrayToString(const QByteArray &a)
|
---|
142 | {
|
---|
143 | QByteArray b = encode(a);
|
---|
144 | QCString c;
|
---|
145 | c.resize(b.size()+1);
|
---|
146 | memcpy(c.data(), b.data(), b.size());
|
---|
147 | return QString::fromLatin1(c);
|
---|
148 | }
|
---|
149 |
|
---|
150 | //!
|
---|
151 | //! Decodes string \a s and returns the result as an array.
|
---|
152 | QByteArray Base64::stringToArray(const QString &s)
|
---|
153 | {
|
---|
154 | if(s.isEmpty())
|
---|
155 | return QByteArray();
|
---|
156 | const char *c = s.latin1();
|
---|
157 | int len = strlen(c);
|
---|
158 | QByteArray b(len);
|
---|
159 | memcpy(b.data(), c, len);
|
---|
160 | QByteArray a = decode(b);
|
---|
161 | return a;
|
---|
162 | }
|
---|
163 |
|
---|
164 | //!
|
---|
165 | //! Encodes string \a s and returns the result as a string.
|
---|
166 | QString Base64::encodeString(const QString &s)
|
---|
167 | {
|
---|
168 | QCString c = s.utf8();
|
---|
169 | int len = c.length();
|
---|
170 | QByteArray b(len);
|
---|
171 | memcpy(b.data(), c.data(), len);
|
---|
172 | return arrayToString(b);
|
---|
173 | }
|
---|