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
31using namespace com::sun::star;
32
33namespace comphelper {
34
35const
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
43const
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
64template <typename C>
65static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, C* aCharBuffer)
66{
67 const sal_Int32 nLen(std::min(nFullLen - nStart, sal_Int32(3)));
68 assert(nLen > 0); // We are never expected to leave the output buffer uninitialized
69
70 sal_Int32 nBinaer;
71 switch (nLen)
72 {
73 case 1:
74 {
75 nBinaer = static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16;
76 }
77 break;
78 case 2:
79 {
80 nBinaer = (static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16) +
81 (static_cast<sal_uInt8>(pBuffer[nStart + 1]) << 8);
82 }
83 break;
84 default:
85 {
86 nBinaer = (static_cast<sal_uInt8>(pBuffer[nStart + 0]) << 16) +
87 (static_cast<sal_uInt8>(pBuffer[nStart + 1]) << 8) +
88 static_cast<sal_uInt8>(pBuffer[nStart + 2]);
89 }
90 break;
91 }
92
93 aCharBuffer[2] = aCharBuffer[3] = '=';
94
95 sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18));
96 aCharBuffer[0] = aBase64EncodeTable [nIndex];
97
98 nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F000) >> 12);
99 aCharBuffer[1] = aBase64EncodeTable [nIndex];
100 if (nLen > 1)
101 {
102 nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6);
103 aCharBuffer[2] = aBase64EncodeTable [nIndex];
104 if (nLen > 2)
105 {
106 nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F));
107 aCharBuffer[3] = aBase64EncodeTable [nIndex];
108 }
109 }
110}
111
112template <typename Buffer>
113static void base64encode(Buffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
114{
115 sal_Int32 i(0);
116 sal_Int32 nBufferLength(aPass.getLength());
117 aStrBuffer.ensureCapacity(aStrBuffer.getLength() + (nBufferLength * 4 + 2) / 3);
118 const sal_Int8* pBuffer = aPass.getConstArray();
119 while (i < nBufferLength)
120 {
121 ThreeByteToFourByte(pBuffer, i, nBufferLength, aStrBuffer.appendUninitialized(4));
122 i += 3;
123 }
124}
125
126void Base64::encode(OStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
127{
128 base64encode(aStrBuffer, aPass);
129}
130
131void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
132{
133 base64encode(aStrBuffer, aPass);
134}
135
136void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, std::u16string_view sBuffer)
137{
138 std::size_t nCharsDecoded = decodeSomeChars( aBuffer, sBuffer );
139 OSL_ENSURE( nCharsDecoded == sBuffer.size(), "some bytes left in base64 decoding!" );
140}
141
142std::size_t Base64::decodeSomeChars(uno::Sequence<sal_Int8>& rOutBuffer, std::u16string_view rInBuffer)
143{
144 std::size_t nInBufferLen = rInBuffer.size();
145 std::size_t nMinOutBufferLen = (nInBufferLen / 4) * 3;
146 if( o3tl::make_unsigned(rOutBuffer.getLength()) < nMinOutBufferLen )
147 rOutBuffer.realloc( nMinOutBufferLen );
148
149 const sal_Unicode *pInBuffer = rInBuffer.data();
150 sal_Int8 *pOutBuffer = rOutBuffer.getArray();
151 sal_Int8 *pOutBufferStart = pOutBuffer;
152 std::size_t nCharsDecoded = 0;
153
154 sal_uInt8 aDecodeBuffer[4];
155 sal_Int32 nBytesToDecode = 0;
156 sal_Int32 nBytesGotFromDecoding = 3;
157 std::size_t nInBufferPos= 0;
158 while( nInBufferPos < nInBufferLen )
159 {
160 sal_Unicode cChar = *pInBuffer;
161 if( cChar >= '+' && cChar <= 'z' )
162 {
163 sal_uInt8 nByte = aBase64DecodeTable[cChar-'+'];
164 if( nByte != 255 )
165 {
166 // We have found a valid character!
167 aDecodeBuffer[nBytesToDecode++] = nByte;
168
169 // One '=' character at the end means 2 out bytes
170 // Two '=' characters at the end mean 1 out bytes
171 if( '=' == cChar && nBytesToDecode > 2 )
172 nBytesGotFromDecoding--;
173 if( 4 == nBytesToDecode )
174 {
175 // Four characters found, so we may convert now!
176 sal_uInt32 nOut = (aDecodeBuffer[0] << 18) +
177 (aDecodeBuffer[1] << 12) +
178 (aDecodeBuffer[2] << 6) +
179 aDecodeBuffer[3];
180
181 *pOutBuffer++ = static_cast<sal_Int8>((nOut & 0xff0000) >> 16);
182 if( nBytesGotFromDecoding > 1 )
183 *pOutBuffer++ = static_cast<sal_Int8>((nOut & 0xff00) >> 8);
184 if( nBytesGotFromDecoding > 2 )
185 *pOutBuffer++ = static_cast<sal_Int8>(nOut & 0xff);
186 nCharsDecoded = nInBufferPos + 1;
187 nBytesToDecode = 0;
188 nBytesGotFromDecoding = 3;
189 }
190 }
191 else
192 {
193 nCharsDecoded++;
194 }
195 }
196 else
197 {
198 nCharsDecoded++;
199 }
200
201 nInBufferPos++;
202 pInBuffer++;
203 }
204 if( (pOutBuffer - pOutBufferStart) != rOutBuffer.getLength() )
205 rOutBuffer.realloc( pOutBuffer - pOutBufferStart );
206
207 return nCharsDecoded;
208}
209
210}
211
212/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int8 > Buffer
static std::size_t decodeSomeChars(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
Definition: base64.cxx:142
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
encodes the given byte sequence into Base64
static void decode(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
Definition: base64.cxx:136
sal_Int32 nIndex
static void base64encode(Buffer &aStrBuffer, const uno::Sequence< sal_Int8 > &aPass)
Definition: base64.cxx:113
const sal_uInt8 aBase64DecodeTable[]
Definition: base64.cxx:44
const char aBase64EncodeTable[]
Definition: base64.cxx:36
static void ThreeByteToFourByte(const sal_Int8 *pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, C *aCharBuffer)
Definition: base64.cxx:65
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
signed char sal_Int8
std::unique_ptr< char[]> aBuffer