1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
33 Deflater::~Deflater(void)
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
))
59 Deflater::Deflater(sal_Int32 nSetLevel
, sal_Bool bNowrap
)
61 , bFinished(sal_False
)
62 , bSetParams(sal_False
)
64 , nStrategy(DEFAULT_STRATEGY
)
68 init(nSetLevel
, DEFAULT_STRATEGY
, bNowrap
);
71 sal_Int32
Deflater::doDeflateBytes (uno::Sequence
< sal_Int8
> &rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
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
;
82 nResult
= deflateParams(pStream
, nLevel
, nStrategy
);
84 nResult
= z_deflateParams(pStream
, nLevel
, nStrategy
);
89 bSetParams
= sal_False
;
90 nOffset
+= nLength
- pStream
->avail_in
;
91 nLength
= pStream
->avail_in
;
92 return nNewLength
- pStream
->avail_out
;
94 bSetParams
= sal_False
;
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
);
110 nResult
= z_deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
115 bFinished
= sal_True
;
117 nOffset
+= nLength
- pStream
->avail_in
;
118 nLength
= pStream
->avail_in
;
119 return nNewLength
- pStream
->avail_out
;
121 bSetParams
= sal_False
;
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()));
134 nOffset
= nNewOffset
;
135 nLength
= nNewLength
;
137 void SAL_CALL
Deflater::setLevel( sal_Int32 nNewLevel
)
139 if ((nNewLevel
< 0 || nNewLevel
> 9) && nNewLevel
!= DEFAULT_COMPRESSION
)
143 if (nNewLevel
!= nLevel
)
146 bSetParams
= sal_True
;
149 sal_Bool SAL_CALL
Deflater::needsInput( )
153 void SAL_CALL
Deflater::finish( )
157 sal_Bool SAL_CALL
Deflater::finished( )
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
);
179 z_deflateReset(pStream
);
182 bFinished
= sal_False
;
183 nOffset
= nLength
= 0;
185 void SAL_CALL
Deflater::end( )
189 #if !defined Z_PREFIX
192 z_deflateEnd(pStream
);
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */