bump product version to 4.1.6.2
[LibreOffice.git] / package / source / zipapi / Deflater.cxx
blobcc79ba02d39a3194eb96fdb7880164600790cca6
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/Deflater.hxx>
21 #include <zlib.h>
22 #include <com/sun/star/packages/zip/ZipConstants.hpp>
23 #include <string.h> // for memset
25 using namespace com::sun::star::packages::zip::ZipConstants;
26 using namespace com::sun::star;
27 using namespace ZipUtils;
29 /** Provides general purpose compression using the ZLIB compression
30 * library.
33 Deflater::~Deflater(void)
35 end();
37 void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, sal_Bool bNowrap)
39 pStream = new z_stream;
40 /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
41 memset (pStream, 0, sizeof(*pStream));
43 switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
44 DEF_MEM_LEVEL, nStrategyArg))
46 case Z_OK:
47 break;
48 case Z_MEM_ERROR:
49 delete pStream;
50 break;
51 case Z_STREAM_ERROR:
52 delete pStream;
53 break;
54 default:
55 break;
59 Deflater::Deflater(sal_Int32 nSetLevel, sal_Bool bNowrap)
60 : bFinish(sal_False)
61 , bFinished(sal_False)
62 , bSetParams(sal_False)
63 , nLevel(nSetLevel)
64 , nStrategy(DEFAULT_STRATEGY)
65 , nOffset(0)
66 , nLength(0)
68 init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
71 sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
73 sal_Int32 nResult;
74 if (bSetParams)
76 pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
77 pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
78 pStream->avail_in = nLength;
79 pStream->avail_out = nNewLength;
81 #if !defined Z_PREFIX
82 nResult = deflateParams(pStream, nLevel, nStrategy);
83 #else
84 nResult = z_deflateParams(pStream, nLevel, nStrategy);
85 #endif
86 switch (nResult)
88 case Z_OK:
89 bSetParams = sal_False;
90 nOffset += nLength - pStream->avail_in;
91 nLength = pStream->avail_in;
92 return nNewLength - pStream->avail_out;
93 case Z_BUF_ERROR:
94 bSetParams = sal_False;
95 return 0;
96 default:
97 return 0;
100 else
102 pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
103 pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
104 pStream->avail_in = nLength;
105 pStream->avail_out = nNewLength;
107 #if !defined Z_PREFIX
108 nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
109 #else
110 nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
111 #endif
112 switch (nResult)
114 case Z_STREAM_END:
115 bFinished = sal_True;
116 case Z_OK:
117 nOffset += nLength - pStream->avail_in;
118 nLength = pStream->avail_in;
119 return nNewLength - pStream->avail_out;
120 case Z_BUF_ERROR:
121 bSetParams = sal_False;
122 return 0;
123 default:
124 return 0;
129 void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
131 OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
133 sInBuffer = rBuffer;
134 nOffset = nNewOffset;
135 nLength = nNewLength;
137 void SAL_CALL Deflater::setLevel( sal_Int32 nNewLevel )
139 if ((nNewLevel < 0 || nNewLevel > 9) && nNewLevel != DEFAULT_COMPRESSION)
141 // do error handling
143 if (nNewLevel != nLevel)
145 nLevel = nNewLevel;
146 bSetParams = sal_True;
149 sal_Bool SAL_CALL Deflater::needsInput( )
151 return nLength <=0;
153 void SAL_CALL Deflater::finish( )
155 bFinish = sal_True;
157 sal_Bool SAL_CALL Deflater::finished( )
159 return bFinished;
161 sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
163 OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
164 return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
166 sal_Int64 SAL_CALL Deflater::getTotalIn( )
168 return pStream->total_in; // FIXME64: zlib doesn't look 64bit clean here
170 sal_Int64 SAL_CALL Deflater::getTotalOut( )
172 return pStream->total_out; // FIXME64: zlib doesn't look 64bit clean here
174 void SAL_CALL Deflater::reset( )
176 #if !defined Z_PREFIX
177 deflateReset(pStream);
178 #else
179 z_deflateReset(pStream);
180 #endif
181 bFinish = sal_False;
182 bFinished = sal_False;
183 nOffset = nLength = 0;
185 void SAL_CALL Deflater::end( )
187 if (pStream != NULL)
189 #if !defined Z_PREFIX
190 deflateEnd(pStream);
191 #else
192 z_deflateEnd(pStream);
193 #endif
194 delete pStream;
196 pStream = NULL;
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */