Line data Source code
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 <comphelper/storagehelper.hxx>
25 :
26 : #include <osl/time.h>
27 : #include <osl/diagnose.h>
28 :
29 : #include <PackageConstants.hxx>
30 : #include <ZipEntry.hxx>
31 : #include <ZipFile.hxx>
32 : #include <ZipPackageBuffer.hxx>
33 : #include <ZipPackageStream.hxx>
34 :
35 : #include <algorithm>
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 10717 : ZipOutputEntry::ZipOutputEntry(
45 : const css::uno::Reference< css::io::XOutputStream >& rxOutput,
46 : const uno::Reference< uno::XComponentContext >& rxContext,
47 : ZipEntry& rEntry,
48 : ZipPackageStream* pStream,
49 : bool bEncrypt)
50 : : m_aDeflateBuffer(n_ConstBufferSize)
51 : , m_aDeflater(DEFAULT_COMPRESSION, true)
52 : , m_pCurrentEntry(&rEntry)
53 : , m_nDigested(0)
54 : , m_bEncryptCurrentEntry(bEncrypt)
55 10717 : , m_pCurrentStream(pStream)
56 : {
57 10717 : if (rxOutput.is())
58 : {
59 10673 : m_xOutStream = rxOutput;
60 : }
61 : else
62 : {
63 44 : m_xTempFile = io::TempFile::create(rxContext);
64 44 : m_xOutStream = m_xTempFile->getOutputStream();
65 : }
66 : assert(m_pCurrentEntry->nMethod == DEFLATED && "Use ZipPackageStream::rawWrite() for STORED entries");
67 10717 : if (m_bEncryptCurrentEntry)
68 : {
69 4 : m_xCipherContext = ZipFile::StaticGetCipher( rxContext, pStream->GetEncryptionData(), true );
70 4 : m_xDigestContext = ZipFile::StaticGetDigestContextForChecksum( rxContext, pStream->GetEncryptionData() );
71 : }
72 10717 : }
73 :
74 10717 : ZipOutputEntry::~ZipOutputEntry()
75 : {
76 10717 : }
77 :
78 44 : uno::Reference< io::XInputStream > ZipOutputEntry::getData()
79 : {
80 44 : m_xOutStream->closeOutput();
81 44 : uno::Reference< io::XSeekable > xTempSeek(m_xOutStream, UNO_QUERY_THROW);
82 44 : xTempSeek->seek(0);
83 44 : return m_xTempFile->getInputStream();
84 : }
85 :
86 10717 : void ZipOutputEntry::closeEntry()
87 : {
88 10717 : m_aDeflater.finish();
89 32151 : while (!m_aDeflater.finished())
90 10717 : doDeflate();
91 :
92 10717 : if ((m_pCurrentEntry->nFlag & 8) == 0)
93 : {
94 0 : if (m_pCurrentEntry->nSize != m_aDeflater.getTotalIn())
95 : {
96 : OSL_FAIL("Invalid entry size");
97 : }
98 0 : if (m_pCurrentEntry->nCompressedSize != m_aDeflater.getTotalOut())
99 : {
100 : // Different compression strategies make the merit of this
101 : // test somewhat dubious
102 0 : m_pCurrentEntry->nCompressedSize = m_aDeflater.getTotalOut();
103 : }
104 0 : if (m_pCurrentEntry->nCrc != m_aCRC.getValue())
105 : {
106 : OSL_FAIL("Invalid entry CRC-32");
107 : }
108 : }
109 : else
110 : {
111 10717 : if ( !m_bEncryptCurrentEntry )
112 : {
113 10713 : m_pCurrentEntry->nSize = m_aDeflater.getTotalIn();
114 10713 : m_pCurrentEntry->nCompressedSize = m_aDeflater.getTotalOut();
115 : }
116 10717 : m_pCurrentEntry->nCrc = m_aCRC.getValue();
117 : }
118 10717 : m_aDeflater.reset();
119 10717 : m_aCRC.reset();
120 :
121 10717 : if (m_bEncryptCurrentEntry)
122 : {
123 4 : m_xCipherContext.clear();
124 :
125 4 : uno::Sequence< sal_Int8 > aDigestSeq;
126 4 : if ( m_xDigestContext.is() )
127 : {
128 4 : aDigestSeq = m_xDigestContext->finalizeDigestAndDispose();
129 4 : m_xDigestContext.clear();
130 : }
131 :
132 4 : if ( m_pCurrentStream )
133 4 : m_pCurrentStream->setDigest( aDigestSeq );
134 : }
135 10717 : }
136 :
137 11053 : void ZipOutputEntry::write( const Sequence< sal_Int8 >& rBuffer )
138 : {
139 11053 : if (!m_aDeflater.finished())
140 : {
141 11053 : m_aDeflater.setInputSegment(rBuffer);
142 33085 : while (!m_aDeflater.needsInput())
143 10979 : doDeflate();
144 11053 : if (!m_bEncryptCurrentEntry)
145 11049 : m_aCRC.updateSegment(rBuffer, rBuffer.getLength());
146 : }
147 11053 : }
148 :
149 21696 : void ZipOutputEntry::doDeflate()
150 : {
151 21696 : sal_Int32 nLength = m_aDeflater.doDeflateSegment(m_aDeflateBuffer, 0, m_aDeflateBuffer.getLength());
152 :
153 21696 : if ( nLength > 0 )
154 : {
155 10813 : uno::Sequence< sal_Int8 > aTmpBuffer( m_aDeflateBuffer.getConstArray(), nLength );
156 10813 : if ( m_bEncryptCurrentEntry && m_xDigestContext.is() && m_xCipherContext.is() )
157 : {
158 : // Need to update our digest before encryption...
159 4 : sal_Int32 nDiff = n_ConstDigestLength - m_nDigested;
160 4 : if ( nDiff )
161 : {
162 4 : sal_Int32 nEat = ::std::min( nLength, nDiff );
163 4 : uno::Sequence< sal_Int8 > aTmpSeq( aTmpBuffer.getConstArray(), nEat );
164 4 : m_xDigestContext->updateDigest( aTmpSeq );
165 4 : m_nDigested = m_nDigested + static_cast< sal_Int16 >( nEat );
166 : }
167 :
168 : // FIXME64: uno::Sequence not 64bit safe.
169 4 : uno::Sequence< sal_Int8 > aEncryptionBuffer = m_xCipherContext->convertWithCipherContext( aTmpBuffer );
170 :
171 4 : m_xOutStream->writeBytes( aEncryptionBuffer );
172 :
173 : // the sizes as well as checksum for encrypted streams is calculated here
174 4 : m_pCurrentEntry->nCompressedSize += aEncryptionBuffer.getLength();
175 4 : m_pCurrentEntry->nSize = m_pCurrentEntry->nCompressedSize;
176 4 : m_aCRC.update( aEncryptionBuffer );
177 : }
178 : else
179 : {
180 10809 : m_xOutStream->writeBytes ( aTmpBuffer );
181 10813 : }
182 : }
183 :
184 21696 : if ( m_aDeflater.finished() && m_bEncryptCurrentEntry && m_xDigestContext.is() && m_xCipherContext.is() )
185 : {
186 : // FIXME64: sequence not 64bit safe.
187 4 : uno::Sequence< sal_Int8 > aEncryptionBuffer = m_xCipherContext->finalizeCipherContextAndDispose();
188 4 : if ( aEncryptionBuffer.getLength() )
189 : {
190 4 : m_xOutStream->writeBytes( aEncryptionBuffer );
191 :
192 : // the sizes as well as checksum for encrypted streams is calculated hier
193 4 : m_pCurrentEntry->nCompressedSize += aEncryptionBuffer.getLength();
194 4 : m_pCurrentEntry->nSize = m_pCurrentEntry->nCompressedSize;
195 4 : m_aCRC.update( aEncryptionBuffer );
196 4 : }
197 : }
198 21696 : }
199 :
200 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|