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 <osl/diagnose.h>
24 : #include <string.h>
25 :
26 : using namespace com::sun::star::packages::zip::ZipConstants;
27 : using namespace com::sun::star;
28 : using namespace ZipUtils;
29 :
30 : /** Provides general purpose compression using the ZLIB compression
31 : * library.
32 : */
33 :
34 21434 : Deflater::~Deflater()
35 : {
36 10717 : end();
37 10717 : }
38 10717 : void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, bool bNowrap)
39 : {
40 10717 : pStream = new z_stream;
41 : /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
42 10717 : memset (pStream, 0, sizeof(*pStream));
43 :
44 10717 : switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
45 : DEF_MEM_LEVEL, nStrategyArg))
46 : {
47 : case Z_OK:
48 10717 : break;
49 : case Z_MEM_ERROR:
50 0 : delete pStream;
51 0 : break;
52 : case Z_STREAM_ERROR:
53 0 : delete pStream;
54 0 : break;
55 : default:
56 0 : break;
57 : }
58 10717 : }
59 :
60 10717 : Deflater::Deflater(sal_Int32 nSetLevel, bool bNowrap)
61 : : bFinish(false)
62 : , bFinished(false)
63 : , nStrategy(DEFAULT_STRATEGY)
64 : , nOffset(0)
65 10717 : , nLength(0)
66 : {
67 10717 : init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
68 10717 : }
69 :
70 21696 : sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
71 : {
72 : sal_Int32 nResult;
73 21696 : pStream->next_in = reinterpret_cast<unsigned char*>(sInBuffer.getArray()) + nOffset;
74 21696 : pStream->next_out = reinterpret_cast<unsigned char*>(rBuffer.getArray())+nNewOffset;
75 21696 : pStream->avail_in = nLength;
76 21696 : pStream->avail_out = nNewLength;
77 :
78 : #if !defined Z_PREFIX
79 21696 : nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
80 : #else
81 : nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
82 : #endif
83 21696 : switch (nResult)
84 : {
85 : case Z_STREAM_END:
86 10717 : bFinished = true;
87 : case Z_OK:
88 21696 : nOffset += nLength - pStream->avail_in;
89 21696 : nLength = pStream->avail_in;
90 21696 : return nNewLength - pStream->avail_out;
91 : default:
92 0 : return 0;
93 : }
94 : }
95 :
96 11053 : void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer )
97 : {
98 11053 : sInBuffer = rBuffer;
99 11053 : nOffset = 0;
100 11053 : nLength = rBuffer.getLength();
101 11053 : }
102 :
103 22032 : bool SAL_CALL Deflater::needsInput( )
104 : {
105 22032 : return nLength <=0;
106 : }
107 10717 : void SAL_CALL Deflater::finish( )
108 : {
109 10717 : bFinish = true;
110 10717 : }
111 21696 : sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
112 : {
113 : OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
114 21696 : return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
115 : }
116 10713 : sal_Int64 SAL_CALL Deflater::getTotalIn( )
117 : {
118 10713 : return pStream->total_in; // FIXME64: zlib doesn't look 64bit clean here
119 : }
120 10713 : sal_Int64 SAL_CALL Deflater::getTotalOut( )
121 : {
122 10713 : return pStream->total_out; // FIXME64: zlib doesn't look 64bit clean here
123 : }
124 10717 : void SAL_CALL Deflater::reset( )
125 : {
126 : #if !defined Z_PREFIX
127 10717 : deflateReset(pStream);
128 : #else
129 : z_deflateReset(pStream);
130 : #endif
131 10717 : bFinish = false;
132 10717 : bFinished = false;
133 10717 : nOffset = nLength = 0;
134 10717 : }
135 10717 : void SAL_CALL Deflater::end( )
136 : {
137 10717 : if (pStream != NULL)
138 : {
139 : #if !defined Z_PREFIX
140 10717 : deflateEnd(pStream);
141 : #else
142 : z_deflateEnd(pStream);
143 : #endif
144 10717 : delete pStream;
145 : }
146 10717 : pStream = NULL;
147 10717 : }
148 :
149 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|