Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <package/Deflater.hxx>
21 : #include <zlib.h>
22 : #include <com/sun/star/packages/zip/ZipConstants.hpp>
23 : #include <string.h>
24 :
25 : using namespace com::sun::star::packages::zip::ZipConstants;
26 : using namespace com::sun::star;
27 : using namespace ZipUtils;
28 :
29 : /** Provides general purpose compression using the ZLIB compression
30 : * library.
31 : */
32 :
33 0 : Deflater::~Deflater(void)
34 : {
35 0 : end();
36 0 : }
37 0 : void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, bool bNowrap)
38 : {
39 0 : pStream = new z_stream;
40 : /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
41 0 : memset (pStream, 0, sizeof(*pStream));
42 :
43 0 : switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
44 : DEF_MEM_LEVEL, nStrategyArg))
45 : {
46 : case Z_OK:
47 0 : break;
48 : case Z_MEM_ERROR:
49 0 : delete pStream;
50 0 : break;
51 : case Z_STREAM_ERROR:
52 0 : delete pStream;
53 0 : break;
54 : default:
55 0 : break;
56 : }
57 0 : }
58 :
59 0 : Deflater::Deflater(sal_Int32 nSetLevel, bool bNowrap)
60 : : bFinish(false)
61 : , bFinished(false)
62 : , bSetParams(false)
63 : , nLevel(nSetLevel)
64 : , nStrategy(DEFAULT_STRATEGY)
65 : , nOffset(0)
66 0 : , nLength(0)
67 : {
68 0 : init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
69 0 : }
70 :
71 0 : sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
72 : {
73 : sal_Int32 nResult;
74 0 : if (bSetParams)
75 : {
76 0 : pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
77 0 : pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
78 0 : pStream->avail_in = nLength;
79 0 : pStream->avail_out = nNewLength;
80 :
81 : #if !defined Z_PREFIX
82 0 : nResult = deflateParams(pStream, nLevel, nStrategy);
83 : #else
84 : nResult = z_deflateParams(pStream, nLevel, nStrategy);
85 : #endif
86 0 : switch (nResult)
87 : {
88 : case Z_OK:
89 0 : bSetParams = false;
90 0 : nOffset += nLength - pStream->avail_in;
91 0 : nLength = pStream->avail_in;
92 0 : return nNewLength - pStream->avail_out;
93 : case Z_BUF_ERROR:
94 0 : bSetParams = false;
95 0 : return 0;
96 : default:
97 0 : return 0;
98 : }
99 : }
100 : else
101 : {
102 0 : pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
103 0 : pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
104 0 : pStream->avail_in = nLength;
105 0 : pStream->avail_out = nNewLength;
106 :
107 : #if !defined Z_PREFIX
108 0 : nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
109 : #else
110 : nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
111 : #endif
112 0 : switch (nResult)
113 : {
114 : case Z_STREAM_END:
115 0 : bFinished = true;
116 : case Z_OK:
117 0 : nOffset += nLength - pStream->avail_in;
118 0 : nLength = pStream->avail_in;
119 0 : return nNewLength - pStream->avail_out;
120 : case Z_BUF_ERROR:
121 0 : bSetParams = false;
122 0 : return 0;
123 : default:
124 0 : return 0;
125 : }
126 : }
127 : }
128 :
129 0 : void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
130 : {
131 : OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
132 :
133 0 : sInBuffer = rBuffer;
134 0 : nOffset = nNewOffset;
135 0 : nLength = nNewLength;
136 0 : }
137 0 : void SAL_CALL Deflater::setLevel( sal_Int32 nNewLevel )
138 : {
139 : if ((nNewLevel < 0 || nNewLevel > 9) && nNewLevel != DEFAULT_COMPRESSION)
140 : {
141 : // do error handling
142 : }
143 0 : if (nNewLevel != nLevel)
144 : {
145 0 : nLevel = nNewLevel;
146 0 : bSetParams = true;
147 : }
148 0 : }
149 0 : bool SAL_CALL Deflater::needsInput( )
150 : {
151 0 : return nLength <=0;
152 : }
153 0 : void SAL_CALL Deflater::finish( )
154 : {
155 0 : bFinish = true;
156 0 : }
157 0 : bool SAL_CALL Deflater::finished( )
158 : {
159 0 : return bFinished;
160 : }
161 0 : sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
162 : {
163 : OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
164 0 : return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
165 : }
166 0 : sal_Int64 SAL_CALL Deflater::getTotalIn( )
167 : {
168 0 : return pStream->total_in; // FIXME64: zlib doesn't look 64bit clean here
169 : }
170 0 : sal_Int64 SAL_CALL Deflater::getTotalOut( )
171 : {
172 0 : return pStream->total_out; // FIXME64: zlib doesn't look 64bit clean here
173 : }
174 0 : void SAL_CALL Deflater::reset( )
175 : {
176 : #if !defined Z_PREFIX
177 0 : deflateReset(pStream);
178 : #else
179 : z_deflateReset(pStream);
180 : #endif
181 0 : bFinish = false;
182 0 : bFinished = false;
183 0 : nOffset = nLength = 0;
184 0 : }
185 0 : void SAL_CALL Deflater::end( )
186 : {
187 0 : if (pStream != NULL)
188 : {
189 : #if !defined Z_PREFIX
190 0 : deflateEnd(pStream);
191 : #else
192 : z_deflateEnd(pStream);
193 : #endif
194 0 : delete pStream;
195 : }
196 0 : pStream = NULL;
197 0 : }
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|