1*ebfcd9afSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ebfcd9afSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ebfcd9afSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ebfcd9afSAndrew Rist  * distributed with this work for additional information
6*ebfcd9afSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ebfcd9afSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ebfcd9afSAndrew Rist  * "License"); you may not use this file except in compliance
9*ebfcd9afSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ebfcd9afSAndrew Rist  *
11*ebfcd9afSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ebfcd9afSAndrew Rist  *
13*ebfcd9afSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ebfcd9afSAndrew Rist  * software distributed under the License is distributed on an
15*ebfcd9afSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ebfcd9afSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ebfcd9afSAndrew Rist  * specific language governing permissions and limitations
18*ebfcd9afSAndrew Rist  * under the License.
19*ebfcd9afSAndrew Rist  *
20*ebfcd9afSAndrew Rist  *************************************************************/
21*ebfcd9afSAndrew Rist 
22*ebfcd9afSAndrew Rist 
23cdf0e10cSrcweir #ifndef _ZIP_PACKAGE_STREAM_HXX
24cdf0e10cSrcweir #define _ZIP_PACKAGE_STREAM_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
27cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
28cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
29cdf0e10cSrcweir #include <com/sun/star/packages/XDataSinkEncrSupport.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/ref.hxx>
32cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <ZipPackageEntry.hxx>
35cdf0e10cSrcweir #include <EncryptionData.hxx>
36cdf0e10cSrcweir #include <mutexholder.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define PACKAGE_STREAM_NOTSET			0
39cdf0e10cSrcweir #define PACKAGE_STREAM_PACKAGEMEMBER	1
40cdf0e10cSrcweir #define PACKAGE_STREAM_DETECT			2
41cdf0e10cSrcweir #define PACKAGE_STREAM_DATA				3
42cdf0e10cSrcweir #define PACKAGE_STREAM_RAW				4
43cdf0e10cSrcweir 
44cdf0e10cSrcweir class ZipPackage;
45cdf0e10cSrcweir struct ZipEntry;
46cdf0e10cSrcweir class ZipPackageStream : public cppu::ImplInheritanceHelper2
47cdf0e10cSrcweir <
48cdf0e10cSrcweir 	ZipPackageEntry,
49cdf0e10cSrcweir 	::com::sun::star::io::XActiveDataSink,
50cdf0e10cSrcweir 	::com::sun::star::packages::XDataSinkEncrSupport
51cdf0e10cSrcweir >
52cdf0e10cSrcweir {
53cdf0e10cSrcweir protected:
54cdf0e10cSrcweir 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream;
55cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory;
56cdf0e10cSrcweir 	ZipPackage 			&rZipPackage;
57cdf0e10cSrcweir 	sal_Bool			bToBeCompressed, bToBeEncrypted, bHaveOwnKey, bIsEncrypted;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     ::rtl::Reference< BaseEncryptionData > m_xBaseEncryptionData;
60cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > m_aStorageEncryptionKeys;
61cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< sal_Int8 > m_aEncryptionKey;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     sal_Int32 m_nImportedStartKeyAlgorithm;
64cdf0e10cSrcweir     sal_Int32 m_nImportedEncryptionAlgorithm;
65cdf0e10cSrcweir     sal_Int32 m_nImportedChecksumAlgorithm;
66cdf0e10cSrcweir     sal_Int32 m_nImportedDerivedKeySize;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	sal_uInt8	m_nStreamMode;
69cdf0e10cSrcweir 	sal_uInt32	m_nMagicalHackPos;
70cdf0e10cSrcweir 	sal_uInt32	m_nMagicalHackSize;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	sal_Bool m_bHasSeekable;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	sal_Bool m_bCompressedIsSetFromOutside;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     sal_Bool m_bFromManifest;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     bool m_bUseWinEncoding;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetOwnSeekStream();
81cdf0e10cSrcweir 
82cdf0e10cSrcweir public:
HasOwnKey() const83cdf0e10cSrcweir 	sal_Bool HasOwnKey () const	 { return bHaveOwnKey;}
IsToBeCompressed() const84cdf0e10cSrcweir 	sal_Bool IsToBeCompressed () const { return bToBeCompressed;}
IsToBeEncrypted() const85cdf0e10cSrcweir 	sal_Bool IsToBeEncrypted () const { return bToBeEncrypted;}
IsEncrypted() const86cdf0e10cSrcweir 	sal_Bool IsEncrypted () const  	 { return bIsEncrypted;}
IsPackageMember() const87cdf0e10cSrcweir 	sal_Bool IsPackageMember () const { return m_nStreamMode == PACKAGE_STREAM_PACKAGEMEMBER;}
88cdf0e10cSrcweir 
IsFromManifest() const89cdf0e10cSrcweir     sal_Bool IsFromManifest() const { return m_bFromManifest; }
SetFromManifest(sal_Bool bValue)90cdf0e10cSrcweir     void SetFromManifest( sal_Bool bValue ) { m_bFromManifest = bValue; }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     ::rtl::Reference< EncryptionData > GetEncryptionData( bool bWinEncoding = false );
93cdf0e10cSrcweir     void SetBaseEncryptionData( const ::rtl::Reference< BaseEncryptionData >& xData );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< sal_Int8 > GetEncryptionKey( bool bWinEncoding = false );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     sal_Int32 GetStartKeyGenID();
98cdf0e10cSrcweir 
getInitialisationVector() const99cdf0e10cSrcweir 	const com::sun::star::uno::Sequence < sal_Int8 > getInitialisationVector () const
100cdf0e10cSrcweir 	{ return m_xBaseEncryptionData->m_aInitVector;}
getDigest() const101cdf0e10cSrcweir 	const com::sun::star::uno::Sequence < sal_Int8 > getDigest () const
102cdf0e10cSrcweir 	{ return m_xBaseEncryptionData->m_aDigest;}
getSalt() const103cdf0e10cSrcweir 	const com::sun::star::uno::Sequence < sal_Int8 > getSalt () const
104cdf0e10cSrcweir 	{ return m_xBaseEncryptionData->m_aSalt;}
getIterationCount() const105cdf0e10cSrcweir 	sal_Int32 getIterationCount () const
106cdf0e10cSrcweir 	{ return m_xBaseEncryptionData->m_nIterationCount;}
getSize() const107cdf0e10cSrcweir 	sal_Int32 getSize () const
108cdf0e10cSrcweir 	{ return aEntry.nSize;}
109cdf0e10cSrcweir 
GetStreamMode() const110cdf0e10cSrcweir 	sal_uInt8 GetStreamMode() const { return m_nStreamMode; }
GetMagicalHackPos() const111cdf0e10cSrcweir 	sal_uInt32 GetMagicalHackPos() const { return m_nMagicalHackPos; }
GetMagicalHackSize() const112cdf0e10cSrcweir 	sal_uInt32 GetMagicalHackSize() const { return m_nMagicalHackSize; }
113cdf0e10cSrcweir     sal_Int32 GetEncryptionAlgorithm() const;
114cdf0e10cSrcweir     sal_Int32 GetBlockSize() const;
115cdf0e10cSrcweir 
SetToBeCompressed(sal_Bool bNewValue)116cdf0e10cSrcweir 	void SetToBeCompressed (sal_Bool bNewValue) { bToBeCompressed = bNewValue;}
SetIsEncrypted(sal_Bool bNewValue)117cdf0e10cSrcweir 	void SetIsEncrypted (sal_Bool bNewValue) { bIsEncrypted = bNewValue;}
SetImportedStartKeyAlgorithm(sal_Int32 nAlgorithm)118cdf0e10cSrcweir     void SetImportedStartKeyAlgorithm( sal_Int32 nAlgorithm ) { m_nImportedStartKeyAlgorithm = nAlgorithm; }
SetImportedEncryptionAlgorithm(sal_Int32 nAlgorithm)119cdf0e10cSrcweir     void SetImportedEncryptionAlgorithm( sal_Int32 nAlgorithm ) { m_nImportedEncryptionAlgorithm = nAlgorithm; }
SetImportedChecksumAlgorithm(sal_Int32 nAlgorithm)120cdf0e10cSrcweir     void SetImportedChecksumAlgorithm( sal_Int32 nAlgorithm ) { m_nImportedChecksumAlgorithm = nAlgorithm; }
SetImportedDerivedKeySize(sal_Int32 nSize)121cdf0e10cSrcweir     void SetImportedDerivedKeySize( sal_Int32 nSize ) { m_nImportedDerivedKeySize = nSize; }
SetToBeEncrypted(sal_Bool bNewValue)122cdf0e10cSrcweir 	void SetToBeEncrypted (sal_Bool bNewValue)
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         bToBeEncrypted  = bNewValue;
125cdf0e10cSrcweir         if ( bToBeEncrypted && !m_xBaseEncryptionData.is())
126cdf0e10cSrcweir             m_xBaseEncryptionData = new BaseEncryptionData;
127cdf0e10cSrcweir 		else if ( !bToBeEncrypted && m_xBaseEncryptionData.is() )
128cdf0e10cSrcweir 			m_xBaseEncryptionData.clear();
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 	void SetPackageMember (sal_Bool bNewValue);
131cdf0e10cSrcweir 
setKey(const com::sun::star::uno::Sequence<sal_Int8> & rNewKey)132cdf0e10cSrcweir 	void setKey (const com::sun::star::uno::Sequence < sal_Int8 >& rNewKey )
133cdf0e10cSrcweir 	{ m_aEncryptionKey = rNewKey; m_aStorageEncryptionKeys.realloc( 0 ); }
setInitialisationVector(const com::sun::star::uno::Sequence<sal_Int8> & rNewVector)134cdf0e10cSrcweir 	void setInitialisationVector (const com::sun::star::uno::Sequence < sal_Int8 >& rNewVector )
135cdf0e10cSrcweir 	{ m_xBaseEncryptionData->m_aInitVector = rNewVector;}
setSalt(const com::sun::star::uno::Sequence<sal_Int8> & rNewSalt)136cdf0e10cSrcweir 	void setSalt (const com::sun::star::uno::Sequence < sal_Int8 >& rNewSalt )
137cdf0e10cSrcweir 	{ m_xBaseEncryptionData->m_aSalt = rNewSalt;}
setDigest(const com::sun::star::uno::Sequence<sal_Int8> & rNewDigest)138cdf0e10cSrcweir 	void setDigest (const com::sun::star::uno::Sequence < sal_Int8 >& rNewDigest )
139cdf0e10cSrcweir 	{ m_xBaseEncryptionData->m_aDigest = rNewDigest;}
setIterationCount(const sal_Int32 nNewCount)140cdf0e10cSrcweir 	void setIterationCount (const sal_Int32 nNewCount)
141cdf0e10cSrcweir 	{ m_xBaseEncryptionData->m_nIterationCount = nNewCount;}
142cdf0e10cSrcweir 	void setSize (const sal_Int32 nNewSize);
143cdf0e10cSrcweir 
GetOwnStreamNoWrap()144cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetOwnStreamNoWrap() { return xStream; }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	void CloseOwnStreamIfAny();
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	ZipPackageStream ( ZipPackage & rNewPackage,
149cdf0e10cSrcweir 						const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& xFactory,
150cdf0e10cSrcweir 						sal_Bool bAllowRemoveOnInsert );
151cdf0e10cSrcweir 	virtual ~ZipPackageStream( void );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetRawEncrStreamNoHeaderCopy();
154cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > TryToGetRawFromDataStream(
155cdf0e10cSrcweir 																					sal_Bool bAddHeaderForEncr );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	sal_Bool ParsePackageRawStream();
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	void setZipEntryOnLoading( const ZipEntry &rInEntry);
160cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData()
161cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	static const ::com::sun::star::uno::Sequence < sal_Int8 >& static_getImplementationId();
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	// XActiveDataSink
166cdf0e10cSrcweir     virtual void SAL_CALL setInputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream )
167cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
168cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream(  )
169cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	// XDataSinkEncrSupport
172cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream()
173cdf0e10cSrcweir 		throw ( ::com::sun::star::packages::WrongPasswordException,
174cdf0e10cSrcweir 				::com::sun::star::io::IOException,
175cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
176cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawStream()
177cdf0e10cSrcweir 		throw ( ::com::sun::star::packages::NoEncryptionException,
178cdf0e10cSrcweir 				::com::sun::star::io::IOException,
179cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
180cdf0e10cSrcweir     virtual void SAL_CALL setDataStream(
181cdf0e10cSrcweir 					const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream )
182cdf0e10cSrcweir 		throw ( ::com::sun::star::io::IOException,
183cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
184cdf0e10cSrcweir     virtual void SAL_CALL setRawStream(
185cdf0e10cSrcweir 					const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream )
186cdf0e10cSrcweir 		throw ( ::com::sun::star::packages::EncryptionNotAllowedException,
187cdf0e10cSrcweir 				::com::sun::star::packages::NoRawFormatException,
188cdf0e10cSrcweir 				::com::sun::star::io::IOException,
189cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
190cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getPlainRawStream()
191cdf0e10cSrcweir 		throw ( ::com::sun::star::io::IOException,
192cdf0e10cSrcweir 				::com::sun::star::uno::RuntimeException );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	// XUnoTunnel
195cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
196cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	// XPropertySet
199cdf0e10cSrcweir     virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Any& aValue )
200cdf0e10cSrcweir 		throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
201cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName )
202cdf0e10cSrcweir 		throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 	// XServiceInfo
205cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName(  )
206cdf0e10cSrcweir 		throw (::com::sun::star::uno::RuntimeException);
207cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
208cdf0e10cSrcweir 		throw (::com::sun::star::uno::RuntimeException);
209cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  )
210cdf0e10cSrcweir 		throw (::com::sun::star::uno::RuntimeException);
211cdf0e10cSrcweir };
212cdf0e10cSrcweir #endif
213