Debian Codegraph @

src/lib/ABWZlibStream.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libabw 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 <zlib.h>
11 #include "ABWZlibStream.h"
12 #include <string.h> // for memcpy
13
14 #define BLOCK_SIZE 16384
15
16 namespace libabw
17 {
18
19 namespace
20 {
21
22 static bool getInflatedBuffer(librevenge::RVNGInputStream *input, std::vector<unsigned char> &buffer)
23 {
24 int ret;
25 z_stream strm;
26 unsigned char in[BLOCK_SIZE];
27 unsigned char out[BLOCK_SIZE];
28
29 strm.zalloc = Z_NULL;
30 strm.zfree = Z_NULL;
31 strm.opaque = Z_NULL;
32 strm.avail_in = 0;
33 strm.next_in = Z_NULL;
34 ret = inflateInit2(&strm, 16 + MAX_WBITS);
35 if (Z_OK != ret)
36 return false;
37
38 do
39 {
40 unsigned long numBytesRead(0);
41 const unsigned char *p = input->read(BLOCK_SIZE, numBytesRead);
42 strm.avail_in = uInt(numBytesRead);
43 if (!strm.avail_in)
44 break;
45 memcpy(in, p, strm.avail_in);
46 strm.next_in = in;
47
48 do
49 {
50 strm.avail_out = BLOCK_SIZE;
51 strm.next_out = out;
52 ret = inflate(&strm, Z_NO_FLUSH);
53 switch (ret)
54 {
55 case Z_NEED_DICT:
56 case Z_DATA_ERROR:
57 case Z_MEM_ERROR:
58 case Z_STREAM_ERROR:
59 (void)inflateEnd(&strm);
60 return false;
61 default:
62 break;
63 }
64 for (unsigned i = 0; i < BLOCK_SIZE - strm.avail_out; ++i)
65 buffer.push_back(out[i]);
66 }
67 while (!strm.avail_out);
68 }
69 while (Z_STREAM_END != ret);
70
71 (void)inflateEnd(&strm);
72 input->seek(0, librevenge::RVNG_SEEK_SET);
73 if (Z_STREAM_END == ret)
74 return true;
75 return false;
76 }
77
78 }
79
80 ABWZlibStream::ABWZlibStream(librevenge::RVNGInputStream *input) :
81 librevenge::RVNGInputStream(),
82 m_input(nullptr),
83 m_offset(0),
84 m_buffer()
85 {
86 if (!getInflatedBuffer(input, m_buffer))
87 {
88 if (input)
89 {
90 input->seek(0, librevenge::RVNG_SEEK_CUR);
91 m_input = input;
92 }
93 else
94 m_buffer.clear();
95 }
96 }
97
98 const unsigned char *ABWZlibStream::read(unsigned long numBytes, unsigned long &numBytesRead)
99 {
100 if (m_input)
101 return m_input->read(numBytes, numBytesRead);
102
103 numBytesRead = 0;
104
105 if (numBytes == 0)
106 return nullptr;
107
108 if (m_offset < 0)
109 return nullptr;
110
111 const unsigned long bufSize = m_buffer.size();
112 const unsigned long pos = static_cast<unsigned long>(m_offset);
113 const unsigned long remaining = pos < bufSize ? bufSize - pos : 0;
114 const unsigned long numBytesToRead = numBytes < remaining ? numBytes : remaining;
115
116 numBytesRead = numBytesToRead; // about as paranoid as we can be..
117
118 if (numBytesToRead == 0)
119 return nullptr;
120
121 long oldOffset = m_offset;
122 m_offset += numBytesToRead;
123
124 return &m_buffer[size_t(oldOffset)];
125 }
126
127 int ABWZlibStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType)
128 {
129 if (m_input)
130 return m_input->seek(offset, seekType);
131
132 if (seekType == librevenge::RVNG_SEEK_CUR)
133 m_offset += offset;
134 else if (seekType == librevenge::RVNG_SEEK_SET)
135 m_offset = offset;
136
137 if (m_offset < 0)
138 {
139 m_offset = 0;
140 return 1;
141 }
142 if ((long)m_offset > (long)m_buffer.size())
143 {
144 m_offset = (long) m_buffer.size();
145 return 1;
146 }
147
148 return 0;
149 }
150
151 long ABWZlibStream::tell()
152 {
153 if (m_input)
154 return m_input->tell();
155
156 return m_offset;
157 }
158
159 bool ABWZlibStream::isEnd()
160 {
161 if (m_input)
162 return m_input->isEnd();
163
164 if ((long)m_offset >= (long)m_buffer.size())
165 return true;
166
167 return false;
168 }
169
170 } // namespace libabw
171 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */