1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Inflater.cxx,v $
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>
38 #include <external/zlib/zlib.h>
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
),
56 pStream
= new z_stream
;
57 /* memset to 0 to set zalloc/opaque etc */
58 memset (pStream
, 0, sizeof(*pStream
));
60 nRes
= inflateInit2(pStream
, bNoWrap
? -MAX_WBITS
: MAX_WBITS
);
81 void SAL_CALL
Inflater::setInput( const Sequence
< sal_Int8
>& rBuffer
)
85 nLength
= rBuffer
.getLength();
88 sal_Bool SAL_CALL
Inflater::needsDictionary( )
93 sal_Bool SAL_CALL
Inflater::finished( )
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())
104 return doInflateBytes(rBuffer
, nNewOffset
, nNewLength
);
107 void SAL_CALL
Inflater::end( )
114 z_inflateEnd(pStream
);
121 sal_Int32
Inflater::doInflateBytes (Sequence
< sal_Int8
> &rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
125 nLastInflateError
= Z_STREAM_ERROR
;
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
;
137 sal_Int32 nResult
= ::inflate(pStream
, Z_PARTIAL_FLUSH
);
139 sal_Int32 nResult
= ::z_inflate(pStream
, Z_PARTIAL_FLUSH
);
145 bFinished
= sal_True
;
147 nOffset
+= nLength
- pStream
->avail_in
;
148 nLength
= pStream
->avail_in
;
149 return nNewLength
- pStream
->avail_out
;
152 bNeedDict
= sal_True
;
153 nOffset
+= nLength
- pStream
->avail_in
;
154 nLength
= pStream
->avail_in
;
158 // it is no error, if there is no input or no output
159 if ( nLength
&& nNewLength
)
160 nLastInflateError
= nResult
;