zipios  2.2.0
Zipios – a small C++ library that provides easy access to .zip files.
zipios_common.cpp
Go to the documentation of this file.
1 /*
2  Zipios -- a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015-2019 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
30 #include "zipios_common.hpp"
31 
33 
34 
35 namespace zipios
36 {
37 
38 
51 char const g_separator = '/';
52 
53 
74 void zipRead(std::istream& is, uint32_t& value)
75 {
76  unsigned char buf[sizeof(value)];
77 
78  if(!is.read(reinterpret_cast<char *>(buf), sizeof(value)))
79  {
80  throw IOException("an I/O error while reading zip archive data from file.");
81  }
82  if(is.gcount() != sizeof(value))
83  {
84  throw IOException("EOF or an I/O error while reading zip archive data from file."); // LCOV_EXCL_LINE
85  }
86 
87  // zip data is always in little endian
88  value = (buf[0] << 0)
89  | (buf[1] << 8)
90  | (buf[2] << 16)
91  | (buf[3] << 24);
92 }
93 
94 
95 void zipRead(std::istream& is, uint16_t& value)
96 {
97  unsigned char buf[sizeof(value)];
98 
99  if(!is.read(reinterpret_cast<char *>(buf), sizeof(value)))
100  {
101  throw IOException("an I/O error while reading zip archive data from file.");
102  }
103  if(is.gcount() != sizeof(value))
104  {
105  throw IOException("EOF or an I/O error while reading zip archive data from file."); // LCOV_EXCL_LINE
106  }
107 
108  // zip data is always in little endian
109  value = (buf[0] << 0)
110  | (buf[1] << 8);
111 }
112 
113 
114 void zipRead(std::istream& is, uint8_t& value)
115 {
116  unsigned char buf[sizeof(value)];
117 
118  if(!is.read(reinterpret_cast<char *>(buf), sizeof(value)))
119  {
120  throw IOException("an I/O error while reading zip archive data from file.");
121  }
122  if(is.gcount() != sizeof(value))
123  {
124  throw IOException("EOF or an I/O error while reading zip archive data from file."); // LCOV_EXCL_LINE
125  }
126 
127  // zip data is always in little endian
128  value = buf[0];
129 }
130 
131 
132 void zipRead(std::istream& is, buffer_t& buffer, ssize_t const count)
133 {
134  buffer.resize(count);
135  if(count > 0)
136  {
137  if(!is.read(reinterpret_cast<char *>(&buffer[0]), count))
138  {
139  throw IOException("an I/O error while reading zip archive data from file.");
140  }
141  if(is.gcount() != count)
142  {
143  throw IOException("EOF or an I/O error while reading zip archive data from file."); // LCOV_EXCL_LINE
144  }
145  }
146 }
147 
148 
149 void zipRead(std::istream& is, std::string& str, ssize_t const count)
150 {
151  str.resize(count);
152  if(count > 0)
153  {
154  if(!is.read(reinterpret_cast<char *>(&str[0]), count))
155  {
156  throw IOException("an I/O error while reading zip archive data from file.");
157  }
158  if(is.gcount() != count)
159  {
160  throw IOException("EOF or an I/O error while reading zip archive data from file."); // LCOV_EXCL_LINE
161  }
162  }
163 }
164 
165 
166 void zipRead(buffer_t const& is, size_t& pos, uint32_t& value)
167 {
168  if(pos + sizeof(value) > is.size())
169  {
170  throw IOException("EOF reached while reading zip archive data from file.");
171  }
172 
173  value = (is[pos + 0] << 0)
174  | (is[pos + 1] << 8)
175  | (is[pos + 2] << 16)
176  | (is[pos + 3] << 24);
177 
178  pos += sizeof(value);
179 }
180 
181 
182 void zipRead(buffer_t const& is, size_t& pos, uint16_t& value)
183 {
184  if(pos + sizeof(value) > is.size())
185  {
186  throw IOException("EOF reached while reading zip archive data from file.");
187  }
188 
189  value = (is[pos + 0] << 0)
190  | (is[pos + 1] << 8);
191 
192  pos += sizeof(value);
193 }
194 
195 
196 void zipRead(buffer_t const& is, size_t& pos, uint8_t& value)
197 {
198  if(pos + sizeof(value) > is.size())
199  {
200  throw IOException("EOF reached while reading zip archive data from file.");
201  }
202 
203  value = is[pos];
204 
205  pos += sizeof(value);
206 }
207 
208 
209 void zipRead(buffer_t const& is, size_t& pos, buffer_t& buffer, ssize_t const count)
210 {
211  if(pos + count > is.size())
212  {
213  throw IOException("EOF reached while reading zip archive data from file.");
214  }
215 
216  buffer.clear();
217  buffer.insert(buffer.begin(), is.begin() + pos, is.begin() + pos + count);
218 
219  pos += count;
220 }
221 
222 
223 void zipRead(buffer_t const& is, size_t& pos, std::string& str, ssize_t const count)
224 {
225  if(pos + count > is.size())
226  {
227  throw IOException("EOF reached while reading zip archive data from file.");
228  }
229 
230  str.clear();
231  str.insert(str.begin(), is.begin() + pos, is.begin() + pos + count);
232 
233  pos += count;
234 }
235 
236 
237 void zipWrite(std::ostream& os, uint32_t const& value)
238 {
239  char buf[sizeof(value)];
240 
241  buf[0] = value >> 0;
242  buf[1] = value >> 8;
243  buf[2] = value >> 16;
244  buf[3] = value >> 24;
245 
246  if(!os.write(buf, sizeof(value)))
247  {
248  throw IOException("an I/O error occurred while writing to a zip archive file.");
249  }
250 }
251 
252 
253 void zipWrite(std::ostream& os, uint16_t const& value)
254 {
255  char buf[sizeof(value)];
256 
257  buf[0] = value >> 0;
258  buf[1] = value >> 8;
259 
260  if(!os.write(buf, sizeof(value)))
261  {
262  throw IOException("an I/O error occurred while writing to a zip archive file.");
263  }
264 }
265 
266 
267 void zipWrite(std::ostream& os, uint8_t const& value)
268 {
269  char buf[sizeof(value)];
270 
271  buf[0] = value;
272 
273  if(!os.write(buf, sizeof(value)))
274  {
275  throw IOException("an I/O error occurred while writing to a zip archive file.");
276  }
277 }
278 
279 
280 void zipWrite(std::ostream& os, buffer_t const& buffer)
281 {
282  if(!os.write(reinterpret_cast<char const *>(&buffer[0]), buffer.size()))
283  {
284  throw IOException("an I/O error occurred while writing to a zip archive file.");
285  }
286 }
287 
288 
289 void zipWrite(std::ostream& os, std::string const& str)
290 {
291  if(!os.write(&str[0], str.length()))
292  {
293  throw IOException("an I/O error occurred while writing to a zip archive file.");
294  }
295 }
296 
297 
298 } // zipios namespace
299 
300 // Local Variables:
301 // mode: cpp
302 // indent-tabs-mode: nil
303 // c-basic-offset: 4
304 // tab-width: 4
305 // End:
306 
307 // vim: ts=4 sw=4 et
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
void zipRead(std::istream &is, uint32_t &value)
void zipWrite(std::ostream &os, uint32_t const &value)
char const g_separator
The character used as the filename separator.
An IOException is used to signal an I/O error.
std::vector< unsigned char > buffer_t
A buffer of characters.
Various functions used throughout the library.