LibreOffice Module package (master) 1
XUnbufferedStream.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 <com/sun/star/packages/zip/ZipConstants.hpp>
21#include <com/sun/star/packages/zip/ZipIOException.hpp>
22#include <com/sun/star/xml/crypto/CipherID.hpp>
23
24#include "XUnbufferedStream.hxx"
25#include <EncryptionData.hxx>
26#include <ZipFile.hxx>
28#include <algorithm>
29#include <string.h>
30
31#include <o3tl/safeint.hxx>
32#include <osl/diagnose.h>
33#include <osl/mutex.hxx>
34#include <utility>
36
37using namespace ::com::sun::star;
38using namespace com::sun::star::packages::zip::ZipConstants;
39using namespace com::sun::star::io;
40using namespace com::sun::star::uno;
41using com::sun::star::packages::zip::ZipIOException;
42
44 const uno::Reference< uno::XComponentContext >& xContext,
46 ZipEntry const & rEntry,
47 Reference < XInputStream > const & xNewZipStream,
48 const ::rtl::Reference< EncryptionData >& rData,
49 sal_Int8 nStreamMode,
50 bool bIsEncrypted,
51 const OUString& aMediaType,
52 bool bRecoveryMode )
53: maMutexHolder(std::move( aMutexHolder ))
54, mxZipStream ( xNewZipStream )
55, mxZipSeek ( xNewZipStream, UNO_QUERY )
56, maEntry ( rEntry )
57, mnBlockSize( 1 )
58, maInflater ( true )
59, mbRawStream ( nStreamMode == UNBUFF_STREAM_RAW || nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
60, mbWrappedRaw ( nStreamMode == UNBUFF_STREAM_WRAPPEDRAW )
61, mnHeaderToRead ( 0 )
62, mnZipCurrent ( 0 )
63, mnZipEnd ( 0 )
64, mnZipSize ( 0 )
65, mnMyCurrent ( 0 )
66, mbCheckCRC(!bRecoveryMode)
67{
68 mnZipCurrent = maEntry.nOffset;
69 sal_Int64 nSize;
70 if ( mbRawStream )
71 {
72 mnZipSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize;
73 nSize = mnZipSize;
74 }
75 else
76 {
77 mnZipSize = maEntry.nSize;
78 nSize = maEntry.nMethod == DEFLATED ? maEntry.nCompressedSize : maEntry.nSize;
79 }
80
81 if (mnZipSize < 0)
82 throw ZipIOException("The stream seems to be broken!");
83
84 if (o3tl::checked_add(maEntry.nOffset, nSize, mnZipEnd))
85 throw ZipIOException("Integer-overflow");
86
87 bool bHaveEncryptData = rData.is() && rData->m_aInitVector.hasElements() &&
88 ((rData->m_aSalt.hasElements() && rData->m_nIterationCount != 0)
89 ||
90 rData->m_aKey.hasElements());
91 bool bMustDecrypt = nStreamMode == UNBUFF_STREAM_DATA && bHaveEncryptData && bIsEncrypted;
92
93 if ( bMustDecrypt )
94 {
95 m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
96 mnBlockSize = ( rData->m_nEncAlg == xml::crypto::CipherID::AES_CBC_W3C_PADDING ? 16 : 1 );
97 }
98
99 if ( !(bHaveEncryptData && mbWrappedRaw && bIsEncrypted) )
100 return;
101
102 // if we have the data needed to decrypt it, but didn't want it decrypted (or
103 // we couldn't decrypt it due to wrong password), then we prepend this
104 // data to the stream
105
106 // Make a buffer big enough to hold both the header and the data itself
107 maHeader.realloc ( n_ConstHeaderSize +
108 rData->m_aInitVector.getLength() +
109 rData->m_aSalt.getLength() +
110 rData->m_aDigest.getLength() +
111 aMediaType.getLength() * sizeof( sal_Unicode ) );
112 sal_Int8 * pHeader = maHeader.getArray();
113 ZipFile::StaticFillHeader( rData, rEntry.nSize, aMediaType, pHeader );
114 mnHeaderToRead = static_cast < sal_Int16 > ( maHeader.getLength() );
115 mnZipSize += mnHeaderToRead;
116}
117
118// allows to read package raw stream
121 const Reference < XInputStream >& xRawStream,
122 const ::rtl::Reference< EncryptionData >& rData )
123: maMutexHolder(std::move( aMutexHolder ))
124, mxZipStream ( xRawStream )
125, mxZipSeek ( xRawStream, UNO_QUERY )
126, mnBlockSize( 1 )
127, maInflater ( true )
128, mbRawStream ( false )
129, mbWrappedRaw ( false )
130, mnHeaderToRead ( 0 )
131, mnZipCurrent ( 0 )
132, mnZipEnd ( 0 )
133, mnZipSize ( 0 )
134, mnMyCurrent ( 0 )
135, mbCheckCRC( false )
136{
137 // for this scenario maEntry is not set !!!
138 OSL_ENSURE( mxZipSeek.is(), "The stream must be seekable!" );
139
140 // skip raw header, it must be already parsed to rData
141 mnZipCurrent = n_ConstHeaderSize + rData->m_aInitVector.getLength() +
142 rData->m_aSalt.getLength() + rData->m_aDigest.getLength();
143
144 try {
145 if ( mxZipSeek.is() )
146 mnZipSize = mxZipSeek->getLength();
147 } catch( const Exception& )
148 {
149 // in case of problem the size will stay set to 0
150 TOOLS_WARN_EXCEPTION("package", "ignoring");
151 }
152
153 mnZipEnd = mnZipCurrent + mnZipSize;
154
155 // the raw data will not be decrypted, no need for the cipher
156 // m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
157}
158
160{
161}
162
163sal_Int32 SAL_CALL XUnbufferedStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
164{
165 ::osl::MutexGuard aGuard( maMutexHolder->GetMutex() );
166
167 sal_Int32 nRequestedBytes = nBytesToRead;
168 OSL_ENSURE( !mnHeaderToRead || mbWrappedRaw, "Only encrypted raw stream can be provided with header!" );
169 if ( mnMyCurrent + nRequestedBytes > mnZipSize + maHeader.getLength() )
170 nRequestedBytes = static_cast < sal_Int32 > ( mnZipSize + maHeader.getLength() - mnMyCurrent );
171
172 sal_Int32 nTotal = 0;
173 aData.realloc ( nRequestedBytes );
174 if ( nRequestedBytes )
175 {
176 sal_Int32 nRead = 0;
177 sal_Int32 nLastRead = 0;
178 if ( mbRawStream )
179 {
180 sal_Int64 nDiff = mnZipEnd - mnZipCurrent;
181
183 {
184 sal_Int16 nHeadRead = static_cast< sal_Int16 >(( nRequestedBytes > mnHeaderToRead ?
185 mnHeaderToRead : nRequestedBytes ));
186 memcpy ( aData.getArray(), maHeader.getConstArray() + maHeader.getLength() - mnHeaderToRead, nHeadRead );
187 mnHeaderToRead = mnHeaderToRead - nHeadRead;
188
189 if ( nHeadRead < nRequestedBytes )
190 {
191 sal_Int32 nToRead = nRequestedBytes - nHeadRead;
192 nToRead = ( nDiff < nToRead ) ? sal::static_int_cast< sal_Int32 >( nDiff ) : nToRead;
193
194 Sequence< sal_Int8 > aPureData( nToRead );
195 mxZipSeek->seek ( mnZipCurrent );
196 nRead = mxZipStream->readBytes ( aPureData, nToRead );
197 mnZipCurrent += nRead;
198
199 aPureData.realloc( nRead );
200 if ( mbCheckCRC )
201 maCRC.update( aPureData );
202
203 aData.realloc( nHeadRead + nRead );
204
205 const sal_Int8* pPureBuffer = aPureData.getConstArray();
206 sal_Int8* pBuffer = aData.getArray();
207 for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ )
208 pBuffer[ nHeadRead + nInd ] = pPureBuffer[ nInd ];
209 }
210
211 nRead += nHeadRead;
212 }
213 else
214 {
215 mxZipSeek->seek ( mnZipCurrent );
216
217 nRead = mxZipStream->readBytes (
218 aData,
219 std::min<sal_Int64>(nDiff, nRequestedBytes) );
220
221 mnZipCurrent += nRead;
222
223 aData.realloc( nRead );
224 if ( mbWrappedRaw && mbCheckCRC )
225 maCRC.update( aData );
226 }
227 }
228 else
229 {
230 for (;;)
231 {
232 nLastRead = maInflater.doInflateSegment( aData, nRead, aData.getLength() - nRead );
233 if ( 0 != nLastRead && ( nRead + nLastRead == nRequestedBytes || mnZipCurrent >= mnZipEnd ) )
234 break;
235 nRead += nLastRead;
236 if ( nRead > nRequestedBytes )
237 throw RuntimeException(
238 "Should not be possible to read more than requested!" );
239
241 throw ZipIOException("The stream seems to be broken!" );
242
244 throw ZipIOException("Dictionaries are not supported!" );
245
246 sal_Int32 nDiff = static_cast< sal_Int32 >( mnZipEnd - mnZipCurrent );
247 if ( nDiff <= 0 )
248 {
249 throw ZipIOException("The stream seems to be broken!" );
250 }
251
252 mxZipSeek->seek ( mnZipCurrent );
253
254 sal_Int32 nToRead = std::max( nRequestedBytes, static_cast< sal_Int32 >( 8192 ) );
255 if ( mnBlockSize > 1 )
256 nToRead = nToRead + mnBlockSize - nToRead % mnBlockSize;
257 nToRead = std::min( nDiff, nToRead );
258
259 sal_Int32 nZipRead = mxZipStream->readBytes( maCompBuffer, nToRead );
260 if ( nZipRead < nToRead )
261 throw ZipIOException("No expected data!" );
262
263 mnZipCurrent += nZipRead;
264 // maCompBuffer now has the data, check if we need to decrypt
265 // before passing to the Inflater
266 if ( m_xCipherContext.is() )
267 {
268 if ( mbCheckCRC )
270
271 maCompBuffer = m_xCipherContext->convertWithCipherContext( maCompBuffer );
272 if ( mnZipCurrent == mnZipEnd )
273 {
274 uno::Sequence< sal_Int8 > aSuffix = m_xCipherContext->finalizeCipherContextAndDispose();
275 if ( aSuffix.hasElements() )
276 {
277 sal_Int32 nOldLen = maCompBuffer.getLength();
278 maCompBuffer.realloc( nOldLen + aSuffix.getLength() );
279 memcpy( maCompBuffer.getArray() + nOldLen, aSuffix.getConstArray(), aSuffix.getLength() );
280 }
281 }
282 }
284
285 }
286 }
287
288 mnMyCurrent += nRead + nLastRead;
289 nTotal = nRead + nLastRead;
290 if ( nTotal < nRequestedBytes)
291 aData.realloc ( nTotal );
292
293 if ( mbCheckCRC && ( !mbRawStream || mbWrappedRaw ) )
294 {
295 if ( !m_xCipherContext.is() && !mbWrappedRaw )
296 maCRC.update( aData );
297
298 if ( mnZipSize + maHeader.getLength() == mnMyCurrent && maCRC.getValue() != maEntry.nCrc )
299 throw ZipIOException("The stream seems to be broken!" );
300 }
301 }
302
303 return nTotal;
304}
305
306sal_Int32 SAL_CALL XUnbufferedStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
307{
308 return readBytes ( aData, nMaxBytesToRead );
309}
310void SAL_CALL XUnbufferedStream::skipBytes( sal_Int32 nBytesToSkip )
311{
312 if ( nBytesToSkip )
313 {
314 Sequence < sal_Int8 > aSequence ( nBytesToSkip );
315 readBytes ( aSequence, nBytesToSkip );
316 }
317}
318
319sal_Int32 SAL_CALL XUnbufferedStream::available( )
320{
321 //available size must include the prepended header in case of wrapped raw stream
322 return static_cast< sal_Int32 > ( std::min< sal_Int64 >( SAL_MAX_INT32, (mnZipSize + mnHeaderToRead - mnMyCurrent) ) );
323}
324
326{
327}
328
329/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_Int32 n_ConstHeaderSize
#define UNBUFF_STREAM_RAW
#define UNBUFF_STREAM_DATA
#define UNBUFF_STREAM_WRAPPEDRAW
sal_Int32 getValue() const
Definition: CRC32.cxx:39
void update(const css::uno::Sequence< sal_Int8 > &b)
Update CRC32 with specified sequence of bytes.
Definition: CRC32.cxx:51
css::uno::Reference< css::io::XInputStream > mxZipStream
virtual void SAL_CALL closeInput() override
virtual ~XUnbufferedStream() override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
rtl::Reference< comphelper::RefCountedMutex > maMutexHolder
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
css::uno::Sequence< sal_Int8 > maHeader
css::uno::Sequence< sal_Int8 > maCompBuffer
ZipUtils::Inflater maInflater
css::uno::Reference< css::xml::crypto::XCipherContext > m_xCipherContext
css::uno::Reference< css::io::XSeekable > mxZipSeek
XUnbufferedStream(const css::uno::Reference< css::uno::XComponentContext > &xContext, rtl::Reference< comphelper::RefCountedMutex > aMutexHolder, ZipEntry const &rEntry, css::uno::Reference< css::io::XInputStream > const &xNewZipStream, const ::rtl::Reference< EncryptionData > &rData, sal_Int8 nStreamMode, bool bIsEncrypted, const OUString &aMediaType, bool bRecoveryMode)
virtual sal_Int32 SAL_CALL available() override
sal_Int32 doInflateSegment(css::uno::Sequence< sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
Definition: Inflater.cxx:69
bool finished() const
Definition: Inflater.hxx:46
sal_Int32 getLastInflateError() const
Definition: Inflater.hxx:50
bool needsDictionary() const
Definition: Inflater.hxx:45
void setInput(const css::uno::Sequence< sal_Int8 > &rBuffer)
Definition: Inflater.cxx:61
#define TOOLS_WARN_EXCEPTION(area, stream)
constexpr OUStringLiteral aData
std::enable_if< std::is_signed< T >::value, bool >::type checked_add(T a, T b, T &result)
sal_Int32 nCrc
Definition: ZipEntry.hxx:30
sal_Int64 nSize
Definition: ZipEntry.hxx:32
#define SAL_MAX_INT32
sal_uInt16 sal_Unicode
signed char sal_Int8