1*9e0fc027SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*9e0fc027SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*9e0fc027SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*9e0fc027SAndrew Rist * distributed with this work for additional information
6*9e0fc027SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*9e0fc027SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*9e0fc027SAndrew Rist * "License"); you may not use this file except in compliance
9*9e0fc027SAndrew Rist * with the License. You may obtain a copy of the License at
10*9e0fc027SAndrew Rist *
11*9e0fc027SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*9e0fc027SAndrew Rist *
13*9e0fc027SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*9e0fc027SAndrew Rist * software distributed under the License is distributed on an
15*9e0fc027SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9e0fc027SAndrew Rist * KIND, either express or implied. See the License for the
17*9e0fc027SAndrew Rist * specific language governing permissions and limitations
18*9e0fc027SAndrew Rist * under the License.
19*9e0fc027SAndrew Rist *
20*9e0fc027SAndrew Rist *************************************************************/
21*9e0fc027SAndrew Rist
22*9e0fc027SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_filter.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir /*
28cdf0e10cSrcweir #include <tools/datetime.hxx>
29cdf0e10cSrcweir */
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include <rtl/crc.h>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include "zip.hxx"
34cdf0e10cSrcweir #include "zipfile.hxx"
35cdf0e10cSrcweir
36cdf0e10cSrcweir using namespace rtl;
37cdf0e10cSrcweir
38cdf0e10cSrcweir /** this struct describes one entry in a zip file */
39cdf0e10cSrcweir struct ZipEntry
40cdf0e10cSrcweir {
41cdf0e10cSrcweir OString name; /* the name we used */
42cdf0e10cSrcweir sal_Int32 offset; /* where the header starts */
43cdf0e10cSrcweir sal_Int32 endOffset; /* where the file data ends */
44cdf0e10cSrcweir sal_Int32 crc;
45cdf0e10cSrcweir sal_Int32 modTime; /* dos mod time & date */
46cdf0e10cSrcweir sal_Int32 fileLen; /* file size, in bytes */
47cdf0e10cSrcweir };
48cdf0e10cSrcweir
49cdf0e10cSrcweir /** put one byte inside this stream */
putC(unsigned char c,osl::File & rFile)50cdf0e10cSrcweir static osl::File::RC putC( unsigned char c, osl::File& rFile )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir sal_uInt64 nBytesWritten;
53cdf0e10cSrcweir osl::File::RC nRC = rFile.write( &c, 1, nBytesWritten );
54cdf0e10cSrcweir
55cdf0e10cSrcweir OSL_ASSERT( nBytesWritten == 1 );
56cdf0e10cSrcweir return nRC;
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
59cdf0e10cSrcweir /** write a short to the ZipFile */
writeShort(sal_Int16 s)60cdf0e10cSrcweir void ZipFile::writeShort( sal_Int16 s)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir if( !isError() )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir mnRC = putC( static_cast< unsigned char >( s & 0xff ), mrFile );
65cdf0e10cSrcweir if( !isError() )
66cdf0e10cSrcweir mnRC = putC( static_cast< unsigned char >( (s >> 8) & 0xff ), mrFile );
67cdf0e10cSrcweir }
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
70cdf0e10cSrcweir /** write a long to the ZipFile */
71cdf0e10cSrcweir
writeLong(sal_Int32 l)72cdf0e10cSrcweir void ZipFile::writeLong( sal_Int32 l )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir if( !isError() )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir mnRC = putC( static_cast< unsigned char >( l & 0xff ), mrFile);
77cdf0e10cSrcweir if( !isError() )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir mnRC = putC( static_cast< unsigned char >( (l >> 8) & 0xff ), mrFile);
80cdf0e10cSrcweir if( !isError() )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir mnRC = putC( static_cast< unsigned char >( (l >> 16) & 0xff ), mrFile);
83cdf0e10cSrcweir if( !isError() )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir mnRC = putC( static_cast< unsigned char >( (l >> 24) & 0xff ), mrFile);
86cdf0e10cSrcweir }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir }
89cdf0e10cSrcweir }
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
92cdf0e10cSrcweir /** copy the zipentries file to the zipfile and updates the crc of that zipentry */
copyAndCRC(ZipEntry * e,osl::File & rFile)93cdf0e10cSrcweir void ZipFile::copyAndCRC(ZipEntry *e, osl::File& rFile)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir char buf[2048];
96cdf0e10cSrcweir sal_uInt64 n, nWritten;
97cdf0e10cSrcweir
98cdf0e10cSrcweir e->crc = rtl_crc32( 0, 0L, 0 );
99cdf0e10cSrcweir
100cdf0e10cSrcweir while( !isError() )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir mnRC = rFile.read( buf, sizeof(buf), n );
103cdf0e10cSrcweir if(n == 0)
104cdf0e10cSrcweir break;
105cdf0e10cSrcweir
106cdf0e10cSrcweir if( !isError() )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir sal_uInt32 nTemp = static_cast<sal_uInt32>(n);
109cdf0e10cSrcweir e->crc = rtl_crc32( e->crc, (const void *) buf, nTemp );
110cdf0e10cSrcweir mnRC = mrFile.write( buf, n, nWritten );
111cdf0e10cSrcweir OSL_ASSERT( n == nWritten );
112cdf0e10cSrcweir }
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir if( !isError() )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir sal_uInt64 nPosition = 0;
118cdf0e10cSrcweir mnRC = mrFile.getPos( nPosition );
119cdf0e10cSrcweir if( !isError() )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir e->endOffset = static_cast< sal_Int32 >( nPosition );
122cdf0e10cSrcweir }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir }
125cdf0e10cSrcweir
126cdf0e10cSrcweir /** write a yet empty local header for a zipentry to the zipfile */
writeDummyLocalHeader(ZipEntry * e)127cdf0e10cSrcweir void ZipFile::writeDummyLocalHeader(ZipEntry *e)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir sal_Int32 len = zf_lfhSIZE + e->name.getLength();
130cdf0e10cSrcweir sal_Int32 i;
131cdf0e10cSrcweir
132cdf0e10cSrcweir sal_uInt64 nPosition = 0;
133cdf0e10cSrcweir mnRC = mrFile.getPos( nPosition );
134cdf0e10cSrcweir if( !isError() )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir e->offset = static_cast< sal_Int32 >( nPosition );
137cdf0e10cSrcweir
138cdf0e10cSrcweir for (i = 0; (i < len) && !isError(); ++i)
139cdf0e10cSrcweir mnRC = putC(0, mrFile);
140cdf0e10cSrcweir }
141cdf0e10cSrcweir }
142cdf0e10cSrcweir
143cdf0e10cSrcweir /** write the local header for a zipentry to the zipfile */
writeLocalHeader(ZipEntry * e)144cdf0e10cSrcweir void ZipFile::writeLocalHeader(ZipEntry *e)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir TimeValue aTime;
147cdf0e10cSrcweir osl_getSystemTime( &aTime );
148cdf0e10cSrcweir
149cdf0e10cSrcweir oslDateTime aDate;
150cdf0e10cSrcweir osl_getDateTimeFromTimeValue( &aTime, &aDate );
151cdf0e10cSrcweir
152cdf0e10cSrcweir e->modTime = ((aDate.Year - 1980) << 25) | (aDate.Month << 21) | (aDate.Day << 16) |
153cdf0e10cSrcweir (aDate.Hours << 11) | (aDate.Minutes << 5) | (aDate.Seconds >> 1);
154cdf0e10cSrcweir
155cdf0e10cSrcweir e->fileLen = e->endOffset - e->offset - zf_lfhSIZE - e->name.getLength();
156cdf0e10cSrcweir
157cdf0e10cSrcweir if(!isError())
158cdf0e10cSrcweir {
159cdf0e10cSrcweir mnRC = mrFile.setPos( Pos_Absolut, e->offset );
160cdf0e10cSrcweir
161cdf0e10cSrcweir writeLong(zf_LFHSIGValue); // magic number
162cdf0e10cSrcweir writeShort(zf_Vers(1, 0)); // extract version
163cdf0e10cSrcweir writeShort(0); // flags
164cdf0e10cSrcweir writeShort(zf_compNone); // compression method
165cdf0e10cSrcweir writeLong(e->modTime); // file mod date & time
166cdf0e10cSrcweir writeLong(e->crc); // file crc
167cdf0e10cSrcweir writeLong(e->fileLen); // compressed size
168cdf0e10cSrcweir writeLong(e->fileLen); // uncompressed size
169cdf0e10cSrcweir writeShort((sal_Int16) e->name.getLength()); // name length
170cdf0e10cSrcweir writeShort(0); // extra length field
171cdf0e10cSrcweir
172cdf0e10cSrcweir if( !isError() )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir sal_uInt64 nWritten;
175cdf0e10cSrcweir mnRC = mrFile.write( e->name.getStr(), e->name.getLength(), nWritten ); // file name
176cdf0e10cSrcweir OSL_ASSERT( nWritten == (sal_uInt64)e->name.getLength() );
177cdf0e10cSrcweir if( !isError() )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir mnRC = mrFile.setPos( Pos_Absolut, e->endOffset );
180cdf0e10cSrcweir }
181cdf0e10cSrcweir }
182cdf0e10cSrcweir }
183cdf0e10cSrcweir }
184cdf0e10cSrcweir
185cdf0e10cSrcweir /* write a zipentry in the central dir to the zipfile */
writeCentralDir(ZipEntry * e)186cdf0e10cSrcweir void ZipFile::writeCentralDir(ZipEntry *e)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir writeLong(zf_CDHSIGValue); // magic number
189cdf0e10cSrcweir writeShort(zf_Vers(1, 0)); // version made by
190cdf0e10cSrcweir writeShort(zf_Vers(1, 0)); // vers to extract
191cdf0e10cSrcweir writeShort(0); // flags
192cdf0e10cSrcweir writeShort(zf_compNone); // compression method
193cdf0e10cSrcweir writeLong(e->modTime); // file mod time & date
194cdf0e10cSrcweir writeLong(e->crc);
195cdf0e10cSrcweir writeLong(e->fileLen); // compressed file size
196cdf0e10cSrcweir writeLong(e->fileLen); // uncompressed file size
197cdf0e10cSrcweir writeShort((sal_Int16) e->name.getLength()); // name length
198cdf0e10cSrcweir writeShort(0); // extra field length
199cdf0e10cSrcweir writeShort(0); // file comment length
200cdf0e10cSrcweir writeShort(0); // disk number start
201cdf0e10cSrcweir writeShort(0); // internal file attributes
202cdf0e10cSrcweir writeLong(0); // external file attributes
203cdf0e10cSrcweir writeLong(e->offset); // offset w.r.t disk
204cdf0e10cSrcweir if( !isError() )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir sal_uInt64 nWritten;
207cdf0e10cSrcweir mrFile.write( e->name.getStr(), e->name.getLength(), nWritten ); // file name
208cdf0e10cSrcweir OSL_ASSERT( nWritten == (sal_uInt64)e->name.getLength() );
209cdf0e10cSrcweir }
210cdf0e10cSrcweir }
211cdf0e10cSrcweir
212cdf0e10cSrcweir /* write the end of the central dir to the zipfile */
writeEndCentralDir(sal_Int32 nCdOffset,sal_Int32 nCdSize)213cdf0e10cSrcweir void ZipFile::writeEndCentralDir(sal_Int32 nCdOffset, sal_Int32 nCdSize)
214cdf0e10cSrcweir {
215cdf0e10cSrcweir writeLong(zf_ECDSIGValue); // magic number
216cdf0e10cSrcweir writeShort(0); // disk num
217cdf0e10cSrcweir writeShort(0); // disk with central dir
218cdf0e10cSrcweir writeShort( static_cast< sal_Int16 >( maEntries.size() ) ); // number of file entries
219cdf0e10cSrcweir writeShort( static_cast< sal_Int16 >( maEntries.size() ) ); // number of file entries
220cdf0e10cSrcweir writeLong(nCdSize); // central dir size
221cdf0e10cSrcweir writeLong(nCdOffset);
222cdf0e10cSrcweir writeShort(0); // comment len
223cdf0e10cSrcweir }
224cdf0e10cSrcweir
225cdf0e10cSrcweir
226cdf0e10cSrcweir /****************************************************************
227cdf0e10cSrcweir * The exported functions
228cdf0e10cSrcweir ****************************************************************/
229cdf0e10cSrcweir
230cdf0e10cSrcweir /* Create a zip file for writing, return a handle for it.
231cdf0e10cSrcweir * RETURNS: A new zip-file output object, or NULL if it failed, in
232cdf0e10cSrcweir * which case *errMsgBuffer will contain an error message string. */
ZipFile(osl::File & rFile)233cdf0e10cSrcweir ZipFile::ZipFile(osl::File& rFile )
234cdf0e10cSrcweir : mrFile( rFile ), mbOpen( true ), mnRC( osl::File::E_None )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir }
237cdf0e10cSrcweir
~ZipFile()238cdf0e10cSrcweir ZipFile::~ZipFile()
239cdf0e10cSrcweir {
240cdf0e10cSrcweir if( mbOpen )
241cdf0e10cSrcweir close();
242cdf0e10cSrcweir }
243cdf0e10cSrcweir
244cdf0e10cSrcweir /* Add a file to this zip with the given name.
245cdf0e10cSrcweir * RETURNS: true if successful, else false. If false, the caller should
246cdf0e10cSrcweir * call zip_Close() and delete the bum zip file.
247cdf0e10cSrcweir */
addFile(osl::File & rFile,const OString & rName)248cdf0e10cSrcweir bool ZipFile::addFile( osl::File& rFile, const OString& rName )
249cdf0e10cSrcweir {
250cdf0e10cSrcweir OSL_ASSERT( mbOpen );
251cdf0e10cSrcweir
252cdf0e10cSrcweir if( !mbOpen )
253cdf0e10cSrcweir return false;
254cdf0e10cSrcweir
255cdf0e10cSrcweir OSL_ASSERT( 0 != rName.getLength() );
256cdf0e10cSrcweir
257cdf0e10cSrcweir if(0 == rName.getLength())
258cdf0e10cSrcweir return false;
259cdf0e10cSrcweir
260cdf0e10cSrcweir mnRC = rFile.open( osl_File_OpenFlag_Read );
261cdf0e10cSrcweir
262cdf0e10cSrcweir if( !isError() )
263cdf0e10cSrcweir {
264cdf0e10cSrcweir ZipEntry *e = new ZipEntry;
265cdf0e10cSrcweir e->name = rName;
266cdf0e10cSrcweir maEntries.push_back(e);
267cdf0e10cSrcweir
268cdf0e10cSrcweir writeDummyLocalHeader(e);
269cdf0e10cSrcweir if( !isError() )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir copyAndCRC(e, rFile);
272cdf0e10cSrcweir if(!isError())
273cdf0e10cSrcweir {
274cdf0e10cSrcweir writeLocalHeader(e);
275cdf0e10cSrcweir }
276cdf0e10cSrcweir }
277cdf0e10cSrcweir
278cdf0e10cSrcweir rFile.close();
279cdf0e10cSrcweir }
280cdf0e10cSrcweir
281cdf0e10cSrcweir return !isError();
282cdf0e10cSrcweir }
283cdf0e10cSrcweir
284cdf0e10cSrcweir /* Finish up the zip file, close it, and deallocate the zip file object.
285cdf0e10cSrcweir * RETURNS: true if successful, else false.
286cdf0e10cSrcweir */
close()287cdf0e10cSrcweir bool ZipFile::close()
288cdf0e10cSrcweir {
289cdf0e10cSrcweir OSL_ASSERT( mbOpen );
290cdf0e10cSrcweir
291cdf0e10cSrcweir if( !mbOpen )
292cdf0e10cSrcweir return false;
293cdf0e10cSrcweir
294cdf0e10cSrcweir if( !isError() )
295cdf0e10cSrcweir {
296cdf0e10cSrcweir sal_uInt64 nCdOffset;
297cdf0e10cSrcweir mrFile.getPos( nCdOffset );
298cdf0e10cSrcweir
299cdf0e10cSrcweir std::vector< ZipEntry* >::iterator aIter( maEntries.begin() );
300cdf0e10cSrcweir while((aIter != maEntries.end()) && !isError())
301cdf0e10cSrcweir {
302cdf0e10cSrcweir writeCentralDir( (*aIter++) );
303cdf0e10cSrcweir }
304cdf0e10cSrcweir
305cdf0e10cSrcweir if( !isError() )
306cdf0e10cSrcweir {
307cdf0e10cSrcweir sal_uInt64 nCdSize;
308cdf0e10cSrcweir mrFile.getPos( nCdSize );
309cdf0e10cSrcweir
310cdf0e10cSrcweir nCdSize -= nCdOffset;
311cdf0e10cSrcweir
312cdf0e10cSrcweir if( !isError() )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir writeEndCentralDir(static_cast<sal_Int32>(nCdOffset), static_cast<sal_Int32>(nCdSize));
315cdf0e10cSrcweir }
316cdf0e10cSrcweir }
317cdf0e10cSrcweir }
318cdf0e10cSrcweir
319cdf0e10cSrcweir std::vector< ZipEntry* >::iterator aIter( maEntries.begin() );
320cdf0e10cSrcweir while( aIter != maEntries.end() )
321cdf0e10cSrcweir {
322cdf0e10cSrcweir delete (*aIter++);
323cdf0e10cSrcweir }
324cdf0e10cSrcweir
325cdf0e10cSrcweir mbOpen = false;
326cdf0e10cSrcweir
327cdf0e10cSrcweir return !isError();
328cdf0e10cSrcweir }
329