Files
libreoffice/package/source/zipapi/ZipOutputStream.cxx

382 lines
12 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-22 18:32:07 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <ZipOutputStream.hxx>
#include <com/sun/star/packages/zip/ZipConstants.hpp>
#include <com/sun/star/io/XInputStream.hpp>
2011-03-09 17:29:09 +01:00
#include <com/sun/star/io/XOutputStream.hpp>
#include <comphelper/storagehelper.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <osl/diagnose.h>
2011-03-09 17:29:09 +01:00
#include <osl/time.h>
#include <PackageConstants.hxx>
#include <ZipEntry.hxx>
#include <ZipOutputEntry.hxx>
#include <ZipPackageStream.hxx>
2001-11-15 19:23:28 +00:00
2011-03-09 17:29:09 +01:00
using namespace com::sun::star;
using namespace com::sun::star::io;
using namespace com::sun::star::uno;
using namespace com::sun::star::packages::zip::ZipConstants;
2000-11-13 12:38:03 +00:00
/** This class is used to write Zip files
*/
ZipOutputStream::ZipOutputStream( const uno::Reference < io::XOutputStream > &xOStream )
: m_xStream(xOStream)
, m_aChucker(xOStream)
, m_pCurrentEntry(nullptr)
, m_rSharedThreadPool(comphelper::ThreadPool::getSharedOptimalPool())
2000-11-13 12:38:03 +00:00
{
}
ZipOutputStream::~ZipOutputStream()
2000-11-13 12:38:03 +00:00
{
}
void ZipOutputStream::setEntry( ZipEntry *pEntry )
{
if (pEntry->nTime == -1)
pEntry->nTime = getCurrentDosTime();
if (pEntry->nMethod == -1)
pEntry->nMethod = DEFLATED;
pEntry->nVersion = 20;
pEntry->nFlag = 1 << 11;
if (pEntry->nSize == -1 || pEntry->nCompressedSize == -1 ||
pEntry->nCrc == -1)
{
pEntry->nSize = pEntry->nCompressedSize = 0;
pEntry->nFlag |= 8;
}
}
void ZipOutputStream::addDeflatingThread( ZipOutputEntry *pEntry, comphelper::ThreadTask *pThread )
{
m_rSharedThreadPool.pushTask(pThread);
m_aEntries.push_back(pEntry);
}
void ZipOutputStream::rawWrite( const Sequence< sal_Int8 >& rBuffer )
throw(IOException, RuntimeException)
{
m_aChucker.WriteBytes( rBuffer );
}
void ZipOutputStream::rawCloseEntry( bool bEncrypt )
throw(IOException, RuntimeException)
{
assert(m_pCurrentEntry && "Forgot to call writeLOC()?");
if ( m_pCurrentEntry->nMethod == DEFLATED && ( m_pCurrentEntry->nFlag & 8 ) )
writeEXT(*m_pCurrentEntry);
if (bEncrypt)
m_pCurrentEntry->nMethod = STORED;
m_pCurrentEntry = nullptr;
}
void ZipOutputStream::consumeScheduledThreadEntry(ZipOutputEntry* pCandidate)
2000-11-13 12:38:03 +00:00
{
//Any exceptions thrown in the threads were caught and stored for now
::css::uno::Any aCaughtException(pCandidate->getParallelDeflateException());
if (aCaughtException.hasValue())
{
m_aDeflateException = aCaughtException; // store it for later throwing
// the exception handler in DeflateThread should have cleaned temp file
delete pCandidate;
return;
}
writeLOC(pCandidate->getZipEntry(), pCandidate->isEncrypt());
sal_Int32 nRead;
uno::Sequence< sal_Int8 > aSequence(n_ConstBufferSize);
uno::Reference< io::XInputStream > xInput = pCandidate->getData();
do
{
nRead = xInput->readBytes(aSequence, n_ConstBufferSize);
if (nRead < n_ConstBufferSize)
aSequence.realloc(nRead);
rawWrite(aSequence);
}
while (nRead == n_ConstBufferSize);
xInput.clear();
rawCloseEntry(pCandidate->isEncrypt());
pCandidate->getZipPackageStream()->successfullyWritten(pCandidate->getZipEntry());
pCandidate->deleteBufferFile();
delete pCandidate;
}
void ZipOutputStream::consumeFinishedScheduledThreadEntries()
{
std::vector< ZipOutputEntry* > aNonFinishedEntries;
for(auto aIter = m_aEntries.begin(); aIter != m_aEntries.end(); ++aIter)
{
if((*aIter)->isFinished())
{
consumeScheduledThreadEntry(*aIter);
}
else
{
aNonFinishedEntries.push_back(*aIter);
}
}
// always reset to non-consumed entries
m_aEntries = aNonFinishedEntries;
}
void ZipOutputStream::consumeAllScheduledThreadEntries()
{
while(!m_aEntries.empty())
{
ZipOutputEntry* pCandidate = m_aEntries.back();
m_aEntries.pop_back();
consumeScheduledThreadEntry(pCandidate);
}
}
void ZipOutputStream::reduceScheduledThreadsToGivenNumberOrLess(sal_Int32 nThreads, sal_Int32 nWaitTimeInTenthSeconds)
{
while(static_cast< sal_Int32 >(m_aEntries.size()) > nThreads)
{
consumeFinishedScheduledThreadEntries();
if(static_cast< sal_Int32 >(m_aEntries.size()) > nThreads)
{
const TimeValue aTimeValue(0, 100000 * nWaitTimeInTenthSeconds);
osl_waitThread(&aTimeValue);
}
}
}
void ZipOutputStream::finish()
throw(IOException, RuntimeException)
{
assert(!m_aZipList.empty() && "Zip file must have at least one entry!");
// Wait for all threads to finish & write
m_rSharedThreadPool.waitUntilEmpty();
// consume all processed entries
consumeAllScheduledThreadEntries();
if (m_aDeflateException.hasValue())
{ // throw once all threads are finished and m_aEntries can be released
::cppu::throwException(m_aDeflateException);
}
sal_Int32 nOffset= static_cast < sal_Int32 > (m_aChucker.GetPosition());
for (ZipEntry* p : m_aZipList)
{
writeCEN( *p );
delete p;
}
writeEND( nOffset, static_cast < sal_Int32 > (m_aChucker.GetPosition()) - nOffset);
m_xStream->flush();
m_aZipList.clear();
2000-11-13 12:38:03 +00:00
}
const css::uno::Reference< css::io::XOutputStream >& ZipOutputStream::getStream()
{
return m_xStream;
}
2000-11-13 12:38:03 +00:00
void ZipOutputStream::writeEND(sal_uInt32 nOffset, sal_uInt32 nLength)
throw(IOException, RuntimeException)
2000-11-13 12:38:03 +00:00
{
m_aChucker.WriteInt32( ENDSIG );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( m_aZipList.size() );
m_aChucker.WriteInt16( m_aZipList.size() );
m_aChucker.WriteUInt32( nLength );
m_aChucker.WriteUInt32( nOffset );
m_aChucker.WriteInt16( 0 );
2000-11-13 12:38:03 +00:00
}
static sal_uInt32 getTruncated( sal_Int64 nNum, bool *pIsTruncated )
{
if( nNum >= 0xffffffff )
{
*pIsTruncated = true;
return 0xffffffff;
}
else
return static_cast< sal_uInt32 >( nNum );
}
void ZipOutputStream::writeCEN( const ZipEntry &rEntry )
throw(IOException, RuntimeException)
2000-11-13 12:38:03 +00:00
{
if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, true ) )
throw IOException("Unexpected character is used in file name." );
CWS-TOOLING: integrate CWS mav44 2009-01-18 22:39:43 +0100 mav r266467 : #i97073# the soffice file should be a shell script, adjust the plugin 2009-01-18 22:35:15 +0100 mav r266466 : #i97073# the soffice file should be a shell script, integrate fix from hro41 cws 2009-01-16 15:32:10 +0100 mav r266426 : #i96456# fix link resolving 2009-01-16 12:21:39 +0100 mav r266413 : #i97073# the soffice file is no more a shell script 2009-01-16 09:58:05 +0100 mav r266402 : #i95409# add complex test using unicode names 2009-01-15 20:27:03 +0100 mav r266392 : #i95408# fix include list 2009-01-15 20:09:28 +0100 mav r266390 : #i95408# deliver new header 2009-01-15 16:16:33 +0100 mav r266382 : #i95408# rebase to m38 2009-01-15 16:11:16 +0100 mav r266380 : #i95408# rebase to m38 2009-01-15 15:38:16 +0100 mav r266376 : CWS-TOOLING: rebase CWS mav44 to trunk@265758 (milestone: DEV300:m38) 2009-01-14 13:23:55 +0100 mav r266294 : #i97073# let the presentation slideshow be started in window mode in plugin 2009-01-13 17:27:52 +0100 mav r266241 : #i95409#,#i95408# support UTF8 encoding for entry names, check the validity of the entries 2009-01-13 16:35:39 +0100 mav r266236 : #i95408# function to check whether a zip entry name is acceptable 2009-01-13 12:00:18 +0100 mav r266205 : #i94003# a readonly document can not be modified 2009-01-09 13:24:49 +0100 mav r266078 : #i95951# let the title be changed 2009-01-09 11:34:49 +0100 mav r266068 : #i80862# close the link only if was closed by the container, othewise leave it open 2009-01-08 11:30:13 +0100 mav r265989 : #i97071# disable Toolbars during window-based slide show in ActiveX control 2009-01-08 08:23:23 +0100 mav r265983 : #i97071# setVisible does not trigger layout in case there is not MenuBar 2009-01-06 12:39:39 +0100 mav r265908 : #i96185# let the MediaDescriptor get the target URL 2009-01-05 08:46:45 +0100 mav r265856 : #i93473# integrate the patch 2009-01-02 17:45:17 +0100 mav r265845 : #i94468#,#i96456# try to follow links 2009-01-02 16:53:41 +0100 mav r265841 : #i94468#,#i96456# use the same parsing mechanics in sharing control file and document lock file 2009-01-02 16:51:24 +0100 mav r265839 : #i94468#,#i96456# use the same parsing mechanics in sharing control file and document lock file
2009-02-12 10:27:21 +00:00
OString sUTF8Name = OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 );
CWS-TOOLING: integrate CWS mav44 2009-01-18 22:39:43 +0100 mav r266467 : #i97073# the soffice file should be a shell script, adjust the plugin 2009-01-18 22:35:15 +0100 mav r266466 : #i97073# the soffice file should be a shell script, integrate fix from hro41 cws 2009-01-16 15:32:10 +0100 mav r266426 : #i96456# fix link resolving 2009-01-16 12:21:39 +0100 mav r266413 : #i97073# the soffice file is no more a shell script 2009-01-16 09:58:05 +0100 mav r266402 : #i95409# add complex test using unicode names 2009-01-15 20:27:03 +0100 mav r266392 : #i95408# fix include list 2009-01-15 20:09:28 +0100 mav r266390 : #i95408# deliver new header 2009-01-15 16:16:33 +0100 mav r266382 : #i95408# rebase to m38 2009-01-15 16:11:16 +0100 mav r266380 : #i95408# rebase to m38 2009-01-15 15:38:16 +0100 mav r266376 : CWS-TOOLING: rebase CWS mav44 to trunk@265758 (milestone: DEV300:m38) 2009-01-14 13:23:55 +0100 mav r266294 : #i97073# let the presentation slideshow be started in window mode in plugin 2009-01-13 17:27:52 +0100 mav r266241 : #i95409#,#i95408# support UTF8 encoding for entry names, check the validity of the entries 2009-01-13 16:35:39 +0100 mav r266236 : #i95408# function to check whether a zip entry name is acceptable 2009-01-13 12:00:18 +0100 mav r266205 : #i94003# a readonly document can not be modified 2009-01-09 13:24:49 +0100 mav r266078 : #i95951# let the title be changed 2009-01-09 11:34:49 +0100 mav r266068 : #i80862# close the link only if was closed by the container, othewise leave it open 2009-01-08 11:30:13 +0100 mav r265989 : #i97071# disable Toolbars during window-based slide show in ActiveX control 2009-01-08 08:23:23 +0100 mav r265983 : #i97071# setVisible does not trigger layout in case there is not MenuBar 2009-01-06 12:39:39 +0100 mav r265908 : #i96185# let the MediaDescriptor get the target URL 2009-01-05 08:46:45 +0100 mav r265856 : #i93473# integrate the patch 2009-01-02 17:45:17 +0100 mav r265845 : #i94468#,#i96456# try to follow links 2009-01-02 16:53:41 +0100 mav r265841 : #i94468#,#i96456# use the same parsing mechanics in sharing control file and document lock file 2009-01-02 16:51:24 +0100 mav r265839 : #i94468#,#i96456# use the same parsing mechanics in sharing control file and document lock file
2009-02-12 10:27:21 +00:00
sal_Int16 nNameLength = static_cast < sal_Int16 > ( sUTF8Name.getLength() );
2000-11-13 12:38:03 +00:00
m_aChucker.WriteInt32( CENSIG );
m_aChucker.WriteInt16( rEntry.nVersion );
m_aChucker.WriteInt16( rEntry.nVersion );
m_aChucker.WriteInt16( rEntry.nFlag );
m_aChucker.WriteInt16( rEntry.nMethod );
bool bWrite64Header = false;
m_aChucker.WriteUInt32( rEntry.nTime );
m_aChucker.WriteUInt32( rEntry.nCrc );
m_aChucker.WriteUInt32( getTruncated( rEntry.nCompressedSize, &bWrite64Header ) );
m_aChucker.WriteUInt32( getTruncated( rEntry.nSize, &bWrite64Header ) );
m_aChucker.WriteInt16( nNameLength );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt32( 0 );
m_aChucker.WriteUInt32( getTruncated( rEntry.nOffset, &bWrite64Header ) );
if( bWrite64Header )
{
// FIXME64: need to append a ZIP64 header instead of throwing
// We're about to silently lose people's data - which they are
// unlikely to appreciate so fail instead:
throw IOException( "File contains streams that are too large." );
}
Sequence < sal_Int8 > aSequence( reinterpret_cast<sal_Int8 const *>(sUTF8Name.getStr()), sUTF8Name.getLength() );
m_aChucker.WriteBytes( aSequence );
2000-11-13 12:38:03 +00:00
}
void ZipOutputStream::writeEXT( const ZipEntry &rEntry )
throw(IOException, RuntimeException)
{
bool bWrite64Header = false;
m_aChucker.WriteInt32( EXTSIG );
m_aChucker.WriteUInt32( rEntry.nCrc );
m_aChucker.WriteUInt32( getTruncated( rEntry.nCompressedSize, &bWrite64Header ) );
m_aChucker.WriteUInt32( getTruncated( rEntry.nSize, &bWrite64Header ) );
if( bWrite64Header )
{
// FIXME64: need to append a ZIP64 header instead of throwing
// We're about to silently lose people's data - which they are
// unlikely to appreciate so fail instead:
throw IOException( "File contains streams that are too large." );
}
}
void ZipOutputStream::writeLOC( ZipEntry *pEntry, bool bEncrypt )
throw(IOException, RuntimeException)
{
assert(!m_pCurrentEntry && "Forgot to close an entry with rawCloseEntry()?");
m_pCurrentEntry = pEntry;
m_aZipList.push_back( m_pCurrentEntry );
const ZipEntry &rEntry = *m_pCurrentEntry;
if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, true ) )
throw IOException("Unexpected character is used in file name." );
OString sUTF8Name = OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 );
sal_Int16 nNameLength = static_cast < sal_Int16 > ( sUTF8Name.getLength() );
m_aChucker.WriteInt32( LOCSIG );
m_aChucker.WriteInt16( rEntry.nVersion );
m_aChucker.WriteInt16( rEntry.nFlag );
// If it's an encrypted entry, we pretend its stored plain text
if (bEncrypt)
m_aChucker.WriteInt16( STORED );
else
m_aChucker.WriteInt16( rEntry.nMethod );
bool bWrite64Header = false;
m_aChucker.WriteUInt32( rEntry.nTime );
if ((rEntry.nFlag & 8) == 8 )
{
m_aChucker.WriteInt32( 0 );
m_aChucker.WriteInt32( 0 );
m_aChucker.WriteInt32( 0 );
}
else
{
m_aChucker.WriteUInt32( rEntry.nCrc );
m_aChucker.WriteUInt32( getTruncated( rEntry.nCompressedSize, &bWrite64Header ) );
m_aChucker.WriteUInt32( getTruncated( rEntry.nSize, &bWrite64Header ) );
}
m_aChucker.WriteInt16( nNameLength );
m_aChucker.WriteInt16( 0 );
if( bWrite64Header )
{
// FIXME64: need to append a ZIP64 header instead of throwing
// We're about to silently lose people's data - which they are
// unlikely to appreciate so fail instead:
throw IOException( "File contains streams that are too large." );
}
Sequence < sal_Int8 > aSequence( reinterpret_cast<sal_Int8 const *>(sUTF8Name.getStr()), sUTF8Name.getLength() );
m_aChucker.WriteBytes( aSequence );
m_pCurrentEntry->nOffset = m_aChucker.GetPosition() - (LOCHDR + nNameLength);
}
sal_uInt32 ZipOutputStream::getCurrentDosTime()
{
oslDateTime aDateTime;
TimeValue aTimeValue;
osl_getSystemTime ( &aTimeValue );
osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime);
// at year 2108, there is an overflow
// -> some decision needs to be made
// how to handle the ZIP file format (just overflow?)
// if the current system time is before 1980,
// then the time traveller will have to make a decision
// how to handle the ZIP file format before it is invented
// (just underflow?)
assert(aDateTime.Year > 1980 && aDateTime.Year < 2108);
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>( ( ( ( aDateTime.Day) +
( 32 * (aDateTime.Month)) +
( 512 * nYear ) ) << 16) |
( ( aDateTime.Seconds/2) +
( 32 * aDateTime.Minutes) +
( 2048 * static_cast <sal_uInt32 > (aDateTime.Hours) ) ) );
return nResult;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */