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 <osl/diagnose.h>
26 using namespace com::sun::star::packages::zip::ZipConstants
;
27 using namespace com::sun::star
;
28 using namespace ZipUtils
;
30 /** Provides general purpose compression using the ZLIB compression
38 void Deflater::init (sal_Int32 nLevelArg
, sal_Int32 nStrategyArg
, bool bNowrap
)
40 pStream
= new z_stream
;
41 /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
42 memset (pStream
, 0, sizeof(*pStream
));
44 switch (deflateInit2(pStream
, nLevelArg
, Z_DEFLATED
, bNowrap
? -MAX_WBITS
: MAX_WBITS
,
45 DEF_MEM_LEVEL
, nStrategyArg
))
60 Deflater::Deflater(sal_Int32 nSetLevel
, bool bNowrap
)
63 , nStrategy(DEFAULT_STRATEGY
)
67 init(nSetLevel
, DEFAULT_STRATEGY
, bNowrap
);
70 sal_Int32
Deflater::doDeflateBytes (uno::Sequence
< sal_Int8
> &rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
73 pStream
->next_in
= reinterpret_cast<unsigned char*>(sInBuffer
.getArray()) + nOffset
;
74 pStream
->next_out
= reinterpret_cast<unsigned char*>(rBuffer
.getArray())+nNewOffset
;
75 pStream
->avail_in
= nLength
;
76 pStream
->avail_out
= nNewLength
;
79 nResult
= deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
81 nResult
= z_deflate(pStream
, bFinish
? Z_FINISH
: Z_NO_FLUSH
);
88 nOffset
+= nLength
- pStream
->avail_in
;
89 nLength
= pStream
->avail_in
;
90 return nNewLength
- pStream
->avail_out
;
96 void SAL_CALL
Deflater::setInputSegment( const uno::Sequence
< sal_Int8
>& rBuffer
)
100 nLength
= rBuffer
.getLength();
103 bool SAL_CALL
Deflater::needsInput( )
107 void SAL_CALL
Deflater::finish( )
111 sal_Int32 SAL_CALL
Deflater::doDeflateSegment( uno::Sequence
< sal_Int8
>& rBuffer
, sal_Int32 nNewOffset
, sal_Int32 nNewLength
)
113 OSL_ASSERT( !(nNewOffset
< 0 || nNewLength
< 0 || nNewOffset
+ nNewLength
> rBuffer
.getLength()));
114 return doDeflateBytes(rBuffer
, nNewOffset
, nNewLength
);
116 sal_Int64 SAL_CALL
Deflater::getTotalIn( )
118 return pStream
->total_in
; // FIXME64: zlib doesn't look 64bit clean here
120 sal_Int64 SAL_CALL
Deflater::getTotalOut( )
122 return pStream
->total_out
; // FIXME64: zlib doesn't look 64bit clean here
124 void SAL_CALL
Deflater::reset( )
126 #if !defined Z_PREFIX
127 deflateReset(pStream
);
129 z_deflateReset(pStream
);
133 nOffset
= nLength
= 0;
135 void SAL_CALL
Deflater::end( )
139 #if !defined Z_PREFIX
142 z_deflateEnd(pStream
);
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */