1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <ZipOutputStream.hxx>
21 
22 #include <com/sun/star/packages/zip/ZipConstants.hpp>
23 #include <com/sun/star/io/XInputStream.hpp>
24 #include <com/sun/star/io/XOutputStream.hpp>
25 #include <comphelper/storagehelper.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
27 
28 #include <osl/time.h>
29 #include <osl/thread.hxx>
30 
31 #include <PackageConstants.hxx>
32 #include <ZipEntry.hxx>
33 #include <ZipOutputEntry.hxx>
34 #include <ZipPackageStream.hxx>
35 
36 using namespace com::sun::star;
37 using namespace com::sun::star::io;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::packages::zip::ZipConstants;
40 
41 /** This class is used to write Zip files
42  */
ZipOutputStream(const uno::Reference<io::XOutputStream> & xOStream)43 ZipOutputStream::ZipOutputStream( const uno::Reference < io::XOutputStream > &xOStream )
44 : m_xStream(xOStream)
45 , mpThreadTaskTag( comphelper::ThreadPool::createThreadTaskTag() )
46 , m_aChucker(xOStream)
47 , m_pCurrentEntry(nullptr)
48 {
49 }
50 
~ZipOutputStream()51 ZipOutputStream::~ZipOutputStream()
52 {
53 }
54 
setEntry(ZipEntry * pEntry)55 void ZipOutputStream::setEntry( ZipEntry *pEntry )
56 {
57     if (pEntry->nTime == -1)
58         pEntry->nTime = getCurrentDosTime();
59     if (pEntry->nMethod == -1)
60         pEntry->nMethod = DEFLATED;
61     pEntry->nVersion = 20;
62     pEntry->nFlag = 1 << 11;
63     if (pEntry->nSize == -1 || pEntry->nCompressedSize == -1 ||
64         pEntry->nCrc == -1)
65     {
66         pEntry->nSize = pEntry->nCompressedSize = 0;
67         pEntry->nFlag |= 8;
68     }
69 }
70 
addDeflatingThreadTask(ZipOutputEntryInThread * pEntry,std::unique_ptr<comphelper::ThreadTask> pTask)71 void ZipOutputStream::addDeflatingThreadTask( ZipOutputEntryInThread *pEntry, std::unique_ptr<comphelper::ThreadTask> pTask )
72 {
73     comphelper::ThreadPool::getSharedOptimalPool().pushTask(std::move(pTask));
74     m_aEntries.push_back(pEntry);
75 }
76 
rawWrite(const Sequence<sal_Int8> & rBuffer)77 void ZipOutputStream::rawWrite( const Sequence< sal_Int8 >& rBuffer )
78 {
79     m_aChucker.WriteBytes( rBuffer );
80 }
81 
rawCloseEntry(bool bEncrypt)82 void ZipOutputStream::rawCloseEntry( bool bEncrypt )
83 {
84     assert(m_pCurrentEntry && "Forgot to call writeLOC()?");
85     if ( m_pCurrentEntry->nMethod == DEFLATED && ( m_pCurrentEntry->nFlag & 8 ) )
86         writeEXT(*m_pCurrentEntry);
87 
88     if (bEncrypt)
89         m_pCurrentEntry->nMethod = STORED;
90 
91     m_pCurrentEntry = nullptr;
92 }
93 
consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread> pCandidate)94 void ZipOutputStream::consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread> pCandidate)
95 {
96     //Any exceptions thrown in the threads were caught and stored for now
97     const std::exception_ptr& rCaughtException(pCandidate->getParallelDeflateException());
98     if (rCaughtException)
99     {
100         m_aDeflateException = rCaughtException; // store it for later throwing
101         // the exception handler in DeflateThreadTask should have cleaned temp file
102         return;
103     }
104 
105     writeLOC(pCandidate->getZipEntry(), pCandidate->isEncrypt());
106 
107     sal_Int32 nRead;
108     uno::Sequence< sal_Int8 > aSequence(n_ConstBufferSize);
109     uno::Reference< io::XInputStream > xInput = pCandidate->getData();
110     do
111     {
112         nRead = xInput->readBytes(aSequence, n_ConstBufferSize);
113         if (nRead < n_ConstBufferSize)
114             aSequence.realloc(nRead);
115 
116         rawWrite(aSequence);
117     }
118     while (nRead == n_ConstBufferSize);
119     xInput.clear();
120 
121     rawCloseEntry(pCandidate->isEncrypt());
122 
123     pCandidate->getZipPackageStream()->successfullyWritten(pCandidate->getZipEntry());
124     pCandidate->deleteBufferFile();
125 }
126 
consumeFinishedScheduledThreadTaskEntries()127 void ZipOutputStream::consumeFinishedScheduledThreadTaskEntries()
128 {
129     std::vector< ZipOutputEntryInThread* > aNonFinishedEntries;
130 
131     for(ZipOutputEntryInThread* pEntry : m_aEntries)
132     {
133         if(pEntry->isFinished())
134         {
135             consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread>(pEntry));
136         }
137         else
138         {
139             aNonFinishedEntries.push_back(pEntry);
140         }
141     }
142 
143     // always reset to non-consumed entries
144     m_aEntries = aNonFinishedEntries;
145 }
146 
reduceScheduledThreadTasksToGivenNumberOrLess(sal_Int32 nThreadTasks)147 void ZipOutputStream::reduceScheduledThreadTasksToGivenNumberOrLess(sal_Int32 nThreadTasks)
148 {
149     while(static_cast< sal_Int32 >(m_aEntries.size()) > nThreadTasks)
150     {
151         consumeFinishedScheduledThreadTaskEntries();
152 
153         if(static_cast< sal_Int32 >(m_aEntries.size()) > nThreadTasks)
154         {
155             osl::Thread::wait(std::chrono::microseconds(100));
156         }
157     }
158 }
159 
finish()160 void ZipOutputStream::finish()
161 {
162     assert(!m_aZipList.empty() && "Zip file must have at least one entry!");
163 
164     // Wait for all thread tasks to finish & write
165     comphelper::ThreadPool::getSharedOptimalPool().waitUntilDone(mpThreadTaskTag);
166 
167     // consume all processed entries
168     while(!m_aEntries.empty())
169     {
170         ZipOutputEntryInThread* pCandidate = m_aEntries.back();
171         m_aEntries.pop_back();
172         consumeScheduledThreadTaskEntry(std::unique_ptr<ZipOutputEntryInThread>(pCandidate));
173     }
174 
175     sal_Int32 nOffset= static_cast < sal_Int32 > (m_aChucker.GetPosition());
176     for (ZipEntry* p : m_aZipList)
177     {
178         writeCEN( *p );
179         delete p;
180     }
181     writeEND( nOffset, static_cast < sal_Int32 > (m_aChucker.GetPosition()) - nOffset);
182     m_xStream->flush();
183     m_aZipList.clear();
184 
185     if (m_aDeflateException)
186     {   // throw once all thread tasks are finished and m_aEntries can be released
187         std::rethrow_exception(m_aDeflateException);
188     }
189 }
190 
getStream() const191 const css::uno::Reference< css::io::XOutputStream >& ZipOutputStream::getStream() const
192 {
193     return m_xStream;
194 }
195 
writeEND(sal_uInt32 nOffset,sal_uInt32 nLength)196 void ZipOutputStream::writeEND(sal_uInt32 nOffset, sal_uInt32 nLength)
197 {
198     m_aChucker.WriteInt32( ENDSIG );
199     m_aChucker.WriteInt16( 0 );
200     m_aChucker.WriteInt16( 0 );
201     m_aChucker.WriteInt16( m_aZipList.size() );
202     m_aChucker.WriteInt16( m_aZipList.size() );
203     m_aChucker.WriteUInt32( nLength );
204     m_aChucker.WriteUInt32( nOffset );
205     m_aChucker.WriteInt16( 0 );
206 }
207 
getTruncated(sal_Int64 nNum,bool * pIsTruncated)208 static sal_uInt32 getTruncated( sal_Int64 nNum, bool *pIsTruncated )
209 {
210     if( nNum >= 0xffffffff )
211     {
212         *pIsTruncated = true;
213         return 0xffffffff;
214     }
215     else
216         return static_cast< sal_uInt32 >( nNum );
217 }
218 
writeCEN(const ZipEntry & rEntry)219 void ZipOutputStream::writeCEN( const ZipEntry &rEntry )
220 {
221     if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, true ) )
222         throw IOException("Unexpected character is used in file name." );
223 
224     OString sUTF8Name = OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 );
225     sal_Int16 nNameLength       = static_cast < sal_Int16 > ( sUTF8Name.getLength() );
226 
227     m_aChucker.WriteInt32( CENSIG );
228     m_aChucker.WriteInt16( rEntry.nVersion );
229     m_aChucker.WriteInt16( rEntry.nVersion );
230     m_aChucker.WriteInt16( rEntry.nFlag );
231     m_aChucker.WriteInt16( rEntry.nMethod );
232     bool bWrite64Header = false;
233 
234     m_aChucker.WriteUInt32( rEntry.nTime );
235     m_aChucker.WriteUInt32( rEntry.nCrc );
236     m_aChucker.WriteUInt32( getTruncated( rEntry.nCompressedSize, &bWrite64Header ) );
237     m_aChucker.WriteUInt32( getTruncated( rEntry.nSize, &bWrite64Header ) );
238     m_aChucker.WriteInt16( nNameLength );
239     m_aChucker.WriteInt16( 0 );
240     m_aChucker.WriteInt16( 0 );
241     m_aChucker.WriteInt16( 0 );
242     m_aChucker.WriteInt16( 0 );
243     m_aChucker.WriteInt32( 0 );
244     m_aChucker.WriteUInt32( getTruncated( rEntry.nOffset, &bWrite64Header ) );
245 
246     if( bWrite64Header )
247     {
248         // FIXME64: need to append a ZIP64 header instead of throwing
249         // We're about to silently lose people's data - which they are
250         // unlikely to appreciate so fail instead:
251         throw IOException( "File contains streams that are too large." );
252     }
253 
254     Sequence < sal_Int8 > aSequence( reinterpret_cast<sal_Int8 const *>(sUTF8Name.getStr()), sUTF8Name.getLength() );
255     m_aChucker.WriteBytes( aSequence );
256 }
257 
writeEXT(const ZipEntry & rEntry)258 void ZipOutputStream::writeEXT( const ZipEntry &rEntry )
259 {
260     bool bWrite64Header = false;
261 
262     m_aChucker.WriteInt32( EXTSIG );
263     m_aChucker.WriteUInt32( rEntry.nCrc );
264     m_aChucker.WriteUInt32( getTruncated( rEntry.nCompressedSize, &bWrite64Header ) );
265     m_aChucker.WriteUInt32( getTruncated( rEntry.nSize, &bWrite64Header ) );
266 
267     if( bWrite64Header )
268     {
269         // FIXME64: need to append a ZIP64 header instead of throwing
270         // We're about to silently lose people's data - which they are
271         // unlikely to appreciate so fail instead:
272         throw IOException( "File contains streams that are too large." );
273     }
274 }
275 
writeLOC(ZipEntry * pEntry,bool bEncrypt)276 void ZipOutputStream::writeLOC( ZipEntry *pEntry, bool bEncrypt )
277 {
278     assert(!m_pCurrentEntry && "Forgot to close an entry with rawCloseEntry()?");
279     m_pCurrentEntry = pEntry;
280     m_aZipList.push_back( m_pCurrentEntry );
281     const ZipEntry &rEntry = *m_pCurrentEntry;
282 
283     if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry.sPath, true ) )
284         throw IOException("Unexpected character is used in file name." );
285 
286     OString sUTF8Name = OUStringToOString( rEntry.sPath, RTL_TEXTENCODING_UTF8 );
287     sal_Int16 nNameLength       = static_cast < sal_Int16 > ( sUTF8Name.getLength() );
288 
289     m_aChucker.WriteInt32( LOCSIG );
290     m_aChucker.WriteInt16( rEntry.nVersion );
291 
292     m_aChucker.WriteInt16( rEntry.nFlag );
293     // If it's an encrypted entry, we pretend its stored plain text
294     if (bEncrypt)
295         m_aChucker.WriteInt16( STORED );
296     else
297         m_aChucker.WriteInt16( rEntry.nMethod );
298 
299     bool bWrite64Header = false;
300 
301     m_aChucker.WriteUInt32( rEntry.nTime );
302     if ((rEntry.nFlag & 8) == 8 )
303     {
304         m_aChucker.WriteInt32( 0 );
305         m_aChucker.WriteInt32( 0 );
306         m_aChucker.WriteInt32( 0 );
307     }
308     else
309     {
310         m_aChucker.WriteUInt32( rEntry.nCrc );
311         m_aChucker.WriteUInt32( getTruncated( rEntry.nCompressedSize, &bWrite64Header ) );
312         m_aChucker.WriteUInt32( getTruncated( rEntry.nSize, &bWrite64Header ) );
313     }
314     m_aChucker.WriteInt16( nNameLength );
315     m_aChucker.WriteInt16( 0 );
316 
317     if( bWrite64Header )
318     {
319         // FIXME64: need to append a ZIP64 header instead of throwing
320         // We're about to silently lose people's data - which they are
321         // unlikely to appreciate so fail instead:
322         throw IOException( "File contains streams that are too large." );
323     }
324 
325     Sequence < sal_Int8 > aSequence( reinterpret_cast<sal_Int8 const *>(sUTF8Name.getStr()), sUTF8Name.getLength() );
326     m_aChucker.WriteBytes( aSequence );
327 
328     m_pCurrentEntry->nOffset = m_aChucker.GetPosition() - (LOCHDR + nNameLength);
329 }
330 
getCurrentDosTime()331 sal_uInt32 ZipOutputStream::getCurrentDosTime()
332 {
333     oslDateTime aDateTime;
334     TimeValue aTimeValue;
335     osl_getSystemTime ( &aTimeValue );
336     osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime);
337 
338     // at year 2108, there is an overflow
339     // -> some decision needs to be made
340     // how to handle the ZIP file format (just overflow?)
341 
342     // if the current system time is before 1980,
343     // then the time traveller will have to make a decision
344     // how to handle the ZIP file format before it is invented
345     // (just underflow?)
346 
347     assert(aDateTime.Year > 1980 && aDateTime.Year < 2108);
348 
349     sal_uInt32 nYear = static_cast <sal_uInt32> (aDateTime.Year);
350 
351     if (nYear>=1980)
352         nYear-=1980;
353     else if (nYear>=80)
354     {
355         nYear-=80;
356     }
357     sal_uInt32 nResult = static_cast < sal_uInt32>( ( ( ( aDateTime.Day) +
358                                           ( 32 * (aDateTime.Month)) +
359                                           ( 512 * nYear ) ) << 16) |
360                                         ( ( aDateTime.Seconds/2) +
361                                             ( 32 * aDateTime.Minutes) +
362                                           ( 2048 * static_cast <sal_uInt32 > (aDateTime.Hours) ) ) );
363     return nResult;
364 }
365 
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
367