xref: /AOO41X/main/tools/source/zcodec/zcodec.cxx (revision 3a2bd4d8359b8c4ed4da5f7c49d8578b6061adee)
1*89b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*89b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89b56da7SAndrew Rist  * distributed with this work for additional information
6*89b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*89b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*89b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist  * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*89b56da7SAndrew Rist  * specific language governing permissions and limitations
18*89b56da7SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*89b56da7SAndrew Rist  *************************************************************/
21*89b56da7SAndrew Rist 
22*89b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir #include <tools/stream.hxx>
27cdf0e10cSrcweir #ifndef _ZLIB_H
28cdf0e10cSrcweir #ifdef SYSTEM_ZLIB
29cdf0e10cSrcweir #include "zlib.h"
30cdf0e10cSrcweir #else
31cdf0e10cSrcweir #include "zlib/zlib.h"
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <tools/zcodec.hxx>
35cdf0e10cSrcweir #include <rtl/crc.h>
36cdf0e10cSrcweir #include <osl/endian.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // -----------
39cdf0e10cSrcweir // - Defines -
40cdf0e10cSrcweir // -----------
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define PZSTREAM ((z_stream*) mpsC_Stream)
43cdf0e10cSrcweir 
44cdf0e10cSrcweir /* gzip flag byte */
45cdf0e10cSrcweir #define GZ_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
46cdf0e10cSrcweir #define GZ_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
47cdf0e10cSrcweir #define GZ_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
48cdf0e10cSrcweir #define GZ_ORIG_NAME    0x08 /* bit 3 set: original file name present */
49cdf0e10cSrcweir #define GZ_COMMENT      0x10 /* bit 4 set: file comment present */
50cdf0e10cSrcweir #define GZ_RESERVED     0xE0 /* bits 5..7: reserved */
51cdf0e10cSrcweir 
52cdf0e10cSrcweir static int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // ----------
56cdf0e10cSrcweir // - ZCodec -
57cdf0e10cSrcweir // ----------
58cdf0e10cSrcweir 
ZCodec(sal_uIntPtr nInBufSize,sal_uIntPtr nOutBufSize,sal_uIntPtr nMemUsage)59cdf0e10cSrcweir ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize, sal_uIntPtr nMemUsage )
60cdf0e10cSrcweir 	: mnCRC(0)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	mnMemUsage = nMemUsage;
63cdf0e10cSrcweir 	mnInBufSize = nInBufSize;
64cdf0e10cSrcweir 	mnOutBufSize = nOutBufSize;
65cdf0e10cSrcweir 	mpsC_Stream = new z_stream;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
ZCodec(void)68cdf0e10cSrcweir ZCodec::ZCodec( void )
69cdf0e10cSrcweir 	: mnCRC(0)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	mnMemUsage = MAX_MEM_USAGE;
72cdf0e10cSrcweir 	mnInBufSize = DEFAULT_IN_BUFSIZE;
73cdf0e10cSrcweir 	mnOutBufSize = DEFAULT_OUT_BUFSIZE;
74cdf0e10cSrcweir 	mpsC_Stream = new z_stream;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir // ------------------------------------------------------------------------
78cdf0e10cSrcweir 
~ZCodec()79cdf0e10cSrcweir ZCodec::~ZCodec()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	delete (z_stream*) mpsC_Stream;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // ------------------------------------------------------------------------
85cdf0e10cSrcweir 
BeginCompression(sal_uIntPtr nCompressMethod)86cdf0e10cSrcweir void ZCodec::BeginCompression( sal_uIntPtr nCompressMethod )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	mbInit = 0;
89cdf0e10cSrcweir 	mbStatus = sal_True;
90cdf0e10cSrcweir 	mbFinish = sal_False;
91cdf0e10cSrcweir 	mpIStm = mpOStm = NULL;
92cdf0e10cSrcweir 	mnInToRead = 0xffffffff;
93cdf0e10cSrcweir 	mpInBuf = mpOutBuf = NULL;
94cdf0e10cSrcweir 	PZSTREAM->total_out = PZSTREAM->total_in = 0;
95cdf0e10cSrcweir 	mnCompressMethod = nCompressMethod;
96cdf0e10cSrcweir 	PZSTREAM->zalloc = ( alloc_func )0;
97cdf0e10cSrcweir 	PZSTREAM->zfree = ( free_func )0;
98cdf0e10cSrcweir 	PZSTREAM->opaque = ( voidpf )0;
99cdf0e10cSrcweir 	PZSTREAM->avail_out = PZSTREAM->avail_in = 0;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir // ------------------------------------------------------------------------
103cdf0e10cSrcweir 
EndCompression()104cdf0e10cSrcweir long ZCodec::EndCompression()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	long retvalue = 0;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	if ( mbInit != 0 )
109cdf0e10cSrcweir 	{
110cdf0e10cSrcweir 		if ( mbInit & 2 )	// 1->decompress, 3->compress
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			do
113cdf0e10cSrcweir 			{
114cdf0e10cSrcweir 				ImplWriteBack();
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 			while ( deflate( PZSTREAM, Z_FINISH ) != Z_STREAM_END );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 			ImplWriteBack();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 			retvalue = PZSTREAM->total_in;
121cdf0e10cSrcweir 			deflateEnd( PZSTREAM );
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 		else
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			retvalue = PZSTREAM->total_out;
126cdf0e10cSrcweir 			inflateEnd( PZSTREAM );
127cdf0e10cSrcweir 		}
128cdf0e10cSrcweir 		delete[] mpOutBuf;
129cdf0e10cSrcweir 		delete[] mpInBuf;
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir 	return ( mbStatus ) ? retvalue : -1;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
135cdf0e10cSrcweir // ------------------------------------------------------------------------
136cdf0e10cSrcweir 
Compress(SvStream & rIStm,SvStream & rOStm)137cdf0e10cSrcweir long ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	long nOldTotal_In = PZSTREAM->total_in;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	if ( mbInit == 0 )
142cdf0e10cSrcweir 	{
143cdf0e10cSrcweir 		mpIStm = &rIStm;
144cdf0e10cSrcweir 		mpOStm = &rOStm;
145cdf0e10cSrcweir 		ImplInitBuf( sal_False );
146cdf0e10cSrcweir 		mpInBuf = new sal_uInt8[ mnInBufSize ];
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 	while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 )
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		if ( PZSTREAM->avail_out == 0 )
151cdf0e10cSrcweir 			ImplWriteBack();
152cdf0e10cSrcweir 		if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
153cdf0e10cSrcweir 		{
154cdf0e10cSrcweir 			mbStatus = sal_False;
155cdf0e10cSrcweir 			break;
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 	};
158cdf0e10cSrcweir 	return ( mbStatus ) ? (long)(PZSTREAM->total_in - nOldTotal_In) : -1;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir // ------------------------------------------------------------------------
162cdf0e10cSrcweir 
Decompress(SvStream & rIStm,SvStream & rOStm)163cdf0e10cSrcweir long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir 	int err;
166cdf0e10cSrcweir 	sal_uIntPtr	nInToRead;
167cdf0e10cSrcweir 	long	nOldTotal_Out = PZSTREAM->total_out;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	if ( mbFinish )
170cdf0e10cSrcweir 		return PZSTREAM->total_out - nOldTotal_Out;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	if ( mbInit == 0 )
173cdf0e10cSrcweir 	{
174cdf0e10cSrcweir 		mpIStm = &rIStm;
175cdf0e10cSrcweir 		mpOStm = &rOStm;
176cdf0e10cSrcweir 		ImplInitBuf( sal_True );
177cdf0e10cSrcweir 		PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 	do
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		if ( PZSTREAM->avail_out == 0 ) ImplWriteBack();
182cdf0e10cSrcweir 		if ( PZSTREAM->avail_in == 0 && mnInToRead )
183cdf0e10cSrcweir 		{
184cdf0e10cSrcweir 			nInToRead = ( mnInBufSize > mnInToRead ) ? mnInToRead : mnInBufSize;
185cdf0e10cSrcweir 			PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, nInToRead );
186cdf0e10cSrcweir 			mnInToRead -= nInToRead;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
189cdf0e10cSrcweir 				mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		err = inflate( PZSTREAM, Z_NO_FLUSH );
193cdf0e10cSrcweir 		if ( err < 0 )
194cdf0e10cSrcweir 		{
195cdf0e10cSrcweir 			mbStatus = sal_False;
196cdf0e10cSrcweir 			break;
197cdf0e10cSrcweir 		}
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 	while ( ( err != Z_STREAM_END) && ( PZSTREAM->avail_in || mnInToRead ) );
201cdf0e10cSrcweir 	ImplWriteBack();
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	if ( err == Z_STREAM_END )
204cdf0e10cSrcweir 		mbFinish = sal_True;
205cdf0e10cSrcweir 	return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1;
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir // ------------------------------------------------------------------------
209cdf0e10cSrcweir 
Write(SvStream & rOStm,const sal_uInt8 * pData,sal_uIntPtr nSize)210cdf0e10cSrcweir long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir 	if ( mbInit == 0 )
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 		mpOStm = &rOStm;
215cdf0e10cSrcweir 		ImplInitBuf( sal_False );
216cdf0e10cSrcweir 	}
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	PZSTREAM->avail_in = nSize;
219cdf0e10cSrcweir 	PZSTREAM->next_in = (unsigned char*)pData;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	while ( PZSTREAM->avail_in || ( PZSTREAM->avail_out == 0 ) )
222cdf0e10cSrcweir 	{
223cdf0e10cSrcweir 		if ( PZSTREAM->avail_out == 0 )
224cdf0e10cSrcweir 			ImplWriteBack();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 		if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
227cdf0e10cSrcweir 		{
228cdf0e10cSrcweir 			mbStatus = sal_False;
229cdf0e10cSrcweir 			break;
230cdf0e10cSrcweir 		}
231cdf0e10cSrcweir 	}
232cdf0e10cSrcweir 	return ( mbStatus ) ? (long)nSize : -1;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir // ------------------------------------------------------------------------
236cdf0e10cSrcweir 
Read(SvStream & rIStm,sal_uInt8 * pData,sal_uIntPtr nSize)237cdf0e10cSrcweir long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	int err;
240cdf0e10cSrcweir 	sal_uIntPtr	nInToRead;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	if ( mbFinish )
243cdf0e10cSrcweir 		return 0;			// PZSTREAM->total_out;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	mpIStm = &rIStm;
246cdf0e10cSrcweir 	if ( mbInit == 0 )
247cdf0e10cSrcweir 	{
248cdf0e10cSrcweir 		ImplInitBuf( sal_True );
249cdf0e10cSrcweir 	}
250cdf0e10cSrcweir 	PZSTREAM->avail_out = nSize;
251cdf0e10cSrcweir 	PZSTREAM->next_out = pData;
252cdf0e10cSrcweir 	do
253cdf0e10cSrcweir 	{
254cdf0e10cSrcweir 		if ( PZSTREAM->avail_in == 0 && mnInToRead )
255cdf0e10cSrcweir 		{
256cdf0e10cSrcweir 			nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
257cdf0e10cSrcweir 			PZSTREAM->avail_in = mpIStm->Read (
258cdf0e10cSrcweir 				PZSTREAM->next_in = mpInBuf, nInToRead);
259cdf0e10cSrcweir 			mnInToRead -= nInToRead;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 			if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
262cdf0e10cSrcweir 				mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 		}
265cdf0e10cSrcweir 		err = inflate( PZSTREAM, Z_NO_FLUSH );
266cdf0e10cSrcweir 		if ( err < 0 )
267cdf0e10cSrcweir 		{
268cdf0e10cSrcweir 			// Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
269cdf0e10cSrcweir 			mbStatus = (err == Z_BUF_ERROR);
270cdf0e10cSrcweir 			break;
271cdf0e10cSrcweir 		}
272cdf0e10cSrcweir 	}
273cdf0e10cSrcweir 	while ( (err != Z_STREAM_END) &&
274cdf0e10cSrcweir 			(PZSTREAM->avail_out != 0) &&
275cdf0e10cSrcweir 			(PZSTREAM->avail_in || mnInToRead) );
276cdf0e10cSrcweir 	if ( err == Z_STREAM_END )
277cdf0e10cSrcweir 		mbFinish = sal_True;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir // ------------------------------------------------------------------------
283cdf0e10cSrcweir 
ReadAsynchron(SvStream & rIStm,sal_uInt8 * pData,sal_uIntPtr nSize)284cdf0e10cSrcweir long ZCodec::ReadAsynchron( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
285cdf0e10cSrcweir {
286cdf0e10cSrcweir 	int err = 0;
287cdf0e10cSrcweir 	sal_uIntPtr	nInToRead;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	if ( mbFinish )
290cdf0e10cSrcweir 		return 0;			// PZSTREAM->total_out;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 	if ( mbInit == 0 )
293cdf0e10cSrcweir 	{
294cdf0e10cSrcweir 		mpIStm = &rIStm;
295cdf0e10cSrcweir 		ImplInitBuf( sal_True );
296cdf0e10cSrcweir 	}
297cdf0e10cSrcweir 	PZSTREAM->avail_out = nSize;
298cdf0e10cSrcweir 	PZSTREAM->next_out = pData;
299cdf0e10cSrcweir 	do
300cdf0e10cSrcweir 	{
301cdf0e10cSrcweir 		if ( PZSTREAM->avail_in == 0 && mnInToRead )
302cdf0e10cSrcweir 		{
303cdf0e10cSrcweir 			nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 			sal_uIntPtr nStreamPos = rIStm.Tell();
306cdf0e10cSrcweir 			rIStm.Seek( STREAM_SEEK_TO_END );
307cdf0e10cSrcweir 			sal_uIntPtr nMaxPos = rIStm.Tell();
308cdf0e10cSrcweir 			rIStm.Seek( nStreamPos );
309cdf0e10cSrcweir 			if ( ( nMaxPos - nStreamPos ) < nInToRead )
310cdf0e10cSrcweir 			{
311cdf0e10cSrcweir 				rIStm.SetError( ERRCODE_IO_PENDING );
312cdf0e10cSrcweir 				err= ! Z_STREAM_END; // TODO What is appropriate code for this?
313cdf0e10cSrcweir 				break;
314cdf0e10cSrcweir 			}
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 			PZSTREAM->avail_in = mpIStm->Read (
317cdf0e10cSrcweir 				PZSTREAM->next_in = mpInBuf, nInToRead);
318cdf0e10cSrcweir 			mnInToRead -= nInToRead;
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 			if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
321cdf0e10cSrcweir 				mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		}
324cdf0e10cSrcweir 		err = inflate( PZSTREAM, Z_NO_FLUSH );
325cdf0e10cSrcweir 		if ( err < 0 )
326cdf0e10cSrcweir 		{
327cdf0e10cSrcweir 			// Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
328cdf0e10cSrcweir 			mbStatus = (err == Z_BUF_ERROR);
329cdf0e10cSrcweir 			break;
330cdf0e10cSrcweir 		}
331cdf0e10cSrcweir 	}
332cdf0e10cSrcweir 	while ( (err != Z_STREAM_END) &&
333cdf0e10cSrcweir 			(PZSTREAM->avail_out != 0) &&
334cdf0e10cSrcweir 			(PZSTREAM->avail_in || mnInToRead) );
335cdf0e10cSrcweir 	if ( err == Z_STREAM_END )
336cdf0e10cSrcweir 		mbFinish = sal_True;
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 	return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
339cdf0e10cSrcweir }
340cdf0e10cSrcweir 
341cdf0e10cSrcweir // ------------------------------------------------------------------------
342cdf0e10cSrcweir 
ImplWriteBack()343cdf0e10cSrcweir void ZCodec::ImplWriteBack()
344cdf0e10cSrcweir {
345cdf0e10cSrcweir 	sal_uIntPtr nAvail = mnOutBufSize - PZSTREAM->avail_out;
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	if ( nAvail )
348cdf0e10cSrcweir 	{
349cdf0e10cSrcweir 		if ( mbInit & 2 && ( mnCompressMethod & ZCODEC_UPDATE_CRC ) )
350cdf0e10cSrcweir 			mnCRC = UpdateCRC( mnCRC, mpOutBuf, nAvail );
351cdf0e10cSrcweir 		mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail );
352cdf0e10cSrcweir 		PZSTREAM->avail_out = mnOutBufSize;
353cdf0e10cSrcweir 	}
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir // ------------------------------------------------------------------------
357cdf0e10cSrcweir 
SetBreak(sal_uIntPtr nInToRead)358cdf0e10cSrcweir void ZCodec::SetBreak( sal_uIntPtr nInToRead )
359cdf0e10cSrcweir {
360cdf0e10cSrcweir 	mnInToRead = nInToRead;
361cdf0e10cSrcweir }
362cdf0e10cSrcweir 
363cdf0e10cSrcweir // ------------------------------------------------------------------------
364cdf0e10cSrcweir 
GetBreak(void)365cdf0e10cSrcweir sal_uIntPtr ZCodec::GetBreak( void )
366cdf0e10cSrcweir {
367cdf0e10cSrcweir 	return ( mnInToRead + PZSTREAM->avail_in );
368cdf0e10cSrcweir }
369cdf0e10cSrcweir 
370cdf0e10cSrcweir // ------------------------------------------------------------------------
371cdf0e10cSrcweir 
SetCRC(sal_uIntPtr nCRC)372cdf0e10cSrcweir void ZCodec::SetCRC( sal_uIntPtr nCRC )
373cdf0e10cSrcweir {
374cdf0e10cSrcweir 	mnCRC = nCRC;
375cdf0e10cSrcweir }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir // ------------------------------------------------------------------------
378cdf0e10cSrcweir 
GetCRC()379cdf0e10cSrcweir sal_uIntPtr ZCodec::GetCRC()
380cdf0e10cSrcweir {
381cdf0e10cSrcweir 	return mnCRC;
382cdf0e10cSrcweir }
383cdf0e10cSrcweir 
384cdf0e10cSrcweir // ------------------------------------------------------------------------
385cdf0e10cSrcweir 
ImplInitBuf(sal_Bool nIOFlag)386cdf0e10cSrcweir void ZCodec::ImplInitBuf ( sal_Bool nIOFlag )
387cdf0e10cSrcweir {
388cdf0e10cSrcweir 	if ( mbInit == 0 )
389cdf0e10cSrcweir 	{
390cdf0e10cSrcweir 		if ( nIOFlag )
391cdf0e10cSrcweir 		{
392cdf0e10cSrcweir 			mbInit = 1;
393cdf0e10cSrcweir 			if ( mbStatus && ( mnCompressMethod & ZCODEC_GZ_LIB ) )
394cdf0e10cSrcweir 			{
395cdf0e10cSrcweir 				sal_uInt8 n1, n2, j, nMethod, nFlags;
396cdf0e10cSrcweir 				for ( int i = 0; i < 2; i++ )	// gz - magic number
397cdf0e10cSrcweir 				{
398cdf0e10cSrcweir 					*mpIStm >> j;
399cdf0e10cSrcweir 					if ( j != gz_magic[ i ] )
400cdf0e10cSrcweir 						mbStatus = sal_False;
401cdf0e10cSrcweir 				}
402cdf0e10cSrcweir 				*mpIStm >> nMethod;
403cdf0e10cSrcweir 				*mpIStm >> nFlags;
404cdf0e10cSrcweir 				if ( nMethod != Z_DEFLATED )
405cdf0e10cSrcweir 					mbStatus = sal_False;
406cdf0e10cSrcweir 				if ( ( nFlags & GZ_RESERVED ) != 0 )
407cdf0e10cSrcweir 					mbStatus = sal_False;
408cdf0e10cSrcweir 				/* Discard time, xflags and OS code: */
409cdf0e10cSrcweir 				mpIStm->SeekRel( 6 );
410cdf0e10cSrcweir 				/* skip the extra field */
411cdf0e10cSrcweir 				if ( nFlags & GZ_EXTRA_FIELD )
412cdf0e10cSrcweir 				{
413cdf0e10cSrcweir 					*mpIStm >> n1 >> n2;
414cdf0e10cSrcweir 					mpIStm->SeekRel( n1 + ( n2 << 8 ) );
415cdf0e10cSrcweir 				}
416cdf0e10cSrcweir 				/* skip the original file name */
417cdf0e10cSrcweir 				if ( nFlags & GZ_ORIG_NAME)
418cdf0e10cSrcweir 				{
419cdf0e10cSrcweir 					do
420cdf0e10cSrcweir 					{
421cdf0e10cSrcweir 						*mpIStm >> j;
422cdf0e10cSrcweir 					}
423cdf0e10cSrcweir 					while ( j && !mpIStm->IsEof() );
424cdf0e10cSrcweir 				}
425cdf0e10cSrcweir 				/* skip the .gz file comment */
426cdf0e10cSrcweir 				if ( nFlags & GZ_COMMENT )
427cdf0e10cSrcweir 				{
428cdf0e10cSrcweir 					do
429cdf0e10cSrcweir 					{
430cdf0e10cSrcweir 						*mpIStm >> j;
431cdf0e10cSrcweir 					}
432cdf0e10cSrcweir 					while ( j && !mpIStm->IsEof() );
433cdf0e10cSrcweir 				}
434cdf0e10cSrcweir 				/* skip the header crc */
435cdf0e10cSrcweir 				if ( nFlags & GZ_HEAD_CRC )
436cdf0e10cSrcweir 					mpIStm->SeekRel( 2 );
437cdf0e10cSrcweir 				if ( mbStatus )
438cdf0e10cSrcweir 					mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? sal_False : sal_True;
439cdf0e10cSrcweir 			}
440cdf0e10cSrcweir 			else
441cdf0e10cSrcweir 			{
442cdf0e10cSrcweir 				mbStatus = ( inflateInit( PZSTREAM ) >= 0 );
443cdf0e10cSrcweir 			}
444cdf0e10cSrcweir 			mpInBuf = new sal_uInt8[ mnInBufSize ];
445cdf0e10cSrcweir 		}
446cdf0e10cSrcweir 		else
447cdf0e10cSrcweir 		{
448cdf0e10cSrcweir 			mbInit = 3;
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 			mbStatus = ( deflateInit2_( PZSTREAM, mnCompressMethod & 0xff, Z_DEFLATED,
451cdf0e10cSrcweir 				MAX_WBITS, mnMemUsage, ( mnCompressMethod >> 8 ) & 0xff,
452cdf0e10cSrcweir 					ZLIB_VERSION, sizeof( z_stream ) ) >= 0 );
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 			PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
455cdf0e10cSrcweir 		}
456cdf0e10cSrcweir 	}
457cdf0e10cSrcweir }
458cdf0e10cSrcweir 
459cdf0e10cSrcweir // ------------------------------------------------------------------------
460cdf0e10cSrcweir 
UpdateCRC(sal_uIntPtr nLatestCRC,sal_uIntPtr nNumber)461cdf0e10cSrcweir sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uIntPtr nNumber )
462cdf0e10cSrcweir {
463cdf0e10cSrcweir 
464cdf0e10cSrcweir #ifdef OSL_LITENDIAN
465cdf0e10cSrcweir 	nNumber = SWAPLONG( nNumber );
466cdf0e10cSrcweir #endif
467cdf0e10cSrcweir 	return rtl_crc32( nLatestCRC, &nNumber, 4 );
468cdf0e10cSrcweir }
469cdf0e10cSrcweir 
470cdf0e10cSrcweir // ------------------------------------------------------------------------
471cdf0e10cSrcweir 
UpdateCRC(sal_uIntPtr nLatestCRC,sal_uInt8 * pSource,long nDatSize)472cdf0e10cSrcweir sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uInt8* pSource, long nDatSize)
473cdf0e10cSrcweir {
474cdf0e10cSrcweir 	return rtl_crc32( nLatestCRC, pSource, nDatSize );
475cdf0e10cSrcweir }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir // ------------------------------------------------------------------------
478cdf0e10cSrcweir 
BeginCompression(sal_uIntPtr nCompressMethod)479cdf0e10cSrcweir void GZCodec::BeginCompression( sal_uIntPtr nCompressMethod )
480cdf0e10cSrcweir {
481cdf0e10cSrcweir 	ZCodec::BeginCompression( nCompressMethod | ZCODEC_GZ_LIB );
482cdf0e10cSrcweir };
483