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 <sal/config.h>
24 #include <tools/stream.hxx>
28 #include <tools/zcodec.hxx>
29 #include <tools/long.hxx>
32 // GZ_ASCII_FLAG = 0x01; /* bit 0 set: file probably ascii text */
33 constexpr sal_uInt8 GZ_HEAD_CRC
= 0x02; /* bit 1 set: header CRC present */
34 constexpr sal_uInt8 GZ_EXTRA_FIELD
= 0x04; /* bit 2 set: extra field present */
35 constexpr sal_uInt8 GZ_ORIG_NAME
= 0x08; /* bit 3 set: original file name present */
36 constexpr sal_uInt8 GZ_COMMENT
= 0x10; /* bit 4 set: file comment present */
37 constexpr sal_uInt8 GZ_RESERVED
= 0xE0; /* bits 5..7: reserved */
38 constexpr sal_uInt16 GZ_MAGIC_BYTES_LE
= 0x8B1F; /* gzip magic bytes, little endian */
39 constexpr sal_uInt8 GZ_DEFLATE
= 0x08;
40 constexpr sal_uInt8 GZ_FS_UNKNOWN
= 0xFF;
42 ZCodec::ZCodec( size_t nInBufSize
, size_t nOutBufSize
)
46 , mnInBufSize(nInBufSize
)
49 , mnOutBufSize(nOutBufSize
)
50 , mnUncompressedSize(0)
52 , mnLastModifiedTime(0)
56 mpsC_Stream
= new z_stream
;
61 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
65 bool ZCodec::IsZCompressed( SvStream
& rIStm
)
67 sal_uInt64 nCurPos
= rIStm
.Tell();
69 sal_uInt16 nFirstTwoBytes
= 0;
70 rIStm
.ReadUInt16( nFirstTwoBytes
);
71 rIStm
.Seek( nCurPos
);
72 return nFirstTwoBytes
== GZ_MAGIC_BYTES_LE
;
75 void ZCodec::BeginCompression( int nCompressLevel
, bool gzLib
)
77 assert(meState
== STATE_INIT
);
81 mnInToRead
= 0xffffffff;
84 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
85 pStream
->total_out
= pStream
->total_in
= 0;
86 mnCompressLevel
= nCompressLevel
;
88 pStream
->zalloc
= nullptr;
89 pStream
->zfree
= nullptr;
90 pStream
->opaque
= nullptr;
91 pStream
->avail_out
= pStream
->avail_in
= 0;
94 tools::Long
ZCodec::EndCompression()
96 tools::Long retvalue
= 0;
97 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
99 if (meState
!= STATE_INIT
)
101 if (meState
== STATE_COMPRESS
)
109 while ( deflate( pStream
, Z_FINISH
) != Z_STREAM_END
);
114 retvalue
= pStream
->total_in
;
115 deflateEnd( pStream
);
118 // metadata must be set to compress as gz format
119 assert(!msFilename
.isEmpty());
120 // overwrite zlib checksum
121 mpOStm
->Seek(STREAM_SEEK_TO_END
);
123 mpOStm
->WriteUInt32( mnInBufCRC32
); // Uncompressed buffer CRC32
124 mpOStm
->WriteUInt32( mnUncompressedSize
); // Uncompressed size mod 2^32
126 mpOStm
->WriteUInt16( GZ_MAGIC_BYTES_LE
) // Magic bytes
127 .WriteUInt8( GZ_DEFLATE
) // Compression algorithm
128 .WriteUInt8( GZ_ORIG_NAME
) // Filename
129 .WriteUInt32( mnLastModifiedTime
) // Modification time
130 .WriteUInt8( 0 ) // Extra flags
131 .WriteUInt8( GZ_FS_UNKNOWN
) // Operating system
132 .WriteBytes( msFilename
.pData
->buffer
, msFilename
.pData
->length
);
133 mpOStm
->WriteUInt8( 0 ); // null terminate the filename string
138 retvalue
= pStream
->total_out
;
139 inflateEnd( pStream
);
143 meState
= STATE_INIT
;
145 return mbStatus
? retvalue
: -1;
148 void ZCodec::SetCompressionMetadata( const OString
& sFilename
, sal_uInt32 nLastModifiedTime
, sal_uInt32 nInBufCRC32
)
151 msFilename
= sFilename
;
152 mnLastModifiedTime
= nLastModifiedTime
;
153 mnInBufCRC32
= nInBufCRC32
;
156 void ZCodec::Compress( SvStream
& rIStm
, SvStream
& rOStm
)
158 assert(meState
== STATE_INIT
);
161 mnUncompressedSize
= rIStm
.TellEnd();
163 mpInBuf
.reset(new sal_uInt8
[ mnInBufSize
]);
164 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
167 pStream
->next_in
= mpInBuf
.get();
168 pStream
->avail_in
= rIStm
.ReadBytes( mpInBuf
.get(), mnInBufSize
);
169 if (pStream
->avail_in
== 0)
171 if ( pStream
->avail_out
== 0 )
173 if ( deflate( pStream
, Z_NO_FLUSH
) < 0 )
181 tools::Long
ZCodec::Decompress( SvStream
& rIStm
, SvStream
& rOStm
)
185 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
186 tools::Long nOldTotal_Out
= pStream
->total_out
;
188 assert(meState
== STATE_INIT
);
190 InitDecompress(rIStm
);
191 pStream
->avail_out
= mnOutBufSize
;
192 mpOutBuf
.reset(new sal_uInt8
[ pStream
->avail_out
]);
193 pStream
->next_out
= mpOutBuf
.get();
196 if ( pStream
->avail_out
== 0 ) ImplWriteBack();
197 if ( pStream
->avail_in
== 0 && mnInToRead
)
199 nInToRead
= std::min( mnInBufSize
, mnInToRead
);
200 pStream
->next_in
= mpInBuf
.get();
201 pStream
->avail_in
= rIStm
.ReadBytes(mpInBuf
.get(), nInToRead
);
202 mnInToRead
-= nInToRead
;
204 err
= mbStatus
? inflate(pStream
, Z_NO_FLUSH
) : Z_ERRNO
;
205 if (err
< 0 || err
== Z_NEED_DICT
)
212 while ( ( err
!= Z_STREAM_END
) && ( pStream
->avail_in
|| mnInToRead
) );
215 return mbStatus
? static_cast<tools::Long
>(pStream
->total_out
- nOldTotal_Out
) : -1;
218 void ZCodec::Write( SvStream
& rOStm
, const sal_uInt8
* pData
, sal_uInt32 nSize
)
220 if (meState
== STATE_INIT
)
225 assert(&rOStm
== mpOStm
);
227 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
228 pStream
->avail_in
= nSize
;
229 pStream
->next_in
= pData
;
231 while ( pStream
->avail_in
|| ( pStream
->avail_out
== 0 ) )
233 if ( pStream
->avail_out
== 0 )
236 if ( deflate( pStream
, Z_NO_FLUSH
) < 0 )
244 tools::Long
ZCodec::Read( SvStream
& rIStm
, sal_uInt8
* pData
, sal_uInt32 nSize
)
250 return 0; // pStream->total_out;
252 if (meState
== STATE_INIT
)
254 InitDecompress(rIStm
);
256 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
257 pStream
->avail_out
= nSize
;
258 pStream
->next_out
= pData
;
261 if ( pStream
->avail_in
== 0 && mnInToRead
)
263 nInToRead
= std::min(mnInBufSize
, mnInToRead
);
264 pStream
->next_in
= mpInBuf
.get();
265 pStream
->avail_in
= rIStm
.ReadBytes(mpInBuf
.get(), nInToRead
);
266 mnInToRead
-= nInToRead
;
268 err
= mbStatus
? inflate(pStream
, Z_NO_FLUSH
) : Z_ERRNO
;
269 if (err
< 0 || err
== Z_NEED_DICT
)
271 // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
272 mbStatus
= (err
== Z_BUF_ERROR
);
276 while ( (err
!= Z_STREAM_END
) &&
277 (pStream
->avail_out
!= 0) &&
278 (pStream
->avail_in
|| mnInToRead
) );
279 if ( err
== Z_STREAM_END
)
282 return (mbStatus
? static_cast<tools::Long
>(nSize
- pStream
->avail_out
) : -1);
285 void ZCodec::ImplWriteBack()
287 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
288 size_t nAvail
= mnOutBufSize
- pStream
->avail_out
;
292 pStream
->next_out
= mpOutBuf
.get();
293 mpOStm
->WriteBytes( mpOutBuf
.get(), nAvail
);
294 pStream
->avail_out
= mnOutBufSize
;
298 void ZCodec::InitCompress()
300 assert(meState
== STATE_INIT
);
303 // Seek just enough so that the zlib header is overwritten after compression
304 // with the gz header
305 // 10 header bytes + filename length + null terminator - 2 bytes for
306 // zlib header that gets overwritten
307 mpOStm
->Seek(10 + msFilename
.getLength() + 1 - 2);
309 meState
= STATE_COMPRESS
;
310 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
311 mbStatus
= deflateInit2_(
312 pStream
, mnCompressLevel
, Z_DEFLATED
, MAX_WBITS
, MAX_MEM_LEVEL
,
313 Z_DEFAULT_STRATEGY
, ZLIB_VERSION
, sizeof (z_stream
)) >= 0;
314 mpOutBuf
.reset(new sal_uInt8
[mnOutBufSize
]);
315 pStream
->next_out
= mpOutBuf
.get();
316 pStream
->avail_out
= mnOutBufSize
;
319 void ZCodec::InitDecompress(SvStream
& inStream
)
321 assert(meState
== STATE_INIT
);
322 auto pStream
= static_cast<z_stream
*>(mpsC_Stream
);
323 if ( mbStatus
&& mbGzLib
)
325 sal_uInt8 j
, nMethod
, nFlags
;
326 sal_uInt16 nFirstTwoBytes
;
328 inStream
.ReadUInt16( nFirstTwoBytes
);
329 if ( nFirstTwoBytes
!= GZ_MAGIC_BYTES_LE
)
331 inStream
.ReadUChar( nMethod
);
332 inStream
.ReadUChar( nFlags
);
333 if ( nMethod
!= Z_DEFLATED
)
335 if ( ( nFlags
& GZ_RESERVED
) != 0 )
337 /* Discard time, xflags and OS code: */
338 inStream
.SeekRel( 6 );
339 /* skip the extra field */
340 if ( nFlags
& GZ_EXTRA_FIELD
)
343 inStream
.ReadUChar( n1
).ReadUChar( n2
);
344 inStream
.SeekRel( n1
+ ( n2
<< 8 ) );
346 /* skip the original file name */
347 if ( nFlags
& GZ_ORIG_NAME
)
351 inStream
.ReadUChar( j
);
353 while ( j
&& !inStream
.eof() );
355 /* skip the .gz file comment */
356 if ( nFlags
& GZ_COMMENT
)
360 inStream
.ReadUChar( j
);
362 while ( j
&& !inStream
.eof() );
364 /* skip the header crc */
365 if ( nFlags
& GZ_HEAD_CRC
)
366 inStream
.SeekRel( 2 );
368 mbStatus
= inflateInit2( pStream
, -MAX_WBITS
) == Z_OK
;
372 mbStatus
= ( inflateInit( pStream
) >= 0 );
375 meState
= STATE_DECOMPRESS
;
376 mpInBuf
.reset(new sal_uInt8
[ mnInBufSize
]);
379 bool ZCodec::AttemptDecompression(SvStream
& rIStm
, SvStream
& rOStm
)
381 assert(meState
== STATE_INIT
);
382 sal_uInt64 nStreamPos
= rIStm
.Tell();
383 BeginCompression(ZCODEC_DEFAULT_COMPRESSION
, true/*gzLib*/);
384 InitDecompress(rIStm
);
386 if ( !mbStatus
|| rIStm
.GetError() )
388 rIStm
.Seek(nStreamPos
);
391 rIStm
.Seek(nStreamPos
);
392 BeginCompression(ZCODEC_DEFAULT_COMPRESSION
, true/*gzLib*/);
393 Decompress(rIStm
, rOStm
);
395 if( !mbStatus
|| rIStm
.GetError() || rOStm
.GetError() )
397 rIStm
.Seek(nStreamPos
);
400 rIStm
.Seek(nStreamPos
);
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */