1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Deflater.cxx,v $
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>
38 #include <external/zlib/zlib.h>
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
51 Deflater::~Deflater(void)
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
))
77 Deflater::Deflater(sal_Int32 nSetLevel
, sal_Bool bNowrap
)
79 , bFinished(sal_False
)
80 , bSetParams(sal_False
)
82 , nStrategy(DEFAULT_STRATEGY
)
86 init(nSetLevel
, DEFAULT_STRATEGY
, bNowrap
);
89 sal_Int32
Deflater::doDeflateBytes (uno::Sequence
< sal_Int8
> &rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
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
;
100 nResult
= deflateParams(pStream
, nLevel
, nStrategy
);
102 nResult
= z_deflateParams(pStream
, nLevel
, nStrategy
);
107 bSetParams
= sal_False
;
108 nOffset
+= nLength
- pStream
->avail_in
;
109 nLength
= pStream
->avail_in
;
110 return nNewLength
- pStream
->avail_out
;
112 bSetParams
= sal_False
;
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
;
126 nResult
= deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
128 nResult
= z_deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
133 bFinished
= sal_True
;
135 nOffset
+= nLength
- pStream
->avail_in
;
136 nLength
= pStream
->avail_in
;
137 return nNewLength
- pStream
->avail_out
;
139 bSetParams
= sal_False
;
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()));
152 nOffset
= nNewOffset
;
153 nLength
= nNewLength
;
155 void SAL_CALL
Deflater::setLevel( sal_Int32 nNewLevel
)
157 if ((nNewLevel
< 0 || nNewLevel
> 9) && nNewLevel
!= DEFAULT_COMPRESSION
)
161 if (nNewLevel
!= nLevel
)
164 bSetParams
= sal_True
;
167 sal_Bool SAL_CALL
Deflater::needsInput( )
171 void SAL_CALL
Deflater::finish( )
175 sal_Bool SAL_CALL
Deflater::finished( )
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( )
195 deflateReset(pStream
);
197 z_deflateReset(pStream
);
200 bFinished
= sal_False
;
201 nOffset
= nLength
= 0;
203 void SAL_CALL
Deflater::end( )
210 z_deflateEnd(pStream
);