Branch data 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 : 18268 : 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 : 18268 : pStream(NULL)
41 : : {
42 [ + - ]: 18268 : pStream = new z_stream;
43 : : /* memset to 0 to set zalloc/opaque etc */
44 : 18268 : memset (pStream, 0, sizeof(*pStream));
45 : : sal_Int32 nRes;
46 [ + - ][ + - ]: 18268 : nRes = inflateInit2(pStream, bNoWrap ? -MAX_WBITS : MAX_WBITS);
47 [ + - - - ]: 18268 : switch (nRes)
48 : : {
49 : : case Z_OK:
50 : 18268 : 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 : 18268 : }
61 : :
62 : 18224 : Inflater::~Inflater()
63 : : {
64 [ + - ]: 18224 : end();
65 : 18224 : }
66 : :
67 : 6050 : void SAL_CALL Inflater::setInput( const Sequence< sal_Int8 >& rBuffer )
68 : : {
69 : 6050 : sInBuffer = rBuffer;
70 : 6050 : nOffset = 0;
71 : 6050 : nLength = rBuffer.getLength();
72 : 6050 : }
73 : :
74 : 6050 : sal_Bool SAL_CALL Inflater::needsDictionary( )
75 : : {
76 : 6050 : return bNeedDict;
77 : : }
78 : :
79 : 6050 : sal_Bool SAL_CALL Inflater::finished( )
80 : : {
81 : 6050 : return bFinished;
82 : : }
83 : :
84 : 12401 : sal_Int32 SAL_CALL Inflater::doInflateSegment( Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
85 : : {
86 [ + - ][ + - ]: 12401 : if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength())
[ - + ]
87 : : {
88 : : // do error handling
89 : : }
90 : 12401 : return doInflateBytes(rBuffer, nNewOffset, nNewLength);
91 : : }
92 : :
93 : 18224 : void SAL_CALL Inflater::end( )
94 : : {
95 [ + - ]: 18224 : if (pStream != NULL)
96 : : {
97 : : #if !defined Z_PREFIX
98 : 18224 : inflateEnd(pStream);
99 : : #else
100 : : z_inflateEnd(pStream);
101 : : #endif
102 : 18224 : delete pStream;
103 : : }
104 : 18224 : pStream = NULL;
105 : 18224 : }
106 : :
107 : 12401 : sal_Int32 Inflater::doInflateBytes (Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
108 : : {
109 [ - + ]: 12401 : if ( !pStream )
110 : : {
111 : 0 : nLastInflateError = Z_STREAM_ERROR;
112 : 0 : return 0;
113 : : }
114 : :
115 : 12401 : nLastInflateError = 0;
116 : :
117 : 12401 : pStream->next_in = ( unsigned char* ) ( sInBuffer.getConstArray() + nOffset );
118 : 12401 : pStream->avail_in = nLength;
119 : 12401 : pStream->next_out = reinterpret_cast < unsigned char* > ( rBuffer.getArray() + nNewOffset );
120 : 12401 : pStream->avail_out = nNewLength;
121 : :
122 : : #if !defined Z_PREFIX
123 : 12401 : sal_Int32 nResult = ::inflate(pStream, Z_PARTIAL_FLUSH);
124 : : #else
125 : : sal_Int32 nResult = ::z_inflate(pStream, Z_PARTIAL_FLUSH);
126 : : #endif
127 : :
128 [ + + - + ]: 12401 : switch (nResult)
129 : : {
130 : : case Z_STREAM_END:
131 : 6050 : bFinished = sal_True;
132 : : case Z_OK:
133 : 6351 : nOffset += nLength - pStream->avail_in;
134 : 6351 : nLength = pStream->avail_in;
135 : 6351 : 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 [ - + ][ # # ]: 6050 : if ( nLength && nNewLength )
146 : 0 : nLastInflateError = nResult;
147 : : }
148 : :
149 : 12401 : return 0;
150 : : }
151 : :
152 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|