1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_package.hxx"
26*b1cdbd2cSJim Jagielski #include <Inflater.hxx>
27*b1cdbd2cSJim Jagielski #ifndef _ZLIB_H
28*b1cdbd2cSJim Jagielski #ifdef SYSTEM_ZLIB
29*b1cdbd2cSJim Jagielski #include <zlib.h>
30*b1cdbd2cSJim Jagielski #else
31*b1cdbd2cSJim Jagielski #include <external/zlib/zlib.h>
32*b1cdbd2cSJim Jagielski #endif
33*b1cdbd2cSJim Jagielski #endif
34*b1cdbd2cSJim Jagielski #include <string.h> // for memset
35*b1cdbd2cSJim Jagielski
36*b1cdbd2cSJim Jagielski using namespace com::sun::star::uno;
37*b1cdbd2cSJim Jagielski
38*b1cdbd2cSJim Jagielski /** Provides general purpose decompression using the ZLIB library */
39*b1cdbd2cSJim Jagielski
Inflater(sal_Bool bNoWrap)40*b1cdbd2cSJim Jagielski Inflater::Inflater(sal_Bool bNoWrap)
41*b1cdbd2cSJim Jagielski : bFinished(sal_False),
42*b1cdbd2cSJim Jagielski bSetParams(sal_False),
43*b1cdbd2cSJim Jagielski bNeedDict(sal_False),
44*b1cdbd2cSJim Jagielski nOffset(0),
45*b1cdbd2cSJim Jagielski nLength(0),
46*b1cdbd2cSJim Jagielski nLastInflateError(0),
47*b1cdbd2cSJim Jagielski pStream(NULL)
48*b1cdbd2cSJim Jagielski {
49*b1cdbd2cSJim Jagielski pStream = new z_stream;
50*b1cdbd2cSJim Jagielski /* memset to 0 to set zalloc/opaque etc */
51*b1cdbd2cSJim Jagielski memset (pStream, 0, sizeof(*pStream));
52*b1cdbd2cSJim Jagielski sal_Int32 nRes;
53*b1cdbd2cSJim Jagielski nRes = inflateInit2(pStream, bNoWrap ? -MAX_WBITS : MAX_WBITS);
54*b1cdbd2cSJim Jagielski switch (nRes)
55*b1cdbd2cSJim Jagielski {
56*b1cdbd2cSJim Jagielski case Z_OK:
57*b1cdbd2cSJim Jagielski break;
58*b1cdbd2cSJim Jagielski case Z_MEM_ERROR:
59*b1cdbd2cSJim Jagielski delete pStream;
60*b1cdbd2cSJim Jagielski break;
61*b1cdbd2cSJim Jagielski case Z_STREAM_ERROR:
62*b1cdbd2cSJim Jagielski delete pStream;
63*b1cdbd2cSJim Jagielski break;
64*b1cdbd2cSJim Jagielski default:
65*b1cdbd2cSJim Jagielski break;
66*b1cdbd2cSJim Jagielski }
67*b1cdbd2cSJim Jagielski }
68*b1cdbd2cSJim Jagielski
~Inflater()69*b1cdbd2cSJim Jagielski Inflater::~Inflater()
70*b1cdbd2cSJim Jagielski {
71*b1cdbd2cSJim Jagielski end();
72*b1cdbd2cSJim Jagielski }
73*b1cdbd2cSJim Jagielski
setInput(const Sequence<sal_Int8> & rBuffer)74*b1cdbd2cSJim Jagielski void SAL_CALL Inflater::setInput( const Sequence< sal_Int8 >& rBuffer )
75*b1cdbd2cSJim Jagielski {
76*b1cdbd2cSJim Jagielski sInBuffer = rBuffer;
77*b1cdbd2cSJim Jagielski nOffset = 0;
78*b1cdbd2cSJim Jagielski nLength = rBuffer.getLength();
79*b1cdbd2cSJim Jagielski }
80*b1cdbd2cSJim Jagielski
needsDictionary()81*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL Inflater::needsDictionary( )
82*b1cdbd2cSJim Jagielski {
83*b1cdbd2cSJim Jagielski return bNeedDict;
84*b1cdbd2cSJim Jagielski }
85*b1cdbd2cSJim Jagielski
finished()86*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL Inflater::finished( )
87*b1cdbd2cSJim Jagielski {
88*b1cdbd2cSJim Jagielski return bFinished;
89*b1cdbd2cSJim Jagielski }
90*b1cdbd2cSJim Jagielski
doInflateSegment(Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)91*b1cdbd2cSJim Jagielski sal_Int32 SAL_CALL Inflater::doInflateSegment( Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
92*b1cdbd2cSJim Jagielski {
93*b1cdbd2cSJim Jagielski if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength())
94*b1cdbd2cSJim Jagielski {
95*b1cdbd2cSJim Jagielski // do error handling
96*b1cdbd2cSJim Jagielski }
97*b1cdbd2cSJim Jagielski return doInflateBytes(rBuffer, nNewOffset, nNewLength);
98*b1cdbd2cSJim Jagielski }
99*b1cdbd2cSJim Jagielski
end()100*b1cdbd2cSJim Jagielski void SAL_CALL Inflater::end( )
101*b1cdbd2cSJim Jagielski {
102*b1cdbd2cSJim Jagielski if (pStream != NULL)
103*b1cdbd2cSJim Jagielski {
104*b1cdbd2cSJim Jagielski #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
105*b1cdbd2cSJim Jagielski inflateEnd(pStream);
106*b1cdbd2cSJim Jagielski #else
107*b1cdbd2cSJim Jagielski z_inflateEnd(pStream);
108*b1cdbd2cSJim Jagielski #endif
109*b1cdbd2cSJim Jagielski delete pStream;
110*b1cdbd2cSJim Jagielski }
111*b1cdbd2cSJim Jagielski pStream = NULL;
112*b1cdbd2cSJim Jagielski }
113*b1cdbd2cSJim Jagielski
doInflateBytes(Sequence<sal_Int8> & rBuffer,sal_Int32 nNewOffset,sal_Int32 nNewLength)114*b1cdbd2cSJim Jagielski sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
115*b1cdbd2cSJim Jagielski {
116*b1cdbd2cSJim Jagielski if ( !pStream )
117*b1cdbd2cSJim Jagielski {
118*b1cdbd2cSJim Jagielski nLastInflateError = Z_STREAM_ERROR;
119*b1cdbd2cSJim Jagielski return 0;
120*b1cdbd2cSJim Jagielski }
121*b1cdbd2cSJim Jagielski
122*b1cdbd2cSJim Jagielski nLastInflateError = 0;
123*b1cdbd2cSJim Jagielski
124*b1cdbd2cSJim Jagielski pStream->next_in = ( unsigned char* ) ( sInBuffer.getConstArray() + nOffset );
125*b1cdbd2cSJim Jagielski pStream->avail_in = nLength;
126*b1cdbd2cSJim Jagielski pStream->next_out = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset );
127*b1cdbd2cSJim Jagielski pStream->avail_out = nNewLength;
128*b1cdbd2cSJim Jagielski
129*b1cdbd2cSJim Jagielski #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
130*b1cdbd2cSJim Jagielski sal_Int32 nResult = ::inflate(pStream, Z_PARTIAL_FLUSH);
131*b1cdbd2cSJim Jagielski #else
132*b1cdbd2cSJim Jagielski sal_Int32 nResult = ::z_inflate(pStream, Z_PARTIAL_FLUSH);
133*b1cdbd2cSJim Jagielski #endif
134*b1cdbd2cSJim Jagielski
135*b1cdbd2cSJim Jagielski switch (nResult)
136*b1cdbd2cSJim Jagielski {
137*b1cdbd2cSJim Jagielski case Z_STREAM_END:
138*b1cdbd2cSJim Jagielski bFinished = sal_True;
139*b1cdbd2cSJim Jagielski case Z_OK:
140*b1cdbd2cSJim Jagielski nOffset += nLength - pStream->avail_in;
141*b1cdbd2cSJim Jagielski nLength = pStream->avail_in;
142*b1cdbd2cSJim Jagielski return nNewLength - pStream->avail_out;
143*b1cdbd2cSJim Jagielski
144*b1cdbd2cSJim Jagielski case Z_NEED_DICT:
145*b1cdbd2cSJim Jagielski bNeedDict = sal_True;
146*b1cdbd2cSJim Jagielski nOffset += nLength - pStream->avail_in;
147*b1cdbd2cSJim Jagielski nLength = pStream->avail_in;
148*b1cdbd2cSJim Jagielski return 0;
149*b1cdbd2cSJim Jagielski
150*b1cdbd2cSJim Jagielski default:
151*b1cdbd2cSJim Jagielski // it is no error, if there is no input or no output
152*b1cdbd2cSJim Jagielski if ( nLength && nNewLength )
153*b1cdbd2cSJim Jagielski nLastInflateError = nResult;
154*b1cdbd2cSJim Jagielski }
155*b1cdbd2cSJim Jagielski
156*b1cdbd2cSJim Jagielski return 0;
157*b1cdbd2cSJim Jagielski }
158*b1cdbd2cSJim Jagielski
159