Debian Codegraph @

src/tools/zip/input.cpp

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw: tools
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
18 *
19 *
20 * All Rights Reserved.
21 *
22 * For minor contributions see the git repository.
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 * in which case the provisions of the LGPLv2+ are applicable
27 * instead of those above.
28 */
29
30 #include <string.h>
31 #include <iostream>
32
33 #include "zip_internal.h"
34
35 #include "input.h"
36
37 namespace libmwaw_zip
38 {
39 //
40 // InputStream
41 //
42 InputStream::~InputStream()
43 {
44 }
45
46 unsigned char InputStream::readU8()
47 {
48 unsigned long nRead;
49 unsigned char const *data = read(1, nRead);
50 if (!data || nRead != 1) return 0;
51 return data[0];
52 }
53 unsigned short InputStream::readU16()
54 {
55 unsigned long nRead;
56 unsigned char const *data = read(2, nRead);
57 if (!data || nRead != 2) return 0;
58 return static_cast<unsigned short>((data[0]<<8)+data[1]);
59 }
60 unsigned int InputStream::readU32()
61 {
62 unsigned long nRead;
63 unsigned char const *data = read(4, nRead);
64 if (!data || nRead != 4) return 0;
65 return static_cast<unsigned int>((data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3]);
66 }
67 char InputStream::read8()
68 {
69 unsigned long nRead;
70 unsigned char const *data = read(1, nRead);
71 if (!data || nRead != 1) return 0;
72 return char(data[0]);
73 }
74 short InputStream::read16()
75 {
76 unsigned long nRead;
77 unsigned char const *data = read(2, nRead);
78 if (!data || nRead != 2) return 0;
79 return short((data[0]<<8)+data[1]);
80 }
81 int InputStream::read32()
82 {
83 unsigned long nRead;
84 unsigned char const *data = read(4, nRead);
85 if (!data || nRead != 4) return 0;
86 return int((data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3]);
87 }
88
89 int InputStream::seek(long _offset, SeekType seekType)
90 {
91 long sz = length();
92 if (seekType == SK_CUR)
93 m_offset += _offset;
94 else if (seekType == SK_SET)
95 m_offset = _offset;
96 else if (seekType == SK_END)
97 m_offset = sz+_offset;
98
99 if (m_offset < 0) {
100 m_offset = 0;
101 return 1;
102 }
103 if (m_offset > sz) {
104 m_offset = sz;
105 return 1;
106 }
107 return 0;
108 }
109
110 //
111 // StringStream
112 //
113 StringStream::StringStream(const unsigned char *data, const unsigned long dataSize)
114 : InputStream()
115 , m_buffer(dataSize)
116 {
117 memcpy(&m_buffer[0], data, dataSize);
118 }
119
120 const unsigned char *StringStream::read(unsigned long numBytes, unsigned long &numBytesRead)
121 {
122 numBytesRead = 0;
123
124 if (numBytes == 0)
125 return 0;
126
127 unsigned long numBytesToRead;
128
129 if ((static_cast<unsigned long>(m_offset)+numBytes) < m_buffer.size())
130 numBytesToRead = numBytes;
131 else
132 numBytesToRead = static_cast<unsigned long>(long(m_buffer.size()) - m_offset);
133
134 numBytesRead = numBytesToRead; // about as paranoid as we can be..
135
136 if (numBytesToRead == 0)
137 return 0;
138
139 long oldOffset = m_offset;
140 m_offset += numBytesToRead;
141
142 return &m_buffer[size_t(oldOffset)];
143 }
144 //
145 // FileStream
146 //
147 FileStream::FileStream(char const *path)
148 : InputStream()
149 , m_file(0)
150 , m_isOk(true)
151 , m_buffer()
152 , m_bufferPos(0)
153 {
154 m_file = fopen(path,"r");
155 if (m_file)
156 return;
157 #ifdef DEBUG
158 std::cerr << "FileStream:FileStream can not open " << path << "\n";
159 #endif
160 m_isOk = false;
161 }
162
163 FileStream::~FileStream()
164 {
165 if (!m_isOk) return;
166 if (m_file) fclose(m_file);
167 }
168
169 unsigned char const *FileStream::read(unsigned long numBytes, unsigned long &numBytesRead)
170 {
171 numBytesRead = 0;
172 if (!m_isOk || !m_file)
173 return 0;
174 long lastPos = m_offset+long(numBytes);
175 long fileLength = length();
176 if (lastPos > fileLength) lastPos = fileLength;
177 // check if the buffer is or not ok
178 auto bufSize = long(m_buffer.size());
179 if (m_offset < m_bufferPos || lastPos > m_bufferPos+bufSize) {
180 auto numToRead = size_t(numBytes);
181 if (numToRead < 4096 && m_offset+4096 <= fileLength)
182 numToRead = 4096;
183 else if (numToRead < 4096)
184 numToRead = size_t(fileLength-m_offset);
185 if (numToRead==0)
186 return 0;
187 // try to read numToRead bytes
188 m_bufferPos = m_offset;
189 m_buffer.resize(size_t(numToRead));
190
191 #if 0
192 MWAW_DEBUG_MSG(("FileStream::read called with %ld[%ld]: read %ld bytes\n", m_offset, numBytes, numToRead));
193 #endif
194 if (fseek(m_file, m_offset, SEEK_SET)==-1) {
195 MWAW_DEBUG_MSG(("FileStream::seek error when going to offset position\n"));
196 return 0;
197 }
198 size_t nRead = fread(&(m_buffer[0]), 1, numToRead, m_file);
199 if (nRead != numToRead) {
200 MWAW_DEBUG_MSG(("FileStream::read get error when reading data\n"));
201 m_buffer.resize(size_t(nRead));
202 }
203 }
204
205 bufSize = long(m_buffer.size());
206 if (!bufSize)
207 return 0;
208 numBytesRead = static_cast<unsigned long>(m_bufferPos + bufSize - m_offset);
209 if (numBytesRead > numBytes)
210 numBytesRead = numBytes;
211 unsigned char const *res = &(m_buffer[size_t(m_offset-m_bufferPos)]);
212 m_offset += long(numBytesRead);
213 return res;
214 }
215
216 long FileStream::length()
217 {
218 if (!m_isOk || !m_file) return 0;
219 fseek(m_file, 0, SEEK_END);
220 return long(ftell(m_file));
221 }
222 }
223 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: