xref: /aoo41x/main/package/source/zipapi/Deflater.cxx (revision a3872823)
1*a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a3872823SAndrew Rist  * distributed with this work for additional information
6*a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9*a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
13*a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a3872823SAndrew Rist  * software distributed under the License is distributed on an
15*a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a3872823SAndrew Rist  * specific language governing permissions and limitations
18*a3872823SAndrew Rist  * under the License.
19*a3872823SAndrew Rist  *
20*a3872823SAndrew Rist  *************************************************************/
21*a3872823SAndrew Rist 
22*a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <Deflater.hxx>
27cdf0e10cSrcweir #ifndef _ZLIB_H
28cdf0e10cSrcweir #ifdef SYSTEM_ZLIB
29cdf0e10cSrcweir #include <zlib.h>
30cdf0e10cSrcweir #else
31cdf0e10cSrcweir #include <external/zlib/zlib.h>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipConstants.hpp>
35cdf0e10cSrcweir #include <string.h> // for memset
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace com::sun::star::packages::zip::ZipConstants;
38cdf0e10cSrcweir using namespace com::sun::star;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /** Provides general purpose compression using the ZLIB compression
41cdf0e10cSrcweir  * library.
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir 
~Deflater(void)44cdf0e10cSrcweir Deflater::~Deflater(void)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	end();
47cdf0e10cSrcweir }
init(sal_Int32 nLevelArg,sal_Int32 nStrategyArg,sal_Bool bNowrap)48cdf0e10cSrcweir void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, sal_Bool bNowrap)
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	pStream = new z_stream;
51cdf0e10cSrcweir 	/* Memset it to 0...sets zalloc/zfree/opaque to NULL */
52cdf0e10cSrcweir 	memset (pStream, 0, sizeof(*pStream));
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
55cdf0e10cSrcweir 				DEF_MEM_LEVEL, nStrategyArg))
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		case Z_OK:
58cdf0e10cSrcweir 			break;
59cdf0e10cSrcweir 		case Z_MEM_ERROR:
60cdf0e10cSrcweir 			delete pStream;
61cdf0e10cSrcweir 			break;
62cdf0e10cSrcweir 		case Z_STREAM_ERROR:
63cdf0e10cSrcweir 			delete pStream;
64cdf0e10cSrcweir 			break;
65cdf0e10cSrcweir 		default:
66cdf0e10cSrcweir 			 break;
67cdf0e10cSrcweir 	}
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
Deflater(sal_Int32 nSetLevel,sal_Bool bNowrap)70cdf0e10cSrcweir Deflater::Deflater(sal_Int32 nSetLevel, sal_Bool bNowrap)
71cdf0e10cSrcweir : bFinish(sal_False)
72cdf0e10cSrcweir , bFinished(sal_False)
73cdf0e10cSrcweir , bSetParams(sal_False)
74cdf0e10cSrcweir , nLevel(nSetLevel)
75cdf0e10cSrcweir , nStrategy(DEFAULT_STRATEGY)
76cdf0e10cSrcweir , nOffset(0)
77cdf0e10cSrcweir , nLength(0)
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
doDeflateBytes(uno::Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)82cdf0e10cSrcweir sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	sal_Int32 nResult;
85cdf0e10cSrcweir 	if (bSetParams)
86cdf0e10cSrcweir 	{
87cdf0e10cSrcweir 		pStream->next_in   = (unsigned char*) sInBuffer.getConstArray() + nOffset;
88cdf0e10cSrcweir 		pStream->next_out  = (unsigned char*) rBuffer.getArray()+nNewOffset;
89cdf0e10cSrcweir 		pStream->avail_in  = nLength;
90cdf0e10cSrcweir 		pStream->avail_out = nNewLength;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
93cdf0e10cSrcweir 		nResult = deflateParams(pStream, nLevel, nStrategy);
94cdf0e10cSrcweir #else
95cdf0e10cSrcweir 		nResult = z_deflateParams(pStream, nLevel, nStrategy);
96cdf0e10cSrcweir #endif
97cdf0e10cSrcweir 		switch (nResult)
98cdf0e10cSrcweir 		{
99cdf0e10cSrcweir 			case Z_OK:
100cdf0e10cSrcweir 				bSetParams = sal_False;
101cdf0e10cSrcweir 				nOffset += nLength - pStream->avail_in;
102cdf0e10cSrcweir 				nLength = pStream->avail_in;
103cdf0e10cSrcweir 				return nNewLength - pStream->avail_out;
104cdf0e10cSrcweir 			case Z_BUF_ERROR:
105cdf0e10cSrcweir 				bSetParams = sal_False;
106cdf0e10cSrcweir 				return 0;
107cdf0e10cSrcweir 			default:
108cdf0e10cSrcweir 				return 0;
109cdf0e10cSrcweir 		}
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir 	else
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 		pStream->next_in   = (unsigned char*) sInBuffer.getConstArray() + nOffset;
114cdf0e10cSrcweir 		pStream->next_out  = (unsigned char*) rBuffer.getArray()+nNewOffset;
115cdf0e10cSrcweir 		pStream->avail_in  = nLength;
116cdf0e10cSrcweir 		pStream->avail_out = nNewLength;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
119cdf0e10cSrcweir 		nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
120cdf0e10cSrcweir #else
121cdf0e10cSrcweir 		nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
122cdf0e10cSrcweir #endif
123cdf0e10cSrcweir 		switch (nResult)
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			case Z_STREAM_END:
126cdf0e10cSrcweir 				bFinished = sal_True;
127cdf0e10cSrcweir 			case Z_OK:
128cdf0e10cSrcweir 				nOffset += nLength - pStream->avail_in;
129cdf0e10cSrcweir 				nLength = pStream->avail_in;
130cdf0e10cSrcweir 				return nNewLength - pStream->avail_out;
131cdf0e10cSrcweir 			case Z_BUF_ERROR:
132cdf0e10cSrcweir 				bSetParams = sal_False;
133cdf0e10cSrcweir 				return 0;
134cdf0e10cSrcweir 			default:
135cdf0e10cSrcweir 				return 0;
136cdf0e10cSrcweir 		}
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
setInputSegment(const uno::Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)140cdf0e10cSrcweir void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     sInBuffer = rBuffer;
145cdf0e10cSrcweir     nOffset = nNewOffset;
146cdf0e10cSrcweir     nLength = nNewLength;
147cdf0e10cSrcweir }
setLevel(sal_Int32 nNewLevel)148cdf0e10cSrcweir void SAL_CALL Deflater::setLevel( sal_Int32 nNewLevel )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 	if ((nNewLevel < 0 || nNewLevel > 9) && nNewLevel != DEFAULT_COMPRESSION)
151cdf0e10cSrcweir 	{
152cdf0e10cSrcweir 		// do error handling
153cdf0e10cSrcweir 	}
154cdf0e10cSrcweir 	if (nNewLevel != nLevel)
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir 		nLevel = nNewLevel;
157cdf0e10cSrcweir 		bSetParams = sal_True;
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir }
needsInput()160cdf0e10cSrcweir sal_Bool SAL_CALL Deflater::needsInput(  )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir 	return nLength <=0;
163cdf0e10cSrcweir }
finish()164cdf0e10cSrcweir void SAL_CALL Deflater::finish(  )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	bFinish = sal_True;
167cdf0e10cSrcweir }
finished()168cdf0e10cSrcweir sal_Bool SAL_CALL Deflater::finished(  )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	return bFinished;
171cdf0e10cSrcweir }
doDeflateSegment(uno::Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)172cdf0e10cSrcweir sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
175cdf0e10cSrcweir     return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
176cdf0e10cSrcweir }
getTotalIn()177cdf0e10cSrcweir sal_Int32 SAL_CALL Deflater::getTotalIn(  )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	return pStream->total_in;
180cdf0e10cSrcweir }
getTotalOut()181cdf0e10cSrcweir sal_Int32 SAL_CALL Deflater::getTotalOut(  )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir 	return pStream->total_out;
184cdf0e10cSrcweir }
reset()185cdf0e10cSrcweir void SAL_CALL Deflater::reset(  )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIXB
188cdf0e10cSrcweir 	deflateReset(pStream);
189cdf0e10cSrcweir #else
190cdf0e10cSrcweir 	z_deflateReset(pStream);
191cdf0e10cSrcweir #endif
192cdf0e10cSrcweir 	bFinish = sal_False;
193cdf0e10cSrcweir 	bFinished = sal_False;
194cdf0e10cSrcweir 	nOffset = nLength = 0;
195cdf0e10cSrcweir }
end()196cdf0e10cSrcweir void SAL_CALL Deflater::end(  )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir 	if (pStream != NULL)
199cdf0e10cSrcweir 	{
200cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
201cdf0e10cSrcweir 		deflateEnd(pStream);
202cdf0e10cSrcweir #else
203cdf0e10cSrcweir 		z_deflateEnd(pStream);
204cdf0e10cSrcweir #endif
205cdf0e10cSrcweir 		delete pStream;
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 	pStream = NULL;
208cdf0e10cSrcweir }
209