~ubuntu-branches/ubuntu/oneiric/notecase/oneiric

« back to all changes in this revision

Viewing changes to src/lib/Base64.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Soyez
  • Date: 2008-11-10 11:29:57 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20081110112957-9uu6p24w7i0c7ib2
Tags: 1.9.7-0ubuntu1
* New Upstream Release
* Updated Standards-Version to 3.8.0, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
//
9
9
// Base64.cpp: implementation of the CBase64 class.
10
10
// Author: Wes Clyburn (clyburnw@enmu.edu)
11
 
// 
 
11
//
12
12
// This code was practically stolen from:
13
 
// Dr. Dobb's Journal, September 1995, 
 
13
// Dr. Dobb's Journal, September 1995,
14
14
// "Mime and Internet Mail", by Tim Kientzle
15
15
//////////////////////////////////////////////////////////////////////
16
16
// WARNING: this class DOES NOT pad  with '=' as per RFC 1521
25
25
//
26
26
 
27
27
// The 7-bit alphabet used to encode binary information
28
 
std::string CBase64::m_sBase64Alphabet = 
 
28
std::string CBase64::m_sBase64Alphabet =
29
29
 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30
30
 
31
31
int CBase64::m_nMask[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
42
42
{
43
43
}
44
44
 
45
 
// Matt Spaulding (1999/01/22) 
 
45
// Matt Spaulding (1999/01/22)
46
46
std::string CBase64::Encode(const char *szEncoding, int nSize)
47
47
{
48
48
        ASSERT( szEncoding != NULL );
49
49
        ASSERT( nSize > 0 );  //don't send empty strings (or empty files)
50
50
        if( szEncoding == NULL || 0 == nSize )
51
51
                return std::string("");
52
 
        
 
52
 
53
53
        char *sOutput = new char[nSize*2+1];  //lazy/safe calculation
54
54
        int nNumBits = 6;
55
55
        unsigned int nDigit;
56
56
        int pos=0, target=BYTES_PER_LINE, lp = 0;
57
57
        std::string result;
58
 
        
 
58
 
59
59
        m_szInput = szEncoding;
60
60
        m_nInputSize = nSize;
61
 
        
 
61
 
62
62
        m_nBitsRemaining = 0;
63
63
        nDigit = read_bits( nNumBits, &nNumBits, lp );
64
64
        while( nNumBits > 0 )
65
65
        {
66
66
                sOutput[pos++] = m_sBase64Alphabet[ (int)nDigit ];
67
67
                nDigit = read_bits( nNumBits, &nNumBits, lp );
68
 
                
 
68
 
69
69
                if(pos==target)
70
70
                {
71
71
                        sOutput[pos++] = '\r';
73
73
                        target=pos+BYTES_PER_LINE;       // Not including CR/LF, already handled
74
74
                }
75
75
        }
76
 
        
 
76
 
77
77
        sOutput[pos]=0;
78
78
        result=sOutput;
79
79
        delete [] sOutput;
104
104
        // Build Decode Table
105
105
        //
106
106
        int i;
107
 
        for( i = 0; i < 256; i++ ) 
 
107
        for( i = 0; i < 256; i++ )
108
108
                nDecode[i] = -2; // Illegal digit
109
109
        for( i=0; i < 64; i++ )
110
110
        {
111
111
                nDecode[ (int) m_sBase64Alphabet[ i ] ] = i;
112
112
                nDecode[ m_sBase64Alphabet[ i ] | 0x80 ] = i; // Ignore 8th bit
113
 
                nDecode[ (int)'=' ] = -1; 
 
113
                nDecode[ (int)'=' ] = -1;
114
114
                nDecode[ '=' | 0x80 ] = -1; // Ignore MIME padding char
115
115
        }
116
116
 
117
117
        // Clear the output buffer
118
118
        memset( szOutput, 0, sInput.size() + 1 );
119
 
        
 
119
 
120
120
        m_lBitStorage = 0;              //(IVO) somebody failed to initialize
121
 
        m_nBitsRemaining =0;    //(IVO) 
 
121
        m_nBitsRemaining =0;    //(IVO)
122
122
 
123
123
        // Decode the Input
124
124
        //
126
126
        for( lp = 0, i = 0; lp < nSize; lp++ )
127
127
        {
128
128
                c = sInput[ lp ];
 
129
 
 
130
                //skip new lines
 
131
                if(c == '\r' || c == '\n')
 
132
                        continue;
 
133
 
129
134
                nDigit = nDecode[ c & 0x7F ];
130
 
                if( nDigit < -1 ) 
 
135
                if( nDigit < -1 )
131
136
                {
132
137
                        return 0;
133
 
                } 
134
 
                else if( nDigit >= 0 ) 
 
138
                }
 
139
                else if( nDigit >= 0 )
135
140
                        // i (index into output) is incremented by write_bits()
136
141
                        write_bits( nDigit & 0x3F, 6, szOutput, i );
137
 
    }   
 
142
    }
138
143
        return i;
139
144
}
140
145
 
142
147
unsigned int CBase64::read_bits(int nNumBits, int * pBitsRead, int& lp)
143
148
{
144
149
    unsigned long lScratch;
145
 
    while( ( m_nBitsRemaining < nNumBits ) && 
146
 
                   ( lp < m_nInputSize ) ) 
 
150
    while( ( m_nBitsRemaining < nNumBits ) &&
 
151
                   ( lp < m_nInputSize ) )
147
152
        {
148
153
                int c = m_szInput[ lp++ ];
149
154
        m_lBitStorage <<= 8;
150
155
        m_lBitStorage |= (c & 0xff);
151
156
                m_nBitsRemaining += 8;
152
157
    }
153
 
    if( m_nBitsRemaining < nNumBits ) 
 
158
    if( m_nBitsRemaining < nNumBits )
154
159
        {
155
160
                lScratch = m_lBitStorage << ( nNumBits - m_nBitsRemaining );
156
161
                *pBitsRead = m_nBitsRemaining;
157
162
                m_nBitsRemaining = 0;
158
 
    } 
159
 
        else 
 
163
    }
 
164
        else
160
165
        {
161
166
                lScratch = m_lBitStorage >> ( m_nBitsRemaining - nNumBits );
162
167
                *pBitsRead = nNumBits;
174
179
 
175
180
        m_lBitStorage = (m_lBitStorage << nNumBits) | nBits;
176
181
        m_nBitsRemaining += nNumBits;
177
 
        while( m_nBitsRemaining > 7 ) 
 
182
        while( m_nBitsRemaining > 7 )
178
183
        {
179
184
                nScratch = m_lBitStorage >> (m_nBitsRemaining - 8);
180
185
                szOutput[ i++ ] = nScratch & 0xFF;
181
186
                m_nBitsRemaining -= 8;
182
187
        }
183
188
}
184