1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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/IOException.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <comphelper/storagehelper.hxx>
#include <osl/time.h>
#include <osl/thread.hxx>
#include <PackageConstants.hxx>
#include <ZipEntry.hxx>
#include <ZipOutputEntry.hxx>
#include <ZipPackageStream.hxx>
#include <thread>
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;
/** This class is used to write Zip files
*/
ZipOutputStream::ZipOutputStream( const uno::Reference < io::XOutputStream > &xOStream )
: m_xStream(xOStream)
, mpThreadTaskTag( comphelper::ThreadPool::createThreadTaskTag() )
, m_aChucker(xOStream)
, m_pCurrentEntry(nullptr)
{
}
ZipOutputStream::~ZipOutputStream()
{
}
void ZipOutputStream::setEntry(ZipEntry& rEntry)
{
if (rEntry.nTime == -1)
rEntry.nTime = getCurrentDosTime();
if (rEntry.nMethod == -1)
rEntry.nMethod = DEFLATED;
rEntry.nVersion = 20;
rEntry.nFlag = 1 << 11;
if (rEntry.nSize == -1 || rEntry.nCompressedSize == -1 ||
rEntry.nCrc == -1)
{
if (rEntry.nSize == -1)
{
assert(false); // how to get here
rEntry.nSize = 0;
}
rEntry.nCompressedSize = 0;
rEntry.nFlag |= 8;
}
}
void ZipOutputStream::addDeflatingThreadTask( ZipOutputEntryInThread *pEntry, std::unique_ptr<comphelper::ThreadTask> pTask )
{
comphelper::ThreadPool::getSharedOptimalPool().pushTask(std::move(pTask));
m_aEntries.push_back(pEntry);
}
void ZipOutputStream::rawWrite( const Sequence< sal_Int8 >& rBuffer )
{
m_aChucker.WriteBytes( rBuffer );
}
void ZipOutputStream::rawCloseEntry( bool bEncrypt )
{
assert(m_pCurrentEntry && "Forgot to call writeLOC()?");
if ( m_pCurrentEntry->nMethod == DEFLATED && ( m_pCurrentEntry->nFlag & 8 ) )
writeDataDescriptor(*m_pCurrentEntry);
if (bEncrypt)
{
m_pCurrentEntry->nMethod = STORED;
assert(m_pCurrentEntry->nSize == m_pCurrentEntry->nCompressedSize);
}
m_pCurrentEntry = nullptr;
}
void ZipOutputStream::consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread> pCandidate)
{
//Any exceptions thrown in the threads were caught and stored for now
const std::exception_ptr& rCaughtException(pCandidate->getParallelDeflateException());
if (rCaughtException)
{
m_aDeflateException = rCaughtException; // store it for later throwing
// the exception handler in DeflateThreadTask should have cleaned temp file
return;
}
writeLOC(pCandidate->moveZipEntry(), 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();
}
void ZipOutputStream::consumeFinishedScheduledThreadTaskEntries()
{
std::vector< ZipOutputEntryInThread* > aNonFinishedEntries;
for(ZipOutputEntryInThread* pEntry : m_aEntries)
{
if(pEntry->isFinished())
{
consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread>(pEntry));
}
else
{
aNonFinishedEntries.push_back(pEntry);
}
}
// always reset to non-consumed entries
m_aEntries = std::move(aNonFinishedEntries);
}
void ZipOutputStream::reduceScheduledThreadTasksToGivenNumberOrLess(std::size_t nThreadTasks)
{
while(m_aEntries.size() > nThreadTasks)
{
consumeFinishedScheduledThreadTaskEntries();
if(m_aEntries.size() > nThreadTasks)
{
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}
}
void ZipOutputStream::finish()
{
assert(!m_aZipList.empty() && "Zip file must have at least one entry!");
// Wait for all thread tasks to finish & write
comphelper::ThreadPool::getSharedOptimalPool().waitUntilDone(mpThreadTaskTag);
// consume all processed entries
while(!m_aEntries.empty())
{
ZipOutputEntryInThread* pCandidate = m_aEntries.back();
m_aEntries.pop_back();
consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread>(pCandidate));
}
sal_Int32 nOffset= static_cast < sal_Int32 > (m_aChucker.GetPosition());
for (auto& p : m_aZipList)
{
writeCEN( *p );
}
writeEND( nOffset, static_cast < sal_Int32 > (m_aChucker.GetPosition()) - nOffset);
m_aZipList.clear();
if (m_aDeflateException)
{ // throw once all thread tasks are finished and m_aEntries can be released
std::rethrow_exception(m_aDeflateException);
}
}
const css::uno::Reference< css::io::XOutputStream >& ZipOutputStream::getStream() const
{
return m_xStream;
}
void ZipOutputStream::writeEND(sal_uInt32 nOffset, sal_uInt32 nLength)
{
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 );
}
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 )
{
if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, true ) )
throw IOException(u"Unexpected character is used in file name."_ustr );
OString sUTF8Name = OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 );
sal_Int16 nNameLength = static_cast < sal_Int16 > ( sUTF8Name.getLength() );
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 ) );
sal_uInt32 nOffset32bit = getTruncated( rEntry.nOffset, &bWrite64Header );
m_aChucker.WriteInt16(nNameLength);
m_aChucker.WriteInt16( bWrite64Header? 32 : 0 ); //in ZIP64 case extra field is 32byte
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt16( 0 );
m_aChucker.WriteInt32( 0 );
m_aChucker.WriteUInt32( nOffset32bit );
Sequence < sal_Int8 > aSequence( reinterpret_cast<sal_Int8 const *>(sUTF8Name.getStr()), sUTF8Name.getLength() );
m_aChucker.WriteBytes( aSequence );
if (bWrite64Header)
{
writeExtraFields(rEntry, false);
}
}
void ZipOutputStream::writeDataDescriptor(const ZipEntry& rEntry)
{
bool bWrite64Header = false;
m_aChucker.WriteInt32( EXTSIG );
m_aChucker.WriteUInt32( rEntry.nCrc );
// For ZIP64(tm) format archives, the compressed and uncompressed sizes are 8 bytes each.
// TODO: Not sure if this is the "when ZIP64(tm) format is used"
bWrite64Header = rEntry.nCompressedSize >= 0x100000000 || rEntry.nSize >= 0x100000000;
if (!bWrite64Header)
{
m_aChucker.WriteUInt32( static_cast<sal_uInt32>(rEntry.nCompressedSize) );
m_aChucker.WriteUInt32( static_cast<sal_uInt32>(rEntry.nSize) );
}
else
{
m_aChucker.WriteUInt64( rEntry.nCompressedSize );
m_aChucker.WriteUInt64( rEntry.nSize );
}
}
void ZipOutputStream::writeExtraFields(const ZipEntry& rEntry, bool const isLOCWithDD)
{
//Could contain more fields, now we only save Zip64 extended information
m_aChucker.WriteInt16( 1 ); //id of Zip64 extended information extra field
m_aChucker.WriteInt16( 28 ); //data size of this field = 3*8+4 byte
m_aChucker.WriteUInt64(isLOCWithDD ? 0 : rEntry.nSize);
m_aChucker.WriteUInt64(isLOCWithDD ? 0 : rEntry.nCompressedSize);
m_aChucker.WriteUInt64( rEntry.nOffset );
m_aChucker.WriteInt32( 0 ); //Number of the disk on which this file starts
}
void ZipOutputStream::writeLOC(std::unique_ptr<ZipEntry>&& pEntry, bool bEncrypt)
{
assert(!m_pCurrentEntry && "Forgot to close an entry with rawCloseEntry()?");
m_aZipList.push_back(std::move(pEntry));
m_pCurrentEntry = m_aZipList.back().get();
const ZipEntry &rEntry = *m_pCurrentEntry;
if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, true ) )
throw IOException(u"Unexpected character is used in file name."_ustr );
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;
// getTruncated must always be called to init bWrite64Header!
auto const nTruncCompressedSize{getTruncated(rEntry.nCompressedSize, &bWrite64Header)};
auto const nTruncSize{getTruncated(rEntry.nSize, &bWrite64Header)};
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(nTruncCompressedSize);
m_aChucker.WriteUInt32(nTruncSize);
}
m_aChucker.WriteInt16( nNameLength );
m_aChucker.WriteInt16( bWrite64Header ? 32 : 0 );
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);
if (bWrite64Header)
{
writeExtraFields(rEntry, (rEntry.nFlag & 8));
}
}
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: */
|