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_FILE_HXX 24 #define _ZIP_FILE_HXX 25 26 #include <com/sun/star/packages/zip/ZipException.hpp> 27 #include <com/sun/star/packages/zip/ZipIOException.hpp> 28 #include <com/sun/star/packages/NoEncryptionException.hpp> 29 #include <com/sun/star/packages/WrongPasswordException.hpp> 30 #include <com/sun/star/xml/crypto/XCipherContext.hpp> 31 #include <com/sun/star/xml/crypto/XDigestContext.hpp> 32 33 #include <rtl/ref.hxx> 34 35 #include <ByteGrabber.hxx> 36 #include <HashMaps.hxx> 37 #include <Inflater.hxx> 38 #include <EncryptionData.hxx> 39 40 #include <mutexholder.hxx> 41 42 namespace com { namespace sun { namespace star { 43 namespace lang { class XMultiServiceFactory; } 44 namespace ucb { class XProgressHandler; } 45 } } } 46 47 /* 48 * We impose arbitrary but reasonable limit on ZIP files. 49 */ 50 51 #define ZIP_MAXNAMELEN 512 52 #define ZIP_MAXEXTRA 256 53 #define ZIP_MAXENTRIES (0x10000 - 2) 54 55 class ZipEnumeration; 56 57 class ZipFile 58 { 59 protected: 60 ::osl::Mutex m_aMutex; 61 62 ::rtl::OUString sComment; /* zip file comment */ 63 EntryHash aEntries; 64 ByteGrabber aGrabber; 65 Inflater aInflater; 66 com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream; 67 com::sun::star::uno::Reference < com::sun::star::io::XSeekable > xSeek; 68 const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory; 69 ::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgressHandler; 70 71 sal_Bool bRecoveryMode; 72 73 com::sun::star::uno::Reference < com::sun::star::io::XInputStream > createMemoryStream( 74 ZipEntry & rEntry, 75 const ::rtl::Reference < EncryptionData > &rData, 76 sal_Bool bRawStream, 77 sal_Bool bDecrypt ); 78 79 com::sun::star::uno::Reference < com::sun::star::io::XInputStream > createFileStream( 80 ZipEntry & rEntry, 81 const ::rtl::Reference < EncryptionData > &rData, 82 sal_Bool bRawStream, 83 sal_Bool bDecrypt ); 84 85 // aMediaType parameter is used only for raw stream header creation 86 com::sun::star::uno::Reference < com::sun::star::io::XInputStream > createUnbufferedStream( 87 SotMutexHolderRef aMutexHolder, 88 ZipEntry & rEntry, 89 const ::rtl::Reference < EncryptionData > &rData, 90 sal_Int8 nStreamMode, 91 sal_Bool bDecrypt, 92 ::rtl::OUString aMediaType = ::rtl::OUString() ); 93 94 sal_Bool hasValidPassword ( ZipEntry & rEntry, const ::rtl::Reference < EncryptionData > &rData ); 95 96 sal_Bool checkSizeAndCRC( const ZipEntry& aEntry ); 97 98 sal_Int32 getCRC( sal_Int32 nOffset, sal_Int32 nSize ); 99 100 void getSizeAndCRC( sal_Int32 nOffset, sal_Int32 nCompressedSize, sal_Int32 *nSize, sal_Int32 *nCRC ); 101 102 public: 103 104 ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput, 105 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory, 106 sal_Bool bInitialise 107 ); 108 109 ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput, 110 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory, 111 sal_Bool bInitialise, 112 sal_Bool bForceRecover, 113 ::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgress 114 ); 115 116 ~ZipFile(); 117 GetEntryHash()118 EntryHash& GetEntryHash() { return aEntries; } 119 120 void setInputStream ( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewStream ); 121 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData( 122 ZipEntry& rEntry, 123 const ::rtl::Reference < EncryptionData > &rData, 124 sal_Bool bDecrypt, 125 SotMutexHolderRef aMutexHolder ); 126 127 128 static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XDigestContext > StaticGetDigestContextForChecksum( 129 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory, 130 const ::rtl::Reference< EncryptionData >& xEncryptionData ); 131 132 static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > StaticGetCipher( 133 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory, 134 const ::rtl::Reference< EncryptionData >& xEncryptionData, 135 bool bEncrypt ); 136 137 static void StaticFillHeader ( const ::rtl::Reference < EncryptionData > & rData, 138 sal_Int32 nSize, 139 const ::rtl::OUString& aMediaType, 140 sal_Int8 * & pHeader ); 141 142 static sal_Bool StaticFillData ( ::rtl::Reference < BaseEncryptionData > & rData, 143 sal_Int32 &rEncAlgorithm, 144 sal_Int32 &rChecksumAlgorithm, 145 sal_Int32 &rDerivedKeySize, 146 sal_Int32 &rStartKeyGenID, 147 sal_Int32 &rSize, 148 ::rtl::OUString& aMediaType, 149 const ::com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream ); 150 151 static ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > StaticGetDataFromRawStream( 152 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, 153 const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& xStream, 154 const ::rtl::Reference < EncryptionData > &rData ); 155 156 static sal_Bool StaticHasValidPassword ( 157 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, 158 const ::com::sun::star::uno::Sequence< sal_Int8 > &aReadBuffer, 159 const ::rtl::Reference < EncryptionData > &rData ); 160 161 162 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream( 163 ZipEntry& rEntry, 164 const ::rtl::Reference < EncryptionData > &rData, 165 sal_Bool bDecrypt, 166 SotMutexHolderRef aMutexHolder ); 167 168 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream( 169 ZipEntry& rEntry, 170 const ::rtl::Reference < EncryptionData > &rData, 171 sal_Bool bDecrypt, 172 SotMutexHolderRef aMutexHolder ); 173 174 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getWrappedRawStream( 175 ZipEntry& rEntry, 176 const ::rtl::Reference < EncryptionData > &rData, 177 const ::rtl::OUString& aMediaType, 178 SotMutexHolderRef aMutexHolder ); 179 180 ZipEnumeration * SAL_CALL entries( ); 181 protected: 182 /** Decode a local file header of an entry and update a ZipEntry 183 * 184 * @param rEntry entry with the nFileHeaderOffset field populated. At return, the ZipEntry::nFileDataOffset and ZipEntry::nLOCExtraLen fields will be set. 185 * 186 * Also searches for a data descriptor after the file data and validates it. 187 * 188 * @return sal_True if successful, otherwise throws exceptions. 189 */ 190 sal_Bool readLOC ( ZipEntry &rEntry); 191 /// Decode the End of central directory record 192 sal_Int32 readCEN(); 193 /// Find the End of central directory record 194 sal_Int32 findEND(); 195 sal_Int32 recover(); 196 197 }; 198 199 #endif 200