1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_package.hxx"
30 #include <Deflater.hxx>
35 #include <external/zlib/zlib.h>
38 #include <com/sun/star/packages/zip/ZipConstants.hpp>
39 #include <string.h> // for memset
41 using namespace com::sun::star::packages::zip::ZipConstants
;
42 using namespace com::sun::star
;
44 /** Provides general purpose compression using the ZLIB compression
48 Deflater::~Deflater(void)
52 void Deflater::init (sal_Int32 nLevelArg
, sal_Int32 nStrategyArg
, sal_Bool bNowrap
)
54 pStream
= new z_stream
;
55 /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
56 memset (pStream
, 0, sizeof(*pStream
));
58 switch (deflateInit2(pStream
, nLevelArg
, Z_DEFLATED
, bNowrap
? -MAX_WBITS
: MAX_WBITS
,
59 DEF_MEM_LEVEL
, nStrategyArg
))
74 Deflater::Deflater(sal_Int32 nSetLevel
, sal_Bool bNowrap
)
76 , bFinished(sal_False
)
77 , bSetParams(sal_False
)
79 , nStrategy(DEFAULT_STRATEGY
)
83 init(nSetLevel
, DEFAULT_STRATEGY
, bNowrap
);
86 sal_Int32
Deflater::doDeflateBytes (uno::Sequence
< sal_Int8
> &rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
91 pStream
->next_in
= (unsigned char*) sInBuffer
.getConstArray() + nOffset
;
92 pStream
->next_out
= (unsigned char*) rBuffer
.getArray()+nNewOffset
;
93 pStream
->avail_in
= nLength
;
94 pStream
->avail_out
= nNewLength
;
96 #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
97 nResult
= deflateParams(pStream
, nLevel
, nStrategy
);
99 nResult
= z_deflateParams(pStream
, nLevel
, nStrategy
);
104 bSetParams
= sal_False
;
105 nOffset
+= nLength
- pStream
->avail_in
;
106 nLength
= pStream
->avail_in
;
107 return nNewLength
- pStream
->avail_out
;
109 bSetParams
= sal_False
;
117 pStream
->next_in
= (unsigned char*) sInBuffer
.getConstArray() + nOffset
;
118 pStream
->next_out
= (unsigned char*) rBuffer
.getArray()+nNewOffset
;
119 pStream
->avail_in
= nLength
;
120 pStream
->avail_out
= nNewLength
;
122 #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
123 nResult
= deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
125 nResult
= z_deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
130 bFinished
= sal_True
;
132 nOffset
+= nLength
- pStream
->avail_in
;
133 nLength
= pStream
->avail_in
;
134 return nNewLength
- pStream
->avail_out
;
136 bSetParams
= sal_False
;
144 void SAL_CALL
Deflater::setInputSegment( const uno::Sequence
< sal_Int8
>& rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
146 OSL_ASSERT( !(nNewOffset
< 0 || nNewLength
< 0 || nNewOffset
+ nNewLength
> rBuffer
.getLength()));
149 nOffset
= nNewOffset
;
150 nLength
= nNewLength
;
152 void SAL_CALL
Deflater::setLevel( sal_Int32 nNewLevel
)
154 if ((nNewLevel
< 0 || nNewLevel
> 9) && nNewLevel
!= DEFAULT_COMPRESSION
)
158 if (nNewLevel
!= nLevel
)
161 bSetParams
= sal_True
;
164 sal_Bool SAL_CALL
Deflater::needsInput( )
168 void SAL_CALL
Deflater::finish( )
172 sal_Bool SAL_CALL
Deflater::finished( )
176 sal_Int32 SAL_CALL
Deflater::doDeflateSegment( uno::Sequence
< sal_Int8
>& rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
178 OSL_ASSERT( !(nNewOffset
< 0 || nNewLength
< 0 || nNewOffset
+ nNewLength
> rBuffer
.getLength()));
179 return doDeflateBytes(rBuffer
, nNewOffset
, nNewLength
);
181 sal_Int32 SAL_CALL
Deflater::getTotalIn( )
183 return pStream
->total_in
;
185 sal_Int32 SAL_CALL
Deflater::getTotalOut( )
187 return pStream
->total_out
;
189 void SAL_CALL
Deflater::reset( )
191 #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIXB
192 deflateReset(pStream
);
194 z_deflateReset(pStream
);
197 bFinished
= sal_False
;
198 nOffset
= nLength
= 0;
200 void SAL_CALL
Deflater::end( )
204 #if defined SYSTEM_ZLIB || !defined ZLIB_PREFIX
207 z_deflateEnd(pStream
);