1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <ZipOutputStream.hxx>
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 #include <osl/diagnose.h>
31 #include <PackageConstants.hxx>
32 #include <ZipEntry.hxx>
33 #include <ZipOutputEntry.hxx>
34 #include <ZipPackageStream.hxx>
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
;
41 /** This class is used to write Zip files
43 ZipOutputStream::ZipOutputStream( const uno::Reference
< io::XOutputStream
> &xOStream
)
45 , m_aChucker(xOStream
)
46 , m_pCurrentEntry(NULL
)
47 , m_rSharedThreadPool(comphelper::ThreadPool::getSharedOptimalPool())
51 ZipOutputStream::~ZipOutputStream()
55 void ZipOutputStream::setEntry( ZipEntry
*pEntry
)
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 ||
66 pEntry
->nSize
= pEntry
->nCompressedSize
= 0;
71 void ZipOutputStream::addDeflatingThread( ZipOutputEntry
*pEntry
, comphelper::ThreadTask
*pThread
)
73 m_rSharedThreadPool
.pushTask(pThread
);
74 m_aEntries
.push_back(pEntry
);
77 void ZipOutputStream::rawWrite( const Sequence
< sal_Int8
>& rBuffer
)
78 throw(IOException
, RuntimeException
)
80 m_aChucker
.WriteBytes( rBuffer
);
83 void ZipOutputStream::rawCloseEntry( bool bEncrypt
)
84 throw(IOException
, RuntimeException
)
86 assert(m_pCurrentEntry
&& "Forgot to call writeLOC()?");
87 if ( m_pCurrentEntry
->nMethod
== DEFLATED
&& ( m_pCurrentEntry
->nFlag
& 8 ) )
88 writeEXT(*m_pCurrentEntry
);
91 m_pCurrentEntry
->nMethod
= STORED
;
93 m_pCurrentEntry
= NULL
;
96 void ZipOutputStream::finish()
97 throw(IOException
, RuntimeException
)
99 assert(!m_aZipList
.empty() && "Zip file must have at least one entry!");
101 // Wait for all threads to finish & write
102 m_rSharedThreadPool
.waitUntilEmpty();
103 for (size_t i
= 0; i
< m_aEntries
.size(); i
++)
105 //Any exceptions thrown in the threads were caught and stored for now
106 ::css::uno::Any
aCaughtException(m_aEntries
[i
]->getParallelDeflateException());
107 if (aCaughtException
.hasValue())
108 ::cppu::throwException(aCaughtException
);
110 writeLOC(m_aEntries
[i
]->getZipEntry(), m_aEntries
[i
]->isEncrypt());
113 uno::Sequence
< sal_Int8
> aSequence(n_ConstBufferSize
);
114 uno::Reference
< io::XInputStream
> xInput
= m_aEntries
[i
]->getData();
117 nRead
= xInput
->readBytes(aSequence
, n_ConstBufferSize
);
118 if (nRead
< n_ConstBufferSize
)
119 aSequence
.realloc(nRead
);
123 while (nRead
== n_ConstBufferSize
);
126 rawCloseEntry(m_aEntries
[i
]->isEncrypt());
128 m_aEntries
[i
]->getZipPackageStream()->successfullyWritten(m_aEntries
[i
]->getZipEntry());
129 m_aEntries
[i
]->deleteBufferFile();
130 delete m_aEntries
[i
];
133 sal_Int32 nOffset
= static_cast < sal_Int32
> (m_aChucker
.GetPosition());
134 for (size_t i
= 0; i
< m_aZipList
.size(); i
++)
136 writeCEN( *m_aZipList
[i
] );
137 delete m_aZipList
[i
];
139 writeEND( nOffset
, static_cast < sal_Int32
> (m_aChucker
.GetPosition()) - nOffset
);
144 css::uno::Reference
< css::io::XOutputStream
> ZipOutputStream::getStream()
149 void ZipOutputStream::writeEND(sal_uInt32 nOffset
, sal_uInt32 nLength
)
150 throw(IOException
, RuntimeException
)
152 m_aChucker
.WriteInt32( ENDSIG
);
153 m_aChucker
.WriteInt16( 0 );
154 m_aChucker
.WriteInt16( 0 );
155 m_aChucker
.WriteInt16( m_aZipList
.size() );
156 m_aChucker
.WriteInt16( m_aZipList
.size() );
157 m_aChucker
.WriteUInt32( nLength
);
158 m_aChucker
.WriteUInt32( nOffset
);
159 m_aChucker
.WriteInt16( 0 );
162 static sal_uInt32
getTruncated( sal_Int64 nNum
, bool *pIsTruncated
)
164 if( nNum
>= 0xffffffff )
166 *pIsTruncated
= true;
170 return static_cast< sal_uInt32
>( nNum
);
173 void ZipOutputStream::writeCEN( const ZipEntry
&rEntry
)
174 throw(IOException
, RuntimeException
)
176 if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry
.sPath
, true ) )
177 throw IOException("Unexpected character is used in file name." );
179 OString sUTF8Name
= OUStringToOString( rEntry
.sPath
, RTL_TEXTENCODING_UTF8
);
180 sal_Int16 nNameLength
= static_cast < sal_Int16
> ( sUTF8Name
.getLength() );
182 m_aChucker
.WriteInt32( CENSIG
);
183 m_aChucker
.WriteInt16( rEntry
.nVersion
);
184 m_aChucker
.WriteInt16( rEntry
.nVersion
);
185 m_aChucker
.WriteInt16( rEntry
.nFlag
);
186 m_aChucker
.WriteInt16( rEntry
.nMethod
);
187 bool bWrite64Header
= false;
189 m_aChucker
.WriteUInt32( rEntry
.nTime
);
190 m_aChucker
.WriteUInt32( rEntry
.nCrc
);
191 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nCompressedSize
, &bWrite64Header
) );
192 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nSize
, &bWrite64Header
) );
193 m_aChucker
.WriteInt16( nNameLength
);
194 m_aChucker
.WriteInt16( 0 );
195 m_aChucker
.WriteInt16( 0 );
196 m_aChucker
.WriteInt16( 0 );
197 m_aChucker
.WriteInt16( 0 );
198 m_aChucker
.WriteInt32( 0 );
199 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nOffset
, &bWrite64Header
) );
203 // FIXME64: need to append a ZIP64 header instead of throwing
204 // We're about to silently lose people's data - which they are
205 // unlikely to appreciate so fail instead:
206 throw IOException( "File contains streams that are too large." );
209 Sequence
< sal_Int8
> aSequence( reinterpret_cast<sal_Int8
const *>(sUTF8Name
.getStr()), sUTF8Name
.getLength() );
210 m_aChucker
.WriteBytes( aSequence
);
213 void ZipOutputStream::writeEXT( const ZipEntry
&rEntry
)
214 throw(IOException
, RuntimeException
)
216 bool bWrite64Header
= false;
218 m_aChucker
.WriteInt32( EXTSIG
);
219 m_aChucker
.WriteUInt32( rEntry
.nCrc
);
220 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nCompressedSize
, &bWrite64Header
) );
221 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nSize
, &bWrite64Header
) );
225 // FIXME64: need to append a ZIP64 header instead of throwing
226 // We're about to silently lose people's data - which they are
227 // unlikely to appreciate so fail instead:
228 throw IOException( "File contains streams that are too large." );
232 void ZipOutputStream::writeLOC( ZipEntry
*pEntry
, bool bEncrypt
)
233 throw(IOException
, RuntimeException
)
235 assert(!m_pCurrentEntry
&& "Forgot to close an entry with rawCloseEntry()?");
236 m_pCurrentEntry
= pEntry
;
237 m_aZipList
.push_back( m_pCurrentEntry
);
238 const ZipEntry
&rEntry
= *m_pCurrentEntry
;
240 if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( rEntry
.sPath
, true ) )
241 throw IOException("Unexpected character is used in file name." );
243 OString sUTF8Name
= OUStringToOString( rEntry
.sPath
, RTL_TEXTENCODING_UTF8
);
244 sal_Int16 nNameLength
= static_cast < sal_Int16
> ( sUTF8Name
.getLength() );
246 m_aChucker
.WriteInt32( LOCSIG
);
247 m_aChucker
.WriteInt16( rEntry
.nVersion
);
249 m_aChucker
.WriteInt16( rEntry
.nFlag
);
250 // If it's an encrypted entry, we pretend its stored plain text
252 m_aChucker
.WriteInt16( STORED
);
254 m_aChucker
.WriteInt16( rEntry
.nMethod
);
256 bool bWrite64Header
= false;
258 m_aChucker
.WriteUInt32( rEntry
.nTime
);
259 if ((rEntry
.nFlag
& 8) == 8 )
261 m_aChucker
.WriteInt32( 0 );
262 m_aChucker
.WriteInt32( 0 );
263 m_aChucker
.WriteInt32( 0 );
267 m_aChucker
.WriteUInt32( rEntry
.nCrc
);
268 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nCompressedSize
, &bWrite64Header
) );
269 m_aChucker
.WriteUInt32( getTruncated( rEntry
.nSize
, &bWrite64Header
) );
271 m_aChucker
.WriteInt16( nNameLength
);
272 m_aChucker
.WriteInt16( 0 );
276 // FIXME64: need to append a ZIP64 header instead of throwing
277 // We're about to silently lose people's data - which they are
278 // unlikely to appreciate so fail instead:
279 throw IOException( "File contains streams that are too large." );
282 Sequence
< sal_Int8
> aSequence( reinterpret_cast<sal_Int8
const *>(sUTF8Name
.getStr()), sUTF8Name
.getLength() );
283 m_aChucker
.WriteBytes( aSequence
);
285 m_pCurrentEntry
->nOffset
= m_aChucker
.GetPosition() - (LOCHDR
+ nNameLength
);
288 sal_uInt32
ZipOutputStream::getCurrentDosTime()
290 oslDateTime aDateTime
;
291 TimeValue aTimeValue
;
292 osl_getSystemTime ( &aTimeValue
);
293 osl_getDateTimeFromTimeValue( &aTimeValue
, &aDateTime
);
295 // at year 2108, there is an overflow
296 // -> some decision needs to be made
297 // how to handle the ZIP file format (just overflow?)
299 // if the current system time is before 1980,
300 // then the time traveller will have to make a decision
301 // how to handle the ZIP file format before it is invented
304 assert(aDateTime
.Year
> 1980 && aDateTime
.Year
< 2108);
306 sal_uInt32 nYear
= static_cast <sal_uInt32
> (aDateTime
.Year
);
314 sal_uInt32 nResult
= static_cast < sal_uInt32
>( ( ( ( aDateTime
.Day
) +
315 ( 32 * (aDateTime
.Month
)) +
316 ( 512 * nYear
) ) << 16) |
317 ( ( aDateTime
.Seconds
/2) +
318 ( 32 * aDateTime
.Minutes
) +
319 ( 2048 * static_cast <sal_uInt32
> (aDateTime
.Hours
) ) ) );
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */