diff options
author | Martin Gallwey <mtg@openoffice.org> | 2001-04-27 13:56:07 +0000 |
---|---|---|
committer | Martin Gallwey <mtg@openoffice.org> | 2001-04-27 13:56:07 +0000 |
commit | fa2de5099c00fabb952c039269f23f7f3689c586 (patch) | |
tree | 0476064e5fcd36451648736e4dcb6fccafd1f062 /package/source/zipapi | |
parent | 7ddd39239c37d8a9b58f4c8fc17ff2b47fe0a20b (diff) |
#86409# Support encryption in package files + a couple of optimisations
Diffstat (limited to 'package/source/zipapi')
-rw-r--r-- | package/source/zipapi/EntryInputStream.cxx | 96 | ||||
-rw-r--r-- | package/source/zipapi/EntryInputStream.hxx | 30 | ||||
-rw-r--r-- | package/source/zipapi/Inflater.cxx | 39 | ||||
-rw-r--r-- | package/source/zipapi/ZipEnumeration.cxx | 22 | ||||
-rw-r--r-- | package/source/zipapi/ZipFile.cxx | 175 | ||||
-rw-r--r-- | package/source/zipapi/ZipOutputStream.cxx | 135 | ||||
-rw-r--r-- | package/source/zipapi/makefile.mk | 18 |
7 files changed, 273 insertions, 242 deletions
diff --git a/package/source/zipapi/EntryInputStream.cxx b/package/source/zipapi/EntryInputStream.cxx index efb893439bb6..023dd53f6989 100644 --- a/package/source/zipapi/EntryInputStream.cxx +++ b/package/source/zipapi/EntryInputStream.cxx @@ -2,9 +2,9 @@ * * $RCSfile: EntryInputStream.cxx,v $ * - * $Revision: 1.12 $ + * $Revision: 1.13 $ * - * last change: $Author: mtg $ $Date: 2001-04-19 14:13:40 $ + * last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -59,55 +59,59 @@ * ************************************************************************/ #ifndef _ENTRY_INPUT_STREAM_HXX -#include "EntryInputStream.hxx" +#include <EntryInputStream.hxx> #endif #ifndef _COM_SUN_STAR_PACKAGES_ZIPCONSTANTS_HPP_ #include <com/sun/star/packages/ZipConstants.hpp> #endif +#ifndef _RTL_CIPHER_H_ +#include <rtl/cipher.h> +#endif #include <memory.h> // for memcpy using namespace rtl; using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::packages::ZipConstants; /** Provides access to the compressed data in a zipfile. * * 04/12/00 - uncompresses the stream into memory and seeks on it 'in memory' - * This is a "temporary" fix which will be changed ASAP (read 2001) + * This and the ZipPackageBuffer used in the ZipOutputStream are memory hogs + * and will hopefully be replaced eventually * * Acts on the same underlying XInputStream as both the full Zip File and other * EntryInputStreams, and thus must maintain its current position in the stream and * seek to it before performing any reads. */ -EntryInputStream::EntryInputStream( uno::Reference < io::XInputStream > xNewInput, - sal_Int64 nNewBegin, - sal_Int64 nNewEnd, - sal_Int64 nNewUncompressedSize, +EntryInputStream::EntryInputStream( Reference < io::XInputStream > xNewInput, + const packages::ZipEntry & rNewEntry, + const vos::ORef < EncryptionData > &xEncryptData, sal_Bool bIsDeflated) : xStream( xNewInput ) -, xSeek( xNewInput, uno::UNO_QUERY ) -, nBegin( nNewBegin ) -, nEnd ( nNewEnd ) +, xSeek( xNewInput, UNO_QUERY ) +, rEntry (rNewEntry ) , nCurrent( 0 ) -, nUncompressedSize (nNewUncompressedSize) , aSequence ( 0 ) -, bReachEOF ( sal_False ) , bHaveInMemory ( sal_False ) , aInflater( sal_True ) , aBuffer( 0 ) , bDeflated ( bIsDeflated ) +, xEncryptionData (xEncryptData) { + nEnd = rEntry.nMethod == DEFLATED ? rEntry.nOffset + rEntry.nCompressedSize : rEntry.nOffset + rEntry.nSize; } void EntryInputStream::readIntoMemory() { if (!bHaveInMemory) { - aBuffer.realloc ( static_cast < sal_Int32 > ( nUncompressedSize ) ); + aBuffer.realloc ( static_cast < sal_Int32 > ( rEntry.nSize ) ); if (bDeflated) { - sal_Int32 nSize = static_cast < sal_Int32 > (nEnd - nBegin ); + sal_Int32 nSize = static_cast < sal_Int32 > (nEnd - rEntry.nOffset ); aSequence.realloc( nSize ); - xSeek->seek(nBegin); + xSeek->seek(rEntry.nOffset); xStream->readBytes(aSequence, nSize ); aInflater.setInputSegment(aSequence, 0, nSize ); aInflater.doInflate(aBuffer); @@ -116,26 +120,46 @@ void EntryInputStream::readIntoMemory() } else { - xSeek->seek(nBegin); - xStream->readBytes(aBuffer, static_cast < sal_Int32 > (nUncompressedSize)); + xSeek->seek(rEntry.nOffset); + xStream->readBytes(aBuffer, static_cast < sal_Int32 > (rEntry.nSize)); } bHaveInMemory = sal_True; + + /* + * Don't have the decryption code yet... + if (rEncryptionKey.getLength()) + { + // An encrypted entry! + rtlCipherError aResult; + aSequence.realloc ( rEntry.nSize ); + rtlCipher aCipher = rtl_cipher_create (rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream); + aResult = rtl_cipher_init( aCipher, rtl_Cipher_DirectionDecode, + reinterpret_cast < const sal_uInt8 * > ( rEncryptionKey.getConstArray()), + rEncryptionKey.getLength(), + reinterpret_cast < const sal_uInt8 * > ( rVector.getConstArray()), + rVector.getLength() ); + OSL_ASSERT (aResult == rtl_Cipher_E_None); + aBuffer.realloc ( 0 ); + aBuffer = aSequence; + aSequence.realloc ( 0 ); + } + */ } } EntryInputStream::~EntryInputStream( void ) { } -sal_Int32 SAL_CALL EntryInputStream::readBytes( uno::Sequence< sal_Int8 >& aData, +sal_Int32 SAL_CALL EntryInputStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException) + throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) { if (nBytesToRead <0) throw io::BufferSizeExceededException(::rtl::OUString(), *this); if (!bHaveInMemory) readIntoMemory(); - if (nBytesToRead + nCurrent > nUncompressedSize) - nBytesToRead = static_cast < sal_Int32> (nUncompressedSize - nCurrent); + if (nBytesToRead + nCurrent > rEntry.nSize) + nBytesToRead = static_cast < sal_Int32> (rEntry.nSize - nCurrent); aData.realloc( nBytesToRead ); memcpy(aData.getArray(), aBuffer.getConstArray() + nCurrent, nBytesToRead); @@ -143,49 +167,49 @@ sal_Int32 SAL_CALL EntryInputStream::readBytes( uno::Sequence< sal_Int8 >& aData return nBytesToRead; } -sal_Int32 SAL_CALL EntryInputStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, +sal_Int32 SAL_CALL EntryInputStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException) + throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) { return readBytes( aData, nMaxBytesToRead ); } void SAL_CALL EntryInputStream::skipBytes( sal_Int32 nBytesToSkip ) - throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException) + throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException) { if (nBytesToSkip < 0) throw io::BufferSizeExceededException(::rtl::OUString(), *this); - if (nBytesToSkip + nCurrent > nUncompressedSize ) - nBytesToSkip = static_cast < sal_Int32 > (nUncompressedSize - nCurrent); + if (nBytesToSkip + nCurrent > rEntry.nSize ) + nBytesToSkip = static_cast < sal_Int32 > (rEntry.nSize - nCurrent); nCurrent+=nBytesToSkip; } sal_Int32 SAL_CALL EntryInputStream::available( ) - throw(io::NotConnectedException, io::IOException, uno::RuntimeException) + throw(io::NotConnectedException, io::IOException, RuntimeException) { - return static_cast < sal_Int32 > (nUncompressedSize - nCurrent); + return static_cast < sal_Int32 > (rEntry.nSize - nCurrent); } void SAL_CALL EntryInputStream::closeInput( ) - throw(io::NotConnectedException, io::IOException, uno::RuntimeException) + throw(io::NotConnectedException, io::IOException, RuntimeException) { } void SAL_CALL EntryInputStream::seek( sal_Int64 location ) - throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException) + throw(lang::IllegalArgumentException, io::IOException, RuntimeException) { - if (location > nUncompressedSize) - location = nUncompressedSize; + if (location > rEntry.nSize) + location = rEntry.nSize; if (location <0) location = 0; nCurrent = location; } sal_Int64 SAL_CALL EntryInputStream::getPosition( ) - throw(io::IOException, uno::RuntimeException) + throw(io::IOException, RuntimeException) { return nCurrent; } sal_Int64 SAL_CALL EntryInputStream::getLength( ) - throw(io::IOException, uno::RuntimeException) + throw(io::IOException, RuntimeException) { - return nUncompressedSize; + return rEntry.nSize; } diff --git a/package/source/zipapi/EntryInputStream.hxx b/package/source/zipapi/EntryInputStream.hxx index d1a7fd128917..82463f4158f9 100644 --- a/package/source/zipapi/EntryInputStream.hxx +++ b/package/source/zipapi/EntryInputStream.hxx @@ -2,9 +2,9 @@ * * $RCSfile: EntryInputStream.hxx,v $ * - * $Revision: 1.1 $ + * $Revision: 1.2 $ * - * last change: $Author: mtg $ $Date: 2001-04-19 14:12:53 $ + * last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -73,23 +73,33 @@ #ifndef _INFLATER_HXX_ #include <Inflater.hxx> #endif - +#ifndef _COM_SUN_STAR_PACKAGES_ZIPENTRY_HPP_ +#include <com/sun/star/packages/ZipEntry.hpp> +#endif +#ifndef _VOS_REF_H_ +#include <vos/ref.hxx> +#endif +#ifndef _ENCRYPTION_DATA_HXX +#include <EncryptionData.hxx> +#endif class EntryInputStream : public cppu::WeakImplHelper2< com::sun::star::io::XInputStream, com::sun::star::io::XSeekable > { private: com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xStream; com::sun::star::uno::Reference< com::sun::star::io::XSeekable > xSeek; - sal_Int64 nBegin, nEnd, nCurrent, nUncompressedSize; - sal_Bool bReachEOF; - sal_Bool bDeflated; - sal_Bool bHaveInMemory; - com::sun::star::uno::Sequence < sal_Int8 > aSequence; - com::sun::star::uno::Sequence < sal_Int8 > aBuffer; + sal_Int64 nEnd, nCurrent; + sal_Bool bDeflated, bHaveInMemory, bEncrypted; + com::sun::star::uno::Sequence < sal_Int8 > aSequence, aBuffer; + const vos::ORef < EncryptionData > xEncryptionData; + const com::sun::star::packages::ZipEntry & rEntry; Inflater aInflater; void readIntoMemory(); public: - EntryInputStream( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xInput, sal_Int64 nBegin, sal_Int64 nEnd, sal_Int64 nNewUncompressedSize, sal_Bool bIsDeflated); + EntryInputStream( com::sun::star::uno::Reference < com::sun::star::io::XInputStream > xInput, + const com::sun::star::packages::ZipEntry &rNewEntry, + const vos::ORef < EncryptionData > &xEncryptData, + sal_Bool bIsDeflated ); virtual ~EntryInputStream(); // XInputStream diff --git a/package/source/zipapi/Inflater.cxx b/package/source/zipapi/Inflater.cxx index e58067a6ee38..bcc6f64c43ec 100644 --- a/package/source/zipapi/Inflater.cxx +++ b/package/source/zipapi/Inflater.cxx @@ -2,9 +2,9 @@ * * $RCSfile: Inflater.cxx,v $ * - * $Revision: 1.7 $ + * $Revision: 1.8 $ * - * last change: $Author: mtg $ $Date: 2001-04-19 14:13:40 $ + * last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -74,13 +74,21 @@ /** Provides general purpose decompression using the ZLIB library */ -void Inflater::init (sal_Bool bNowrap) + +Inflater::Inflater(sal_Bool bNoWrap) +: bFinish(sal_False), + bFinished(sal_False), + bSetParams(sal_False), + bNeedDict(sal_False), + nOffset(0), + nLength(0), + pStream(NULL) { pStream = new z_stream; /* memset to 0 to set zalloc/opaque etc */ memset (pStream, 0, sizeof(*pStream)); sal_Int32 nRes; - nRes = inflateInit2(pStream, bNowrap ? -MAX_WBITS : MAX_WBITS); + nRes = inflateInit2(pStream, bNoWrap ? -MAX_WBITS : MAX_WBITS); switch (nRes) { case Z_OK: @@ -99,29 +107,6 @@ void Inflater::init (sal_Bool bNowrap) } } -Inflater::Inflater(sal_Bool bNoWrap) -: bFinish(sal_False), - bFinished(sal_False), - bSetParams(sal_False), - bNeedDict(sal_False), - nOffset(0), - nLength(0), - pStream(NULL) -{ - init(bNoWrap); -} - -Inflater::Inflater() -: bFinish(sal_False), - bFinished(sal_False), - bSetParams(sal_False), - bNeedDict(sal_False), - nOffset(0), - nLength(0), - pStream(NULL) -{ - init(sal_False); -} Inflater::~Inflater() { end(); diff --git a/package/source/zipapi/ZipEnumeration.cxx b/package/source/zipapi/ZipEnumeration.cxx index e5c373d4a6c4..dedd320b1d3a 100644 --- a/package/source/zipapi/ZipEnumeration.cxx +++ b/package/source/zipapi/ZipEnumeration.cxx @@ -2,9 +2,9 @@ * * $RCSfile: ZipEnumeration.cxx,v $ * - * $Revision: 1.8 $ + * $Revision: 1.9 $ * - * last change: $Author: mtg $ $Date: 2001-04-19 14:13:40 $ + * last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -61,12 +61,10 @@ #ifndef _ZIP_ENUMERATION_HXX #include <ZipEnumeration.hxx> #endif -#ifndef _COM_SUN_STAR_PACKAGES_ZIPCONSTANTS_HPP_ -#include <com/sun/star/packages/ZipConstants.hpp> -#endif using namespace rtl; using namespace com::sun::star; +using namespace com::sun::star::packages; /** Provides an Enumeration over the contents of a Zip file */ @@ -78,17 +76,15 @@ ZipEnumeration::ZipEnumeration( EntryHash & rNewEntryHash) ZipEnumeration::~ZipEnumeration( void ) { } -sal_Bool SAL_CALL ZipEnumeration::hasMoreElements() throw (uno::RuntimeException) +sal_Bool SAL_CALL ZipEnumeration::hasMoreElements() { return (aIterator != rEntryHash.end()); } -uno::Any SAL_CALL ZipEnumeration::nextElement() throw (uno::RuntimeException) +const ZipEntry* SAL_CALL ZipEnumeration::nextElement() { - uno::Any aAny; - if (aIterator == rEntryHash.end()) - throw container::NoSuchElementException(); - aAny <<= (*aIterator).second; - aIterator++; - return aAny; + if (aIterator != rEntryHash.end()) + return &((*aIterator++).second); + else + return NULL; } diff --git a/package/source/zipapi/ZipFile.cxx b/package/source/zipapi/ZipFile.cxx index 23c45905011a..87a6081653eb 100644 --- a/package/source/zipapi/ZipFile.cxx +++ b/package/source/zipapi/ZipFile.cxx @@ -2,9 +2,9 @@ * * $RCSfile: ZipFile.cxx,v $ * - * $Revision: 1.19 $ + * $Revision: 1.20 $ * - * last change: $Author: mtg $ $Date: 2001-04-19 14:13:40 $ + * last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -78,12 +78,14 @@ using namespace rtl; using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::packages; using namespace com::sun::star::packages::ZipConstants; /** This class is used to read entries from a zip file */ -ZipFile::ZipFile( uno::Reference < io::XInputStream > &xInput, sal_Bool bInitialise) - throw(io::IOException, packages::ZipException, uno::RuntimeException) +ZipFile::ZipFile( Reference < io::XInputStream > &xInput, sal_Bool bInitialise) + throw(io::IOException, ZipException, RuntimeException) : xStream(xInput) , aGrabber(xInput) , aInflater (sal_True) @@ -91,7 +93,7 @@ ZipFile::ZipFile( uno::Reference < io::XInputStream > &xInput, sal_Bool bInitial if (bInitialise) readCEN(); } -void ZipFile::setInputStream ( uno::Reference < io::XInputStream > xNewStream ) +void ZipFile::setInputStream ( Reference < io::XInputStream > xNewStream ) { xStream = xNewStream; aGrabber.setInputStream ( xStream ); @@ -107,7 +109,7 @@ void ZipFile::updateFromManList(std::vector < ManifestEntry * > &rManList) // I'm not sure how evil this is in this case... for (;i < nSize ; i++) { - packages::ZipEntry * pEntry = &rManList[i]->aEntry; + ZipEntry * pEntry = &rManList[i]->aEntry; aEntries[pEntry->sName] = *pEntry; } } @@ -118,145 +120,91 @@ ZipFile::~ZipFile() } void SAL_CALL ZipFile::close( ) - throw(io::IOException, uno::RuntimeException) + throw(io::IOException, RuntimeException) { } -uno::Reference< container::XEnumeration > SAL_CALL ZipFile::entries( ) - throw(uno::RuntimeException) +ZipEnumeration * SAL_CALL ZipFile::entries( ) { - uno::Reference< container::XEnumeration> xEnumRef = new ZipEnumeration( aEntries ); - return xEnumRef; + return new ZipEnumeration ( aEntries ); } ::rtl::OUString SAL_CALL ZipFile::getName( ) - throw(uno::RuntimeException) + throw(RuntimeException) { return sName; } sal_Int32 SAL_CALL ZipFile::getSize( ) - throw(uno::RuntimeException) + throw(RuntimeException) { return aEntries.size(); } -uno::Type SAL_CALL ZipFile::getElementType( ) - throw(uno::RuntimeException) +Type SAL_CALL ZipFile::getElementType( ) + throw(RuntimeException) { - return ::getCppuType((packages::ZipEntry *) 0); + return ::getCppuType((ZipEntry *) 0); } sal_Bool SAL_CALL ZipFile::hasElements( ) - throw(uno::RuntimeException) + throw(RuntimeException) { return (aEntries.size()>0); } -uno::Any SAL_CALL ZipFile::getByName( const ::rtl::OUString& aName ) - throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) +Any SAL_CALL ZipFile::getByName( const ::rtl::OUString& aName ) + throw(container::NoSuchElementException, lang::WrappedTargetException, RuntimeException) { - uno::Any aAny; - if (!aEntries.count(sName)) - throw container::NoSuchElementException(); + Any aAny; EntryHash::const_iterator aCI = aEntries.find(sName); + if (aCI == aEntries.end()) + throw container::NoSuchElementException(); aAny <<= (*aCI).second; return aAny; } -uno::Sequence< ::rtl::OUString > SAL_CALL ZipFile::getElementNames( ) - throw(uno::RuntimeException) +Sequence< ::rtl::OUString > SAL_CALL ZipFile::getElementNames( ) + throw(RuntimeException) { sal_uInt32 i=0, nSize = aEntries.size(); OUString *pNames = new OUString[aEntries.size()]; for (EntryHash::const_iterator aIterator = aEntries.begin(); aIterator != aEntries.end(); aIterator++,i++) pNames[i] = (*aIterator).first; - return uno::Sequence<OUString> (pNames, nSize); + return Sequence<OUString> (pNames, nSize); } sal_Bool SAL_CALL ZipFile::hasByName( const ::rtl::OUString& aName ) - throw(uno::RuntimeException) + throw(RuntimeException) { - return aEntries.count(aName); + return aEntries.find(aName) != aEntries.end(); } -uno::Reference< io::XInputStream > SAL_CALL ZipFile::getInputStream( const packages::ZipEntry& rEntry ) - throw(io::IOException, packages::ZipException, uno::RuntimeException) +Reference< io::XInputStream > SAL_CALL ZipFile::getInputStream( ZipEntry& rEntry, + const vos::ORef < EncryptionData > &rData) + throw(io::IOException, ZipException, RuntimeException) { sal_Int64 nSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize; if (rEntry.nOffset <= 0) readLOC(rEntry); - uno::Reference< io::XInputStream > xStreamRef = - new EntryInputStream(xStream, - rEntry.nOffset, - rEntry.nOffset + nSize, - rEntry.nSize, - rEntry.nMethod == DEFLATED ); + Reference< io::XInputStream > xStreamRef = new EntryInputStream(xStream, rEntry, rData, rEntry.nMethod == DEFLATED ); return xStreamRef; } -sal_uInt32 SAL_CALL ZipFile::getHeader(const packages::ZipEntry& rEntry) - throw(io::IOException, packages::ZipException, uno::RuntimeException) -{ - uno::Sequence < sal_Int8 > aSequence (4); - - try - { - if (rEntry.nOffset <= 0) - readLOC(rEntry); - } - catch (packages::ZipException&) - { - VOS_ENSURE(0, "Zip file bug!"); - return 0; - } - - aGrabber.seek(rEntry.nOffset); - if (rEntry.nMethod == STORED) - { - if (xStream->readBytes(aSequence, 4) < 4) - return 0; - } - else if (rEntry.nMethod == DEFLATED) - { - /* - uno::Reference < io::XInputStream > xEntryStream = getInputStream (rEntry); - if (xEntryStream->readBytes(aSequence, 4) < 4) - return 0; - */ - sal_Int32 nSize = rEntry.nCompressedSize < 32768 ? rEntry.nCompressedSize : 32768; - uno::Sequence < sal_Int8 > aCompSeq (nSize ); - if (xStream->readBytes(aCompSeq, nSize) < nSize) - return 0; - aInflater.finish(); - aInflater.setInput(aCompSeq); - aInflater.doInflate(aSequence); - aInflater.reset(); - } - return (static_cast < sal_uInt32 > - (static_cast < sal_uInt8> (aSequence[0]& 0xFF) - | static_cast < sal_uInt8> (aSequence[1]& 0xFF) << 8 - | static_cast < sal_uInt8> (aSequence[2]& 0xFF) << 16 - | static_cast < sal_uInt8> (aSequence[3]& 0xFF) << 24)); -} - -uno::Reference< io::XInputStream > SAL_CALL ZipFile::getRawStream( const packages::ZipEntry& rEntry ) - throw(io::IOException, packages::ZipException, uno::RuntimeException) +Reference< io::XInputStream > SAL_CALL ZipFile::getRawStream( ZipEntry& rEntry, + const vos::ORef < EncryptionData > &rData) + throw(io::IOException, ZipException, RuntimeException) { sal_Int64 nSize = rEntry.nMethod == DEFLATED ? rEntry.nCompressedSize : rEntry.nSize; if (rEntry.nOffset <= 0) readLOC(rEntry); - uno::Reference< io::XInputStream > xStreamRef = - new EntryInputStream(xStream, - rEntry.nOffset, - rEntry.nOffset + nSize, - nSize, - sal_False ); + Reference< io::XInputStream > xStreamRef = + new EntryInputStream(xStream, rEntry, rData, sal_False ); return xStreamRef; } -sal_Bool ZipFile::readLOC(const packages::ZipEntry &rEntry) - throw(io::IOException, packages::ZipException, uno::RuntimeException) +sal_Bool ZipFile::readLOC( ZipEntry &rEntry ) + throw(io::IOException, ZipException, RuntimeException) { sal_uInt32 nTestSig, nTime, nCRC, nSize, nCompressedSize; sal_uInt16 nVersion, nFlag, nHow, nNameLen, nExtraLen; @@ -266,7 +214,7 @@ sal_Bool ZipFile::readLOC(const packages::ZipEntry &rEntry) aGrabber >> nTestSig; if (nTestSig != LOCSIG) - throw packages::ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid LOC header (bad signature") ), uno::Reference < uno::XInterface > () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid LOC header (bad signature") ), Reference < XInterface > () ); aGrabber >> nVersion; aGrabber >> nFlag; aGrabber >> nHow; @@ -276,21 +224,20 @@ sal_Bool ZipFile::readLOC(const packages::ZipEntry &rEntry) aGrabber >> nSize; aGrabber >> nNameLen; aGrabber >> nExtraLen; - packages::ZipEntry *pNonConstEntry = const_cast < packages::ZipEntry* > (&rEntry); - pNonConstEntry->nOffset = static_cast < sal_Int32 > (aGrabber.getPosition()) + nNameLen + nExtraLen; + rEntry.nOffset = static_cast < sal_Int32 > (aGrabber.getPosition()) + nNameLen + nExtraLen; return sal_True; } sal_Int32 ZipFile::findEND( ) - throw(io::IOException, packages::ZipException, uno::RuntimeException) + throw(io::IOException, ZipException, RuntimeException) { sal_Int32 nLength=0, nPos=0; - uno::Sequence < sal_Int8 > aByteSeq; + Sequence < sal_Int8 > aByteSeq; nLength = nPos = static_cast <sal_Int32 > (aGrabber.getLength()); if (nLength == 0) return -1; - //throw (packages::ZipException( OUString::createFromAscii("Trying to find Zip END signature in a zero length file!"), uno::Reference < uno::XInterface> () )); + //throw (ZipException( OUString::createFromAscii("Trying to find Zip END signature in a zero length file!"), Reference < XInterface> () )); aGrabber.seek( nLength ); @@ -317,7 +264,7 @@ sal_Int32 ZipFile::findEND( ) if (nCommentLength>0) { aByteSeq.realloc(nCommentLength+1); - aGrabber.readBytes(uno::Sequence< sal_Int8>(aByteSeq.getArray(), nCommentLength), nCommentLength); + aGrabber.readBytes(Sequence< sal_Int8>(aByteSeq.getArray(), nCommentLength), nCommentLength); aByteSeq[nCommentLength]='\0'; sComment = OUString((sal_Char*)aByteSeq.getConstArray(), nCommentLength+1, RTL_TEXTENCODING_ASCII_US); @@ -327,11 +274,11 @@ sal_Int32 ZipFile::findEND( ) } } } - throw packages::ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), uno::Reference < uno::XInterface> () ); + throw ZipException( OUString( RTL_CONSTASCII_USTRINGPARAM ( "Zip END signature not found!") ), Reference < XInterface> () ); } sal_Int32 ZipFile::readCEN() - throw(io::IOException, packages::ZipException, uno::RuntimeException) + throw(io::IOException, ZipException, RuntimeException) { sal_Int32 nEndPos, nLocPos; sal_Int16 nCount, nTotal; @@ -346,23 +293,23 @@ sal_Int32 ZipFile::readCEN() aGrabber >> nCenOff; if (nTotal<0 || nTotal * CENHDR > nCenLen) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "invalid END header (bad entry count)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "invalid END header (bad entry count)") ), Reference < XInterface > ()); if (nTotal > ZIP_MAXENTRIES) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "too many entries in ZIP File") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "too many entries in ZIP File") ), Reference < XInterface > ()); if (nCenLen < 0 || nCenLen > nEndPos) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), Reference < XInterface > ()); nCenPos = nEndPos - nCenLen; if (nCenOff < 0 || nCenOff > nCenPos) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid END header (bad central directory size)") ), Reference < XInterface > ()); nLocPos = nCenPos - nCenOff; aGrabber.seek(nCenPos); - packages::ZipEntry *pEntry = new packages::ZipEntry; + ZipEntry *pEntry = new ZipEntry; for (nCount = 0 ; nCount < nTotal; nCount++) { sal_Int32 nTestSig, nCRC, nCompressedSize, nTime, nSize, nExtAttr, nOffset; @@ -370,21 +317,21 @@ sal_Int32 ZipFile::readCEN() sal_Int16 nDisk, nIntAttr; if (aGrabber.getPosition() - nCenPos + CENHDR > nCenLen) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad header size check 1)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad header size check 1)") ), Reference < XInterface > ()); aGrabber >> nTestSig; if (nTestSig != CENSIG) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad signature)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad signature)") ), Reference < XInterface > ()); aGrabber >> nVerMade; aGrabber >> nVersion; if ((nVersion & 1) == 1) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (encrypted entry)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (encrypted entry)") ), Reference < XInterface > ()); aGrabber >> nFlag; aGrabber >> nHow; if (nHow != STORED && nHow != DEFLATED) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad compression method)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad compression method)") ), Reference < XInterface > ()); aGrabber >> nTime; aGrabber >> nCRC; @@ -399,13 +346,13 @@ sal_Int32 ZipFile::readCEN() aGrabber >> nOffset; if (aGrabber.getPosition() - nCenPos + nNameLen + nExtraLen + nCommentLen > nCenLen) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad header size check 2)") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Invalid CEN header (bad header size check 2)") ), Reference < XInterface > ()); if (nNameLen > ZIP_MAXNAMELEN) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "name length exceeds 512 bytes" ) ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "name length exceeds 512 bytes" ) ), Reference < XInterface > ()); if (nExtraLen > ZIP_MAXEXTRA) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "extra header info exceeds 256 bytes") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "extra header info exceeds 256 bytes") ), Reference < XInterface > ()); pEntry->nTime = nTime; pEntry->nCrc = nCRC; @@ -421,14 +368,14 @@ sal_Int32 ZipFile::readCEN() if (nHow == STORED) pEntry->nCompressedSize = 0; */ - uno::Sequence < sal_Int8> aSequence (nNameLen); + Sequence < sal_Int8> aSequence (nNameLen); aGrabber.readBytes(aSequence, nNameLen); pEntry->sName = OUString((sal_Char*)aSequence.getConstArray(), nNameLen, RTL_TEXTENCODING_ASCII_US); aGrabber.seek(aGrabber.getPosition() + nExtraLen); if (nCommentLen>0) { - uno::Sequence < sal_Int8 > aCommentSeq( nCommentLen ); + Sequence < sal_Int8 > aCommentSeq( nCommentLen ); aGrabber.readBytes(aCommentSeq, nCommentLen); pEntry->sComment = OUString((sal_Char*)aCommentSeq.getConstArray(), nNameLen, RTL_TEXTENCODING_ASCII_US); } @@ -437,7 +384,7 @@ sal_Int32 ZipFile::readCEN() delete pEntry; if (nCount != nTotal) - throw packages::ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Count != Total") ), uno::Reference < uno::XInterface > ()); + throw ZipException(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Count != Total") ), Reference < XInterface > ()); return nCenPos; } diff --git a/package/source/zipapi/ZipOutputStream.cxx b/package/source/zipapi/ZipOutputStream.cxx index d8078ca0dcbf..ee0ef68f6660 100644 --- a/package/source/zipapi/ZipOutputStream.cxx +++ b/package/source/zipapi/ZipOutputStream.cxx @@ -2,9 +2,9 @@ * * $RCSfile: ZipOutputStream.cxx,v $ * - * $Revision: 1.24 $ + * $Revision: 1.25 $ * - * last change: $Author: mtg $ $Date: 2001-04-19 14:13:40 $ + * last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -64,14 +64,19 @@ #ifndef _VOS_DIAGNOSE_H_ #include <vos/diagnose.hxx> #endif +#ifndef _VOS_REF_H_ +#include <vos/ref.hxx> +#endif #ifndef _COM_SUN_STAR_PACKAGES_ZIPCONSTANTS_HPP_ #include <com/sun/star/packages/ZipConstants.hpp> #endif -#include <time.h> -#include <utime.h> +#ifndef _OSL_TIME_H_ +#include <osl/time.h> +#endif using namespace rtl; using namespace com::sun::star; +using namespace com::sun::star::uno; using namespace com::sun::star::packages::ZipConstants; /** This class is used to write Zip files @@ -82,6 +87,7 @@ ZipOutputStream::ZipOutputStream( uno::Reference < io::XOutputStream > &xOStream , nMethod(DEFLATED) , pCurrentEntry(NULL) , bFinished(sal_False) +, bEncryptCurrentEntry(sal_False) , aBuffer(nNewBufferSize) , aDeflater(DEFAULT_COMPRESSION, sal_True) { @@ -108,30 +114,64 @@ void SAL_CALL ZipOutputStream::setLevel( sal_Int32 nNewLevel ) { aDeflater.setLevel( nNewLevel); } -void SAL_CALL ZipOutputStream::putNextEntry( const packages::ZipEntry& rEntry ) + +Sequence < sal_Int8 > ZipOutputStream::getInitialisationVector() +{ + Sequence < sal_Int8 > aSequence( 8 ); + TimeValue aTimeVal; + sal_Int8 * pVector = aSequence.getArray(); + osl_getSystemTime( &aTimeVal ); + + pVector[0] = static_cast < sal_Int8 > ( (aTimeVal.Seconds >> 0 ) & 0xFF ); + pVector[1] = static_cast < sal_Int8 > ( (aTimeVal.Seconds >> 8 ) & 0xFF ); + pVector[2] = static_cast < sal_Int8 > ( (aTimeVal.Seconds >> 16 ) & 0xFF ); + pVector[3] = static_cast < sal_Int8 > ( (aTimeVal.Seconds >> 24 ) & 0xFF ); + pVector[4] = static_cast < sal_Int8 > ( (aTimeVal.Nanosec >> 0 ) & 0xFF ); + pVector[5] = static_cast < sal_Int8 > ( (aTimeVal.Nanosec >> 8 ) & 0xFF ); + pVector[6] = static_cast < sal_Int8 > ( (aTimeVal.Nanosec >> 16 ) & 0xFF ); + pVector[7] = static_cast < sal_Int8 > ( (aTimeVal.Nanosec >> 24 ) & 0xFF ); + return aSequence; +} + +void SAL_CALL ZipOutputStream::putNextEntry( packages::ZipEntry& rEntry, + const vos::ORef < EncryptionData > &xEncryptData, + sal_Bool bEncrypt) throw(io::IOException, uno::RuntimeException) { - packages::ZipEntry *pNonConstEntry = const_cast < packages::ZipEntry* >(&rEntry); if (pCurrentEntry != NULL) closeEntry(); - if (pNonConstEntry->nTime == -1) - pNonConstEntry->nTime = getCurrentDosTime(); - if (pNonConstEntry->nMethod == -1) - { - pNonConstEntry->nMethod = nMethod; - } - pNonConstEntry->nVersion = 20; - if (pNonConstEntry->nSize == -1 || pNonConstEntry->nCompressedSize == -1 || - pNonConstEntry->nCrc == -1) - pNonConstEntry->nFlag = 8; - else if (pNonConstEntry->nSize != -1 && pNonConstEntry->nCompressedSize != -1 && - pNonConstEntry->nCrc != -1) - pNonConstEntry->nFlag = 0; + if (rEntry.nTime == -1) + rEntry.nTime = getCurrentDosTime(); + if (rEntry.nMethod == -1) + rEntry.nMethod = nMethod; + rEntry.nVersion = 20; + if (rEntry.nSize == -1 || rEntry.nCompressedSize == -1 || + rEntry.nCrc == -1) + rEntry.nFlag = 8; + else if (rEntry.nSize != -1 && rEntry.nCompressedSize != -1 && + rEntry.nCrc != -1) + rEntry.nFlag = 0; - pNonConstEntry->nOffset = static_cast < sal_Int32 > (aChucker.getPosition()); + rEntry.nOffset = static_cast < sal_Int32 > (aChucker.getPosition()); writeLOC(rEntry); - aZipList.push_back( pNonConstEntry ); - pCurrentEntry=pNonConstEntry; + aZipList.push_back( &rEntry ); + pCurrentEntry = &rEntry; + /* Don't have encryption code yet... + if (bEncrypt) + { + + bEncryptCurrentEntry = sal_True; + rtlCipherError aResult; + + aCipher = rtl_cipher_create ( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream); + aResult = rtl_cipher_init( aCipher, rtl_Cipher_DirectionEncode, + reinterpret_cast < const sal_uInt8 *> (rKey.getConstArray()), + rKey.getLength(), + reinterpret_cast < const sal_uInt8 *> (rVector.getConstArray()), + rVector.getLength()); + OSL_ASSERT( aResult == rtl_Cipher_E_None ); + } + */ } void SAL_CALL ZipOutputStream::close( ) throw(io::IOException, uno::RuntimeException) @@ -220,6 +260,11 @@ void SAL_CALL ZipOutputStream::closeEntry( ) break; } aCRC.reset(); + if (bEncryptCurrentEntry) + { + aEncryptionBuffer.realloc ( 0 ); + bEncryptCurrentEntry = sal_False; + } pCurrentEntry = NULL; } } @@ -241,7 +286,17 @@ void SAL_CALL ZipOutputStream::write( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nOldLength = rBuffer.getLength(); uno::Sequence < sal_Int8 > *pBuffer = const_cast < uno::Sequence < sal_Int8 > *> (&rBuffer); pBuffer->realloc(nNewLength); - aChucker.writeBytes(*pBuffer); + if (bEncryptCurrentEntry) + { + rtlCipherError aResult; + aEncryptionBuffer.realloc ( nNewLength ); + aResult = rtl_cipher_encode ( aCipher, static_cast < const void * > (pBuffer->getConstArray()), + nNewLength, reinterpret_cast < sal_uInt8 * > (aEncryptionBuffer.getArray()), nNewLength ); + aChucker.writeBytes ( aEncryptionBuffer ); + aEncryptionBuffer.realloc ( nOldLength ); + } + else + aChucker.writeBytes( *pBuffer ); pBuffer->realloc(nOldLength); break; } @@ -286,7 +341,17 @@ void ZipOutputStream::doDeflate() if (nLength> 0 ) { aBuffer.realloc(nLength); - aChucker.writeBytes(aBuffer); + if (bEncryptCurrentEntry) + { + rtlCipherError aResult; + aEncryptionBuffer.realloc ( nLength ); + aResult = rtl_cipher_encode ( aCipher, static_cast < const void * > (aBuffer.getConstArray()), + nLength, reinterpret_cast < sal_uInt8 * > (aEncryptionBuffer.getArray()), nLength ); + aChucker.writeBytes ( aEncryptionBuffer ); + aEncryptionBuffer.realloc ( nOldLength ); + } + else + aChucker.writeBytes(aBuffer); aBuffer.realloc(nOldLength); } } @@ -421,22 +486,23 @@ void ZipOutputStream::writeLOC( const packages::ZipEntry &rEntry ) } sal_uInt32 ZipOutputStream::getCurrentDosTime( ) { - time_t nTime = time (NULL); - // pTime is a static internal to the time library and shouldn't be deleted - struct tm *pTime = localtime ( &nTime); + oslDateTime aDateTime; + TimeValue aTimeValue; + osl_getSystemTime ( &aTimeValue ); + osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime); - sal_uInt32 nYear = static_cast <sal_uInt32> (pTime->tm_year); + sal_uInt32 nYear = static_cast <sal_uInt32> (aDateTime.Year); if (nYear>1980) nYear-=1980; else if (nYear>80) nYear-=80; - sal_uInt32 nResult = static_cast < sal_uInt32>( ( ( ( pTime->tm_mday) + - ( 32 * (pTime->tm_mon+1)) + + sal_uInt32 nResult = static_cast < sal_uInt32>( ( ( ( aDateTime.Day) + + ( 32 * (aDateTime.Month)) + ( 512 * nYear ) ) << 16) | - ( ( pTime->tm_sec/2) + - ( 32 * pTime->tm_min) + - ( 2048 * static_cast <sal_uInt32 > (pTime->tm_hour) ) ) ); + ( ( aDateTime.Seconds/2) + + ( 32 * aDateTime.Minutes) + + ( 2048 * static_cast <sal_uInt32 > (aDateTime.Hours) ) ) ); return nResult; } /* @@ -444,6 +510,9 @@ sal_uInt32 ZipOutputStream::getCurrentDosTime( ) This is actually never used, so I removed it, but thought that the implementation details may be useful in the future...mtg 20010307 + I stopped using the time library and used the OSL version instead, but + it might still be useful to have this code here.. + void ZipOutputStream::dosDateToTMDate ( tm &rTime, sal_uInt32 nDosDate) { sal_uInt32 nDate = static_cast < sal_uInt32 > (nDosDate >> 16); diff --git a/package/source/zipapi/makefile.mk b/package/source/zipapi/makefile.mk index 5bda73ee2225..389c46e7dd95 100644 --- a/package/source/zipapi/makefile.mk +++ b/package/source/zipapi/makefile.mk @@ -2,9 +2,9 @@ # # $RCSfile: makefile.mk,v $ # -# $Revision: 1.7 $ +# $Revision: 1.8 $ # -# last change: $Author: mtg $ $Date: 2001-04-19 14:13:40 $ +# last change: $Author: mtg $ $Date: 2001-04-27 14:56:06 $ # # The Contents of this file are made available subject to the terms of # either of the following licenses @@ -97,15 +97,15 @@ UNOUCRDEP= $(SOLARBINDIR)$/applicat.rdb UNOUCRRDB= $(SOLARBINDIR)$/applicat.rdb UNOTYPES=\ - com.sun.star.packages.XChecksum \ - com.sun.star.packages.XInflater \ - com.sun.star.packages.XDeflater \ - com.sun.star.packages.XZipFile \ - com.sun.star.packages.XZipInputStream \ - com.sun.star.packages.XZipOutputStream \ - com.sun.star.packages.ZipConstants \ com.sun.star.packages.ZipEntry \ + com.sun.star.packages.ZipConstants \ com.sun.star.packages.ZipException +# com.sun.star.packages.XChecksum \ +# com.sun.star.packages.XInflater \ +# com.sun.star.packages.XDeflater \ +# com.sun.star.packages.XZipFile \ +# com.sun.star.packages.XZipInputStream \ +# com.sun.star.packages.XZipOutputStream \ # --- Targets ------------------------------------------------------ |