| 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 | // CS_NAMESPACE_BEGIN
|
|---|
| 24 |
|
|---|
| 25 | //! \class Base64 base64.h
|
|---|
| 26 | //! \brief Base64 conversion functions.
|
|---|
| 27 | //!
|
|---|
| 28 | //! Converts Base64 data between arrays and strings.
|
|---|
| 29 | //!
|
|---|
| 30 | //! \code
|
|---|
| 31 | //! #include "base64.h"
|
|---|
| 32 | //!
|
|---|
| 33 | //! ...
|
|---|
| 34 | //!
|
|---|
| 35 | //! // encode a block of data into base64
|
|---|
| 36 | //! QByteArray block(1024);
|
|---|
| 37 | //! QByteArray enc = Base64::encode(block);
|
|---|
| 38 | //!
|
|---|
| 39 | //! \endcode
|
|---|
| 40 |
|
|---|
| 41 | //!
|
|---|
| 42 | //! Encodes array \a s and returns the result.
|
|---|
| 43 | QByteArray Base64::encode(const QByteArray &s)
|
|---|
| 44 | {
|
|---|
| 45 | int i;
|
|---|
| 46 | int len = s.size();
|
|---|
| 47 | char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|---|
| 48 | int a, b, c;
|
|---|
| 49 |
|
|---|
| 50 | QByteArray p((len+2)/3*4);
|
|---|
| 51 | int at = 0;
|
|---|
| 52 | for( i = 0; i < len; i += 3 ) {
|
|---|
| 53 | a = ((unsigned char)s[i] & 3) << 4;
|
|---|
| 54 | if(i + 1 < len) {
|
|---|
| 55 | a += (unsigned char)s[i + 1] >> 4;
|
|---|
| 56 | b = ((unsigned char)s[i + 1] & 0xF) << 2;
|
|---|
| 57 | if(i + 2 < len) {
|
|---|
| 58 | b += (unsigned char)s[i + 2] >> 6;
|
|---|
| 59 | c = (unsigned char)s[i + 2] & 0x3F;
|
|---|
| 60 | }
|
|---|
| 61 | else
|
|---|
| 62 | c = 64;
|
|---|
| 63 | }
|
|---|
| 64 | else
|
|---|
| 65 | b = c = 64;
|
|---|
| 66 |
|
|---|
| 67 | p[at++] = tbl[(unsigned char)s[i] >> 2];
|
|---|
| 68 | p[at++] = tbl[a];
|
|---|
| 69 | p[at++] = tbl[b];
|
|---|
| 70 | p[at++] = tbl[c];
|
|---|
| 71 | }
|
|---|
| 72 | return p;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | //!
|
|---|
| 76 | //! Decodes array \a s and returns the result.
|
|---|
| 77 | QByteArray Base64::decode(const QByteArray &s)
|
|---|
| 78 | {
|
|---|
| 79 | // return value
|
|---|
| 80 | QByteArray p;
|
|---|
| 81 |
|
|---|
| 82 | // -1 specifies invalid
|
|---|
| 83 | // 64 specifies eof
|
|---|
| 84 | // everything else specifies data
|
|---|
| 85 |
|
|---|
| 86 | char tbl[] = {
|
|---|
| 87 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|---|
| 88 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|---|
| 89 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
|---|
| 90 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,64,-1,-1,
|
|---|
| 91 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
|
|---|
| 92 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
|
|---|
| 93 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
|
|---|
| 94 | 41,42,43,44,45,46,47,48,49,50,51,-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 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|---|
| 102 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | // this should be a multiple of 4
|
|---|
| 106 | int len = s.size();
|
|---|
| 107 |
|
|---|
| 108 | if(len % 4)
|
|---|
| 109 | return p;
|
|---|
| 110 |
|
|---|
| 111 | p.resize(len / 4 * 3);
|
|---|
| 112 |
|
|---|
| 113 | int i;
|
|---|
| 114 | int at = 0;
|
|---|
| 115 |
|
|---|
| 116 | int a, b, c, d;
|
|---|
| 117 | c = d = 0;
|
|---|
| 118 |
|
|---|
| 119 | for( i = 0; i < len; i += 4 ) {
|
|---|
| 120 | a = tbl[(int)s[i]];
|
|---|
| 121 | b = tbl[(int)s[i + 1]];
|
|---|
| 122 | c = tbl[(int)s[i + 2]];
|
|---|
| 123 | d = tbl[(int)s[i + 3]];
|
|---|
| 124 | if((a == 64 || b == 64) || (a < 0 || b < 0 || c < 0 || d < 0)) {
|
|---|
| 125 | p.resize(0);
|
|---|
| 126 | return p;
|
|---|
| 127 | }
|
|---|
| 128 | p[at++] = ((a & 0x3F) << 2) | ((b >> 4) & 0x03);
|
|---|
| 129 | p[at++] = ((b & 0x0F) << 4) | ((c >> 2) & 0x0F);
|
|---|
| 130 | p[at++] = ((c & 0x03) << 6) | ((d >> 0) & 0x3F);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if(c & 64)
|
|---|
| 134 | p.resize(at - 2);
|
|---|
| 135 | else if(d & 64)
|
|---|
| 136 | p.resize(at - 1);
|
|---|
| 137 |
|
|---|
| 138 | return p;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | //!
|
|---|
| 142 | //! Encodes array \a a and returns the result as a string.
|
|---|
| 143 | QString Base64::arrayToString(const QByteArray &a)
|
|---|
| 144 | {
|
|---|
| 145 | QByteArray b = encode(a);
|
|---|
| 146 | QCString c;
|
|---|
| 147 | c.resize(b.size()+1);
|
|---|
| 148 | memcpy(c.data(), b.data(), b.size());
|
|---|
| 149 | return QString::fromLatin1(c);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | //!
|
|---|
| 153 | //! Decodes string \a s and returns the result as an array.
|
|---|
| 154 | QByteArray Base64::stringToArray(const QString &s)
|
|---|
| 155 | {
|
|---|
| 156 | if(s.isEmpty())
|
|---|
| 157 | return QByteArray();
|
|---|
| 158 |
|
|---|
| 159 | // Unfold data
|
|---|
| 160 | QString us(s);
|
|---|
| 161 | us.remove('\n');
|
|---|
| 162 |
|
|---|
| 163 | const char *c = us.latin1();
|
|---|
| 164 | int len = strlen(c);
|
|---|
| 165 | QByteArray b(len);
|
|---|
| 166 | memcpy(b.data(), c, len);
|
|---|
| 167 | QByteArray a = decode(b);
|
|---|
| 168 | return a;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | //!
|
|---|
| 172 | //! Encodes string \a s and returns the result as a string.
|
|---|
| 173 | QString Base64::encodeString(const QString &s)
|
|---|
| 174 | {
|
|---|
| 175 | QCString c = s.utf8();
|
|---|
| 176 | int len = c.length();
|
|---|
| 177 | QByteArray b(len);
|
|---|
| 178 | memcpy(b.data(), c.data(), len);
|
|---|
| 179 | return arrayToString(b);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | // CS_NAMESPACE_END
|
|---|