LibreOffice Module tools (master) 1
zcodec.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <algorithm>
23
24#include <tools/stream.hxx>
25
26#include <zlib.h>
27
28#include <tools/zcodec.hxx>
29#include <tools/long.hxx>
30
31/* gzip flag byte */
32// GZ_ASCII_FLAG = 0x01; /* bit 0 set: file probably ascii text */
33constexpr sal_uInt8 GZ_HEAD_CRC = 0x02; /* bit 1 set: header CRC present */
34constexpr sal_uInt8 GZ_EXTRA_FIELD = 0x04; /* bit 2 set: extra field present */
35constexpr sal_uInt8 GZ_ORIG_NAME = 0x08; /* bit 3 set: original file name present */
36constexpr sal_uInt8 GZ_COMMENT = 0x10; /* bit 4 set: file comment present */
37constexpr sal_uInt8 GZ_RESERVED = 0xE0; /* bits 5..7: reserved */
38constexpr sal_uInt16 GZ_MAGIC_BYTES_LE = 0x8B1F; /* gzip magic bytes, little endian */
39constexpr sal_uInt8 GZ_DEFLATE = 0x08;
40constexpr sal_uInt8 GZ_FS_UNKNOWN = 0xFF;
41
42ZCodec::ZCodec( size_t nInBufSize, size_t nOutBufSize )
43 : meState(STATE_INIT)
44 , mbStatus(false)
45 , mbFinish(false)
46 , mnInBufSize(nInBufSize)
47 , mnInToRead(0)
48 , mpOStm(nullptr)
49 , mnOutBufSize(nOutBufSize)
50 , mnUncompressedSize(0)
51 , mnInBufCRC32(0)
52 , mnLastModifiedTime(0)
53 , mnCompressLevel(0)
54 , mbGzLib(false)
55{
56 mpsC_Stream = new z_stream;
57}
58
60{
61 auto pStream = static_cast<z_stream*>(mpsC_Stream);
62 delete pStream;
63}
64
66{
67 sal_uInt64 nCurPos = rIStm.Tell();
68 rIStm.Seek( 0 );
69 sal_uInt16 nFirstTwoBytes = 0;
70 rIStm.ReadUInt16( nFirstTwoBytes );
71 rIStm.Seek( nCurPos );
72 return nFirstTwoBytes == GZ_MAGIC_BYTES_LE;
73}
74
75void ZCodec::BeginCompression( int nCompressLevel, bool gzLib )
76{
77 assert(meState == STATE_INIT);
78 mbStatus = true;
79 mbFinish = false;
80 mpOStm = nullptr;
81 mnInToRead = 0xffffffff;
82 mpInBuf.reset();
83 mpOutBuf.reset();
84 auto pStream = static_cast<z_stream*>(mpsC_Stream);
85 pStream->total_out = pStream->total_in = 0;
86 mnCompressLevel = nCompressLevel;
87 mbGzLib = gzLib;
88 pStream->zalloc = nullptr;
89 pStream->zfree = nullptr;
90 pStream->opaque = nullptr;
91 pStream->avail_out = pStream->avail_in = 0;
92}
93
95{
96 tools::Long retvalue = 0;
97 auto pStream = static_cast<z_stream*>(mpsC_Stream);
98
99 if (meState != STATE_INIT)
100 {
101 if (meState == STATE_COMPRESS)
102 {
103 if (mbStatus)
104 {
105 do
106 {
108 }
109 while ( deflate( pStream, Z_FINISH ) != Z_STREAM_END );
110
112 }
113
114 retvalue = pStream->total_in;
115 deflateEnd( pStream );
116 if ( mbGzLib )
117 {
118 // metadata must be set to compress as gz format
119 assert(!msFilename.isEmpty());
120 // overwrite zlib checksum
122 mpOStm->SeekRel(-4);
123 mpOStm->WriteUInt32( mnInBufCRC32 ); // Uncompressed buffer CRC32
124 mpOStm->WriteUInt32( mnUncompressedSize ); // Uncompressed size mod 2^32
125 mpOStm->Seek( 0 );
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
134 }
135 }
136 else
137 {
138 retvalue = pStream->total_out;
139 inflateEnd( pStream );
140 }
141 mpOutBuf.reset();
142 mpInBuf.reset();
144 }
145 return mbStatus ? retvalue : -1;
146}
147
148void ZCodec::SetCompressionMetadata( const OString& sFilename, sal_uInt32 nLastModifiedTime, sal_uInt32 nInBufCRC32 )
149{
150 assert( mbGzLib );
151 msFilename = sFilename;
152 mnLastModifiedTime = nLastModifiedTime;
153 mnInBufCRC32 = nInBufCRC32;
154}
155
156void ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
157{
158 assert(meState == STATE_INIT);
159 mpOStm = &rOStm;
160 rIStm.Seek(0);
161 mnUncompressedSize = rIStm.TellEnd();
162 InitCompress();
163 mpInBuf.reset(new sal_uInt8[ mnInBufSize ]);
164 auto pStream = static_cast<z_stream*>(mpsC_Stream);
165 for (;;)
166 {
167 pStream->next_in = mpInBuf.get();
168 pStream->avail_in = rIStm.ReadBytes( mpInBuf.get(), mnInBufSize );
169 if (pStream->avail_in == 0)
170 break;
171 if ( pStream->avail_out == 0 )
173 if ( deflate( pStream, Z_NO_FLUSH ) < 0 )
174 {
175 mbStatus = false;
176 break;
177 }
178 };
179}
180
182{
183 int err;
184 size_t nInToRead;
185 auto pStream = static_cast<z_stream*>(mpsC_Stream);
186 tools::Long nOldTotal_Out = pStream->total_out;
187
188 assert(meState == STATE_INIT);
189 mpOStm = &rOStm;
190 InitDecompress(rIStm);
191 pStream->avail_out = mnOutBufSize;
192 mpOutBuf.reset(new sal_uInt8[ pStream->avail_out ]);
193 pStream->next_out = mpOutBuf.get();
194 do
195 {
196 if ( pStream->avail_out == 0 ) ImplWriteBack();
197 if ( pStream->avail_in == 0 && mnInToRead )
198 {
199 nInToRead = std::min( mnInBufSize, mnInToRead );
200 pStream->next_in = mpInBuf.get();
201 pStream->avail_in = rIStm.ReadBytes(mpInBuf.get(), nInToRead);
202 mnInToRead -= nInToRead;
203 }
204 err = mbStatus ? inflate(pStream, Z_NO_FLUSH) : Z_ERRNO;
205 if (err < 0 || err == Z_NEED_DICT)
206 {
207 mbStatus = false;
208 break;
209 }
210
211 }
212 while ( ( err != Z_STREAM_END) && ( pStream->avail_in || mnInToRead ) );
214
215 return mbStatus ? static_cast<tools::Long>(pStream->total_out - nOldTotal_Out) : -1;
216}
217
218void ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uInt32 nSize )
219{
220 if (meState == STATE_INIT)
221 {
222 mpOStm = &rOStm;
223 InitCompress();
224 }
225 assert(&rOStm == mpOStm);
226
227 auto pStream = static_cast<z_stream*>(mpsC_Stream);
228 pStream->avail_in = nSize;
229 pStream->next_in = pData;
230
231 while ( pStream->avail_in || ( pStream->avail_out == 0 ) )
232 {
233 if ( pStream->avail_out == 0 )
235
236 if ( deflate( pStream, Z_NO_FLUSH ) < 0 )
237 {
238 mbStatus = false;
239 break;
240 }
241 }
242}
243
244tools::Long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uInt32 nSize )
245{
246 int err;
247 size_t nInToRead;
248
249 if ( mbFinish )
250 return 0; // pStream->total_out;
251
252 if (meState == STATE_INIT)
253 {
254 InitDecompress(rIStm);
255 }
256 auto pStream = static_cast<z_stream*>(mpsC_Stream);
257 pStream->avail_out = nSize;
258 pStream->next_out = pData;
259 do
260 {
261 if ( pStream->avail_in == 0 && mnInToRead )
262 {
263 nInToRead = std::min(mnInBufSize, mnInToRead);
264 pStream->next_in = mpInBuf.get();
265 pStream->avail_in = rIStm.ReadBytes(mpInBuf.get(), nInToRead);
266 mnInToRead -= nInToRead;
267 }
268 err = mbStatus ? inflate(pStream, Z_NO_FLUSH) : Z_ERRNO;
269 if (err < 0 || err == Z_NEED_DICT)
270 {
271 // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
272 mbStatus = (err == Z_BUF_ERROR);
273 break;
274 }
275 }
276 while ( (err != Z_STREAM_END) &&
277 (pStream->avail_out != 0) &&
278 (pStream->avail_in || mnInToRead) );
279 if ( err == Z_STREAM_END )
280 mbFinish = true;
281
282 return (mbStatus ? static_cast<tools::Long>(nSize - pStream->avail_out) : -1);
283}
284
286{
287 auto pStream = static_cast<z_stream*>(mpsC_Stream);
288 size_t nAvail = mnOutBufSize - pStream->avail_out;
289
290 if ( nAvail > 0 )
291 {
292 pStream->next_out = mpOutBuf.get();
293 mpOStm->WriteBytes( mpOutBuf.get(), nAvail );
294 pStream->avail_out = mnOutBufSize;
295 }
296}
297
299{
300 assert(meState == STATE_INIT);
301 if (mbGzLib)
302 {
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);
308 }
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;
317}
318
320{
321 assert(meState == STATE_INIT);
322 auto pStream = static_cast<z_stream*>(mpsC_Stream);
323 if ( mbStatus && mbGzLib )
324 {
325 sal_uInt8 j, nMethod, nFlags;
326 sal_uInt16 nFirstTwoBytes;
327 inStream.Seek( 0 );
328 inStream.ReadUInt16( nFirstTwoBytes );
329 if ( nFirstTwoBytes != GZ_MAGIC_BYTES_LE )
330 mbStatus = false;
331 inStream.ReadUChar( nMethod );
332 inStream.ReadUChar( nFlags );
333 if ( nMethod != Z_DEFLATED )
334 mbStatus = false;
335 if ( ( nFlags & GZ_RESERVED ) != 0 )
336 mbStatus = false;
337 /* Discard time, xflags and OS code: */
338 inStream.SeekRel( 6 );
339 /* skip the extra field */
340 if ( nFlags & GZ_EXTRA_FIELD )
341 {
342 sal_uInt8 n1, n2;
343 inStream.ReadUChar( n1 ).ReadUChar( n2 );
344 inStream.SeekRel( n1 + ( n2 << 8 ) );
345 }
346 /* skip the original file name */
347 if ( nFlags & GZ_ORIG_NAME)
348 {
349 do
350 {
351 inStream.ReadUChar( j );
352 }
353 while ( j && !inStream.eof() );
354 }
355 /* skip the .gz file comment */
356 if ( nFlags & GZ_COMMENT )
357 {
358 do
359 {
360 inStream.ReadUChar( j );
361 }
362 while ( j && !inStream.eof() );
363 }
364 /* skip the header crc */
365 if ( nFlags & GZ_HEAD_CRC )
366 inStream.SeekRel( 2 );
367 if ( mbStatus )
368 mbStatus = inflateInit2( pStream, -MAX_WBITS) == Z_OK;
369 }
370 else
371 {
372 mbStatus = ( inflateInit( pStream ) >= 0 );
373 }
374 if ( mbStatus )
376 mpInBuf.reset(new sal_uInt8[ mnInBufSize ]);
377}
378
380{
381 assert(meState == STATE_INIT);
382 sal_uInt64 nStreamPos = rIStm.Tell();
384 InitDecompress(rIStm);
386 if ( !mbStatus || rIStm.GetError() )
387 {
388 rIStm.Seek(nStreamPos);
389 return false;
390 }
391 rIStm.Seek(nStreamPos);
393 Decompress(rIStm, rOStm);
395 if( !mbStatus || rIStm.GetError() || rOStm.GetError() )
396 {
397 rIStm.Seek(nStreamPos);
398 return false;
399 }
400 rIStm.Seek(nStreamPos);
401 rOStm.Seek(0);
402 return true;
403}
404
405/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt64 Tell() const
Definition: stream.hxx:271
virtual sal_uInt64 TellEnd()
Definition: stream.cxx:1328
std::size_t WriteBytes(const void *pData, std::size_t nSize)
Definition: stream.cxx:1192
bool eof() const
end of input seen during previous i/o operation
Definition: stream.hxx:395
SvStream & WriteUInt16(sal_uInt16 nUInt16)
Definition: stream.cxx:949
SvStream & WriteUInt32(sal_uInt32 nUInt32)
Definition: stream.cxx:950
SvStream & WriteUInt8(sal_uInt8 nuInt8)
Definition: stream.cxx:1012
sal_uInt64 Seek(sal_uInt64 nPos)
Definition: stream.cxx:1262
std::size_t ReadBytes(void *pData, std::size_t nSize)
Definition: stream.cxx:1114
sal_uInt64 SeekRel(sal_Int64 nPos)
Definition: stream.cxx:784
ErrCode GetError() const
Definition: stream.hxx:204
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
Definition: stream.cxx:821
SvStream & ReadUChar(unsigned char &rChar)
Definition: stream.cxx:858
tools::Long Decompress(SvStream &rIStm, SvStream &rOStm)
Definition: zcodec.cxx:181
bool mbStatus
Definition: zcodec.hxx:44
void * mpsC_Stream
Definition: zcodec.hxx:59
int mnCompressLevel
Definition: zcodec.hxx:57
tools::Long Read(SvStream &rIStm, sal_uInt8 *pData, sal_uInt32 nSize)
Definition: zcodec.cxx:244
bool mbGzLib
Definition: zcodec.hxx:58
~ZCodec()
Definition: zcodec.cxx:59
bool AttemptDecompression(SvStream &rIStm, SvStream &rOStm)
Definition: zcodec.cxx:379
State meState
Definition: zcodec.hxx:43
OString msFilename
Definition: zcodec.hxx:55
void InitDecompress(SvStream &inStream)
Definition: zcodec.cxx:319
sal_uInt32 mnLastModifiedTime
Definition: zcodec.hxx:54
SvStream * mpOStm
Definition: zcodec.hxx:49
size_t mnInToRead
Definition: zcodec.hxx:48
void SetCompressionMetadata(const OString &sFilename, sal_uInt32 nLastModifiedTime, sal_uInt32 nInBufCRC32)
Set metadata for gzlib compression.
Definition: zcodec.cxx:148
static bool IsZCompressed(SvStream &rIStm)
Checks whether a stream is Z compressed.
Definition: zcodec.cxx:65
ZCodec(size_t nInBufSize=32768, size_t nOutBufSize=32768)
Definition: zcodec.cxx:42
@ STATE_DECOMPRESS
Definition: zcodec.hxx:42
@ STATE_COMPRESS
Definition: zcodec.hxx:42
@ STATE_INIT
Definition: zcodec.hxx:42
void ImplWriteBack()
Definition: zcodec.cxx:285
tools::Long EndCompression()
Definition: zcodec.cxx:94
bool mbFinish
Definition: zcodec.hxx:45
sal_uInt32 mnUncompressedSize
Definition: zcodec.hxx:52
void InitCompress()
Definition: zcodec.cxx:298
void BeginCompression(int nCompressLevel=ZCODEC_DEFAULT_COMPRESSION, bool gzLib=false)
Definition: zcodec.cxx:75
void Write(SvStream &rOStm, const sal_uInt8 *pData, sal_uInt32 nSize)
Definition: zcodec.cxx:218
size_t mnInBufSize
Definition: zcodec.hxx:47
size_t mnOutBufSize
Definition: zcodec.hxx:51
sal_uInt32 mnInBufCRC32
Definition: zcodec.hxx:53
std::unique_ptr< sal_uInt8[]> mpInBuf
Definition: zcodec.hxx:46
std::unique_ptr< sal_uInt8[]> mpOutBuf
Definition: zcodec.hxx:50
void Compress(SvStream &rIStm, SvStream &rOStm)
Definition: zcodec.cxx:156
std::unique_ptr< sal_Int32[]> pData
int n2
int n1
err
long Long
Definition: long.hxx:34
#define STREAM_SEEK_TO_END
Definition: stream.hxx:73
unsigned char sal_uInt8
constexpr sal_uInt8 GZ_EXTRA_FIELD
Definition: zcodec.cxx:34
constexpr sal_uInt8 GZ_COMMENT
Definition: zcodec.cxx:36
constexpr sal_uInt8 GZ_DEFLATE
Definition: zcodec.cxx:39
constexpr sal_uInt8 GZ_RESERVED
Definition: zcodec.cxx:37
constexpr sal_uInt8 GZ_ORIG_NAME
Definition: zcodec.cxx:35
constexpr sal_uInt8 GZ_HEAD_CRC
Definition: zcodec.cxx:33
constexpr sal_uInt8 GZ_FS_UNKNOWN
Definition: zcodec.cxx:40
constexpr sal_uInt16 GZ_MAGIC_BYTES_LE
Definition: zcodec.cxx:38
#define ZCODEC_DEFAULT_COMPRESSION
Definition: zcodec.hxx:29