xref: /aoo41x/main/package/source/zipapi/Inflater.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 <Inflater.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 <string.h> // for memset
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace com::sun::star::uno;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /** Provides general purpose decompression using the ZLIB library */
39cdf0e10cSrcweir 
Inflater(sal_Bool bNoWrap)40cdf0e10cSrcweir Inflater::Inflater(sal_Bool bNoWrap)
41cdf0e10cSrcweir : bFinished(sal_False),
42cdf0e10cSrcweir   bSetParams(sal_False),
43cdf0e10cSrcweir   bNeedDict(sal_False),
44cdf0e10cSrcweir   nOffset(0),
45cdf0e10cSrcweir   nLength(0),
46cdf0e10cSrcweir   nLastInflateError(0),
47cdf0e10cSrcweir   pStream(NULL)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	pStream = new z_stream;
50cdf0e10cSrcweir 	/* memset to 0 to set zalloc/opaque etc */
51cdf0e10cSrcweir 	memset (pStream, 0, sizeof(*pStream));
52cdf0e10cSrcweir 	sal_Int32 nRes;
53cdf0e10cSrcweir 	nRes = inflateInit2(pStream, bNoWrap ? -MAX_WBITS : MAX_WBITS);
54cdf0e10cSrcweir 	switch (nRes)
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		case Z_OK:
57cdf0e10cSrcweir 			break;
58cdf0e10cSrcweir 		case Z_MEM_ERROR:
59cdf0e10cSrcweir 			delete pStream;
60cdf0e10cSrcweir 			break;
61cdf0e10cSrcweir 		case Z_STREAM_ERROR:
62cdf0e10cSrcweir 			delete pStream;
63cdf0e10cSrcweir 			break;
64cdf0e10cSrcweir 		default:
65cdf0e10cSrcweir 			break;
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
~Inflater()69cdf0e10cSrcweir Inflater::~Inflater()
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	end();
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
setInput(const Sequence<sal_Int8> & rBuffer)74cdf0e10cSrcweir void SAL_CALL Inflater::setInput( const Sequence< sal_Int8 >& rBuffer )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	sInBuffer = rBuffer;
77cdf0e10cSrcweir 	nOffset = 0;
78cdf0e10cSrcweir 	nLength = rBuffer.getLength();
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
needsDictionary()81cdf0e10cSrcweir sal_Bool SAL_CALL Inflater::needsDictionary(  )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir 	return bNeedDict;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
finished()86cdf0e10cSrcweir sal_Bool SAL_CALL Inflater::finished(  )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	return bFinished;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
doInflateSegment(Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)91cdf0e10cSrcweir sal_Int32 SAL_CALL Inflater::doInflateSegment( Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength())
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		// do error handling
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir 	return doInflateBytes(rBuffer, nNewOffset, nNewLength);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
end()100cdf0e10cSrcweir void SAL_CALL Inflater::end(  )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	if (pStream != NULL)
103cdf0e10cSrcweir 	{
104cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
105cdf0e10cSrcweir 		inflateEnd(pStream);
106cdf0e10cSrcweir #else
107cdf0e10cSrcweir 		z_inflateEnd(pStream);
108cdf0e10cSrcweir #endif
109cdf0e10cSrcweir 		delete pStream;
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir 	pStream = NULL;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
doInflateBytes(Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)114cdf0e10cSrcweir sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 >  &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     if ( !pStream )
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         nLastInflateError = Z_STREAM_ERROR;
119cdf0e10cSrcweir         return 0;
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     nLastInflateError = 0;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	pStream->next_in   = ( unsigned char* ) ( sInBuffer.getConstArray() + nOffset );
125cdf0e10cSrcweir 	pStream->avail_in  = nLength;
126cdf0e10cSrcweir 	pStream->next_out  = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset );
127cdf0e10cSrcweir 	pStream->avail_out = nNewLength;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
130cdf0e10cSrcweir 	sal_Int32 nResult = ::inflate(pStream, Z_PARTIAL_FLUSH);
131cdf0e10cSrcweir #else
132cdf0e10cSrcweir 	sal_Int32 nResult = ::z_inflate(pStream, Z_PARTIAL_FLUSH);
133cdf0e10cSrcweir #endif
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	switch (nResult)
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		case Z_STREAM_END:
138cdf0e10cSrcweir 			bFinished = sal_True;
139cdf0e10cSrcweir 		case Z_OK:
140cdf0e10cSrcweir 			nOffset += nLength - pStream->avail_in;
141cdf0e10cSrcweir 			nLength = pStream->avail_in;
142cdf0e10cSrcweir 			return nNewLength - pStream->avail_out;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 		case Z_NEED_DICT:
145cdf0e10cSrcweir 			bNeedDict = sal_True;
146cdf0e10cSrcweir 			nOffset += nLength - pStream->avail_in;
147cdf0e10cSrcweir 			nLength = pStream->avail_in;
148cdf0e10cSrcweir             return 0;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         default:
151cdf0e10cSrcweir             // it is no error, if there is no input or no output
152cdf0e10cSrcweir             if ( nLength && nNewLength )
153cdf0e10cSrcweir                 nLastInflateError = nResult;
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	return 0;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159