1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
24 #include "XUnbufferedStream.hxx"
25 #include <EncryptionData.hxx>
26 #include <PackageConstants.hxx>
27 #include <ZipFile.hxx>
28 #include <EncryptedDataHeader.hxx>
32 #include <osl/diagnose.h>
33 #include <osl/mutex.hxx>
35 using namespace ::com::sun::star
;
36 using namespace com::sun::star::packages::zip::ZipConstants
;
37 using namespace com::sun::star::io
;
38 using namespace com::sun::star::uno
;
39 using com::sun::star::packages::zip::ZipIOException
;
41 XUnbufferedStream::XUnbufferedStream(
42 const uno::Reference
< uno::XComponentContext
>& xContext
,
43 const rtl::Reference
< comphelper::RefCountedMutex
>& aMutexHolder
,
44 ZipEntry
const & rEntry
,
45 Reference
< XInputStream
> const & xNewZipStream
,
46 const ::rtl::Reference
< EncryptionData
>& rData
,
49 const OUString
& aMediaType
,
51 : maMutexHolder( aMutexHolder
)
52 , mxZipStream ( xNewZipStream
)
53 , mxZipSeek ( xNewZipStream
, UNO_QUERY
)
57 , mbRawStream ( nStreamMode
== UNBUFF_STREAM_RAW
|| nStreamMode
== UNBUFF_STREAM_WRAPPEDRAW
)
58 , mbWrappedRaw ( nStreamMode
== UNBUFF_STREAM_WRAPPEDRAW
)
59 , mnHeaderToRead ( 0 )
64 , mbCheckCRC( !bRecoveryMode
)
66 mnZipCurrent
= maEntry
.nOffset
;
69 mnZipSize
= maEntry
.nMethod
== DEFLATED
? maEntry
.nCompressedSize
: maEntry
.nSize
;
70 mnZipEnd
= maEntry
.nOffset
+ mnZipSize
;
74 mnZipSize
= maEntry
.nSize
;
75 mnZipEnd
= maEntry
.nMethod
== DEFLATED
? maEntry
.nOffset
+ maEntry
.nCompressedSize
: maEntry
.nOffset
+ maEntry
.nSize
;
79 throw ZipIOException("The stream seems to be broken!");
81 bool bHaveEncryptData
= rData
.is() && rData
->m_aInitVector
.getLength() &&
82 ((rData
->m_aSalt
.getLength() && rData
->m_nIterationCount
!= 0)
84 rData
->m_aKey
.getLength());
85 bool bMustDecrypt
= nStreamMode
== UNBUFF_STREAM_DATA
&& bHaveEncryptData
&& bIsEncrypted
;
89 m_xCipherContext
= ZipFile::StaticGetCipher( xContext
, rData
, false );
90 mnBlockSize
= ( rData
->m_nEncAlg
== xml::crypto::CipherID::AES_CBC_W3C_PADDING
? 16 : 1 );
93 if ( bHaveEncryptData
&& mbWrappedRaw
&& bIsEncrypted
)
95 // if we have the data needed to decrypt it, but didn't want it decrypted (or
96 // we couldn't decrypt it due to wrong password), then we prepend this
99 // Make a buffer big enough to hold both the header and the data itself
100 maHeader
.realloc ( n_ConstHeaderSize
+
101 rData
->m_aInitVector
.getLength() +
102 rData
->m_aSalt
.getLength() +
103 rData
->m_aDigest
.getLength() +
104 aMediaType
.getLength() * sizeof( sal_Unicode
) );
105 sal_Int8
* pHeader
= maHeader
.getArray();
106 ZipFile::StaticFillHeader( rData
, rEntry
.nSize
, aMediaType
, pHeader
);
107 mnHeaderToRead
= static_cast < sal_Int16
> ( maHeader
.getLength() );
108 mnZipSize
+= mnHeaderToRead
;
112 // allows to read package raw stream
113 XUnbufferedStream::XUnbufferedStream(
114 const rtl::Reference
< comphelper::RefCountedMutex
>& aMutexHolder
,
115 const Reference
< XInputStream
>& xRawStream
,
116 const ::rtl::Reference
< EncryptionData
>& rData
)
117 : maMutexHolder( aMutexHolder
)
118 , mxZipStream ( xRawStream
)
119 , mxZipSeek ( xRawStream
, UNO_QUERY
)
121 , maInflater ( true )
122 , mbRawStream ( false )
123 , mbWrappedRaw ( false )
124 , mnHeaderToRead ( 0 )
129 , mbCheckCRC( false )
131 // for this scenario maEntry is not set !!!
132 OSL_ENSURE( mxZipSeek
.is(), "The stream must be seekable!" );
134 // skip raw header, it must be already parsed to rData
135 mnZipCurrent
= n_ConstHeaderSize
+ rData
->m_aInitVector
.getLength() +
136 rData
->m_aSalt
.getLength() + rData
->m_aDigest
.getLength();
139 if ( mxZipSeek
.is() )
140 mnZipSize
= mxZipSeek
->getLength();
141 } catch( Exception
& e
)
143 // in case of problem the size will stay set to 0
144 SAL_WARN("package", "ignoring " << e
);
147 mnZipEnd
= mnZipCurrent
+ mnZipSize
;
149 // the raw data will not be decrypted, no need for the cipher
150 // m_xCipherContext = ZipFile::StaticGetCipher( xContext, rData, false );
153 XUnbufferedStream::~XUnbufferedStream()
157 sal_Int32 SAL_CALL
XUnbufferedStream::readBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
159 ::osl::MutexGuard
aGuard( maMutexHolder
->GetMutex() );
161 sal_Int32 nRequestedBytes
= nBytesToRead
;
162 OSL_ENSURE( !mnHeaderToRead
|| mbWrappedRaw
, "Only encrypted raw stream can be provided with header!" );
163 if ( mnMyCurrent
+ nRequestedBytes
> mnZipSize
+ maHeader
.getLength() )
164 nRequestedBytes
= static_cast < sal_Int32
> ( mnZipSize
+ maHeader
.getLength() - mnMyCurrent
);
166 sal_Int32 nTotal
= 0;
167 aData
.realloc ( nRequestedBytes
);
168 if ( nRequestedBytes
)
171 sal_Int32 nLastRead
= 0;
174 sal_Int64 nDiff
= mnZipEnd
- mnZipCurrent
;
176 if ( mbWrappedRaw
&& mnHeaderToRead
)
178 sal_Int16 nHeadRead
= static_cast< sal_Int16
>(( nRequestedBytes
> mnHeaderToRead
?
179 mnHeaderToRead
: nRequestedBytes
));
180 memcpy ( aData
.getArray(), maHeader
.getConstArray() + maHeader
.getLength() - mnHeaderToRead
, nHeadRead
);
181 mnHeaderToRead
= mnHeaderToRead
- nHeadRead
;
183 if ( nHeadRead
< nRequestedBytes
)
185 sal_Int32 nToRead
= nRequestedBytes
- nHeadRead
;
186 nToRead
= ( nDiff
< nToRead
) ? sal::static_int_cast
< sal_Int32
>( nDiff
) : nToRead
;
188 Sequence
< sal_Int8
> aPureData( nToRead
);
189 mxZipSeek
->seek ( mnZipCurrent
);
190 nRead
= mxZipStream
->readBytes ( aPureData
, nToRead
);
191 mnZipCurrent
+= nRead
;
193 aPureData
.realloc( nRead
);
195 maCRC
.update( aPureData
);
197 aData
.realloc( nHeadRead
+ nRead
);
199 sal_Int8
* pPureBuffer
= aPureData
.getArray();
200 sal_Int8
* pBuffer
= aData
.getArray();
201 for ( sal_Int32 nInd
= 0; nInd
< nRead
; nInd
++ )
202 pBuffer
[ nHeadRead
+ nInd
] = pPureBuffer
[ nInd
];
209 mxZipSeek
->seek ( mnZipCurrent
);
211 nRead
= mxZipStream
->readBytes (
213 std::min
<sal_Int64
>(nDiff
, nRequestedBytes
) );
215 mnZipCurrent
+= nRead
;
217 aData
.realloc( nRead
);
218 if ( mbWrappedRaw
&& mbCheckCRC
)
219 maCRC
.update( aData
);
224 while ( 0 == ( nLastRead
= maInflater
.doInflateSegment( aData
, nRead
, aData
.getLength() - nRead
) ) ||
225 ( nRead
+ nLastRead
!= nRequestedBytes
&& mnZipCurrent
< mnZipEnd
) )
229 if ( nRead
> nRequestedBytes
)
230 throw RuntimeException(
231 "Should not be possible to read more than requested!" );
233 if ( maInflater
.finished() || maInflater
.getLastInflateError() )
234 throw ZipIOException("The stream seems to be broken!" );
236 if ( maInflater
.needsDictionary() )
237 throw ZipIOException("Dictionaries are not supported!" );
239 sal_Int32 nDiff
= static_cast< sal_Int32
>( mnZipEnd
- mnZipCurrent
);
242 throw ZipIOException("The stream seems to be broken!" );
245 mxZipSeek
->seek ( mnZipCurrent
);
247 sal_Int32 nToRead
= std::max( nRequestedBytes
, static_cast< sal_Int32
>( 8192 ) );
248 if ( mnBlockSize
> 1 )
249 nToRead
= nToRead
+ mnBlockSize
- nToRead
% mnBlockSize
;
250 nToRead
= std::min( nDiff
, nToRead
);
252 sal_Int32 nZipRead
= mxZipStream
->readBytes( maCompBuffer
, nToRead
);
253 if ( nZipRead
< nToRead
)
254 throw ZipIOException("No expected data!" );
256 mnZipCurrent
+= nZipRead
;
257 // maCompBuffer now has the data, check if we need to decrypt
258 // before passing to the Inflater
259 if ( m_xCipherContext
.is() )
262 maCRC
.update( maCompBuffer
);
264 maCompBuffer
= m_xCipherContext
->convertWithCipherContext( maCompBuffer
);
265 if ( mnZipCurrent
== mnZipEnd
)
267 uno::Sequence
< sal_Int8
> aSuffix
= m_xCipherContext
->finalizeCipherContextAndDispose();
268 if ( aSuffix
.getLength() )
270 sal_Int32 nOldLen
= maCompBuffer
.getLength();
271 maCompBuffer
.realloc( nOldLen
+ aSuffix
.getLength() );
272 memcpy( maCompBuffer
.getArray() + nOldLen
, aSuffix
.getConstArray(), aSuffix
.getLength() );
276 maInflater
.setInput ( maCompBuffer
);
281 mnMyCurrent
+= nRead
+ nLastRead
;
282 nTotal
= nRead
+ nLastRead
;
283 if ( nTotal
< nRequestedBytes
)
284 aData
.realloc ( nTotal
);
286 if ( mbCheckCRC
&& ( !mbRawStream
|| mbWrappedRaw
) )
288 if ( !m_xCipherContext
.is() && !mbWrappedRaw
)
289 maCRC
.update( aData
);
291 if ( mnZipSize
+ maHeader
.getLength() == mnMyCurrent
&& maCRC
.getValue() != maEntry
.nCrc
)
292 throw ZipIOException("The stream seems to be broken!" );
299 sal_Int32 SAL_CALL
XUnbufferedStream::readSomeBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
301 return readBytes ( aData
, nMaxBytesToRead
);
303 void SAL_CALL
XUnbufferedStream::skipBytes( sal_Int32 nBytesToSkip
)
307 Sequence
< sal_Int8
> aSequence ( nBytesToSkip
);
308 readBytes ( aSequence
, nBytesToSkip
);
312 sal_Int32 SAL_CALL
XUnbufferedStream::available( )
314 //available size must include the prepended header in case of wrapped raw stream
315 return static_cast< sal_Int32
> ( std::min
< sal_Int64
>( SAL_MAX_INT32
, (mnZipSize
+ mnHeaderToRead
- mnMyCurrent
) ) );
318 void SAL_CALL
XUnbufferedStream::closeInput( )
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */