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/Inflater.hxx>
21 : #include <zlib.h>
22 : #include <string.h> // for memset
23 :
24 : using namespace com::sun::star::uno;
25 : using namespace ZipUtils;
26 :
27 : /** Provides general purpose decompression using the ZLIB library */
28 :
29 10287 : Inflater::Inflater(sal_Bool bNoWrap)
30 : : bFinished(sal_False),
31 : bSetParams(sal_False),
32 : bNeedDict(sal_False),
33 : nOffset(0),
34 : nLength(0),
35 : nLastInflateError(0),
36 10287 : pStream(NULL)
37 : {
38 10287 : pStream = new z_stream;
39 : /* memset to 0 to set zalloc/opaque etc */
40 10287 : memset (pStream, 0, sizeof(*pStream));
41 : sal_Int32 nRes;
42 10287 : nRes = inflateInit2(pStream, bNoWrap ? -MAX_WBITS : MAX_WBITS);
43 10287 : switch (nRes)
44 : {
45 : case Z_OK:
46 10287 : break;
47 : case Z_MEM_ERROR:
48 0 : delete pStream;
49 0 : break;
50 : case Z_STREAM_ERROR:
51 0 : delete pStream;
52 0 : break;
53 : default:
54 0 : break;
55 : }
56 10287 : }
57 :
58 20514 : Inflater::~Inflater()
59 : {
60 10257 : end();
61 10257 : }
62 :
63 3878 : void SAL_CALL Inflater::setInput( const Sequence< sal_Int8 >& rBuffer )
64 : {
65 3878 : sInBuffer = rBuffer;
66 3878 : nOffset = 0;
67 3878 : nLength = rBuffer.getLength();
68 3878 : }
69 :
70 3878 : sal_Bool SAL_CALL Inflater::needsDictionary( )
71 : {
72 3878 : return bNeedDict;
73 : }
74 :
75 3879 : sal_Bool SAL_CALL Inflater::finished( )
76 : {
77 3879 : return bFinished;
78 : }
79 :
80 8182 : sal_Int32 SAL_CALL Inflater::doInflateSegment( Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
81 : {
82 8182 : if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength())
83 : {
84 : // do error handling
85 : }
86 8182 : return doInflateBytes(rBuffer, nNewOffset, nNewLength);
87 : }
88 :
89 10257 : void SAL_CALL Inflater::end( )
90 : {
91 10257 : if (pStream != NULL)
92 : {
93 : #if !defined Z_PREFIX
94 10257 : inflateEnd(pStream);
95 : #else
96 : z_inflateEnd(pStream);
97 : #endif
98 10257 : delete pStream;
99 : }
100 10257 : pStream = NULL;
101 10257 : }
102 :
103 8182 : sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
104 : {
105 8182 : if ( !pStream )
106 : {
107 0 : nLastInflateError = Z_STREAM_ERROR;
108 0 : return 0;
109 : }
110 :
111 8182 : nLastInflateError = 0;
112 :
113 8182 : pStream->next_in = ( unsigned char* ) ( sInBuffer.getConstArray() + nOffset );
114 8182 : pStream->avail_in = nLength;
115 8182 : pStream->next_out = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset );
116 8182 : pStream->avail_out = nNewLength;
117 :
118 : #if !defined Z_PREFIX
119 8182 : sal_Int32 nResult = ::inflate(pStream, Z_PARTIAL_FLUSH);
120 : #else
121 : sal_Int32 nResult = ::z_inflate(pStream, Z_PARTIAL_FLUSH);
122 : #endif
123 :
124 8182 : switch (nResult)
125 : {
126 : case Z_STREAM_END:
127 3877 : bFinished = sal_True;
128 : case Z_OK:
129 4303 : nOffset += nLength - pStream->avail_in;
130 4303 : nLength = pStream->avail_in;
131 4303 : return nNewLength - pStream->avail_out;
132 :
133 : case Z_NEED_DICT:
134 0 : bNeedDict = sal_True;
135 0 : nOffset += nLength - pStream->avail_in;
136 0 : nLength = pStream->avail_in;
137 0 : return 0;
138 :
139 : default:
140 : // it is no error, if there is no input or no output
141 3879 : if ( nLength && nNewLength )
142 1 : nLastInflateError = nResult;
143 : }
144 :
145 3879 : return 0;
146 : }
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|