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