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 <tools/stream.hxx>
21 :
22 : #include <zlib.h>
23 :
24 : #include <tools/zcodec.hxx>
25 : #include <rtl/crc.h>
26 : #include <osl/endian.h>
27 :
28 : #define PZSTREAM ((z_stream*) mpsC_Stream)
29 :
30 : /* gzip flag byte */
31 : // GZ_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
32 : #define GZ_HEAD_CRC 0x02 /* bit 1 set: header CRC present */
33 : #define GZ_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
34 : #define GZ_ORIG_NAME 0x08 /* bit 3 set: original file name present */
35 : #define GZ_COMMENT 0x10 /* bit 4 set: file comment present */
36 : #define GZ_RESERVED 0xE0 /* bits 5..7: reserved */
37 :
38 : static const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
39 :
40 4472 : ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize, sal_uIntPtr nMemUsage )
41 : : mbInit(0)
42 : , mbStatus(false)
43 : , mbFinish(false)
44 : , mnMemUsage(nMemUsage)
45 : , mpIStm(NULL)
46 : , mpInBuf(NULL)
47 : , mnInBufSize(nInBufSize)
48 : , mnInToRead(0)
49 : , mpOStm(NULL)
50 : , mpOutBuf(NULL)
51 : , mnOutBufSize(nOutBufSize)
52 : , mnCRC(0)
53 4472 : , mnCompressMethod(0)
54 : {
55 4472 : mpsC_Stream = new z_stream;
56 4472 : }
57 :
58 972 : ZCodec::ZCodec()
59 : : mbInit(0)
60 : , mbStatus(false)
61 : , mbFinish(false)
62 : , mnMemUsage(MAX_MEM_USAGE)
63 : , mpIStm(NULL)
64 : , mpInBuf(NULL)
65 : , mnInBufSize(DEFAULT_IN_BUFSIZE)
66 : , mnInToRead(0)
67 : , mpOStm(NULL)
68 : , mpOutBuf(NULL)
69 : , mnOutBufSize(DEFAULT_OUT_BUFSIZE)
70 : , mnCRC(0)
71 972 : , mnCompressMethod(0)
72 : {
73 972 : mpsC_Stream = new z_stream;
74 972 : }
75 :
76 9844 : ZCodec::~ZCodec()
77 : {
78 5444 : delete (z_stream*) mpsC_Stream;
79 9844 : }
80 :
81 5439 : void ZCodec::BeginCompression( sal_uIntPtr nCompressMethod )
82 : {
83 5439 : mbInit = 0;
84 5439 : mbStatus = true;
85 5439 : mbFinish = false;
86 5439 : mpIStm = mpOStm = NULL;
87 5439 : mnInToRead = 0xffffffff;
88 5439 : mpInBuf = mpOutBuf = NULL;
89 5439 : PZSTREAM->total_out = PZSTREAM->total_in = 0;
90 5439 : mnCompressMethod = nCompressMethod;
91 5439 : PZSTREAM->zalloc = ( alloc_func )0;
92 5439 : PZSTREAM->zfree = ( free_func )0;
93 5439 : PZSTREAM->opaque = ( voidpf )0;
94 5439 : PZSTREAM->avail_out = PZSTREAM->avail_in = 0;
95 5439 : }
96 :
97 5439 : long ZCodec::EndCompression()
98 : {
99 5439 : long retvalue = 0;
100 :
101 5439 : if ( mbInit != 0 )
102 : {
103 5439 : if ( mbInit & 2 ) // 1->decompress, 3->compress
104 : {
105 1071 : do
106 : {
107 1071 : ImplWriteBack();
108 : }
109 1071 : while ( deflate( PZSTREAM, Z_FINISH ) != Z_STREAM_END );
110 :
111 1071 : ImplWriteBack();
112 :
113 1071 : retvalue = PZSTREAM->total_in;
114 1071 : deflateEnd( PZSTREAM );
115 : }
116 : else
117 : {
118 4368 : retvalue = PZSTREAM->total_out;
119 4368 : inflateEnd( PZSTREAM );
120 : }
121 5439 : delete[] mpOutBuf;
122 5439 : delete[] mpInBuf;
123 : }
124 5439 : return ( mbStatus ) ? retvalue : -1;
125 : }
126 :
127 0 : long ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
128 : {
129 0 : long nOldTotal_In = PZSTREAM->total_in;
130 :
131 0 : if ( mbInit == 0 )
132 : {
133 0 : mpIStm = &rIStm;
134 0 : mpOStm = &rOStm;
135 0 : ImplInitBuf( false );
136 0 : mpInBuf = new sal_uInt8[ mnInBufSize ];
137 : }
138 0 : while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 )
139 : {
140 0 : if ( PZSTREAM->avail_out == 0 )
141 0 : ImplWriteBack();
142 0 : if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
143 : {
144 0 : mbStatus = false;
145 0 : break;
146 : }
147 : };
148 0 : return ( mbStatus ) ? (long)(PZSTREAM->total_in - nOldTotal_In) : -1;
149 : }
150 :
151 57 : long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
152 : {
153 : int err;
154 : sal_uIntPtr nInToRead;
155 57 : long nOldTotal_Out = PZSTREAM->total_out;
156 :
157 57 : if ( mbFinish )
158 0 : return PZSTREAM->total_out - nOldTotal_Out;
159 :
160 57 : if ( mbInit == 0 )
161 : {
162 57 : mpIStm = &rIStm;
163 57 : mpOStm = &rOStm;
164 57 : ImplInitBuf( true );
165 57 : PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
166 : }
167 60 : do
168 : {
169 64 : if ( PZSTREAM->avail_out == 0 ) ImplWriteBack();
170 64 : if ( PZSTREAM->avail_in == 0 && mnInToRead )
171 : {
172 57 : nInToRead = ( mnInBufSize > mnInToRead ) ? mnInToRead : mnInBufSize;
173 57 : PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, nInToRead );
174 57 : mnInToRead -= nInToRead;
175 :
176 57 : if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
177 0 : mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
178 :
179 : }
180 64 : err = inflate( PZSTREAM, Z_NO_FLUSH );
181 64 : if ( err < 0 )
182 : {
183 4 : mbStatus = false;
184 4 : break;
185 : }
186 :
187 : }
188 7 : while ( ( err != Z_STREAM_END) && ( PZSTREAM->avail_in || mnInToRead ) );
189 57 : ImplWriteBack();
190 :
191 57 : if ( err == Z_STREAM_END )
192 53 : mbFinish = true;
193 57 : return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1;
194 : }
195 :
196 14488 : long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize )
197 : {
198 14488 : if ( mbInit == 0 )
199 : {
200 1071 : mpOStm = &rOStm;
201 1071 : ImplInitBuf( false );
202 : }
203 :
204 14488 : PZSTREAM->avail_in = nSize;
205 14488 : PZSTREAM->next_in = (unsigned char*)pData;
206 :
207 43467 : while ( PZSTREAM->avail_in || ( PZSTREAM->avail_out == 0 ) )
208 : {
209 14491 : if ( PZSTREAM->avail_out == 0 )
210 3 : ImplWriteBack();
211 :
212 14491 : if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
213 : {
214 0 : mbStatus = false;
215 0 : break;
216 : }
217 : }
218 14488 : return ( mbStatus ) ? (long)nSize : -1;
219 : }
220 :
221 1 : long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
222 : {
223 : int err;
224 : sal_uIntPtr nInToRead;
225 :
226 1 : if ( mbFinish )
227 0 : return 0; // PZSTREAM->total_out;
228 :
229 1 : mpIStm = &rIStm;
230 1 : if ( mbInit == 0 )
231 : {
232 1 : ImplInitBuf( true );
233 : }
234 1 : PZSTREAM->avail_out = nSize;
235 1 : PZSTREAM->next_out = pData;
236 1 : do
237 : {
238 1 : if ( PZSTREAM->avail_in == 0 && mnInToRead )
239 : {
240 1 : nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
241 : PZSTREAM->avail_in = mpIStm->Read (
242 1 : PZSTREAM->next_in = mpInBuf, nInToRead);
243 1 : mnInToRead -= nInToRead;
244 :
245 1 : if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
246 0 : mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
247 :
248 : }
249 1 : err = inflate( PZSTREAM, Z_NO_FLUSH );
250 1 : if ( err < 0 )
251 : {
252 : // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
253 0 : mbStatus = (err == Z_BUF_ERROR);
254 0 : break;
255 : }
256 : }
257 0 : while ( (err != Z_STREAM_END) &&
258 0 : (PZSTREAM->avail_out != 0) &&
259 0 : (PZSTREAM->avail_in || mnInToRead) );
260 1 : if ( err == Z_STREAM_END )
261 1 : mbFinish = true;
262 :
263 1 : return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
264 : }
265 :
266 201612 : long ZCodec::ReadAsynchron( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
267 : {
268 201612 : int err = 0;
269 : sal_uIntPtr nInToRead;
270 :
271 201612 : if ( mbFinish )
272 0 : return 0; // PZSTREAM->total_out;
273 :
274 201612 : if ( mbInit == 0 )
275 : {
276 4310 : mpIStm = &rIStm;
277 4310 : ImplInitBuf( true );
278 : }
279 201612 : PZSTREAM->avail_out = nSize;
280 201612 : PZSTREAM->next_out = pData;
281 201794 : do
282 : {
283 201795 : if ( PZSTREAM->avail_in == 0 && mnInToRead )
284 : {
285 4691 : nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
286 :
287 4691 : sal_uIntPtr nStreamPos = rIStm.Tell();
288 4691 : rIStm.Seek( STREAM_SEEK_TO_END );
289 4691 : sal_uIntPtr nMaxPos = rIStm.Tell();
290 4691 : rIStm.Seek( nStreamPos );
291 4691 : if ( ( nMaxPos - nStreamPos ) < nInToRead )
292 : {
293 0 : rIStm.SetError( ERRCODE_IO_PENDING );
294 0 : err= int(!Z_STREAM_END); // TODO What is appropriate code for this?
295 0 : break;
296 : }
297 :
298 : PZSTREAM->avail_in = mpIStm->Read (
299 4691 : PZSTREAM->next_in = mpInBuf, nInToRead);
300 4691 : mnInToRead -= nInToRead;
301 :
302 4691 : if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
303 4691 : mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
304 :
305 : }
306 201795 : err = inflate( PZSTREAM, Z_NO_FLUSH );
307 201795 : if ( err < 0 )
308 : {
309 : // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
310 1 : mbStatus = (err == Z_BUF_ERROR);
311 1 : break;
312 : }
313 : }
314 197485 : while ( (err != Z_STREAM_END) &&
315 197865 : (PZSTREAM->avail_out != 0) &&
316 380 : (PZSTREAM->avail_in || mnInToRead) );
317 201612 : if ( err == Z_STREAM_END )
318 4309 : mbFinish = true;
319 :
320 201612 : return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
321 : }
322 :
323 2209 : void ZCodec::ImplWriteBack()
324 : {
325 2209 : sal_uIntPtr nAvail = mnOutBufSize - PZSTREAM->avail_out;
326 :
327 2209 : if ( nAvail )
328 : {
329 2209 : if ( mbInit & 2 && ( mnCompressMethod & ZCODEC_UPDATE_CRC ) )
330 173 : mnCRC = UpdateCRC( mnCRC, mpOutBuf, nAvail );
331 2209 : mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail );
332 2209 : PZSTREAM->avail_out = mnOutBufSize;
333 : }
334 2209 : }
335 :
336 4507 : void ZCodec::SetBreak( sal_uIntPtr nInToRead )
337 : {
338 4507 : mnInToRead = nInToRead;
339 4507 : }
340 :
341 201612 : sal_uIntPtr ZCodec::GetBreak( void )
342 : {
343 201612 : return ( mnInToRead + PZSTREAM->avail_in );
344 : }
345 :
346 85 : void ZCodec::SetCRC( sal_uIntPtr nCRC )
347 : {
348 85 : mnCRC = nCRC;
349 85 : }
350 :
351 85 : sal_uIntPtr ZCodec::GetCRC()
352 : {
353 85 : return mnCRC;
354 : }
355 :
356 5439 : void ZCodec::ImplInitBuf ( bool nIOFlag )
357 : {
358 5439 : if ( mbInit == 0 )
359 : {
360 5439 : if ( nIOFlag )
361 : {
362 4368 : mbInit = 1;
363 4368 : if ( mbStatus && ( mnCompressMethod & ZCODEC_GZ_LIB ) )
364 : {
365 : sal_uInt8 n1, n2, j, nMethod, nFlags;
366 0 : for ( int i = 0; i < 2; i++ ) // gz - magic number
367 : {
368 0 : mpIStm->ReadUChar( j );
369 0 : if ( j != gz_magic[ i ] )
370 0 : mbStatus = false;
371 : }
372 0 : mpIStm->ReadUChar( nMethod );
373 0 : mpIStm->ReadUChar( nFlags );
374 0 : if ( nMethod != Z_DEFLATED )
375 0 : mbStatus = false;
376 0 : if ( ( nFlags & GZ_RESERVED ) != 0 )
377 0 : mbStatus = false;
378 : /* Discard time, xflags and OS code: */
379 0 : mpIStm->SeekRel( 6 );
380 : /* skip the extra field */
381 0 : if ( nFlags & GZ_EXTRA_FIELD )
382 : {
383 0 : mpIStm->ReadUChar( n1 ).ReadUChar( n2 );
384 0 : mpIStm->SeekRel( n1 + ( n2 << 8 ) );
385 : }
386 : /* skip the original file name */
387 0 : if ( nFlags & GZ_ORIG_NAME)
388 : {
389 0 : do
390 : {
391 0 : mpIStm->ReadUChar( j );
392 : }
393 0 : while ( j && !mpIStm->IsEof() );
394 : }
395 : /* skip the .gz file comment */
396 0 : if ( nFlags & GZ_COMMENT )
397 : {
398 0 : do
399 : {
400 0 : mpIStm->ReadUChar( j );
401 : }
402 0 : while ( j && !mpIStm->IsEof() );
403 : }
404 : /* skip the header crc */
405 0 : if ( nFlags & GZ_HEAD_CRC )
406 0 : mpIStm->SeekRel( 2 );
407 0 : if ( mbStatus )
408 0 : mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? false : true;
409 : }
410 : else
411 : {
412 4368 : mbStatus = ( inflateInit( PZSTREAM ) >= 0 );
413 : }
414 4368 : mpInBuf = new sal_uInt8[ mnInBufSize ];
415 : }
416 : else
417 : {
418 1071 : mbInit = 3;
419 :
420 : mbStatus = ( deflateInit2_( PZSTREAM, mnCompressMethod & 0xff, Z_DEFLATED,
421 1071 : MAX_WBITS, mnMemUsage, ( mnCompressMethod >> 8 ) & 0xff,
422 1071 : ZLIB_VERSION, sizeof( z_stream ) ) >= 0 );
423 :
424 1071 : PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
425 : }
426 : }
427 5439 : }
428 :
429 4864 : sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uInt8* pSource, long nDatSize)
430 : {
431 4864 : return rtl_crc32( nLatestCRC, pSource, nDatSize );
432 : }
433 :
434 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|