/src/libreoffice/package/source/zipapi/InflaterBytesZlib.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
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 | | |
10 | | #include <cstring> |
11 | | #include <InflaterBytesZlib.hxx> |
12 | | #include <string.h> |
13 | | #include <zlib.h> |
14 | | |
15 | | using namespace ZipUtils; |
16 | | |
17 | | InflaterBytesZlib::InflaterBytesZlib() |
18 | 0 | : bFinished(false), |
19 | 0 | nOffset(0), |
20 | 0 | nLength(0), |
21 | 0 | sInBuffer(nullptr), |
22 | 0 | pStream(std::make_unique<z_stream>()) |
23 | 0 | { |
24 | 0 | memset(pStream.get(), 0, sizeof(*pStream)); |
25 | 0 | sal_Int32 nRes = inflateInit2(pStream.get(), -MAX_WBITS); |
26 | 0 | switch (nRes) |
27 | 0 | { |
28 | 0 | case Z_OK: |
29 | 0 | break; |
30 | 0 | case Z_MEM_ERROR: |
31 | 0 | case Z_STREAM_ERROR: |
32 | 0 | pStream.reset(); |
33 | 0 | break; |
34 | 0 | default: |
35 | 0 | break; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | 0 | InflaterBytesZlib::~InflaterBytesZlib() { end(); } |
40 | | |
41 | | void InflaterBytesZlib::setInput(const sal_Int8* rBuffer, sal_Int32 nBufLen) |
42 | 0 | { |
43 | 0 | sInBuffer = rBuffer; |
44 | 0 | nOffset = 0; |
45 | 0 | nLength = nBufLen; |
46 | 0 | } |
47 | | |
48 | | sal_Int32 InflaterBytesZlib::doInflateSegment(sal_Int8* pOutBuffer, sal_Int32 nBufLen, sal_Int32 nNewOffset, sal_Int32 nNewLength) |
49 | 0 | { |
50 | 0 | if (nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > nBufLen) |
51 | 0 | { |
52 | 0 | return 0; |
53 | 0 | } |
54 | 0 | return doInflateBytes(pOutBuffer, nNewOffset, nNewLength); |
55 | 0 | } |
56 | | |
57 | | void InflaterBytesZlib::end() |
58 | 0 | { |
59 | 0 | if (pStream) |
60 | 0 | { |
61 | 0 | #if !defined Z_PREFIX |
62 | 0 | inflateEnd(pStream.get()); |
63 | | #else |
64 | | z_inflateEnd(pStream.get()); |
65 | | #endif |
66 | 0 | pStream.reset(); |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | sal_Int32 InflaterBytesZlib::doInflateBytes(sal_Int8* pOutBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength) |
71 | 0 | { |
72 | 0 | if (!pStream) |
73 | 0 | return 0; |
74 | | |
75 | 0 | pStream->next_in = reinterpret_cast<const unsigned char*>(sInBuffer + nOffset); |
76 | 0 | pStream->avail_in = nLength; |
77 | 0 | pStream->next_out = reinterpret_cast<unsigned char*>(pOutBuffer + nNewOffset); |
78 | 0 | pStream->avail_out = nNewLength; |
79 | |
|
80 | 0 | #if !defined Z_PREFIX |
81 | 0 | sal_Int32 nResult = ::inflate(pStream.get(), Z_PARTIAL_FLUSH); |
82 | | #else |
83 | | sal_Int32 nResult = ::z_inflate(pStream.get(), Z_PARTIAL_FLUSH); |
84 | | #endif |
85 | |
|
86 | 0 | switch (nResult) |
87 | 0 | { |
88 | 0 | case Z_STREAM_END: |
89 | 0 | bFinished = true; |
90 | 0 | [[fallthrough]]; |
91 | 0 | case Z_OK: |
92 | 0 | nOffset += nLength - pStream->avail_in; |
93 | 0 | nLength = pStream->avail_in; |
94 | 0 | return nNewLength - pStream->avail_out; |
95 | 0 | case Z_NEED_DICT: |
96 | 0 | nOffset += nLength - pStream->avail_in; |
97 | 0 | nLength = pStream->avail_in; |
98 | 0 | return 0; |
99 | 0 | default: |
100 | 0 | break; |
101 | 0 | } |
102 | 0 | return 0; |
103 | 0 | } |
104 | | |
105 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |