Zipios++
|
00001 #ifndef ZIPHEAD_H 00002 #define ZIPHEAD_H 00003 00004 #include "zipios++/zipios-config.h" 00005 00006 #include "zipios++/meta-iostreams.h" 00007 #include <string> 00008 #include <vector> 00009 00010 #include "zipios++/fileentry.h" 00011 #include "zipios++/zipios_defs.h" 00012 00013 namespace zipios { 00014 00015 using std::streampos ; 00016 00017 class ZipCDirEntry ; 00018 00022 class ZipLocalEntry : public FileEntry { 00023 friend istream &operator>> ( istream &is, ZipLocalEntry &zcdh ) ; 00024 friend ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ; 00025 friend bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ; 00026 public: 00027 inline ZipLocalEntry( const string &_filename = "", 00028 const vector< unsigned char > &_extra_field = 00029 vector< unsigned char >() ) 00030 : gp_bitfield( 0 ), 00031 _valid( false ) { 00032 setDefaultExtract() ; 00033 setName( _filename ) ; 00034 setExtra( _extra_field ) ; 00035 } 00036 00037 void setDefaultExtract() ; 00038 inline ZipLocalEntry &operator=( const class ZipLocalEntry &src ) ; 00039 virtual string getComment() const ; 00040 virtual uint32 getCompressedSize() const ; 00041 virtual uint32 getCrc() const ; 00042 virtual vector< unsigned char > getExtra() const ; 00043 virtual StorageMethod getMethod() const ; 00044 virtual string getName() const ; 00045 virtual string getFileName() const ; 00046 virtual uint32 getSize() const ; 00047 virtual int getTime() const ; 00048 virtual bool isValid() const ; 00049 00050 virtual bool isDirectory() const ; 00051 00052 virtual void setComment( const string &comment ) ; 00053 virtual void setCompressedSize( uint32 size ) ; 00054 virtual void setCrc( uint32 crc ) ; 00055 virtual void setExtra( const vector< unsigned char > &extra ) ; 00056 virtual void setMethod( StorageMethod method ) ; 00057 virtual void setName( const string &name ) ; 00058 virtual void setSize( uint32 size ) ; 00059 virtual void setTime( int time ) ; 00060 00061 virtual string toString() const ; 00062 00063 int getLocalHeaderSize() const ; 00064 00065 bool trailingDataDescriptor() const ; 00066 00067 virtual FileEntry *clone() const ; 00068 00069 virtual ~ZipLocalEntry() {} 00070 protected: 00071 static const uint32 signature ; 00072 uint16 extract_version ; 00073 uint16 gp_bitfield ; 00074 uint16 compress_method ; 00075 uint16 last_mod_ftime ; 00076 uint16 last_mod_fdate ; 00077 uint32 crc_32 ; 00078 uint32 compress_size ; 00079 uint32 uncompress_size ; 00080 uint16 filename_len ; 00081 uint16 extra_field_len ; 00082 00083 string filename ; 00084 vector< unsigned char > extra_field ; 00085 00086 bool _valid ; 00087 }; 00088 00093 struct DataDescriptor { 00094 uint32 crc_32 ; 00095 uint32 compress_size ; 00096 uint32 uncompress_size ; 00097 }; 00098 00102 class ZipCDirEntry : public ZipLocalEntry { 00103 friend istream &operator>> ( istream &is, ZipCDirEntry &zcdh ) ; 00104 friend ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ; 00105 friend bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ; 00106 public: 00107 00108 inline ZipCDirEntry( const string &_filename = "", 00109 const string &_file_comment = "", 00110 const vector< unsigned char > &_extra_field = 00111 vector< unsigned char >() ) 00112 : ZipLocalEntry ( _filename, _extra_field ), 00113 disk_num_start ( 0x0 ), 00114 intern_file_attr( 0x0 ), 00115 extern_file_attr( 0x81B40000 ) 00116 // FIXME: I don't understand the external mapping, simply 00117 // copied value for a file with -rw-rw-r-- permissions 00118 // compressed with info-zip 00119 { 00120 setComment( _file_comment ) ; 00121 setDefaultWriter() ; 00122 } 00123 00124 void setDefaultWriter() ; 00125 00126 inline ZipCDirEntry &operator=( const class ZipCDirEntry &src ) ; 00127 virtual string toString() const ; 00128 00129 virtual string getComment() const ; 00130 00131 virtual void setComment( const string &comment ) ; 00132 00133 virtual uint32 getLocalHeaderOffset() const ; 00134 virtual void setLocalHeaderOffset( uint32 offset ) ; 00135 00136 int getCDirHeaderSize() const ; 00137 00138 virtual FileEntry *clone() const ; 00139 00140 virtual ~ZipCDirEntry() {} 00141 private: 00142 static const uint32 signature ; 00143 uint16 writer_version ; 00144 00145 uint16 file_comment_len ; 00146 uint16 disk_num_start ; 00147 uint16 intern_file_attr ; 00148 uint32 extern_file_attr ; 00149 00150 uint32 rel_offset_loc_head ; 00151 00152 string file_comment ; 00153 }; 00154 00159 class EndOfCentralDirectory { 00160 friend ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ; 00161 public: 00162 explicit EndOfCentralDirectory( const string &_zip_comment = "", 00163 uint16 _disk_num = 0, uint16 _cdir_disk_num = 0, 00164 uint16 _cdir_entries = 0, 00165 uint16 _cdir_tot_entries = 0, 00166 uint32 _cdir_size = 0, uint32 _cdir_offset = 0 ) 00167 : disk_num ( _disk_num ), 00168 cdir_disk_num ( _cdir_disk_num ), 00169 cdir_entries ( _cdir_entries ), 00170 cdir_tot_entries ( _cdir_tot_entries ), 00171 cdir_size ( _cdir_size ), 00172 cdir_offset ( _cdir_offset ), 00173 zip_comment_len ( _zip_comment.size() ), 00174 zip_comment ( _zip_comment ) {} 00175 00176 uint32 offset() const { return cdir_offset ; } 00177 uint16 totalCount() const { return cdir_tot_entries ; } 00178 void setCDirSize( uint32 size ) { cdir_size = size ; } 00179 void setOffset( uint32 offset ) { cdir_offset = offset ; } 00180 00181 void setTotalCount( uint16 c ) { cdir_entries = c ; cdir_tot_entries = c ; } 00182 int eocdOffSetFromEnd() const { return eocd_offset_from_end ; } 00183 bool read( vector<unsigned char> &buf, int pos ) ; 00184 private: 00185 static const uint32 signature; 00186 uint16 disk_num ; 00187 uint16 cdir_disk_num ; 00188 uint16 cdir_entries ; 00189 uint16 cdir_tot_entries ; 00190 uint32 cdir_size ; 00191 uint32 cdir_offset ; 00192 uint16 zip_comment_len ; 00193 00194 streampos eocd_offset_from_end ; // Not a Zip defined field 00195 string zip_comment; 00196 bool checkSignature( unsigned char *buf ) const ; 00197 inline bool checkSignature( uint32 sig ) const ; 00198 }; 00199 00200 00201 bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ; 00202 inline bool operator== ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) { 00203 return zlh == ze ; 00204 } 00205 inline bool operator!= ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) { 00206 return ! ( zlh == ze ) ; 00207 } 00208 inline bool operator!= ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) { 00209 return ! ( zlh == ze ) ; 00210 } 00211 00212 // Inline member functions 00213 00214 ZipCDirEntry &ZipCDirEntry::operator=( const class ZipCDirEntry &src ) { 00215 writer_version = src.writer_version ; 00216 extract_version = src.extract_version ; 00217 gp_bitfield = src.gp_bitfield ; 00218 compress_method = src.compress_method ; 00219 last_mod_ftime = src.last_mod_ftime ; 00220 last_mod_fdate = src.last_mod_fdate ; 00221 crc_32 = src.crc_32 ; 00222 compress_size = src.compress_size ; 00223 uncompress_size = src.uncompress_size ; 00224 filename_len = src.filename_len ; 00225 extra_field_len = src.extra_field_len ; 00226 file_comment_len = src.file_comment_len ; 00227 disk_num_start = src.disk_num_start ; 00228 intern_file_attr = src.intern_file_attr ; 00229 extern_file_attr = src.extern_file_attr ; 00230 rel_offset_loc_head = src.rel_offset_loc_head ; 00231 00232 filename = src.filename ; 00233 extra_field = src.extra_field ; 00234 file_comment = src.file_comment ; 00235 00236 return *this ; 00237 } 00238 00239 bool EndOfCentralDirectory::checkSignature ( uint32 sig ) const { 00240 return signature == sig ; 00241 } 00242 00243 00244 } // namespace 00245 00246 #endif 00247 00248 00254 /* 00255 Zipios++ - a small C++ library that provides easy access to .zip files. 00256 Copyright (C) 2000 Thomas Søndergaard 00257 00258 This library is free software; you can redistribute it and/or 00259 modify it under the terms of the GNU Lesser General Public 00260 License as published by the Free Software Foundation; either 00261 version 2 of the License, or (at your option) any later version. 00262 00263 This library is distributed in the hope that it will be useful, 00264 but WITHOUT ANY WARRANTY; without even the implied warranty of 00265 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00266 Lesser General Public License for more details. 00267 00268 You should have received a copy of the GNU Lesser General Public 00269 License along with this library; if not, write to the Free Software 00270 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00271 */