LibPkg
zipfile.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 //
3 // Copyright 2003-2020 Graham Shaw
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef LIBPKG_ZIPFILE
18 #define LIBPKG_ZIPFILE
19 
20 #include <vector>
21 #include <map>
22 #include <string>
23 #include <fstream>
24 #include <stdexcept>
25 
26 namespace pkg {
27 
28 using std::string;
29 
31 class zipfile
32 {
33 public:
34  class file_info;
35  class extra_info;
36  class riscos_info;
37 
39  typedef unsigned short uint16;
40 
42  typedef unsigned long uint32;
43 
44  class not_found;
46  class zlib_error;
47 private:
49  string _pathname;
50 
52  mutable std::fstream _zfs;
53 
57  std::vector<file_info*> _directory;
58 public:
62  zipfile(const string& pathname);
63 private:
68  zipfile(const zipfile&);
69 public:
71  ~zipfile();
72 private:
77  zipfile& operator=(const zipfile&);
78 public:
82  unsigned int size() const;
83 
88  const file_info& operator[](unsigned int index) const;
89 
95  const file_info* find(const string& pathname) const;
96 
102  void extract(const string& src_pathname,const string& dst_pathname) const;
103 };
104 
107 {
108 private:
110  uint32 _offset;
111 
113  uint16 _xversion;
114 
116  uint16 _gpbits;
117 
119  uint16 _method;
120 
122  uint16 _modtime;
123 
125  uint16 _moddate;
126 
128  uint32 _crc32;
129 
131  uint32 _csize;
132 
134  uint32 _usize;
135 
137  string _pathname;
138 
140  std::map<uint16,extra_info*> _extra;
141 public:
143  file_info();
144 
148  file_info(std::istream& in);
149 
153  file_info(const file_info&);
154 
156  ~file_info();
157 
161  file_info& operator=(const file_info&);
162 
166  uint32 offset() const
167  { return _offset; }
168 
172  uint16 method() const
173  { return _method; }
174 
178  uint32 csize() const
179  { return _csize; }
180 
184  uint32 usize() const
185  { return _usize; }
186 
192  const string& pathname() const
193  { return _pathname; }
194 
198  template<class extra_type>
199  extra_type* create_extra();
200 
204  template<class extra_type>
205  const extra_type* find_extra() const;
206 private:
210  void read(std::istream& in);
211 
216  void read_extra(std::istream& in,int length);
217 };
218 
221 {
222 public:
224  extra_info();
225 
227  virtual ~extra_info();
228 };
229 
232  public zipfile::extra_info
233 {
234 private:
236  uint32 _sig;
237 
239  uint32 _loadaddr;
240 
242  uint32 _execaddr;
243 
245  uint32 _attr;
246 public:
248  riscos_info();
249 
253  riscos_info(std::istream& in);
254 
256  virtual ~riscos_info();
257 
262  bool valid() const
263  { return _sig==0x30435241; }
264 
268  uint32 loadaddr() const
269  { return _loadaddr; }
270 
274  uint32 execaddr() const
275  { return _execaddr; }
276 
280  uint32 attr() const
281  { return _attr; }
282 private:
286  void read(std::istream& in);
287 public:
292  static uint16 tag()
293  { return 0x4341; }
294 };
295 
296 template<class extra_type>
298 {
299  extra_info*& einfo=_extra[extra_type::tag()];
300  if (!einfo) einfo=new extra_type();
301  return dynamic_cast<extra_type*>(einfo);
302 }
303 
304 template<class extra_type>
305 const extra_type* zipfile::file_info::find_extra() const
306 {
307  std::map<uint16,extra_info*>::const_iterator f=
308  _extra.find(extra_type::tag());
309  return (f!=_extra.end())?dynamic_cast<const extra_type*>(f->second):0;
310 }
311 
314  public std::runtime_error
315 {
316 public:
320  not_found(const string& pathname);
321 };
322 
325  public std::runtime_error
326 {
327 public:
331  unsupported_compression_method(unsigned int method);
332 };
333 
336  public std::runtime_error
337 {
338 public:
342  zlib_error(int code);
343 
348  static const char* make_message(int code);
349 };
350 
351 }; /* namespace pkg */
352 
353 #endif
static uint16 tag()
Get tag.
Definition: zipfile.h:292
An exception class for reporting not found errors.
Definition: zipfile.h:313
extra_type * create_extra()
Create extra information record.
Definition: zipfile.h:297
zipfile(const string &pathname)
Construct zip file object.
Definition: zipfile.cc:100
unsigned long uint32
A type to represent an unsigned 32-bit integer.
Definition: zipfile.h:42
unsigned int size() const
Get number of file information records.
Definition: zipfile.cc:144
uint32 usize() const
Get uncompressed file size.
Definition: zipfile.h:184
A class to represent a file information record from a zip file.
Definition: zipfile.h:106
A base class to represent an extra information record from a zip file.
Definition: zipfile.h:220
~zipfile()
Destroy zip file object.
Definition: zipfile.cc:122
uint32 execaddr() const
Get RISC OS execution address.
Definition: zipfile.h:274
bool valid() const
Determine whether extra information record is valid.
Definition: zipfile.h:262
uint16 method() const
Get compression method.
Definition: zipfile.h:172
The namespace used to hold the package management library.
Definition: auto_dir.cc:23
const string & pathname() const
Get pathname.
Definition: zipfile.h:192
uint32 attr() const
Get RISC OS attributes.
Definition: zipfile.h:280
unsigned short uint16
A type to represent an unsigned 16-bit integer.
Definition: zipfile.h:36
uint32 csize() const
Get compressed file size.
Definition: zipfile.h:178
const file_info * find(const string &pathname) const
Find const file information record for pathname.
Definition: zipfile.cc:137
An exception class for reporting errors in zlib.
Definition: zipfile.h:335
A class to represent a RISC OS extra information record from a zip file.
Definition: zipfile.h:231
const char * dst_pathname
Definition: path_table.cc:32
uint32 loadaddr() const
Get RISC OS load address.
Definition: zipfile.h:268
const file_info & operator[](unsigned int index) const
Get const file information record at index.
Definition: zipfile.cc:132
uint32 offset() const
Get offset to file data.
Definition: zipfile.h:166
const extra_type * find_extra() const
Find extra information record.
Definition: zipfile.h:305
const char * src_pathname
Definition: path_table.cc:31
void extract(const string &src_pathname, const string &dst_pathname) const
Extract file from zip file.
Definition: zipfile.cc:149
An exception class for reporting unsupported compression method errors.
Definition: zipfile.h:324
An interface class to represent a zip file.
Definition: zipfile.h:31

Reference Manual LibPkg Version 0.9.0 (4 Sep 2020)