1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #ifndef _ZIP_FILE_HXX 28*cdf0e10cSrcweir #define _ZIP_FILE_HXX 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipException.hpp> 31*cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp> 32*cdf0e10cSrcweir #include <com/sun/star/packages/NoEncryptionException.hpp> 33*cdf0e10cSrcweir #include <com/sun/star/packages/WrongPasswordException.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XCipherContext.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XDigestContext.hpp> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <rtl/ref.hxx> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <ByteGrabber.hxx> 40*cdf0e10cSrcweir #include <HashMaps.hxx> 41*cdf0e10cSrcweir #include <Inflater.hxx> 42*cdf0e10cSrcweir #include <EncryptionData.hxx> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #include <mutexholder.hxx> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir namespace com { namespace sun { namespace star { 47*cdf0e10cSrcweir namespace lang { class XMultiServiceFactory; } 48*cdf0e10cSrcweir namespace ucb { class XProgressHandler; } 49*cdf0e10cSrcweir } } } 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir /* 52*cdf0e10cSrcweir * We impose arbitrary but reasonable limit on ZIP files. 53*cdf0e10cSrcweir */ 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir #define ZIP_MAXNAMELEN 512 56*cdf0e10cSrcweir #define ZIP_MAXEXTRA 256 57*cdf0e10cSrcweir #define ZIP_MAXENTRIES (0x10000 - 2) 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir class ZipEnumeration; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir class ZipFile 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir protected: 64*cdf0e10cSrcweir ::osl::Mutex m_aMutex; 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir ::rtl::OUString sComment; /* zip file comment */ 67*cdf0e10cSrcweir EntryHash aEntries; 68*cdf0e10cSrcweir ByteGrabber aGrabber; 69*cdf0e10cSrcweir Inflater aInflater; 70*cdf0e10cSrcweir com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xStream; 71*cdf0e10cSrcweir com::sun::star::uno::Reference < com::sun::star::io::XSeekable > xSeek; 72*cdf0e10cSrcweir const ::com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > m_xFactory; 73*cdf0e10cSrcweir ::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgressHandler; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir sal_Bool bRecoveryMode; 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir com::sun::star::uno::Reference < com::sun::star::io::XInputStream > createMemoryStream( 78*cdf0e10cSrcweir ZipEntry & rEntry, 79*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 80*cdf0e10cSrcweir sal_Bool bRawStream, 81*cdf0e10cSrcweir sal_Bool bDecrypt ); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir com::sun::star::uno::Reference < com::sun::star::io::XInputStream > createFileStream( 84*cdf0e10cSrcweir ZipEntry & rEntry, 85*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 86*cdf0e10cSrcweir sal_Bool bRawStream, 87*cdf0e10cSrcweir sal_Bool bDecrypt ); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // aMediaType parameter is used only for raw stream header creation 90*cdf0e10cSrcweir com::sun::star::uno::Reference < com::sun::star::io::XInputStream > createUnbufferedStream( 91*cdf0e10cSrcweir SotMutexHolderRef aMutexHolder, 92*cdf0e10cSrcweir ZipEntry & rEntry, 93*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 94*cdf0e10cSrcweir sal_Int8 nStreamMode, 95*cdf0e10cSrcweir sal_Bool bDecrypt, 96*cdf0e10cSrcweir ::rtl::OUString aMediaType = ::rtl::OUString() ); 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir sal_Bool hasValidPassword ( ZipEntry & rEntry, const ::rtl::Reference < EncryptionData > &rData ); 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir sal_Bool checkSizeAndCRC( const ZipEntry& aEntry ); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir sal_Int32 getCRC( sal_Int32 nOffset, sal_Int32 nSize ); 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir void getSizeAndCRC( sal_Int32 nOffset, sal_Int32 nCompressedSize, sal_Int32 *nSize, sal_Int32 *nCRC ); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir public: 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput, 109*cdf0e10cSrcweir const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory, 110*cdf0e10cSrcweir sal_Bool bInitialise 111*cdf0e10cSrcweir ) 112*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException); 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir ZipFile( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > &xInput, 115*cdf0e10cSrcweir const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory > &xNewFactory, 116*cdf0e10cSrcweir sal_Bool bInitialise, 117*cdf0e10cSrcweir sal_Bool bForceRecover, 118*cdf0e10cSrcweir ::com::sun::star::uno::Reference < ::com::sun::star::ucb::XProgressHandler > xProgress 119*cdf0e10cSrcweir ) 120*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir ~ZipFile(); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir EntryHash& GetEntryHash() { return aEntries; } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir void setInputStream ( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xNewStream ); 127*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getRawData( 128*cdf0e10cSrcweir ZipEntry& rEntry, 129*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 130*cdf0e10cSrcweir sal_Bool bDecrypt, 131*cdf0e10cSrcweir SotMutexHolderRef aMutexHolder ) 132*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, ::com::sun::star::packages::zip::ZipException, ::com::sun::star::uno::RuntimeException); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XDigestContext > StaticGetDigestContextForChecksum( 136*cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory, 137*cdf0e10cSrcweir const ::rtl::Reference< EncryptionData >& xEncryptionData ); 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir static ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > StaticGetCipher( 140*cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xArgFactory, 141*cdf0e10cSrcweir const ::rtl::Reference< EncryptionData >& xEncryptionData, 142*cdf0e10cSrcweir bool bEncrypt ); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir static void StaticFillHeader ( const ::rtl::Reference < EncryptionData > & rData, 145*cdf0e10cSrcweir sal_Int32 nSize, 146*cdf0e10cSrcweir const ::rtl::OUString& aMediaType, 147*cdf0e10cSrcweir sal_Int8 * & pHeader ); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir static sal_Bool StaticFillData ( ::rtl::Reference < BaseEncryptionData > & rData, 150*cdf0e10cSrcweir sal_Int32 &rEncAlgorithm, 151*cdf0e10cSrcweir sal_Int32 &rChecksumAlgorithm, 152*cdf0e10cSrcweir sal_Int32 &rDerivedKeySize, 153*cdf0e10cSrcweir sal_Int32 &rStartKeyGenID, 154*cdf0e10cSrcweir sal_Int32 &rSize, 155*cdf0e10cSrcweir ::rtl::OUString& aMediaType, 156*cdf0e10cSrcweir const ::com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream ); 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir static ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > StaticGetDataFromRawStream( 159*cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, 160*cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& xStream, 161*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData ) 162*cdf0e10cSrcweir throw ( ::com::sun::star::packages::WrongPasswordException, 163*cdf0e10cSrcweir ::com::sun::star::packages::zip::ZipIOException, 164*cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException ); 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir static sal_Bool StaticHasValidPassword ( 167*cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory, 168*cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< sal_Int8 > &aReadBuffer, 169*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData ); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream( 173*cdf0e10cSrcweir ZipEntry& rEntry, 174*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 175*cdf0e10cSrcweir sal_Bool bDecrypt, 176*cdf0e10cSrcweir SotMutexHolderRef aMutexHolder ) 177*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, ::com::sun::star::packages::zip::ZipException, ::com::sun::star::uno::RuntimeException); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getDataStream( 180*cdf0e10cSrcweir ZipEntry& rEntry, 181*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 182*cdf0e10cSrcweir sal_Bool bDecrypt, 183*cdf0e10cSrcweir SotMutexHolderRef aMutexHolder ) 184*cdf0e10cSrcweir throw ( ::com::sun::star::packages::WrongPasswordException, 185*cdf0e10cSrcweir ::com::sun::star::io::IOException, 186*cdf0e10cSrcweir ::com::sun::star::packages::zip::ZipException, 187*cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException ); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getWrappedRawStream( 190*cdf0e10cSrcweir ZipEntry& rEntry, 191*cdf0e10cSrcweir const ::rtl::Reference < EncryptionData > &rData, 192*cdf0e10cSrcweir const ::rtl::OUString& aMediaType, 193*cdf0e10cSrcweir SotMutexHolderRef aMutexHolder ) 194*cdf0e10cSrcweir throw ( ::com::sun::star::packages::NoEncryptionException, 195*cdf0e10cSrcweir ::com::sun::star::io::IOException, 196*cdf0e10cSrcweir ::com::sun::star::packages::zip::ZipException, 197*cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException ); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir ZipEnumeration * SAL_CALL entries( ); 200*cdf0e10cSrcweir protected: 201*cdf0e10cSrcweir sal_Bool readLOC ( ZipEntry &rEntry) 202*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException); 203*cdf0e10cSrcweir sal_Int32 readCEN() 204*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException); 205*cdf0e10cSrcweir sal_Int32 findEND() 206*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException); 207*cdf0e10cSrcweir sal_Int32 recover() 208*cdf0e10cSrcweir throw(::com::sun::star::io::IOException, com::sun::star::packages::zip::ZipException, com::sun::star::uno::RuntimeException); 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir }; 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir #endif 213