Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / package / source / zipapi / Deflater.cxx
blobc14765c00ec7ea85abc45e4cf143dfc47be16cf2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <zlib.h>
22 #include <com/sun/star/packages/zip/ZipConstants.hpp>
23 #include <osl/diagnose.h>
24 #include <string.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
31 * library.
34 Deflater::~Deflater()
36 end();
38 void Deflater::init (sal_Int32 nLevelArg, bool bNowrap)
40 pStream.reset(new z_stream);
41 /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
42 memset (pStream.get(), 0, sizeof(*pStream));
44 switch (deflateInit2(pStream.get(), nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
45 DEF_MEM_LEVEL, DEFAULT_STRATEGY))
47 case Z_OK:
48 break;
49 case Z_MEM_ERROR:
50 case Z_STREAM_ERROR:
51 pStream.reset();
52 break;
53 default:
54 break;
58 Deflater::Deflater(sal_Int32 nSetLevel, bool bNowrap)
59 : bFinish(false)
60 , bFinished(false)
61 , nOffset(0)
62 , nLength(0)
63 , nTotalOut64(0)
64 , nTotalIn64(0)
66 init(nSetLevel, bNowrap);
69 sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
71 sal_Int32 nResult;
72 pStream->next_in = reinterpret_cast<const unsigned char*>( sInBuffer.getConstArray() + nOffset );
73 pStream->next_out = reinterpret_cast<unsigned char*>(rBuffer.getArray())+nNewOffset;
74 pStream->avail_in = nLength;
75 pStream->avail_out = nNewLength;
76 auto nLastTotalIn = pStream->total_in;
77 auto nLastTotalOut = pStream->total_out;
79 #if !defined Z_PREFIX
80 nResult = deflate(pStream.get(), bFinish ? Z_FINISH : Z_NO_FLUSH);
81 #else
82 nResult = z_deflate(pStream.get(), bFinish ? Z_FINISH : Z_NO_FLUSH);
83 #endif
84 // total_in / total_out may stored only in 32bit, and can overflow during deflate
85 // 1 deflate call, uncompress only a few data, so only 1 overflow can happen at once.
86 if (pStream->total_in < nLastTotalIn)
88 nTotalIn64 += 0x100000000;
90 if (pStream->total_out < nLastTotalOut)
92 nTotalOut64 += 0x100000000;
94 switch (nResult)
96 case Z_STREAM_END:
97 bFinished = true;
98 [[fallthrough]];
99 case Z_OK:
100 nOffset += nLength - pStream->avail_in;
101 nLength = pStream->avail_in;
102 return nNewLength - pStream->avail_out;
103 default:
104 return 0;
108 void Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer )
110 sInBuffer = rBuffer;
111 nOffset = 0;
112 nLength = rBuffer.getLength();
115 bool Deflater::needsInput() const
117 return nLength <=0;
119 void Deflater::finish( )
121 bFinish = true;
123 sal_Int32 Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewLength )
125 OSL_ASSERT( !(nNewLength < 0 || nNewLength > rBuffer.getLength()));
126 return doDeflateBytes(rBuffer, /*nNewOffset*/0, nNewLength);
128 sal_Int64 Deflater::getTotalIn() const
130 return pStream->total_in + nTotalIn64;
132 sal_Int64 Deflater::getTotalOut() const
134 return pStream->total_out + nTotalOut64;
136 void Deflater::reset( )
138 #if !defined Z_PREFIX
139 deflateReset(pStream.get());
140 #else
141 z_deflateReset(pStream.get());
142 #endif
143 bFinish = false;
144 bFinished = false;
145 nOffset = nLength = 0;
147 void Deflater::end( )
149 if (pStream)
151 #if !defined Z_PREFIX
152 deflateEnd(pStream.get());
153 #else
154 z_deflateEnd(pStream.get());
155 #endif
156 pStream.reset();
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */