LibreOffice Module comphelper (master)  1
base64.cxx
Go to the documentation of this file.
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 <sal/config.h>
21 
22 #include <cstddef>
23 
24 #include <comphelper/base64.hxx>
25 
26 #include <com/sun/star/uno/Sequence.hxx>
27 
28 #include <o3tl/safeint.hxx>
29 #include <osl/diagnose.h>
30 
31 using namespace com::sun::star;
32 
33 namespace comphelper {
34 
35 const
37  { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
38  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
39  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
40  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
41  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
42 
43 const
45  { 62,255,255,255, 63, // 43-47
46 // + /
47 
48  52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, // 48-63
49 // 0 1 2 3 4 5 6 7 8 9 =
50 
51  255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
52 // A B C D E F G H I J K L M N O
53 
54  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, // 80-95
55 // P Q R S T U V W X Y Z
56 
57  0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
58 // a b c d e f g h i j k l m n o
59 
60  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; // 112-123
61 // p q r s t u v w x y z
62 
63 
64 static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, char* aCharBuffer)
65 {
66  sal_Int32 nLen(nFullLen - nStart);
67  if (nLen > 3)
68  nLen = 3;
69  if (nLen == 0)
70  {
71  return;
72  }
73 
74  sal_Int32 nBinaer;
75  switch (nLen)
76  {
77  case 1:
78  {
79  nBinaer = static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16;
80  }
81  break;
82  case 2:
83  {
84  nBinaer = (static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16) +
85  (static_cast<sal_uInt8>(pBuffer[nStart + 1]) << 8);
86  }
87  break;
88  default:
89  {
90  nBinaer = (static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16) +
91  (static_cast<sal_uInt8>(pBuffer[nStart + 1]) << 8) +
92  static_cast<sal_uInt8>(pBuffer[nStart + 2]);
93  }
94  break;
95  }
96 
97  aCharBuffer[0] = aCharBuffer[1] = aCharBuffer[2] = aCharBuffer[3] = '=';
98 
99  sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18));
100  aCharBuffer[0] = aBase64EncodeTable [nIndex];
101 
102  nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F000) >> 12);
103  aCharBuffer[1] = aBase64EncodeTable [nIndex];
104  if (nLen > 1)
105  {
106  nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6);
107  aCharBuffer[2] = aBase64EncodeTable [nIndex];
108  if (nLen > 2)
109  {
110  nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F));
111  aCharBuffer[3] = aBase64EncodeTable [nIndex];
112  }
113  }
114 }
115 
116 void Base64::encode(OStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
117 {
118  sal_Int32 i(0);
119  sal_Int32 nBufferLength(aPass.getLength());
120  const sal_Int8* pBuffer = aPass.getConstArray();
121  while (i < nBufferLength)
122  {
123  char aCharBuffer[4];
124  ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
125  aStrBuffer.append(aCharBuffer, std::size(aCharBuffer));
126  i += 3;
127  }
128 }
129 
130 void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
131 {
132  sal_Int32 i(0);
133  sal_Int32 nBufferLength(aPass.getLength());
134  const sal_Int8* pBuffer = aPass.getConstArray();
135  while (i < nBufferLength)
136  {
137  char aCharBuffer[4];
138  ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
139  aStrBuffer.appendAscii(aCharBuffer, std::size(aCharBuffer));
140  i += 3;
141  }
142 }
143 
144 void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, std::u16string_view sBuffer)
145 {
146  std::size_t nCharsDecoded = decodeSomeChars( aBuffer, sBuffer );
147  OSL_ENSURE( nCharsDecoded == sBuffer.size(), "some bytes left in base64 decoding!" );
148 }
149 
150 std::size_t Base64::decodeSomeChars(uno::Sequence<sal_Int8>& rOutBuffer, std::u16string_view rInBuffer)
151 {
152  std::size_t nInBufferLen = rInBuffer.size();
153  std::size_t nMinOutBufferLen = (nInBufferLen / 4) * 3;
154  if( o3tl::make_unsigned(rOutBuffer.getLength()) < nMinOutBufferLen )
155  rOutBuffer.realloc( nMinOutBufferLen );
156 
157  const sal_Unicode *pInBuffer = rInBuffer.data();
158  sal_Int8 *pOutBuffer = rOutBuffer.getArray();
159  sal_Int8 *pOutBufferStart = pOutBuffer;
160  std::size_t nCharsDecoded = 0;
161 
162  sal_uInt8 aDecodeBuffer[4];
163  sal_Int32 nBytesToDecode = 0;
164  sal_Int32 nBytesGotFromDecoding = 3;
165  std::size_t nInBufferPos= 0;
166  while( nInBufferPos < nInBufferLen )
167  {
168  sal_Unicode cChar = *pInBuffer;
169  if( cChar >= '+' && cChar <= 'z' )
170  {
171  sal_uInt8 nByte = aBase64DecodeTable[cChar-'+'];
172  if( nByte != 255 )
173  {
174  // We have found a valid character!
175  aDecodeBuffer[nBytesToDecode++] = nByte;
176 
177  // One '=' character at the end means 2 out bytes
178  // Two '=' characters at the end mean 1 out bytes
179  if( '=' == cChar && nBytesToDecode > 2 )
180  nBytesGotFromDecoding--;
181  if( 4 == nBytesToDecode )
182  {
183  // Four characters found, so we may convert now!
184  sal_uInt32 nOut = (aDecodeBuffer[0] << 18) +
185  (aDecodeBuffer[1] << 12) +
186  (aDecodeBuffer[2] << 6) +
187  aDecodeBuffer[3];
188 
189  *pOutBuffer++ = static_cast<sal_Int8>((nOut & 0xff0000) >> 16);
190  if( nBytesGotFromDecoding > 1 )
191  *pOutBuffer++ = static_cast<sal_Int8>((nOut & 0xff00) >> 8);
192  if( nBytesGotFromDecoding > 2 )
193  *pOutBuffer++ = static_cast<sal_Int8>(nOut & 0xff);
194  nCharsDecoded = nInBufferPos + 1;
195  nBytesToDecode = 0;
196  nBytesGotFromDecoding = 3;
197  }
198  }
199  else
200  {
201  nCharsDecoded++;
202  }
203  }
204  else
205  {
206  nCharsDecoded++;
207  }
208 
209  nInBufferPos++;
210  pInBuffer++;
211  }
212  if( (pOutBuffer - pOutBufferStart) != rOutBuffer.getLength() )
213  rOutBuffer.realloc( pOutBuffer - pOutBufferStart );
214 
215  return nCharsDecoded;
216 }
217 
218 }
219 
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
signed char sal_Int8
static void ThreeByteToFourByte(const sal_Int8 *pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, char *aCharBuffer)
Definition: base64.cxx:64
sal_uInt16 sal_Unicode
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
const char aBase64EncodeTable[]
unsigned char sal_uInt8
const sal_uInt8 aBase64DecodeTable[]
Definition: base64.cxx:44