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