/src/libreoffice/package/source/zipapi/ZipOutputEntry.cxx
Line | Count | Source |
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 <ZipOutputEntry.hxx> |
21 | | |
22 | | #include <com/sun/star/io/TempFile.hpp> |
23 | | #include <com/sun/star/packages/zip/ZipConstants.hpp> |
24 | | #include <com/sun/star/xml/crypto/CipherID.hpp> |
25 | | |
26 | | #include <osl/diagnose.h> |
27 | | |
28 | | #include <PackageConstants.hxx> |
29 | | #include <ThreadedDeflater.hxx> |
30 | | #include <ZipEntry.hxx> |
31 | | #include <ZipFile.hxx> |
32 | | #include <ZipPackageStream.hxx> |
33 | | |
34 | | #include <algorithm> |
35 | | #include <utility> |
36 | | |
37 | | using namespace com::sun::star; |
38 | | using namespace com::sun::star::io; |
39 | | using namespace com::sun::star::uno; |
40 | | using namespace com::sun::star::packages::zip::ZipConstants; |
41 | | |
42 | | /** This class is used to deflate Zip entries |
43 | | */ |
44 | | ZipOutputEntryBase::ZipOutputEntryBase( |
45 | | css::uno::Reference< css::io::XOutputStream > xOutput, |
46 | | uno::Reference< uno::XComponentContext > xContext, |
47 | | ZipEntry* pEntry, |
48 | | ZipPackageStream* pStream, |
49 | | bool bEncrypt, |
50 | | bool checkStream) |
51 | 0 | : m_xContext(std::move(xContext)) |
52 | 0 | , m_xOutStream(std::move(xOutput)) |
53 | 0 | , m_pCurrentEntry(pEntry) |
54 | 0 | , m_nDigested(0) |
55 | 0 | , m_pCurrentStream(pStream) |
56 | 0 | , m_bEncryptCurrentEntry(bEncrypt) |
57 | 0 | { |
58 | 0 | assert(pEntry); |
59 | 0 | assert(m_pCurrentEntry->nMethod == DEFLATED && "Use ZipPackageStream::rawWrite() for STORED entries"); |
60 | 0 | (void)checkStream; |
61 | 0 | assert(!checkStream || m_xOutStream.is()); |
62 | 0 | if (m_bEncryptCurrentEntry) |
63 | 0 | { |
64 | 0 | m_xCipherContext = ZipFile::StaticGetCipher( m_xContext, pStream->GetEncryptionData(), true ); |
65 | 0 | if (pStream->GetEncryptionData()->m_oCheckAlg) |
66 | 0 | { |
67 | 0 | assert(pStream->GetEncryptionData()->m_nEncAlg != xml::crypto::CipherID::AES_GCM_W3C); |
68 | 0 | m_xDigestContext = ZipFile::StaticGetDigestContextForChecksum(m_xContext, pStream->GetEncryptionData()); |
69 | 0 | } |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | void ZipOutputEntryBase::closeEntry() |
74 | 0 | { |
75 | 0 | finishDeflater(); |
76 | |
|
77 | 0 | if ((m_pCurrentEntry->nFlag & 8) == 0) |
78 | 0 | { |
79 | 0 | if (m_pCurrentEntry->nSize != getDeflaterTotalIn()) |
80 | 0 | { |
81 | 0 | OSL_FAIL("Invalid entry size"); |
82 | 0 | } |
83 | 0 | if (m_pCurrentEntry->nCompressedSize != getDeflaterTotalOut()) |
84 | 0 | { |
85 | | // Different compression strategies make the merit of this |
86 | | // test somewhat dubious |
87 | 0 | m_pCurrentEntry->nCompressedSize = getDeflaterTotalOut(); |
88 | 0 | } |
89 | 0 | if (m_pCurrentEntry->nCrc != m_aCRC.getValue()) |
90 | 0 | { |
91 | 0 | OSL_FAIL("Invalid entry CRC-32"); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | else |
95 | 0 | { |
96 | 0 | if ( !m_bEncryptCurrentEntry ) |
97 | 0 | { |
98 | 0 | m_pCurrentEntry->nSize = getDeflaterTotalIn(); |
99 | 0 | m_pCurrentEntry->nCompressedSize = getDeflaterTotalOut(); |
100 | 0 | } |
101 | 0 | m_pCurrentEntry->nCrc = m_aCRC.getValue(); |
102 | 0 | } |
103 | 0 | deflaterReset(); |
104 | 0 | m_aCRC.reset(); |
105 | |
|
106 | 0 | if (!m_bEncryptCurrentEntry) |
107 | 0 | return; |
108 | | |
109 | 0 | m_xCipherContext.clear(); |
110 | |
|
111 | 0 | uno::Sequence< sal_Int8 > aDigestSeq; |
112 | 0 | if ( m_xDigestContext.is() ) |
113 | 0 | { |
114 | 0 | aDigestSeq = m_xDigestContext->finalizeDigestAndDispose(); |
115 | 0 | m_xDigestContext.clear(); |
116 | 0 | } |
117 | |
|
118 | 0 | if ( m_pCurrentStream ) |
119 | 0 | m_pCurrentStream->setDigest( aDigestSeq ); |
120 | 0 | } |
121 | | |
122 | | void ZipOutputEntryBase::processDeflated( const uno::Sequence< sal_Int8 >& deflateBuffer, sal_Int32 nLength ) |
123 | 0 | { |
124 | 0 | if ( nLength > 0 ) |
125 | 0 | { |
126 | 0 | uno::Sequence< sal_Int8 > aTmpBuffer( deflateBuffer.getConstArray(), nLength ); |
127 | 0 | if (m_bEncryptCurrentEntry && m_xCipherContext.is()) |
128 | 0 | { |
129 | | // Need to update our digest before encryption... |
130 | 0 | sal_Int32 nDiff = n_ConstDigestLength - m_nDigested; |
131 | 0 | if (m_xDigestContext.is() && nDiff) |
132 | 0 | { |
133 | 0 | sal_Int32 nEat = ::std::min( nLength, nDiff ); |
134 | 0 | uno::Sequence< sal_Int8 > aTmpSeq( aTmpBuffer.getConstArray(), nEat ); |
135 | 0 | m_xDigestContext->updateDigest( aTmpSeq ); |
136 | 0 | m_nDigested = m_nDigested + static_cast< sal_Int16 >( nEat ); |
137 | 0 | } |
138 | | |
139 | | // FIXME64: uno::Sequence not 64bit safe. |
140 | 0 | uno::Sequence< sal_Int8 > aEncryptionBuffer = m_xCipherContext->convertWithCipherContext( aTmpBuffer ); |
141 | |
|
142 | 0 | m_xOutStream->writeBytes( aEncryptionBuffer ); |
143 | | |
144 | | // the sizes as well as checksum for encrypted streams is calculated here |
145 | 0 | m_pCurrentEntry->nCompressedSize += aEncryptionBuffer.getLength(); |
146 | 0 | m_pCurrentEntry->nSize = m_pCurrentEntry->nCompressedSize; |
147 | 0 | m_aCRC.update( aEncryptionBuffer ); |
148 | 0 | } |
149 | 0 | else |
150 | 0 | { |
151 | 0 | m_xOutStream->writeBytes ( aTmpBuffer ); |
152 | 0 | } |
153 | 0 | } |
154 | |
|
155 | 0 | if (!(isDeflaterFinished() && m_bEncryptCurrentEntry && m_xCipherContext.is())) |
156 | 0 | return; |
157 | | |
158 | | // FIXME64: sequence not 64bit safe. |
159 | 0 | uno::Sequence< sal_Int8 > aEncryptionBuffer = m_xCipherContext->finalizeCipherContextAndDispose(); |
160 | 0 | if ( aEncryptionBuffer.hasElements() ) |
161 | 0 | { |
162 | 0 | m_xOutStream->writeBytes( aEncryptionBuffer ); |
163 | | |
164 | | // the sizes as well as checksum for encrypted streams are calculated here |
165 | 0 | m_pCurrentEntry->nCompressedSize += aEncryptionBuffer.getLength(); |
166 | 0 | m_pCurrentEntry->nSize = m_pCurrentEntry->nCompressedSize; |
167 | 0 | m_aCRC.update( aEncryptionBuffer ); |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | | void ZipOutputEntryBase::processInput( const uno::Sequence< sal_Int8 >& rBuffer ) |
172 | 0 | { |
173 | 0 | if (!m_bEncryptCurrentEntry) |
174 | 0 | m_aCRC.updateSegment(rBuffer, rBuffer.getLength()); |
175 | 0 | } |
176 | | |
177 | | ZipOutputEntry::ZipOutputEntry( |
178 | | const css::uno::Reference< css::io::XOutputStream >& rxOutput, |
179 | | const uno::Reference< uno::XComponentContext >& rxContext, |
180 | | ZipEntry* pEntry, |
181 | | ZipPackageStream* pStream, |
182 | | bool bEncrypt, |
183 | | bool checkStream) |
184 | 0 | : ZipOutputEntryBase(rxOutput, rxContext, pEntry, pStream, bEncrypt, checkStream) |
185 | 0 | , m_aDeflateBuffer(n_ConstBufferSize) |
186 | 0 | , m_aDeflater(DEFAULT_COMPRESSION, true) |
187 | 0 | { |
188 | 0 | } |
189 | | |
190 | | ZipOutputEntry::ZipOutputEntry( |
191 | | const css::uno::Reference< css::io::XOutputStream >& rxOutput, |
192 | | const uno::Reference< uno::XComponentContext >& rxContext, |
193 | | ZipEntry* pEntry, |
194 | | ZipPackageStream* pStream, |
195 | | bool bEncrypt) |
196 | 0 | : ZipOutputEntry( rxOutput, rxContext, pEntry, pStream, bEncrypt, true) |
197 | 0 | { |
198 | 0 | } |
199 | | |
200 | | void ZipOutputEntry::write( const Sequence< sal_Int8 >& rBuffer ) |
201 | 0 | { |
202 | 0 | if (!m_aDeflater.finished()) |
203 | 0 | { |
204 | 0 | m_aDeflater.setInputSegment(rBuffer); |
205 | 0 | while (!m_aDeflater.needsInput()) |
206 | 0 | doDeflate(); |
207 | 0 | processInput(rBuffer); |
208 | 0 | } |
209 | 0 | } |
210 | | |
211 | | void ZipOutputEntry::doDeflate() |
212 | 0 | { |
213 | 0 | sal_Int32 nLength = m_aDeflater.doDeflateSegment(m_aDeflateBuffer, m_aDeflateBuffer.getLength()); |
214 | 0 | processDeflated( m_aDeflateBuffer, nLength ); |
215 | 0 | } |
216 | | |
217 | | void ZipOutputEntry::finishDeflater() |
218 | 0 | { |
219 | 0 | m_aDeflater.finish(); |
220 | 0 | while (!m_aDeflater.finished()) |
221 | 0 | doDeflate(); |
222 | 0 | } |
223 | | |
224 | | sal_Int64 ZipOutputEntry::getDeflaterTotalIn() const |
225 | 0 | { |
226 | 0 | return m_aDeflater.getTotalIn(); |
227 | 0 | } |
228 | | |
229 | | sal_Int64 ZipOutputEntry::getDeflaterTotalOut() const |
230 | 0 | { |
231 | 0 | return m_aDeflater.getTotalOut(); |
232 | 0 | } |
233 | | |
234 | | void ZipOutputEntry::deflaterReset() |
235 | 0 | { |
236 | 0 | m_aDeflater.reset(); |
237 | 0 | } |
238 | | |
239 | | bool ZipOutputEntry::isDeflaterFinished() const |
240 | 0 | { |
241 | 0 | return m_aDeflater.finished(); |
242 | 0 | } |
243 | | |
244 | | |
245 | | ZipOutputEntryInThread::ZipOutputEntryInThread( |
246 | | const uno::Reference< uno::XComponentContext >& rxContext, |
247 | | std::unique_ptr<ZipEntry>&& pEntry, |
248 | | ZipPackageStream* pStream, |
249 | | bool bEncrypt) |
250 | 0 | : ZipOutputEntry( uno::Reference< css::io::XOutputStream >(), rxContext, pEntry.get(), pStream, bEncrypt, false ) |
251 | 0 | , m_pOwnedZipEntry(std::move(pEntry)) |
252 | 0 | , m_bFinished(false) |
253 | 0 | { |
254 | 0 | } |
255 | | |
256 | | void ZipOutputEntryInThread::createBufferFile() |
257 | 0 | { |
258 | 0 | assert(!m_xOutStream && !m_xTempFile && |
259 | 0 | "should only be called in the threaded mode where there is no existing stream yet"); |
260 | 0 | m_xTempFile = new utl::TempFileFastService; |
261 | 0 | m_xOutStream = m_xTempFile->getOutputStream(); |
262 | 0 | } |
263 | | |
264 | | void ZipOutputEntryInThread::closeBufferFile() |
265 | 0 | { |
266 | 0 | m_xOutStream->closeOutput(); |
267 | 0 | m_xOutStream.clear(); |
268 | 0 | } |
269 | | |
270 | | void ZipOutputEntryInThread::deleteBufferFile() |
271 | 0 | { |
272 | 0 | assert(!m_xOutStream.is() && m_xTempFile); |
273 | 0 | m_xTempFile.clear(); |
274 | 0 | } |
275 | | |
276 | | uno::Reference< io::XInputStream > ZipOutputEntryInThread::getData() const |
277 | 0 | { |
278 | 0 | return m_xTempFile->getInputStream(); |
279 | 0 | } |
280 | | |
281 | | class ZipOutputEntryInThread::Task : public comphelper::ThreadTask |
282 | | { |
283 | | ZipOutputEntryInThread *mpEntry; |
284 | | uno::Reference< io::XInputStream > mxInStream; |
285 | | |
286 | | public: |
287 | | Task( const std::shared_ptr<comphelper::ThreadTaskTag>& pTag, ZipOutputEntryInThread *pEntry, |
288 | | uno::Reference< io::XInputStream > xInStream ) |
289 | 0 | : comphelper::ThreadTask(pTag) |
290 | 0 | , mpEntry(pEntry) |
291 | 0 | , mxInStream(std::move(xInStream)) |
292 | 0 | {} |
293 | | |
294 | | private: |
295 | | virtual void doWork() override |
296 | 0 | { |
297 | 0 | try |
298 | 0 | { |
299 | 0 | mpEntry->createBufferFile(); |
300 | 0 | mpEntry->writeStream(mxInStream); |
301 | 0 | mxInStream.clear(); |
302 | 0 | mpEntry->closeBufferFile(); |
303 | 0 | mpEntry->setFinished(); |
304 | 0 | } |
305 | 0 | catch (...) |
306 | 0 | { |
307 | 0 | mpEntry->setParallelDeflateException(std::current_exception()); |
308 | 0 | try |
309 | 0 | { |
310 | 0 | if (mpEntry->m_xOutStream.is()) |
311 | 0 | mpEntry->closeBufferFile(); |
312 | 0 | if (mpEntry->m_xTempFile) |
313 | 0 | mpEntry->deleteBufferFile(); |
314 | 0 | } |
315 | 0 | catch (uno::Exception const&) |
316 | 0 | { |
317 | 0 | } |
318 | 0 | mpEntry->setFinished(); |
319 | 0 | } |
320 | 0 | } |
321 | | }; |
322 | | |
323 | | std::unique_ptr<comphelper::ThreadTask> ZipOutputEntryInThread::createTask( |
324 | | const std::shared_ptr<comphelper::ThreadTaskTag>& pTag, |
325 | | const uno::Reference< io::XInputStream >& xInStream ) |
326 | 0 | { |
327 | 0 | return std::make_unique<Task>(pTag, this, xInStream); |
328 | 0 | } |
329 | | |
330 | | void ZipOutputEntry::writeStream(const uno::Reference< io::XInputStream >& xInStream) |
331 | 0 | { |
332 | 0 | sal_Int32 nLength = 0; |
333 | 0 | uno::Sequence< sal_Int8 > aSeq(n_ConstBufferSize); |
334 | 0 | do |
335 | 0 | { |
336 | 0 | nLength = xInStream->readBytes(aSeq, n_ConstBufferSize); |
337 | 0 | if (nLength != n_ConstBufferSize) |
338 | 0 | aSeq.realloc(nLength); |
339 | |
|
340 | 0 | write(aSeq); |
341 | 0 | } |
342 | 0 | while (nLength == n_ConstBufferSize); |
343 | 0 | closeEntry(); |
344 | 0 | } |
345 | | |
346 | | |
347 | | ZipOutputEntryParallel::ZipOutputEntryParallel( |
348 | | const css::uno::Reference< css::io::XOutputStream >& rxOutput, |
349 | | const uno::Reference< uno::XComponentContext >& rxContext, |
350 | | ZipEntry* pEntry, |
351 | | ZipPackageStream* pStream, |
352 | | bool bEncrypt) |
353 | 0 | : ZipOutputEntryBase(rxOutput, rxContext, pEntry, pStream, bEncrypt, true) |
354 | 0 | , totalIn(0) |
355 | 0 | , totalOut(0) |
356 | 0 | , finished(false) |
357 | 0 | { |
358 | 0 | } |
359 | | |
360 | | void ZipOutputEntryParallel::writeStream(const uno::Reference< io::XInputStream >& xInStream) |
361 | 0 | { |
362 | 0 | ZipUtils::ThreadedDeflater deflater( DEFAULT_COMPRESSION ); |
363 | 0 | deflater.deflateWrite(xInStream, |
364 | 0 | [this](const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nLen) { |
365 | 0 | if (!m_bEncryptCurrentEntry) |
366 | 0 | m_aCRC.updateSegment(rBuffer, nLen); |
367 | 0 | }, |
368 | 0 | [this](const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nLen) { |
369 | 0 | processDeflated(rBuffer, nLen); |
370 | 0 | } |
371 | 0 | ); |
372 | 0 | finished = true; |
373 | 0 | processDeflated( uno::Sequence< sal_Int8 >(), 0 ); // finish encrypting, etc. |
374 | 0 | totalIn = deflater.getTotalIn(); |
375 | 0 | totalOut = deflater.getTotalOut(); |
376 | 0 | closeEntry(); |
377 | 0 | } |
378 | | |
379 | | void ZipOutputEntryParallel::finishDeflater() |
380 | 0 | { |
381 | | // ThreadedDeflater is called synchronously in one call, so nothing to do here. |
382 | 0 | } |
383 | | |
384 | | sal_Int64 ZipOutputEntryParallel::getDeflaterTotalIn() const |
385 | 0 | { |
386 | 0 | return totalIn; |
387 | 0 | } |
388 | | |
389 | | sal_Int64 ZipOutputEntryParallel::getDeflaterTotalOut() const |
390 | 0 | { |
391 | 0 | return totalOut; |
392 | 0 | } |
393 | | |
394 | | void ZipOutputEntryParallel::deflaterReset() |
395 | 0 | { |
396 | 0 | totalIn = 0; |
397 | 0 | totalOut = 0; |
398 | 0 | finished = false; |
399 | 0 | } |
400 | | |
401 | | bool ZipOutputEntryParallel::isDeflaterFinished() const |
402 | 0 | { |
403 | 0 | return finished; |
404 | 0 | } |
405 | | |
406 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |