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