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