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 38328 : Deflater::~Deflater(void)
34 : {
35 19164 : end();
36 19164 : }
37 19164 : void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, bool bNowrap)
38 : {
39 19164 : pStream = new z_stream;
40 : /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
41 19164 : memset (pStream, 0, sizeof(*pStream));
42 :
43 19164 : switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
44 : DEF_MEM_LEVEL, nStrategyArg))
45 : {
46 : case Z_OK:
47 19164 : 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 19164 : }
58 :
59 19164 : Deflater::Deflater(sal_Int32 nSetLevel, bool bNowrap)
60 : : bFinish(false)
61 : , bFinished(false)
62 : , nStrategy(DEFAULT_STRATEGY)
63 : , nOffset(0)
64 19164 : , nLength(0)
65 : {
66 19164 : init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
67 19164 : }
68 :
69 35778 : sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
70 : {
71 : sal_Int32 nResult;
72 35778 : pStream->next_in = (unsigned char*) sInBuffer.getConstArray() + nOffset;
73 35778 : pStream->next_out = (unsigned char*) rBuffer.getArray()+nNewOffset;
74 35778 : pStream->avail_in = nLength;
75 35778 : pStream->avail_out = nNewLength;
76 :
77 : #if !defined Z_PREFIX
78 35778 : nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
79 : #else
80 : nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
81 : #endif
82 35778 : switch (nResult)
83 : {
84 : case Z_STREAM_END:
85 17544 : bFinished = true;
86 : case Z_OK:
87 35778 : nOffset += nLength - pStream->avail_in;
88 35778 : nLength = pStream->avail_in;
89 35778 : return nNewLength - pStream->avail_out;
90 : default:
91 0 : return 0;
92 : }
93 : }
94 :
95 18334 : void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
96 : {
97 : OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
98 :
99 18334 : sInBuffer = rBuffer;
100 18334 : nOffset = nNewOffset;
101 18334 : nLength = nNewLength;
102 18334 : }
103 :
104 36568 : bool SAL_CALL Deflater::needsInput( )
105 : {
106 36568 : return nLength <=0;
107 : }
108 17544 : void SAL_CALL Deflater::finish( )
109 : {
110 17544 : bFinish = true;
111 17544 : }
112 35778 : sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
113 : {
114 : OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
115 35778 : return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
116 : }
117 17534 : sal_Int64 SAL_CALL Deflater::getTotalIn( )
118 : {
119 17534 : return pStream->total_in; // FIXME64: zlib doesn't look 64bit clean here
120 : }
121 17534 : sal_Int64 SAL_CALL Deflater::getTotalOut( )
122 : {
123 17534 : return pStream->total_out; // FIXME64: zlib doesn't look 64bit clean here
124 : }
125 17544 : void SAL_CALL Deflater::reset( )
126 : {
127 : #if !defined Z_PREFIX
128 17544 : deflateReset(pStream);
129 : #else
130 : z_deflateReset(pStream);
131 : #endif
132 17544 : bFinish = false;
133 17544 : bFinished = false;
134 17544 : nOffset = nLength = 0;
135 17544 : }
136 19164 : void SAL_CALL Deflater::end( )
137 : {
138 19164 : if (pStream != NULL)
139 : {
140 : #if !defined Z_PREFIX
141 19164 : deflateEnd(pStream);
142 : #else
143 : z_deflateEnd(pStream);
144 : #endif
145 19164 : delete pStream;
146 : }
147 19164 : pStream = NULL;
148 19164 : }
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|