1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _ZIP_PACKAGE_STREAM_HXX
28 #define _ZIP_PACKAGE_STREAM_HXX
29 
30 #include <com/sun/star/io/XActiveDataSink.hpp>
31 #include <com/sun/star/io/XSeekable.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <com/sun/star/packages/XDataSinkEncrSupport.hpp>
34 
35 #include <rtl/ref.hxx>
36 #include <cppuhelper/implbase2.hxx>
37 
38 #include <ZipPackageEntry.hxx>
39 #include <EncryptionData.hxx>
40 #include <mutexholder.hxx>
41 
42 #define PACKAGE_STREAM_NOTSET			0
43 #define PACKAGE_STREAM_PACKAGEMEMBER	1
44 #define PACKAGE_STREAM_DETECT			2
45 #define PACKAGE_STREAM_DATA				3
46 #define PACKAGE_STREAM_RAW				4
47 
48 class ZipPackage;
49 struct ZipEntry;
50 class ZipPackageStream : public cppu::ImplInheritanceHelper2
51 <
52 	ZipPackageEntry,
53 	::com::sun::star::io::XActiveDataSink,
54 	::com::sun::star::packages::XDataSinkEncrSupport
55 >
56 {
57 protected:
58 	com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream;
59 	const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory;
60 	ZipPackage 			&rZipPackage;
61 	sal_Bool			bToBeCompressed, bToBeEncrypted, bHaveOwnKey, bIsEncrypted;
62 
63     ::rtl::Reference< BaseEncryptionData > m_xBaseEncryptionData;
64 	::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > m_aStorageEncryptionKeys;
65 	::com::sun::star::uno::Sequence< sal_Int8 > m_aEncryptionKey;
66 
67     sal_Int32 m_nImportedStartKeyAlgorithm;
68     sal_Int32 m_nImportedEncryptionAlgorithm;
69     sal_Int32 m_nImportedChecksumAlgorithm;
70     sal_Int32 m_nImportedDerivedKeySize;
71 
72 	sal_uInt8	m_nStreamMode;
73 	sal_uInt32	m_nMagicalHackPos;
74 	sal_uInt32	m_nMagicalHackSize;
75 
76 	sal_Bool m_bHasSeekable;
77 
78 	sal_Bool m_bCompressedIsSetFromOutside;
79 
80     sal_Bool m_bFromManifest;
81 
82     bool m_bUseWinEncoding;
83 
84 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetOwnSeekStream();
85 
86 public:
87 	sal_Bool HasOwnKey () const	 { return bHaveOwnKey;}
88 	sal_Bool IsToBeCompressed () const { return bToBeCompressed;}
89 	sal_Bool IsToBeEncrypted () const { return bToBeEncrypted;}
90 	sal_Bool IsEncrypted () const  	 { return bIsEncrypted;}
91 	sal_Bool IsPackageMember () const { return m_nStreamMode == PACKAGE_STREAM_PACKAGEMEMBER;}
92 
93     sal_Bool IsFromManifest() const { return m_bFromManifest; }
94     void SetFromManifest( sal_Bool bValue ) { m_bFromManifest = bValue; }
95 
96     ::rtl::Reference< EncryptionData > GetEncryptionData( bool bWinEncoding = false );
97     void SetBaseEncryptionData( const ::rtl::Reference< BaseEncryptionData >& xData );
98 
99     ::com::sun::star::uno::Sequence< sal_Int8 > GetEncryptionKey( bool bWinEncoding = false );
100 
101     sal_Int32 GetStartKeyGenID();
102 
103 	const com::sun::star::uno::Sequence < sal_Int8 > getInitialisationVector () const
104 	{ return m_xBaseEncryptionData->m_aInitVector;}
105 	const com::sun::star::uno::Sequence < sal_Int8 > getDigest () const
106 	{ return m_xBaseEncryptionData->m_aDigest;}
107 	const com::sun::star::uno::Sequence < sal_Int8 > getSalt () const
108 	{ return m_xBaseEncryptionData->m_aSalt;}
109 	sal_Int32 getIterationCount () const
110 	{ return m_xBaseEncryptionData->m_nIterationCount;}
111 	sal_Int32 getSize () const
112 	{ return aEntry.nSize;}
113 
114 	sal_uInt8 GetStreamMode() const { return m_nStreamMode; }
115 	sal_uInt32 GetMagicalHackPos() const { return m_nMagicalHackPos; }
116 	sal_uInt32 GetMagicalHackSize() const { return m_nMagicalHackSize; }
117     sal_Int32 GetEncryptionAlgorithm() const;
118     sal_Int32 GetBlockSize() const;
119 
120 	void SetToBeCompressed (sal_Bool bNewValue) { bToBeCompressed = bNewValue;}
121 	void SetIsEncrypted (sal_Bool bNewValue) { bIsEncrypted = bNewValue;}
122     void SetImportedStartKeyAlgorithm( sal_Int32 nAlgorithm ) { m_nImportedStartKeyAlgorithm = nAlgorithm; }
123     void SetImportedEncryptionAlgorithm( sal_Int32 nAlgorithm ) { m_nImportedEncryptionAlgorithm = nAlgorithm; }
124     void SetImportedChecksumAlgorithm( sal_Int32 nAlgorithm ) { m_nImportedChecksumAlgorithm = nAlgorithm; }
125     void SetImportedDerivedKeySize( sal_Int32 nSize ) { m_nImportedDerivedKeySize = nSize; }
126 	void SetToBeEncrypted (sal_Bool bNewValue)
127     {
128         bToBeEncrypted  = bNewValue;
129         if ( bToBeEncrypted && !m_xBaseEncryptionData.is())
130             m_xBaseEncryptionData = new BaseEncryptionData;
131 		else if ( !bToBeEncrypted && m_xBaseEncryptionData.is() )
132 			m_xBaseEncryptionData.clear();
133     }
134 	void SetPackageMember (sal_Bool bNewValue);
135 
136 	void setKey (const com::sun::star::uno::Sequence < sal_Int8 >& rNewKey )
137 	{ m_aEncryptionKey = rNewKey; m_aStorageEncryptionKeys.realloc( 0 ); }
138 	void setInitialisationVector (const com::sun::star::uno::Sequence < sal_Int8 >& rNewVector )
139 	{ m_xBaseEncryptionData->m_aInitVector = rNewVector;}
140 	void setSalt (const com::sun::star::uno::Sequence < sal_Int8 >& rNewSalt )
141 	{ m_xBaseEncryptionData->m_aSalt = rNewSalt;}
142 	void setDigest (const com::sun::star::uno::Sequence < sal_Int8 >& rNewDigest )
143 	{ m_xBaseEncryptionData->m_aDigest = rNewDigest;}
144 	void setIterationCount (const sal_Int32 nNewCount)
145 	{ m_xBaseEncryptionData->m_nIterationCount = nNewCount;}
146 	void setSize (const sal_Int32 nNewSize);
147 
148 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetOwnStreamNoWrap() { return xStream; }
149 
150 	void CloseOwnStreamIfAny();
151 
152 	ZipPackageStream ( ZipPackage & rNewPackage,
153 						const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& xFactory,
154 						sal_Bool bAllowRemoveOnInsert );
155 	virtual ~ZipPackageStream( void );
156 
157 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > GetRawEncrStreamNoHeaderCopy();
158 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > TryToGetRawFromDataStream(
159 																					sal_Bool bAddHeaderForEncr );
160 
161 	sal_Bool ParsePackageRawStream();
162 
163 	void setZipEntryOnLoading( const ZipEntry &rInEntry);
164     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData()
165 		throw(::com::sun::star::uno::RuntimeException);
166 
167 	static const ::com::sun::star::uno::Sequence < sal_Int8 >& static_getImplementationId();
168 
169 	// XActiveDataSink
170     virtual void SAL_CALL setInputStream( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream )
171 		throw(::com::sun::star::uno::RuntimeException);
172     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream(  )
173 		throw(::com::sun::star::uno::RuntimeException);
174 
175 	// XDataSinkEncrSupport
176     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream()
177 		throw ( ::com::sun::star::packages::WrongPasswordException,
178 				::com::sun::star::io::IOException,
179 				::com::sun::star::uno::RuntimeException );
180     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawStream()
181 		throw ( ::com::sun::star::packages::NoEncryptionException,
182 				::com::sun::star::io::IOException,
183 				::com::sun::star::uno::RuntimeException );
184     virtual void SAL_CALL setDataStream(
185 					const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream )
186 		throw ( ::com::sun::star::io::IOException,
187 				::com::sun::star::uno::RuntimeException );
188     virtual void SAL_CALL setRawStream(
189 					const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aStream )
190 		throw ( ::com::sun::star::packages::EncryptionNotAllowedException,
191 				::com::sun::star::packages::NoRawFormatException,
192 				::com::sun::star::io::IOException,
193 				::com::sun::star::uno::RuntimeException );
194     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getPlainRawStream()
195 		throw ( ::com::sun::star::io::IOException,
196 				::com::sun::star::uno::RuntimeException );
197 
198 	// XUnoTunnel
199     virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
200 		throw(::com::sun::star::uno::RuntimeException);
201 
202 	// XPropertySet
203     virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Any& aValue )
204 		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);
205     virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName )
206 		throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
207 
208 	// XServiceInfo
209     virtual ::rtl::OUString SAL_CALL getImplementationName(  )
210 		throw (::com::sun::star::uno::RuntimeException);
211     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
212 		throw (::com::sun::star::uno::RuntimeException);
213     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  )
214 		throw (::com::sun::star::uno::RuntimeException);
215 };
216 #endif
217