1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <comphelper/base64.hxx>
21 
22 #include <com/sun/star/uno/Sequence.hxx>
23 
24 #include <osl/diagnose.h>
25 
26 using namespace com::sun::star;
27 
28 namespace comphelper {
29 
30 const
31   char aBase64EncodeTable[] =
32     { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
33       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
34       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
35       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
36       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
37 
38 const
39   sal_uInt8 aBase64DecodeTable[]  =
40     {                                            62,255,255,255, 63, // 43-47
41 //                                                +               /
42 
43      52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255, // 48-63
44 //    0   1   2   3   4   5   6   7   8   9               =
45 
46     255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, // 64-79
47 //        A   B   C   D   E   F   G   H   I   J   K   L   M   N   O
48 
49      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, // 80-95
50 //    P   Q   R   S   T   U   V   W   X   Y   Z
51 
52       0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
53 //        a   b   c   d   e   f   g   h   i   j   k   l   m   n   o
54 
55      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; // 112-123
56 //    p   q   r   s   t   u   v   w   x   y   z
57 
58 
ThreeByteToFourByte(const sal_Int8 * pBuffer,const sal_Int32 nStart,const sal_Int32 nFullLen,char * aCharBuffer)59 static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, char* aCharBuffer)
60 {
61     sal_Int32 nLen(nFullLen - nStart);
62     if (nLen > 3)
63         nLen = 3;
64     if (nLen == 0)
65     {
66         return;
67     }
68 
69     sal_Int32 nBinaer;
70     switch (nLen)
71     {
72         case 1:
73         {
74             nBinaer = static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16;
75         }
76         break;
77         case 2:
78         {
79             nBinaer = (static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16) +
80                     (static_cast<sal_uInt8>(pBuffer[nStart + 1]) <<  8);
81         }
82         break;
83         default:
84         {
85             nBinaer = (static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16) +
86                     (static_cast<sal_uInt8>(pBuffer[nStart + 1]) <<  8) +
87                     static_cast<sal_uInt8>(pBuffer[nStart + 2]);
88         }
89         break;
90     }
91 
92     aCharBuffer[0] = aCharBuffer[1] = aCharBuffer[2] = aCharBuffer[3] = '=';
93 
94     sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18));
95     aCharBuffer[0] = aBase64EncodeTable [nIndex];
96 
97     nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F000) >> 12);
98     aCharBuffer[1] = aBase64EncodeTable [nIndex];
99     if (nLen > 1)
100     {
101         nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6);
102         aCharBuffer[2] = aBase64EncodeTable [nIndex];
103         if (nLen > 2)
104         {
105             nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F));
106             aCharBuffer[3] = aBase64EncodeTable [nIndex];
107         }
108     }
109 }
110 
encode(OStringBuffer & aStrBuffer,const uno::Sequence<sal_Int8> & aPass)111 void Base64::encode(OStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
112 {
113     sal_Int32 i(0);
114     sal_Int32 nBufferLength(aPass.getLength());
115     const sal_Int8* pBuffer = aPass.getConstArray();
116     while (i < nBufferLength)
117     {
118         char aCharBuffer[4];
119         ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
120         aStrBuffer.append(aCharBuffer, SAL_N_ELEMENTS(aCharBuffer));
121         i += 3;
122     }
123 }
124 
encode(OUStringBuffer & aStrBuffer,const uno::Sequence<sal_Int8> & aPass)125 void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
126 {
127     sal_Int32 i(0);
128     sal_Int32 nBufferLength(aPass.getLength());
129     const sal_Int8* pBuffer = aPass.getConstArray();
130     while (i < nBufferLength)
131     {
132         char aCharBuffer[4];
133         ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
134         aStrBuffer.appendAscii(aCharBuffer, SAL_N_ELEMENTS(aCharBuffer));
135         i += 3;
136     }
137 }
138 
decode(uno::Sequence<sal_Int8> & aBuffer,const OUString & sBuffer)139 void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, const OUString& sBuffer)
140 {
141     sal_Int32 nCharsDecoded = decodeSomeChars( aBuffer, sBuffer );
142     OSL_ENSURE( nCharsDecoded == sBuffer.getLength(), "some bytes left in base64 decoding!" );
143 }
144 
decodeSomeChars(uno::Sequence<sal_Int8> & rOutBuffer,const OUString & rInBuffer)145 sal_Int32 Base64::decodeSomeChars(uno::Sequence<sal_Int8>& rOutBuffer, const OUString& rInBuffer)
146 {
147     sal_Int32 nInBufferLen = rInBuffer.getLength();
148     sal_Int32 nMinOutBufferLen = (nInBufferLen / 4) * 3;
149     if( rOutBuffer.getLength() < nMinOutBufferLen )
150         rOutBuffer.realloc( nMinOutBufferLen );
151 
152     const sal_Unicode *pInBuffer = rInBuffer.getStr();
153     sal_Int8 *pOutBuffer = rOutBuffer.getArray();
154     sal_Int8 *pOutBufferStart = pOutBuffer;
155     sal_Int32 nCharsDecoded = 0;
156 
157     sal_uInt8 aDecodeBuffer[4];
158     sal_Int32 nBytesToDecode = 0;
159     sal_Int32 nBytesGotFromDecoding = 3;
160     sal_Int32 nInBufferPos= 0;
161     while( nInBufferPos < nInBufferLen )
162     {
163         sal_Unicode cChar = *pInBuffer;
164         if( cChar >= '+' && cChar <= 'z' )
165         {
166             sal_uInt8 nByte = aBase64DecodeTable[cChar-'+'];
167             if( nByte != 255 )
168             {
169                 // We have found a valid character!
170                 aDecodeBuffer[nBytesToDecode++] = nByte;
171 
172                 // One '=' character at the end means 2 out bytes
173                 // Two '=' characters at the end mean 1 out bytes
174                 if( '=' == cChar && nBytesToDecode > 2 )
175                     nBytesGotFromDecoding--;
176                 if( 4 == nBytesToDecode )
177                 {
178                     // Four characters found, so we may convert now!
179                     sal_uInt32 nOut = (aDecodeBuffer[0] << 18) +
180                                       (aDecodeBuffer[1] << 12) +
181                                       (aDecodeBuffer[2] << 6) +
182                                        aDecodeBuffer[3];
183 
184                     *pOutBuffer++  = static_cast<sal_Int8>((nOut & 0xff0000) >> 16);
185                     if( nBytesGotFromDecoding > 1 )
186                         *pOutBuffer++  = static_cast<sal_Int8>((nOut & 0xff00) >> 8);
187                     if( nBytesGotFromDecoding > 2 )
188                         *pOutBuffer++  = static_cast<sal_Int8>(nOut & 0xff);
189                     nCharsDecoded = nInBufferPos + 1;
190                     nBytesToDecode = 0;
191                     nBytesGotFromDecoding = 3;
192                 }
193             }
194             else
195             {
196                 nCharsDecoded++;
197             }
198         }
199         else
200         {
201             nCharsDecoded++;
202         }
203 
204         nInBufferPos++;
205         pInBuffer++;
206     }
207     if( (pOutBuffer - pOutBufferStart) != rOutBuffer.getLength() )
208         rOutBuffer.realloc( pOutBuffer - pOutBufferStart );
209 
210     return nCharsDecoded;
211 }
212 
213 }
214 
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
216