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