xref: /aoo41x/main/package/source/zipapi/Inflater.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_package.hxx"
30*cdf0e10cSrcweir #include <Inflater.hxx>
31*cdf0e10cSrcweir #ifndef _ZLIB_H
32*cdf0e10cSrcweir #ifdef SYSTEM_ZLIB
33*cdf0e10cSrcweir #include <zlib.h>
34*cdf0e10cSrcweir #else
35*cdf0e10cSrcweir #include <external/zlib/zlib.h>
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir #include <string.h> // for memset
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace com::sun::star::uno;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /** Provides general purpose decompression using the ZLIB library */
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir Inflater::Inflater(sal_Bool bNoWrap)
45*cdf0e10cSrcweir : bFinished(sal_False),
46*cdf0e10cSrcweir   bSetParams(sal_False),
47*cdf0e10cSrcweir   bNeedDict(sal_False),
48*cdf0e10cSrcweir   nOffset(0),
49*cdf0e10cSrcweir   nLength(0),
50*cdf0e10cSrcweir   nLastInflateError(0),
51*cdf0e10cSrcweir   pStream(NULL)
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	pStream = new z_stream;
54*cdf0e10cSrcweir 	/* memset to 0 to set zalloc/opaque etc */
55*cdf0e10cSrcweir 	memset (pStream, 0, sizeof(*pStream));
56*cdf0e10cSrcweir 	sal_Int32 nRes;
57*cdf0e10cSrcweir 	nRes = inflateInit2(pStream, bNoWrap ? -MAX_WBITS : MAX_WBITS);
58*cdf0e10cSrcweir 	switch (nRes)
59*cdf0e10cSrcweir 	{
60*cdf0e10cSrcweir 		case Z_OK:
61*cdf0e10cSrcweir 			break;
62*cdf0e10cSrcweir 		case Z_MEM_ERROR:
63*cdf0e10cSrcweir 			delete pStream;
64*cdf0e10cSrcweir 			break;
65*cdf0e10cSrcweir 		case Z_STREAM_ERROR:
66*cdf0e10cSrcweir 			delete pStream;
67*cdf0e10cSrcweir 			break;
68*cdf0e10cSrcweir 		default:
69*cdf0e10cSrcweir 			break;
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir Inflater::~Inflater()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	end();
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir void SAL_CALL Inflater::setInput( const Sequence< sal_Int8 >& rBuffer )
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	sInBuffer = rBuffer;
81*cdf0e10cSrcweir 	nOffset = 0;
82*cdf0e10cSrcweir 	nLength = rBuffer.getLength();
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir sal_Bool SAL_CALL Inflater::needsDictionary(  )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir 	return bNeedDict;
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir sal_Bool SAL_CALL Inflater::finished(  )
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	return bFinished;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir sal_Int32 SAL_CALL Inflater::doInflateSegment( Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength())
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		// do error handling
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir 	return doInflateBytes(rBuffer, nNewOffset, nNewLength);
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir void SAL_CALL Inflater::end(  )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir 	if (pStream != NULL)
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
109*cdf0e10cSrcweir 		inflateEnd(pStream);
110*cdf0e10cSrcweir #else
111*cdf0e10cSrcweir 		z_inflateEnd(pStream);
112*cdf0e10cSrcweir #endif
113*cdf0e10cSrcweir 		delete pStream;
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir 	pStream = NULL;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 >  &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir     if ( !pStream )
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         nLastInflateError = Z_STREAM_ERROR;
123*cdf0e10cSrcweir         return 0;
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     nLastInflateError = 0;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	pStream->next_in   = ( unsigned char* ) ( sInBuffer.getConstArray() + nOffset );
129*cdf0e10cSrcweir 	pStream->avail_in  = nLength;
130*cdf0e10cSrcweir 	pStream->next_out  = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset );
131*cdf0e10cSrcweir 	pStream->avail_out = nNewLength;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
134*cdf0e10cSrcweir 	sal_Int32 nResult = ::inflate(pStream, Z_PARTIAL_FLUSH);
135*cdf0e10cSrcweir #else
136*cdf0e10cSrcweir 	sal_Int32 nResult = ::z_inflate(pStream, Z_PARTIAL_FLUSH);
137*cdf0e10cSrcweir #endif
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	switch (nResult)
140*cdf0e10cSrcweir 	{
141*cdf0e10cSrcweir 		case Z_STREAM_END:
142*cdf0e10cSrcweir 			bFinished = sal_True;
143*cdf0e10cSrcweir 		case Z_OK:
144*cdf0e10cSrcweir 			nOffset += nLength - pStream->avail_in;
145*cdf0e10cSrcweir 			nLength = pStream->avail_in;
146*cdf0e10cSrcweir 			return nNewLength - pStream->avail_out;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 		case Z_NEED_DICT:
149*cdf0e10cSrcweir 			bNeedDict = sal_True;
150*cdf0e10cSrcweir 			nOffset += nLength - pStream->avail_in;
151*cdf0e10cSrcweir 			nLength = pStream->avail_in;
152*cdf0e10cSrcweir             return 0;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir         default:
155*cdf0e10cSrcweir             // it is no error, if there is no input or no output
156*cdf0e10cSrcweir             if ( nLength && nNewLength )
157*cdf0e10cSrcweir                 nLastInflateError = nResult;
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	return 0;
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163