Kaydet (Commit) 736d7a0d authored tarafından Ivo Hinkelmann's avatar Ivo Hinkelmann

CWS-TOOLING: integrate CWS fwk165

...@@ -39,7 +39,12 @@ namespace beans ...@@ -39,7 +39,12 @@ namespace beans
{ {
struct PropertyValue; struct PropertyValue;
} }
namespace packages
{
class ContentInfo;
}
} } } } } }
class ZipFile; class ZipFile;
class ZipPackage; class ZipPackage;
class ZipOutputStream; class ZipOutputStream;
...@@ -85,6 +90,8 @@ public: ...@@ -85,6 +90,8 @@ public:
void setPackageFormat_Impl( sal_Int32 nFormat ) { m_nFormat = nFormat; } void setPackageFormat_Impl( sal_Int32 nFormat ) { m_nFormat = nFormat; }
void setRemoveOnInsertMode_Impl( sal_Bool bRemove ) { this->mbAllowRemoveOnInsert = bRemove; } void setRemoveOnInsertMode_Impl( sal_Bool bRemove ) { this->mbAllowRemoveOnInsert = bRemove; }
bool saveChild(const rtl::OUString &rShortName, const com::sun::star::packages::ContentInfo &rInfo, rtl::OUString &rPath, std::vector < com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue > > &rManList, ZipOutputStream & rZipOut, com::sun::star::uno::Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool & rRandomPool);
// Recursive functions // Recursive functions
void saveContents(rtl::OUString &rPath, std::vector < com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue > > &rManList, ZipOutputStream & rZipOut, com::sun::star::uno::Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool & rRandomPool) void saveContents(rtl::OUString &rPath, std::vector < com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue > > &rManList, ZipOutputStream & rZipOut, com::sun::star::uno::Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool & rRandomPool)
throw(::com::sun::star::uno::RuntimeException); throw(::com::sun::star::uno::RuntimeException);
......
...@@ -48,8 +48,8 @@ ...@@ -48,8 +48,8 @@
using namespace com::sun::star::packages::zip::ZipConstants; using namespace com::sun::star::packages::zip::ZipConstants;
using namespace com::sun::star::packages::zip; using namespace com::sun::star::packages::zip;
using namespace com::sun::star::container;
using namespace com::sun::star::packages; using namespace com::sun::star::packages;
using namespace com::sun::star::container;
using namespace com::sun::star::beans; using namespace com::sun::star::beans;
using namespace com::sun::star::lang; using namespace com::sun::star::lang;
using namespace com::sun::star::uno; using namespace com::sun::star::uno;
...@@ -305,12 +305,10 @@ static void ImplSetStoredData( ZipEntry & rEntry, Reference < XInputStream> & rS ...@@ -305,12 +305,10 @@ static void ImplSetStoredData( ZipEntry & rEntry, Reference < XInputStream> & rS
rEntry.nCrc = aCRC32.getValue(); rEntry.nCrc = aCRC32.getValue();
} }
void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool &rRandomPool) bool ZipPackageFolder::saveChild( const OUString &rShortName, const ContentInfo &rInfo, OUString &rPath, std::vector < Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool &rRandomPool)
throw(RuntimeException)
{ {
sal_Bool bWritingFailed = sal_False; bool bSuccess = true;
ZipPackageFolder *pFolder = NULL;
ZipPackageStream *pStream = NULL;
const OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ); const OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) );
const OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) ); const OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) );
const OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) ); const OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) );
...@@ -320,386 +318,407 @@ void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < Pr ...@@ -320,386 +318,407 @@ void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < Pr
const OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) ); const OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) );
const OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) ); const OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) );
sal_Bool bHaveEncryptionKey = rEncryptionKey.getLength() ? sal_True : sal_False; Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST);
if ( maContents.begin() == maContents.end() && rPath.getLength() && m_nFormat != embed::StorageFormats::OFOPXML ) OSL_ENSURE( ( rInfo.bFolder && rInfo.pFolder ) || ( !rInfo.bFolder && rInfo.pStream ), "A valid child object is expected!" );
if ( rInfo.bFolder )
{ {
// it is an empty subfolder, use workaround to store it OUString sTempName = rPath + rShortName + OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) );
ZipEntry* pTempEntry = new ZipEntry();
ZipPackageFolder::copyZipEntry ( *pTempEntry, aEntry );
pTempEntry->nPathLen = (sal_Int16)( ::rtl::OUStringToOString( rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
pTempEntry->nExtraLen = -1;
pTempEntry->sPath = rPath;
try if ( rInfo.pFolder->GetMediaType().getLength() )
{ {
vos::ORef < EncryptionData > aEmptyEncr; aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
rZipOut.putNextEntry ( *pTempEntry, aEmptyEncr, sal_False ); aPropSet[PKG_MNFST_MEDIATYPE].Value <<= rInfo.pFolder->GetMediaType();
rZipOut.rawCloseEntry(); aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty;
} aPropSet[PKG_MNFST_VERSION].Value <<= rInfo.pFolder->GetVersion();
catch ( ZipException& ) aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
{ aPropSet[PKG_MNFST_FULLPATH].Value <<= sTempName;
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bWritingFailed = sal_True;
}
catch ( IOException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bWritingFailed = sal_True;
} }
} else
aPropSet.realloc( 0 );
for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end(); rInfo.pFolder->saveContents( sTempName, rManList, rZipOut, rEncryptionKey, rRandomPool);
aCI != aEnd; }
aCI++) else
{ {
const OUString &rShortName = (*aCI).first; // if pTempEntry is necessary, it will be released and passed to the ZipOutputStream
const ContentInfo &rInfo = *(*aCI).second; // and be deleted in the ZipOutputStream destructor
auto_ptr < ZipEntry > pAutoTempEntry ( new ZipEntry );
Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST); ZipEntry* pTempEntry = pAutoTempEntry.get();
if ( rInfo.bFolder ) // In case the entry we are reading is also the entry we are writing, we will
pFolder = rInfo.pFolder; // store the ZipEntry data in pTempEntry
else
pStream = rInfo.pStream;
if ( rInfo.bFolder ) ZipPackageFolder::copyZipEntry ( *pTempEntry, rInfo.pStream->aEntry );
{ pTempEntry->sPath = rPath + rShortName;
OUString sTempName = rPath + rShortName + OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) ); pTempEntry->nPathLen = (sal_Int16)( ::rtl::OUStringToOString( pTempEntry->sPath, RTL_TEXTENCODING_UTF8 ).getLength() );
if ( pFolder->GetMediaType().getLength() ) sal_Bool bToBeEncrypted = rInfo.pStream->IsToBeEncrypted() && (rEncryptionKey.getLength() || rInfo.pStream->HasOwnKey());
{ sal_Bool bToBeCompressed = bToBeEncrypted ? sal_True : rInfo.pStream->IsToBeCompressed();
aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
aPropSet[PKG_MNFST_MEDIATYPE].Value <<= pFolder->GetMediaType();
aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty;
aPropSet[PKG_MNFST_VERSION].Value <<= pFolder->GetVersion();
aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
aPropSet[PKG_MNFST_FULLPATH].Value <<= sTempName;
}
else
aPropSet.realloc( 0 );
pFolder->saveContents( sTempName, rManList, rZipOut, rEncryptionKey, rRandomPool); aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
} aPropSet[PKG_MNFST_MEDIATYPE].Value <<= rInfo.pStream->GetMediaType( );
else aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty;
{ aPropSet[PKG_MNFST_VERSION].Value <<= ::rtl::OUString(); // no version is stored for streams currently
// if pTempEntry is necessary, it will be released and passed to the ZipOutputStream aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
// and be deleted in the ZipOutputStream destructor aPropSet[PKG_MNFST_FULLPATH].Value <<= pTempEntry->sPath;
auto_ptr < ZipEntry > pAutoTempEntry ( new ZipEntry );
ZipEntry* pTempEntry = pAutoTempEntry.get();
// In case the entry we are reading is also the entry we are writing, we will
// store the ZipEntry data in pTempEntry
ZipPackageFolder::copyZipEntry ( *pTempEntry, pStream->aEntry ); OSL_ENSURE( rInfo.pStream->GetStreamMode() != PACKAGE_STREAM_NOTSET, "Unacceptable ZipPackageStream mode!" );
pTempEntry->sPath = rPath + rShortName;
pTempEntry->nPathLen = (sal_Int16)( ::rtl::OUStringToOString( pTempEntry->sPath, RTL_TEXTENCODING_UTF8 ).getLength() );
sal_Bool bToBeEncrypted = pStream->IsToBeEncrypted() && (bHaveEncryptionKey || pStream->HasOwnKey()); sal_Bool bRawStream = sal_False;
sal_Bool bToBeCompressed = bToBeEncrypted ? sal_True : pStream->IsToBeCompressed(); if ( rInfo.pStream->GetStreamMode() == PACKAGE_STREAM_DETECT )
bRawStream = rInfo.pStream->ParsePackageRawStream();
else if ( rInfo.pStream->GetStreamMode() == PACKAGE_STREAM_RAW )
bRawStream = sal_True;
aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty; sal_Bool bTransportOwnEncrStreamAsRaw = sal_False;
aPropSet[PKG_MNFST_MEDIATYPE].Value <<= pStream->GetMediaType( ); // During the storing the original size of the stream can be changed
aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty; // TODO/LATER: get rid of this hack
aPropSet[PKG_MNFST_VERSION].Value <<= ::rtl::OUString(); // no version is stored for streams currently sal_Int32 nOwnStreamOrigSize = bRawStream ? rInfo.pStream->GetMagicalHackSize() : rInfo.pStream->getSize();
aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
aPropSet[PKG_MNFST_FULLPATH].Value <<= pTempEntry->sPath;
sal_Bool bUseNonSeekableAccess = sal_False;
Reference < XInputStream > xStream;
if ( !rInfo.pStream->IsPackageMember() && !bRawStream && !bToBeEncrypted && bToBeCompressed )
{
// the stream is not a package member, not a raw stream,
// it should not be encrypted and it should be compressed,
// in this case nonseekable access can be used
OSL_ENSURE( pStream->GetStreamMode() != PACKAGE_STREAM_NOTSET, "Unacceptable ZipPackageStream mode!" ); xStream = rInfo.pStream->GetOwnStreamNoWrap();
Reference < XSeekable > xSeek ( xStream, UNO_QUERY );
sal_Bool bRawStream = sal_False; bUseNonSeekableAccess = ( xStream.is() && !xSeek.is() );
if ( pStream->GetStreamMode() == PACKAGE_STREAM_DETECT ) }
bRawStream = pStream->ParsePackageRawStream();
else if ( pStream->GetStreamMode() == PACKAGE_STREAM_RAW )
bRawStream = sal_True;
sal_Bool bTransportOwnEncrStreamAsRaw = sal_False; if ( !bUseNonSeekableAccess )
// During the storing the original size of the stream can be changed {
// TODO/LATER: get rid of this hack xStream = rInfo.pStream->getRawData();
sal_Int32 nOwnStreamOrigSize = bRawStream ? pStream->GetMagicalHackSize() : pStream->getSize();
sal_Bool bUseNonSeekableAccess = sal_False; if ( !xStream.is() )
Reference < XInputStream > xStream;
if ( !pStream->IsPackageMember() && !bRawStream && !bToBeEncrypted && bToBeCompressed )
{ {
// the stream is not a package member, not a raw stream, VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
// it should not be encrypted and it should be compressed, bSuccess = false;
// in this case nonseekable access can be used return bSuccess;
xStream = pStream->GetOwnStreamNoWrap();
Reference < XSeekable > xSeek ( xStream, UNO_QUERY );
bUseNonSeekableAccess = ( xStream.is() && !xSeek.is() );
} }
if ( !bUseNonSeekableAccess ) Reference < XSeekable > xSeek ( xStream, UNO_QUERY );
try
{ {
xStream = pStream->getRawData(); if ( xSeek.is() )
if ( !xStream.is() )
{ {
VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" ); // If the stream is a raw one, then we should be positioned
bWritingFailed = sal_True; // at the beginning of the actual data
continue; if ( !bToBeCompressed || bRawStream )
} {
// The raw stream can neither be encrypted nor connected
OSL_ENSURE( !bRawStream || !bToBeCompressed && !bToBeEncrypted, "The stream is already encrypted!\n" );
xSeek->seek ( bRawStream ? rInfo.pStream->GetMagicalHackPos() : 0 );
ImplSetStoredData ( *pTempEntry, xStream );
Reference < XSeekable > xSeek ( xStream, UNO_QUERY ); // TODO/LATER: Get rid of hacks related to switching of Flag Method and Size properties!
try }
{ else if ( bToBeEncrypted )
if ( xSeek.is() )
{ {
// If the stream is a raw one, then we should be positioned // this is the correct original size
// at the beginning of the actual data pTempEntry->nSize = static_cast < sal_Int32 > ( xSeek->getLength() );
if ( !bToBeCompressed || bRawStream ) nOwnStreamOrigSize = pTempEntry->nSize;
{ }
// The raw stream can neither be encrypted nor connected
OSL_ENSURE( !bRawStream || !bToBeCompressed && !bToBeEncrypted, "The stream is already encrypted!\n" );
xSeek->seek ( bRawStream ? pStream->GetMagicalHackPos() : 0 );
ImplSetStoredData ( *pTempEntry, xStream );
// TODO/LATER: Get rid of hacks related to switching of Flag Method and Size properties! xSeek->seek ( 0 );
} }
else if ( bToBeEncrypted ) else
{
// Okay, we don't have an xSeekable stream. This is possibly bad.
// check if it's one of our own streams, if it is then we know that
// each time we ask for it we'll get a new stream that will be
// at position zero...otherwise, assert and skip this stream...
if ( rInfo.pStream->IsPackageMember() )
{
// if the password has been changed than the stream should not be package member any more
if ( rInfo.pStream->IsEncrypted() && rInfo.pStream->IsToBeEncrypted() )
{ {
// this is the correct original size // Should be handled close to the raw stream handling
pTempEntry->nSize = static_cast < sal_Int32 > ( xSeek->getLength() ); bTransportOwnEncrStreamAsRaw = sal_True;
nOwnStreamOrigSize = pTempEntry->nSize; pTempEntry->nMethod = STORED;
// TODO/LATER: get rid of this situation
// this size should be different from the one that will be stored in manifest.xml
// it is used in storing algorithms and after storing the correct size will be set
pTempEntry->nSize = pTempEntry->nCompressedSize;
} }
xSeek->seek ( 0 );
} }
else else
{ {
// Okay, we don't have an xSeekable stream. This is possibly bad. VOS_ENSURE( 0, "The package component requires that every stream either be FROM a package or it must support XSeekable!" );
// check if it's one of our own streams, if it is then we know that bSuccess = false;
// each time we ask for it we'll get a new stream that will be return bSuccess;
// at position zero...otherwise, assert and skip this stream...
if ( pStream->IsPackageMember() )
{
// if the password has been changed than the stream should not be package member any more
if ( pStream->IsEncrypted() && pStream->IsToBeEncrypted() )
{
// Should be handled close to the raw stream handling
bTransportOwnEncrStreamAsRaw = sal_True;
pTempEntry->nMethod = STORED;
// TODO/LATER: get rid of this situation
// this size should be different from the one that will be stored in manifest.xml
// it is used in storing algorithms and after storing the correct size will be set
pTempEntry->nSize = pTempEntry->nCompressedSize;
}
}
else
{
VOS_ENSURE( 0, "The package component requires that every stream either be FROM a package or it must support XSeekable!" );
continue;
}
} }
} }
catch ( Exception& ) }
{ catch ( Exception& )
VOS_ENSURE( 0, "The stream provided to the package component has problems!" ); {
bWritingFailed = sal_True; VOS_ENSURE( 0, "The stream provided to the package component has problems!" );
continue; bSuccess = false;
} return bSuccess;
}
if ( bToBeEncrypted || bRawStream || bTransportOwnEncrStreamAsRaw ) if ( bToBeEncrypted || bRawStream || bTransportOwnEncrStreamAsRaw )
{
if ( bToBeEncrypted && !bTransportOwnEncrStreamAsRaw )
{ {
if ( bToBeEncrypted && !bTransportOwnEncrStreamAsRaw ) Sequence < sal_uInt8 > aSalt ( 16 ), aVector ( 8 );
{ rtl_random_getBytes ( rRandomPool, aSalt.getArray(), 16 );
Sequence < sal_uInt8 > aSalt ( 16 ), aVector ( 8 ); rtl_random_getBytes ( rRandomPool, aVector.getArray(), 8 );
rtl_random_getBytes ( rRandomPool, aSalt.getArray(), 16 ); sal_Int32 nIterationCount = 1024;
rtl_random_getBytes ( rRandomPool, aVector.getArray(), 8 );
sal_Int32 nIterationCount = 1024;
if ( !pStream->HasOwnKey() ) if ( !rInfo.pStream->HasOwnKey() )
pStream->setKey ( rEncryptionKey ); rInfo.pStream->setKey ( rEncryptionKey );
pStream->setInitialisationVector ( aVector ); rInfo.pStream->setInitialisationVector ( aVector );
pStream->setSalt ( aSalt ); rInfo.pStream->setSalt ( aSalt );
pStream->setIterationCount ( nIterationCount ); rInfo.pStream->setIterationCount ( nIterationCount );
} }
// last property is digest, which is inserted later if we didn't have // last property is digest, which is inserted later if we didn't have
// a magic header // a magic header
aPropSet.realloc(PKG_SIZE_ENCR_MNFST); aPropSet.realloc(PKG_SIZE_ENCR_MNFST);
aPropSet[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty; aPropSet[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty;
aPropSet[PKG_MNFST_INIVECTOR].Value <<= pStream->getInitialisationVector(); aPropSet[PKG_MNFST_INIVECTOR].Value <<= rInfo.pStream->getInitialisationVector();
aPropSet[PKG_MNFST_SALT].Name = sSaltProperty; aPropSet[PKG_MNFST_SALT].Name = sSaltProperty;
aPropSet[PKG_MNFST_SALT].Value <<= pStream->getSalt(); aPropSet[PKG_MNFST_SALT].Value <<= rInfo.pStream->getSalt();
aPropSet[PKG_MNFST_ITERATION].Name = sIterationCountProperty; aPropSet[PKG_MNFST_ITERATION].Name = sIterationCountProperty;
aPropSet[PKG_MNFST_ITERATION].Value <<= pStream->getIterationCount (); aPropSet[PKG_MNFST_ITERATION].Value <<= rInfo.pStream->getIterationCount ();
// Need to store the uncompressed size in the manifest // Need to store the uncompressed size in the manifest
OSL_ENSURE( nOwnStreamOrigSize >= 0, "The stream size was not correctly initialized!\n" ); OSL_ENSURE( nOwnStreamOrigSize >= 0, "The stream size was not correctly initialized!\n" );
aPropSet[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty; aPropSet[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty;
aPropSet[PKG_MNFST_UCOMPSIZE].Value <<= nOwnStreamOrigSize; aPropSet[PKG_MNFST_UCOMPSIZE].Value <<= nOwnStreamOrigSize;
if ( bRawStream || bTransportOwnEncrStreamAsRaw ) if ( bRawStream || bTransportOwnEncrStreamAsRaw )
{ {
aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty; aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty;
aPropSet[PKG_MNFST_DIGEST].Value <<= pStream->getDigest(); aPropSet[PKG_MNFST_DIGEST].Value <<= rInfo.pStream->getDigest();
}
} }
} }
}
// If the entry is already stored in the zip file in the format we // If the entry is already stored in the zip file in the format we
// want for this write...copy it raw // want for this write...copy it raw
if ( !bUseNonSeekableAccess if ( !bUseNonSeekableAccess
&& ( bRawStream || bTransportOwnEncrStreamAsRaw && ( bRawStream || bTransportOwnEncrStreamAsRaw
|| ( pStream->IsPackageMember() && !bToBeEncrypted || ( rInfo.pStream->IsPackageMember() && !bToBeEncrypted
&& ( ( pStream->aEntry.nMethod == DEFLATED && bToBeCompressed ) && ( ( rInfo.pStream->aEntry.nMethod == DEFLATED && bToBeCompressed )
|| ( pStream->aEntry.nMethod == STORED && !bToBeCompressed ) ) ) ) ) || ( rInfo.pStream->aEntry.nMethod == STORED && !bToBeCompressed ) ) ) ) )
{
// If it's a PackageMember, then it's an unbuffered stream and we need
// to get a new version of it as we can't seek backwards.
if ( rInfo.pStream->IsPackageMember() )
{ {
// If it's a PackageMember, then it's an unbuffered stream and we need xStream = rInfo.pStream->getRawData();
// to get a new version of it as we can't seek backwards. if ( !xStream.is() )
if ( pStream->IsPackageMember() )
{ {
xStream = pStream->getRawData(); // Make sure that we actually _got_ a new one !
if ( !xStream.is() ) VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
{ bSuccess = false;
// Make sure that we actually _got_ a new one ! return bSuccess;
VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
continue;
}
} }
}
try try
{ {
if ( bRawStream ) if ( bRawStream )
xStream->skipBytes( pStream->GetMagicalHackPos() ); xStream->skipBytes( rInfo.pStream->GetMagicalHackPos() );
rZipOut.putNextEntry ( *pTempEntry, pStream->getEncryptionData(), sal_False );
// the entry is provided to the ZipOutputStream that will delete it
pAutoTempEntry.release();
Sequence < sal_Int8 > aSeq ( n_ConstBufferSize ); rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream->getEncryptionData(), sal_False );
sal_Int32 nLength; // the entry is provided to the ZipOutputStream that will delete it
pAutoTempEntry.release();
do Sequence < sal_Int8 > aSeq ( n_ConstBufferSize );
{ sal_Int32 nLength;
nLength = xStream->readBytes( aSeq, n_ConstBufferSize );
rZipOut.rawWrite(aSeq, 0, nLength);
}
while ( nLength == n_ConstBufferSize );
rZipOut.rawCloseEntry(); do
}
catch ( ZipException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bWritingFailed = sal_True;
}
catch ( IOException& )
{ {
VOS_ENSURE( 0, "Error writing ZipOutputStream" ); nLength = xStream->readBytes( aSeq, n_ConstBufferSize );
bWritingFailed = sal_True; rZipOut.rawWrite(aSeq, 0, nLength);
} }
while ( nLength == n_ConstBufferSize );
rZipOut.rawCloseEntry();
} }
else catch ( ZipException& )
{ {
// This stream is defenitly not a raw stream VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bSuccess = false;
}
catch ( IOException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bSuccess = false;
}
}
else
{
// This stream is defenitly not a raw stream
// If nonseekable access is used the stream should be at the beginning and // If nonseekable access is used the stream should be at the beginning and
// is useless after the storing. Thus if the storing fails the package should // is useless after the storing. Thus if the storing fails the package should
// be thrown away ( as actually it is done currently )! // be thrown away ( as actually it is done currently )!
// To allow to reuse the package after the error, the optimization must be removed! // To allow to reuse the package after the error, the optimization must be removed!
// If it's a PackageMember, then our previous reference held a 'raw' stream // If it's a PackageMember, then our previous reference held a 'raw' stream
// so we need to re-get it, unencrypted, uncompressed and positioned at the // so we need to re-get it, unencrypted, uncompressed and positioned at the
// beginning of the stream // beginning of the stream
if ( pStream->IsPackageMember() ) if ( rInfo.pStream->IsPackageMember() )
{
xStream = rInfo.pStream->getInputStream();
if ( !xStream.is() )
{ {
xStream = pStream->getInputStream(); // Make sure that we actually _got_ a new one !
if ( !xStream.is() ) VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
{ bSuccess = false;
// Make sure that we actually _got_ a new one ! return bSuccess;
VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
continue;
}
} }
}
if ( bToBeCompressed )
{
pTempEntry->nMethod = DEFLATED;
pTempEntry->nCrc = pTempEntry->nCompressedSize = pTempEntry->nSize = -1;
}
try
{
rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream->getEncryptionData(), bToBeEncrypted);
// the entry is provided to the ZipOutputStream that will delete it
pAutoTempEntry.release();
if ( bToBeCompressed ) sal_Int32 nLength;
Sequence < sal_Int8 > aSeq (n_ConstBufferSize);
do
{ {
pTempEntry->nMethod = DEFLATED; nLength = xStream->readBytes(aSeq, n_ConstBufferSize);
pTempEntry->nCrc = pTempEntry->nCompressedSize = pTempEntry->nSize = -1; rZipOut.write(aSeq, 0, nLength);
} }
while ( nLength == n_ConstBufferSize );
try rZipOut.closeEntry();
{ }
rZipOut.putNextEntry ( *pTempEntry, pStream->getEncryptionData(), bToBeEncrypted); catch ( ZipException& )
// the entry is provided to the ZipOutputStream that will delete it {
pAutoTempEntry.release(); VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bSuccess = false;
}
catch ( IOException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bSuccess = false;
}
sal_Int32 nLength; if ( bToBeEncrypted )
Sequence < sal_Int8 > aSeq (n_ConstBufferSize); {
do aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty;
{ aPropSet[PKG_MNFST_DIGEST].Value <<= rInfo.pStream->getDigest();
nLength = xStream->readBytes(aSeq, n_ConstBufferSize); rInfo.pStream->SetIsEncrypted ( sal_True );
rZipOut.write(aSeq, 0, nLength); }
} }
while ( nLength == n_ConstBufferSize );
rZipOut.closeEntry(); if( bSuccess )
} {
catch ( ZipException& ) if ( !rInfo.pStream->IsPackageMember() )
{ {
VOS_ENSURE( 0, "Error writing ZipOutputStream" ); rInfo.pStream->CloseOwnStreamIfAny();
bWritingFailed = sal_True; rInfo.pStream->SetPackageMember ( sal_True );
} }
catch ( IOException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bWritingFailed = sal_True;
}
if ( bToBeEncrypted ) if ( bRawStream )
{ {
aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty; // the raw stream was integrated and now behaves
aPropSet[PKG_MNFST_DIGEST].Value <<= pStream->getDigest(); // as usual encrypted stream
pStream->SetIsEncrypted ( sal_True ); rInfo.pStream->SetToBeEncrypted( sal_True );
}
} }
if( !bWritingFailed ) // Remove hacky bit from entry flags
if ( pTempEntry->nFlag & ( 1 << 4 ) )
{ {
if ( !pStream->IsPackageMember() ) pTempEntry->nFlag &= ~( 1 << 4 );
{ pTempEntry->nMethod = STORED;
pStream->CloseOwnStreamIfAny(); }
pStream->SetPackageMember ( sal_True );
}
if ( bRawStream ) // Then copy it back afterwards...
{ ZipPackageFolder::copyZipEntry ( rInfo.pStream->aEntry, *pTempEntry );
// the raw stream was integrated and now behaves
// as usual encrypted stream
pStream->SetToBeEncrypted( sal_True );
}
// Remove hacky bit from entry flags // TODO/LATER: get rid of this hack ( the encrypted stream size property is changed during saving )
if ( pTempEntry->nFlag & ( 1 << 4 ) ) if ( rInfo.pStream->IsEncrypted() )
{ rInfo.pStream->setSize( nOwnStreamOrigSize );
pTempEntry->nFlag &= ~( 1 << 4 );
pTempEntry->nMethod = STORED;
}
// Then copy it back afterwards... rInfo.pStream->aEntry.nOffset *= -1;
ZipPackageFolder::copyZipEntry ( pStream->aEntry, *pTempEntry ); }
}
// TODO/LATER: get rid of this hack ( the encrypted stream size property is changed during saving ) // folder can have a mediatype only in package format
if ( pStream->IsEncrypted() ) if ( aPropSet.getLength()
pStream->setSize( nOwnStreamOrigSize ); && ( m_nFormat == embed::StorageFormats::PACKAGE || ( m_nFormat == embed::StorageFormats::OFOPXML && !rInfo.bFolder ) ) )
rManList.push_back( aPropSet );
pStream->aEntry.nOffset *= -1; return bSuccess;
} }
void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool &rRandomPool)
throw(RuntimeException)
{
bool bWritingFailed = false;
if ( maContents.begin() == maContents.end() && rPath.getLength() && m_nFormat != embed::StorageFormats::OFOPXML )
{
// it is an empty subfolder, use workaround to store it
ZipEntry* pTempEntry = new ZipEntry();
ZipPackageFolder::copyZipEntry ( *pTempEntry, aEntry );
pTempEntry->nPathLen = (sal_Int16)( ::rtl::OUStringToOString( rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
pTempEntry->nExtraLen = -1;
pTempEntry->sPath = rPath;
try
{
vos::ORef < EncryptionData > aEmptyEncr;
rZipOut.putNextEntry ( *pTempEntry, aEmptyEncr, sal_False );
rZipOut.rawCloseEntry();
}
catch ( ZipException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bWritingFailed = true;
}
catch ( IOException& )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
bWritingFailed = true;
}
}
bool bMimeTypeStreamStored = false;
::rtl::OUString aMimeTypeStreamName( RTL_CONSTASCII_USTRINGPARAM( "mimetype" ) );
if ( m_nFormat == embed::StorageFormats::ZIP && !rPath.getLength() )
{
// let the "mimtype" stream in root folder be stored as the first stream if it is zip format
ContentHash::iterator aIter = maContents.find ( aMimeTypeStreamName );
if ( aIter != maContents.end() && !(*aIter).second->bFolder )
{
bMimeTypeStreamStored = true;
bWritingFailed = !saveChild( (*aIter).first, *(*aIter).second, rPath, rManList, rZipOut, rEncryptionKey, rRandomPool );
} }
}
for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
aCI != aEnd;
aCI++)
{
const OUString &rShortName = (*aCI).first;
const ContentInfo &rInfo = *(*aCI).second;
// folder can have a mediatype only in package format if ( !bMimeTypeStreamStored || !rShortName.equals( aMimeTypeStreamName ) )
if ( aPropSet.getLength() bWritingFailed = !saveChild( rShortName, rInfo, rPath, rManList, rZipOut, rEncryptionKey, rRandomPool );
&& ( m_nFormat == embed::StorageFormats::PACKAGE || ( m_nFormat == embed::StorageFormats::OFOPXML && !rInfo.bFolder ) ) )
rManList.push_back( aPropSet );
} }
if( bWritingFailed ) if( bWritingFailed )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment