~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to iris/src/xmpp/base64/base64.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "xmpp/base64/base64.h"
 
2
 
 
3
namespace XMPP {
 
4
 
 
5
QString Base64::encode(const QByteArray &s)
 
6
{
 
7
        int i;
 
8
        int len = s.size();
 
9
        char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 
10
        int a, b, c;
 
11
 
 
12
        QByteArray p;
 
13
        p.resize((len+2)/3*4);
 
14
        int at = 0;
 
15
        for( i = 0; i < len; i += 3 ) {
 
16
                a = ((unsigned char)s[i] & 3) << 4;
 
17
                if(i + 1 < len) {
 
18
                        a += (unsigned char)s[i + 1] >> 4;
 
19
                        b = ((unsigned char)s[i + 1] & 0xF) << 2;
 
20
                        if(i + 2 < len) {
 
21
                                b += (unsigned char)s[i + 2] >> 6;
 
22
                                c = (unsigned char)s[i + 2] & 0x3F;
 
23
                        }
 
24
                        else
 
25
                                c = 64;
 
26
                }
 
27
                else {
 
28
                        b = c = 64;
 
29
                }
 
30
 
 
31
                p[at++] = tbl[(unsigned char)s[i] >> 2];
 
32
                p[at++] = tbl[a];
 
33
                p[at++] = tbl[b];
 
34
                p[at++] = tbl[c];
 
35
        }
 
36
        return QString::fromAscii(p);
 
37
}
 
38
 
 
39
QByteArray Base64::decode(const QString& input)
 
40
{
 
41
        QByteArray s(QString(input).remove('\n').toUtf8());
 
42
        QByteArray p;
 
43
 
 
44
        // -1 specifies invalid
 
45
        // 64 specifies eof
 
46
        // everything else specifies data
 
47
 
 
48
        char tbl[] = {
 
49
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
50
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
51
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
 
52
                52,53,54,55,56,57,58,59,60,61,-1,-1,-1,64,-1,-1,
 
53
                -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
 
54
                15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
 
55
                -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
 
56
                41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
 
57
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
58
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
59
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
60
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
61
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
62
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
63
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
64
                -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
 
65
        };
 
66
 
 
67
        // this should be a multiple of 4
 
68
        int len = s.size();
 
69
 
 
70
        if(len % 4) {
 
71
                return p;
 
72
        }
 
73
 
 
74
        p.resize(len / 4 * 3);
 
75
 
 
76
        int i;
 
77
        int at = 0;
 
78
 
 
79
        int a, b, c, d;
 
80
        c = d = 0;
 
81
 
 
82
        for( i = 0; i < len; i += 4 ) {
 
83
                a = tbl[(int)s[i]];
 
84
                b = tbl[(int)s[i + 1]];
 
85
                c = tbl[(int)s[i + 2]];
 
86
                d = tbl[(int)s[i + 3]];
 
87
                if((a == 64 || b == 64) || (a < 0 || b < 0 || c < 0 || d < 0)) {
 
88
                        p.resize(0);
 
89
                        return p;
 
90
                }
 
91
                p[at++] = ((a & 0x3F) << 2) | ((b >> 4) & 0x03);
 
92
                p[at++] = ((b & 0x0F) << 4) | ((c >> 2) & 0x0F);
 
93
                p[at++] = ((c & 0x03) << 6) | ((d >> 0) & 0x3F);
 
94
        }
 
95
 
 
96
        if(c & 64)
 
97
                p.resize(at - 2);
 
98
        else if(d & 64)
 
99
                p.resize(at - 1);
 
100
 
 
101
        return p;
 
102
}
 
103
 
 
104
}