Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / package / source / zipapi / Inflater.cxx
blob5a09415abe6c29a0fa605d6299994434b5ab3f15
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <zlib.h>
22 #include <string.h>
24 using namespace com::sun::star::uno;
25 using namespace ZipUtils;
27 /** Provides general purpose decompression using the ZLIB library */
29 Inflater::Inflater(bool bNoWrap)
30 : bFinished(false),
31 bNeedDict(false),
32 nOffset(0),
33 nLength(0),
34 nLastInflateError(0)
36 pStream.reset(new z_stream);
37 /* memset to 0 to set zalloc/opaque etc */
38 memset (pStream.get(), 0, sizeof(*pStream));
39 sal_Int32 nRes;
40 nRes = inflateInit2(pStream.get(), bNoWrap ? -MAX_WBITS : MAX_WBITS);
41 switch (nRes)
43 case Z_OK:
44 break;
45 case Z_MEM_ERROR:
46 pStream.reset();
47 break;
48 case Z_STREAM_ERROR:
49 pStream.reset();
50 break;
51 default:
52 break;
56 Inflater::~Inflater()
58 end();
61 void Inflater::setInput( const Sequence< sal_Int8 >& rBuffer )
63 sInBuffer = rBuffer;
64 nOffset = 0;
65 nLength = rBuffer.getLength();
69 sal_Int32 Inflater::doInflateSegment( Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
71 if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength())
73 // do error handling
75 return doInflateBytes(rBuffer, nNewOffset, nNewLength);
78 void Inflater::end( )
80 if (pStream)
82 #if !defined Z_PREFIX
83 inflateEnd(pStream.get());
84 #else
85 z_inflateEnd(pStream.get());
86 #endif
87 pStream.reset();
91 sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
93 if ( !pStream )
95 nLastInflateError = Z_STREAM_ERROR;
96 return 0;
99 nLastInflateError = 0;
101 pStream->next_in = reinterpret_cast<const unsigned char*>( sInBuffer.getConstArray() + nOffset );
102 pStream->avail_in = nLength;
103 pStream->next_out = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset );
104 pStream->avail_out = nNewLength;
106 #if !defined Z_PREFIX
107 sal_Int32 nResult = ::inflate(pStream.get(), Z_PARTIAL_FLUSH);
108 #else
109 sal_Int32 nResult = ::z_inflate(pStream.get(), Z_PARTIAL_FLUSH);
110 #endif
112 switch (nResult)
114 case Z_STREAM_END:
115 bFinished = true;
116 [[fallthrough]];
117 case Z_OK:
118 nOffset += nLength - pStream->avail_in;
119 nLength = pStream->avail_in;
120 return nNewLength - pStream->avail_out;
122 case Z_NEED_DICT:
123 bNeedDict = true;
124 nOffset += nLength - pStream->avail_in;
125 nLength = pStream->avail_in;
126 return 0;
128 default:
129 // it is no error, if there is no input or no output
130 if ( nLength && nNewLength )
131 nLastInflateError = nResult;
134 return 0;
137 InflaterBytes::InflaterBytes(bool bNoWrap)
138 : bFinished(false),
139 nOffset(0),
140 nLength(0),
141 sInBuffer(nullptr)
143 pStream.reset(new z_stream);
144 /* memset to 0 to set zalloc/opaque etc */
145 memset (pStream.get(), 0, sizeof(*pStream));
146 sal_Int32 nRes;
147 nRes = inflateInit2(pStream.get(), bNoWrap ? -MAX_WBITS : MAX_WBITS);
148 switch (nRes)
150 case Z_OK:
151 break;
152 case Z_MEM_ERROR:
153 pStream.reset();
154 break;
155 case Z_STREAM_ERROR:
156 pStream.reset();
157 break;
158 default:
159 break;
163 InflaterBytes::~InflaterBytes()
165 end();
168 void InflaterBytes::setInput( const sal_Int8* rBuffer, sal_Int32 nBufLen )
170 sInBuffer = rBuffer;
171 nOffset = 0;
172 nLength = nBufLen;
176 sal_Int32 InflaterBytes::doInflateSegment( sal_Int8* pOutBuffer, sal_Int32 nBufLen, sal_Int32 nNewOffset, sal_Int32 nNewLength )
178 if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > nBufLen)
180 // do error handling
182 return doInflateBytes(pOutBuffer, nNewOffset, nNewLength);
185 void InflaterBytes::end( )
187 if (pStream)
189 #if !defined Z_PREFIX
190 inflateEnd(pStream.get());
191 #else
192 z_inflateEnd(pStream.get());
193 #endif
194 pStream.reset();
198 sal_Int32 InflaterBytes::doInflateBytes (sal_Int8* pOutBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
200 if ( !pStream )
202 return 0;
205 pStream->next_in = reinterpret_cast<const unsigned char*>( sInBuffer + nOffset );
206 pStream->avail_in = nLength;
207 pStream->next_out = reinterpret_cast < unsigned char* > ( pOutBuffer + nNewOffset );
208 pStream->avail_out = nNewLength;
210 #if !defined Z_PREFIX
211 sal_Int32 nResult = ::inflate(pStream.get(), Z_PARTIAL_FLUSH);
212 #else
213 sal_Int32 nResult = ::z_inflate(pStream.get(), Z_PARTIAL_FLUSH);
214 #endif
216 switch (nResult)
218 case Z_STREAM_END:
219 bFinished = true;
220 [[fallthrough]];
221 case Z_OK:
222 nOffset += nLength - pStream->avail_in;
223 nLength = pStream->avail_in;
224 return nNewLength - pStream->avail_out;
226 case Z_NEED_DICT:
227 nOffset += nLength - pStream->avail_in;
228 nLength = pStream->avail_in;
229 return 0;
231 default:
232 // it is no error, if there is no input or no output
233 break;
236 return 0;
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */