1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <package/Inflater.hxx>
22 #include <string.h> // for memset
24 using namespace com::sun::star::uno
;
25 using namespace ZipUtils
;
27 /** Provides general purpose decompression using the ZLIB library */
29 Inflater::Inflater(sal_Bool bNoWrap
)
30 : bFinished(sal_False
),
31 bSetParams(sal_False
),
38 pStream
= new z_stream
;
39 /* memset to 0 to set zalloc/opaque etc */
40 memset (pStream
, 0, sizeof(*pStream
));
42 nRes
= inflateInit2(pStream
, bNoWrap
? -MAX_WBITS
: MAX_WBITS
);
63 void SAL_CALL
Inflater::setInput( const Sequence
< sal_Int8
>& rBuffer
)
67 nLength
= rBuffer
.getLength();
70 sal_Bool SAL_CALL
Inflater::needsDictionary( )
75 sal_Bool SAL_CALL
Inflater::finished( )
80 sal_Int32 SAL_CALL
Inflater::doInflateSegment( Sequence
< sal_Int8
>& rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
82 if (nNewOffset
< 0 || nNewLength
< 0 || nNewOffset
+ nNewLength
> rBuffer
.getLength())
86 return doInflateBytes(rBuffer
, nNewOffset
, nNewLength
);
89 void SAL_CALL
Inflater::end( )
96 z_inflateEnd(pStream
);
103 sal_Int32
Inflater::doInflateBytes (Sequence
< sal_Int8
> &rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
107 nLastInflateError
= Z_STREAM_ERROR
;
111 nLastInflateError
= 0;
113 pStream
->next_in
= ( unsigned char* ) ( sInBuffer
.getConstArray() + nOffset
);
114 pStream
->avail_in
= nLength
;
115 pStream
->next_out
= reinterpret_cast < unsigned char* > ( rBuffer
.getArray() + nNewOffset
);
116 pStream
->avail_out
= nNewLength
;
118 #if !defined Z_PREFIX
119 sal_Int32 nResult
= ::inflate(pStream
, Z_PARTIAL_FLUSH
);
121 sal_Int32 nResult
= ::z_inflate(pStream
, Z_PARTIAL_FLUSH
);
127 bFinished
= sal_True
;
129 nOffset
+= nLength
- pStream
->avail_in
;
130 nLength
= pStream
->avail_in
;
131 return nNewLength
- pStream
->avail_out
;
134 bNeedDict
= sal_True
;
135 nOffset
+= nLength
- pStream
->avail_in
;
136 nLength
= pStream
->avail_in
;
140 // it is no error, if there is no input or no output
141 if ( nLength
&& nNewLength
)
142 nLastInflateError
= nResult
;
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */