]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/zipios++/ziphead.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / zipios++ / ziphead.h
1 #ifndef ZIPHEAD_H
2 #define ZIPHEAD_H
3
4 #include "zipios++/zipios-config.h"
5
6 #include "zipios++/meta-iostreams.h"
7 #include <string>
8 #include <vector>
9
10 #include "zipios++/fileentry.h"
11 #include "zipios++/zipios_defs.h"
12
13 namespace zipios {
14
15 using std::streampos ;
16
17 class ZipCDirEntry ;
18
19 /** A concrete implementation of the abstract FileEntry base class for
20  ZipFile entries, specifically for representing the information
21  present in the local headers of file entries in a zip file. */
22 class ZipLocalEntry : public FileEntry {
23   friend istream &operator>> ( istream &is, ZipLocalEntry &zcdh ) ;
24   friend ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
25   friend bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
26 public:
27   inline ZipLocalEntry( const string &_filename = "",
28                         const vector< unsigned char > &_extra_field = 
29                         vector< unsigned char >() ) 
30     : gp_bitfield( 0 ),
31       _valid( false ) { 
32     setDefaultExtract() ;
33     setName( _filename ) ;
34     setExtra( _extra_field ) ; 
35   }
36
37   void setDefaultExtract() ;
38   inline ZipLocalEntry &operator=( const class ZipLocalEntry &src ) ;
39   virtual string getComment() const ;
40   virtual uint32 getCompressedSize() const ;
41   virtual uint32 getCrc() const ;
42   virtual vector< unsigned char > getExtra() const ;
43   virtual StorageMethod getMethod() const ;
44   virtual string getName() const ;
45   virtual string getFileName() const ;
46   virtual uint32 getSize() const ;
47   virtual int getTime() const ;
48   virtual bool isValid() const ;
49   
50   virtual bool isDirectory() const ;
51   
52   virtual void setComment( const string &comment ) ;
53   virtual void setCompressedSize( uint32 size ) ;
54   virtual void setCrc( uint32 crc ) ;
55   virtual void setExtra( const vector< unsigned char > &extra ) ;
56   virtual void setMethod( StorageMethod method ) ;
57   virtual void setName( const string &name ) ;
58   virtual void setSize( uint32 size ) ;
59   virtual void setTime( int time ) ;
60   
61   virtual string toString() const ;
62
63   int getLocalHeaderSize() const ;
64
65   bool trailingDataDescriptor() const ;
66
67   virtual FileEntry *clone() const ;
68
69   virtual ~ZipLocalEntry() {}
70 protected:
71   static const uint32 signature ;
72   uint16 extract_version ;
73   uint16 gp_bitfield     ;
74   uint16 compress_method ;
75   uint16 last_mod_ftime  ;
76   uint16 last_mod_fdate  ;
77   uint32 crc_32          ;
78   uint32 compress_size   ;
79   uint32 uncompress_size ;
80   uint16 filename_len    ;
81   uint16 extra_field_len ;
82
83   string filename ;
84   vector< unsigned char > extra_field ; 
85
86   bool _valid ;
87 };
88
89 /** A struct containing fields for the entries in a zip file data
90  descriptor, that trails the compressed data in files that were
91  created by streaming, ie where the zip compressor cannot seek back
92  to the local header and store the data. */
93 struct DataDescriptor {
94   uint32 crc_32          ;
95   uint32 compress_size   ;
96   uint32 uncompress_size ;
97 };
98
99 /** Specialization of ZipLocalEntry, that add fields for storing the
100     extra information, that is only present in the entries in the zip
101     central directory and not in the local entry headers. */
102 class ZipCDirEntry : public ZipLocalEntry {
103 friend istream &operator>> ( istream &is, ZipCDirEntry &zcdh ) ;
104 friend ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
105 friend bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
106 public:
107
108   inline ZipCDirEntry(  const string &_filename = "",
109                         const string &_file_comment = "",
110                         const vector< unsigned char > &_extra_field = 
111                                         vector< unsigned char >() ) 
112     : ZipLocalEntry   ( _filename, _extra_field ),
113       disk_num_start  ( 0x0 ),
114       intern_file_attr( 0x0 ),
115       extern_file_attr( 0x81B40000 ) 
116     // FIXME: I don't understand the external mapping, simply
117     // copied value for a file with -rw-rw-r-- permissions
118     // compressed with info-zip
119   { 
120     setComment( _file_comment ) ;
121     setDefaultWriter() ;
122   } 
123
124   void setDefaultWriter() ;
125
126   inline ZipCDirEntry &operator=( const class ZipCDirEntry &src ) ;
127   virtual string toString() const ;
128
129   virtual string getComment() const ;
130
131   virtual void setComment( const string &comment ) ;
132
133   virtual uint32 getLocalHeaderOffset() const ;
134   virtual void   setLocalHeaderOffset( uint32 offset ) ;
135
136   int getCDirHeaderSize() const ;
137
138   virtual FileEntry *clone() const ;
139
140   virtual ~ZipCDirEntry() {}
141 private:
142   static const uint32 signature ;
143   uint16 writer_version      ;
144
145   uint16 file_comment_len    ; 
146   uint16 disk_num_start      ;
147   uint16 intern_file_attr    ;
148   uint32 extern_file_attr    ;
149
150   uint32 rel_offset_loc_head ;
151
152   string file_comment ;
153 };
154
155 /** The end of the Central directory structure. This structure is
156     stored in the end of the zipfile, and contains information about
157     the zipfile, including the position of the start of the central
158     directory. */
159 class EndOfCentralDirectory {
160   friend ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
161 public:
162   explicit EndOfCentralDirectory( const string &_zip_comment = "", 
163                                   uint16 _disk_num = 0, uint16 _cdir_disk_num = 0, 
164                                   uint16 _cdir_entries = 0, 
165                                   uint16 _cdir_tot_entries = 0, 
166                                   uint32 _cdir_size = 0, uint32 _cdir_offset = 0 )
167     :   disk_num         ( _disk_num           ),
168         cdir_disk_num    ( _cdir_disk_num      ),
169         cdir_entries     ( _cdir_entries       ),
170         cdir_tot_entries ( _cdir_tot_entries   ),
171         cdir_size        ( _cdir_size          ),
172         cdir_offset      ( _cdir_offset        ),
173         zip_comment_len  ( static_cast< uint16 >( _zip_comment.size() ) ),
174         zip_comment      ( _zip_comment        )  {}
175
176   uint32    offset() const          { return cdir_offset ;          }
177   uint16    totalCount() const      { return cdir_tot_entries ;     }
178   void setCDirSize( uint32 size )   { cdir_size = size ;            }
179   void setOffset( uint32 offset )   { cdir_offset = offset ;        }
180
181   void setTotalCount( uint16 c )    { cdir_entries = c ; cdir_tot_entries = c ; }
182   int  eocdOffSetFromEnd() const { return static_cast< int >( eocd_offset_from_end ) ; }
183   bool read( vector<unsigned char> &buf, int pos ) ;
184 private:
185   static const uint32 signature;
186   uint16 disk_num         ;
187   uint16 cdir_disk_num    ;
188   uint16 cdir_entries     ;
189   uint16 cdir_tot_entries ;
190   uint32 cdir_size        ;
191   uint32 cdir_offset      ;
192   uint16 zip_comment_len  ;
193   
194   streampos eocd_offset_from_end ; // Not a Zip defined field
195   string zip_comment;
196   bool checkSignature( unsigned char *buf ) const ;
197   inline bool checkSignature( uint32 sig ) const ;
198 };
199
200
201 bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
202 inline bool operator== ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) {
203   return zlh == ze ;
204 }
205 inline bool operator!= ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) {
206   return ! ( zlh == ze ) ;
207 }
208 inline bool operator!= ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) {
209   return ! ( zlh == ze ) ;
210 }
211
212 // Inline member functions
213
214 ZipCDirEntry &ZipCDirEntry::operator=( const class ZipCDirEntry &src ) {
215   writer_version      = src.writer_version      ;
216   extract_version     = src.extract_version     ;
217   gp_bitfield         = src.gp_bitfield         ;
218   compress_method     = src.compress_method     ;
219   last_mod_ftime      = src.last_mod_ftime      ;
220   last_mod_fdate      = src.last_mod_fdate      ;
221   crc_32              = src.crc_32              ;
222   compress_size       = src.compress_size       ; 
223   uncompress_size     = src.uncompress_size     ;
224   filename_len        = src.filename_len        ;
225   extra_field_len     = src.extra_field_len     ;
226   file_comment_len    = src.file_comment_len    ; 
227   disk_num_start      = src.disk_num_start      ;
228   intern_file_attr    = src.intern_file_attr    ;
229   extern_file_attr    = src.extern_file_attr    ;
230   rel_offset_loc_head = src.rel_offset_loc_head ;
231
232   filename     = src.filename     ;
233   extra_field  = src.extra_field  ; 
234   file_comment = src.file_comment ;
235
236   return *this ;
237 }
238
239 bool EndOfCentralDirectory::checkSignature ( uint32 sig ) const {
240   return signature == sig ;
241 }
242
243
244 } // namespace
245
246 #endif
247
248
249 /** \file
250     Header file containing classes and functions for reading the central
251     directory and local header fields in a zip archive.
252 */
253
254 /*
255   Zipios++ - a small C++ library that provides easy access to .zip files.
256   Copyright (C) 2000  Thomas Søndergaard
257   
258   This library is free software; you can redistribute it and/or
259   modify it under the terms of the GNU Lesser General Public
260   License as published by the Free Software Foundation; either
261   version 2 of the License, or (at your option) any later version.
262   
263   This library is distributed in the hope that it will be useful,
264   but WITHOUT ANY WARRANTY; without even the implied warranty of
265   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
266   Lesser General Public License for more details.
267   
268   You should have received a copy of the GNU Lesser General Public
269   License along with this library; if not, write to the Free Software
270   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
271 */