update dev300-m58
[ooovba.git] / package / source / zipapi / Deflater.cxx
blob91b38db508d13ad80785645ff0f04b621c7be44a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Deflater.cxx,v $
10 * $Revision: 1.17 $
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 <Deflater.hxx>
34 #ifndef _ZLIB_H
35 #ifdef SYSTEM_ZLIB
36 #include <zlib.h>
37 #else
38 #include <external/zlib/zlib.h>
39 #endif
40 #endif
41 #include <com/sun/star/packages/zip/ZipConstants.hpp>
42 #include <string.h> // for memset
44 using namespace com::sun::star::packages::zip::ZipConstants;
45 using namespace com::sun::star;
47 /** Provides general purpose compression using the ZLIB compression
48 * library.
51 Deflater::~Deflater(void)
53 end();
55 void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, sal_Bool bNowrap)
57 pStream = new z_stream;
58 /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
59 memset (pStream, 0, sizeof(*pStream));
61 switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
62 DEF_MEM_LEVEL, nStrategyArg))
64 case Z_OK:
65 break;
66 case Z_MEM_ERROR:
67 delete pStream;
68 break;
69 case Z_STREAM_ERROR:
70 delete pStream;
71 break;
72 default:
73 break;
77 Deflater::Deflater(sal_Int32 nSetLevel, sal_Bool bNowrap)
78 : bFinish(sal_False)
79 , bFinished(sal_False)
80 , bSetParams(sal_False)
81 , nLevel(nSetLevel)
82 , nStrategy(DEFAULT_STRATEGY)
83 , nOffset(0)
84 , nLength(0)
86 init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
89 sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
91 sal_Int32 nResult;
92 if (bSetParams)
94 pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
95 pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
96 pStream->avail_in = nLength;
97 pStream->avail_out = nNewLength;
99 #ifdef SYSTEM_ZLIB
100 nResult = deflateParams(pStream, nLevel, nStrategy);
101 #else
102 nResult = z_deflateParams(pStream, nLevel, nStrategy);
103 #endif
104 switch (nResult)
106 case Z_OK:
107 bSetParams = sal_False;
108 nOffset += nLength - pStream->avail_in;
109 nLength = pStream->avail_in;
110 return nNewLength - pStream->avail_out;
111 case Z_BUF_ERROR:
112 bSetParams = sal_False;
113 return 0;
114 default:
115 return 0;
118 else
120 pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
121 pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
122 pStream->avail_in = nLength;
123 pStream->avail_out = nNewLength;
125 #ifdef SYSTEM_ZLIB
126 nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
127 #else
128 nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
129 #endif
130 switch (nResult)
132 case Z_STREAM_END:
133 bFinished = sal_True;
134 case Z_OK:
135 nOffset += nLength - pStream->avail_in;
136 nLength = pStream->avail_in;
137 return nNewLength - pStream->avail_out;
138 case Z_BUF_ERROR:
139 bSetParams = sal_False;
140 return 0;
141 default:
142 return 0;
147 void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
149 OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
151 sInBuffer = rBuffer;
152 nOffset = nNewOffset;
153 nLength = nNewLength;
155 void SAL_CALL Deflater::setLevel( sal_Int32 nNewLevel )
157 if ((nNewLevel < 0 || nNewLevel > 9) && nNewLevel != DEFAULT_COMPRESSION)
159 // do error handling
161 if (nNewLevel != nLevel)
163 nLevel = nNewLevel;
164 bSetParams = sal_True;
167 sal_Bool SAL_CALL Deflater::needsInput( )
169 return nLength <=0;
171 void SAL_CALL Deflater::finish( )
173 bFinish = sal_True;
175 sal_Bool SAL_CALL Deflater::finished( )
177 return bFinished;
179 sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
181 OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
182 return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
184 sal_Int32 SAL_CALL Deflater::getTotalIn( )
186 return pStream->total_in;
188 sal_Int32 SAL_CALL Deflater::getTotalOut( )
190 return pStream->total_out;
192 void SAL_CALL Deflater::reset( )
194 #ifdef SYSTEM_ZLIB
195 deflateReset(pStream);
196 #else
197 z_deflateReset(pStream);
198 #endif
199 bFinish = sal_False;
200 bFinished = sal_False;
201 nOffset = nLength = 0;
203 void SAL_CALL Deflater::end( )
205 if (pStream != NULL)
207 #ifdef SYSTEM_ZLIB
208 deflateEnd(pStream);
209 #else
210 z_deflateEnd(pStream);
211 #endif
212 delete pStream;
214 pStream = NULL;