Main Page   Class Hierarchy   Compound List   File List   Compound Members  

ziphead.cpp

Go to the documentation of this file.
00001 
00002 #include "zipios++/zipios-config.h"
00003 
00004 #include "zipios++/meta-iostreams.h"
00005 #include <iterator>
00006 #include <string>
00007 
00008 #include "zipios_common.h"
00009 #include "zipios++/ziphead.h"
00010 #include "zipios++/zipheadio.h"
00011 #include "zipios++/zipios_defs.h"
00012 
00013 #include "outputstringstream.h"
00014 
00015 namespace zipios {
00016 
00017 using std::ios ;
00018 
00019 bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) {
00020   // Not all fields need to be identical. Some of the information
00021   // may be put in a data descriptor that trails the compressed
00022   // data, according to the specs (The trailing data descriptor
00023   // can contain crc_32, compress_size and uncompress_size.)
00024 
00025   // Experience has shown that extra_field and extra_field_len
00026   // can differ too.
00027 
00028 //    cerr << "----- BEGIN -----" << endl ;
00029 //    cerr << ( zlh.extract_version == ze.extract_version     ) << endl ; 
00030 //    cerr << ( zlh.gp_bitfield     == ze.gp_bitfield         ) << endl ; 
00031 //    cerr << ( zlh.compress_method == ze.compress_method     ) << endl ; 
00032 //    cerr << ( zlh.last_mod_ftime  == ze.last_mod_ftime      ) << endl ; 
00033 //    cerr << ( zlh.last_mod_fdate  == ze.last_mod_fdate      ) << endl ; 
00034 
00035 //    cerr << ( zlh.filename_len    == ze.filename_len        ) << endl ; 
00036   
00037 //    cerr << ( zlh.filename        == ze.filename            ) << endl ; 
00038 //    cerr << "----- END -----" << endl ;
00039   return ( zlh.extract_version == ze.extract_version     &&
00040            zlh.gp_bitfield     == ze.gp_bitfield         &&
00041            zlh.compress_method == ze.compress_method     &&
00042            zlh.last_mod_ftime  == ze.last_mod_ftime      &&
00043            zlh.last_mod_fdate  == ze.last_mod_fdate      &&
00044            zlh.filename_len    == ze.filename_len        &&
00045            
00046            zlh.filename        == ze.filename               ) ;
00047 }
00048 
00049 //
00050 // ZipLocalEntry methods
00051 //
00052 
00053 const uint32 ZipLocalEntry::signature = 0x04034b50 ;
00054 
00055 
00056 
00057 void ZipLocalEntry::setDefaultExtract() {
00058   extract_version = 20 ; // version number
00059 }
00060 
00061 string ZipLocalEntry::getComment() const {
00062   return "" ; // No comment in a local entry
00063 }
00064 
00065 uint32 ZipLocalEntry::getCompressedSize() const {
00066   return compress_size ;
00067 }
00068 
00069 uint32 ZipLocalEntry::getCrc() const {
00070   return crc_32 ;
00071 }
00072 
00073 vector< unsigned char > ZipLocalEntry::getExtra() const {
00074   return extra_field ;
00075 }
00076 
00077 StorageMethod ZipLocalEntry::getMethod() const {
00078   return static_cast< StorageMethod >( compress_method ) ;
00079 }
00080 
00081 string ZipLocalEntry::getName() const {
00082   return filename ;
00083 }
00084 
00085 string ZipLocalEntry::getFileName() const {
00086   if ( isDirectory() )
00087     return string() ;
00088   string::size_type pos ;
00089   pos = filename.find_last_of( separator ) ;
00090   if ( pos != string::npos ) { // separator found!
00091     // isDirectory() check means pos should not be last, so pos+1 is ok 
00092     return filename.substr( pos + 1 ) ;
00093   } else {
00094     return filename ;
00095   }
00096 }
00097 
00098 uint32 ZipLocalEntry::getSize() const {
00099   return uncompress_size ;
00100 }
00101 
00102 int ZipLocalEntry::getTime() const {
00103   return ( last_mod_fdate << 16 ) + last_mod_ftime ; 
00104   // FIXME: what to do with this time date thing? (not only here?)
00105 }
00106 
00107 bool ZipLocalEntry::isValid() const {
00108   return _valid ;
00109 }
00110 
00111 bool ZipLocalEntry::isDirectory() const {
00112   assert( filename.size() != 0 ) ;
00113   return  filename[ filename.size() - 1 ] == separator ;
00114 }
00115 
00116 
00117 void ZipLocalEntry::setComment( const string &comment ) {
00118   // A local entry cannot hold a comment
00119 }
00120 
00121 void ZipLocalEntry::setCompressedSize( uint32 size ) {
00122   compress_size = size ;
00123 }
00124 
00125 void ZipLocalEntry::setCrc( uint32 crc ) {
00126   crc_32 = crc ;
00127 }
00128 
00129 void ZipLocalEntry::setExtra( const vector< unsigned char > &extra ) {
00130   extra_field = extra ;
00131   extra_field_len = extra_field.size() ;
00132 }
00133 
00134 void ZipLocalEntry::setMethod( StorageMethod method ) {
00135   compress_method = static_cast< uint16 >( method ) ;
00136 }
00137 
00138 void ZipLocalEntry::setName( const string &name ) {
00139   filename = name ;
00140   filename_len = filename.size() ;
00141 }
00142 
00143 void ZipLocalEntry::setSize( uint32 size ) {
00144   uncompress_size = size ;
00145 }
00146 
00147 void ZipLocalEntry::setTime( int time ) {
00148   // FIXME: fix time setting here, and ind flist and elsewhere. Define the
00149   // date time semantics before mucking about - how surprising
00150 }
00151 
00152 string ZipLocalEntry::toString() const {
00153   OutputStringStream sout ;
00154   sout << filename << " (" << uncompress_size << " bytes, " ;
00155   sout << compress_size << " bytes compressed)" ;
00156   return sout.str() ;
00157 }
00158 
00159 int ZipLocalEntry::getLocalHeaderSize() const {
00160   return 30 + filename.size() + extra_field.size() ;
00161 }
00162 
00163 bool ZipLocalEntry::trailingDataDescriptor() const {
00164   // gp_bitfield bit 3 is one, if this entry uses a trailing data
00165   // descriptor to keep size, compressed size and crc-32
00166   // fields.
00167   if ( ( gp_bitfield & 4 ) == 1 )
00168     return true ;
00169   else
00170     return false ;
00171 }
00172 
00173 FileEntry *ZipLocalEntry::clone() const {
00174   return new ZipLocalEntry( *this ) ;
00175 }
00176 
00177 
00178 //
00179 // ZipCDirEntry methods
00180 //
00181 
00182 const uint32 ZipCDirEntry::signature = 0x02014b50 ;
00183 
00184 void ZipCDirEntry::setDefaultWriter() {
00185   writer_version = 0 ;
00186 #ifdef WIN32
00187     writer_version |= static_cast< uint16 >( 0 ) << 8 ; // Windows, DOS
00188 #else
00189     writer_version |= static_cast< uint16 >( 3 ) << 8 ; // Unix
00190 #endif
00191     writer_version |= 20 ; // version number
00192 }
00193 
00194 string ZipCDirEntry::getComment() const {
00195   return file_comment ;
00196 }
00197 
00198 uint32 ZipCDirEntry::getLocalHeaderOffset() const {
00199   return rel_offset_loc_head ;
00200 }
00201 
00202 void ZipCDirEntry::setLocalHeaderOffset( uint32 offset ) {
00203   rel_offset_loc_head = offset ;
00204 }
00205 
00206 
00207 void ZipCDirEntry::setComment( const string &comment ) {
00208   file_comment = comment ;
00209   file_comment_len = file_comment.size() ;
00210 }
00211 
00212 
00213 string ZipCDirEntry::toString() const {
00214   OutputStringStream sout ;
00215   sout << filename << " (" << uncompress_size << " bytes, " ;
00216   sout << compress_size << " bytes compressed)" ;
00217   return sout.str() ;
00218 }
00219 
00220 
00221 int ZipCDirEntry::getCDirHeaderSize() const {
00222   return 46 + filename.size() + extra_field.size() + file_comment.size() ;
00223 }
00224 
00225 
00226 FileEntry *ZipCDirEntry::clone() const {
00227   return new ZipCDirEntry( *this ) ;
00228 }
00229 
00230 
00231 //
00232 // EndOfCentralDirectory methods
00233 //
00234 
00235 const uint32 EndOfCentralDirectory::signature = 0x06054b50 ;
00236 
00237 bool EndOfCentralDirectory::read( vector<unsigned char> &buf, int pos ) {
00238   if ( ( buf.size() - pos < sizeof( uint32 ) )   || 
00239        ( ! checkSignature( &( buf[ pos ] ) ) )     )
00240     return false ;
00241 
00242   eocd_offset_from_end = buf.size() - pos ;
00243   pos += sizeof( uint32 ) ;
00244   disk_num         = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
00245   cdir_disk_num    = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
00246   cdir_entries     = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
00247   cdir_tot_entries = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
00248   cdir_size        = ztohl( &( buf[ pos  ] ) ) ; pos += sizeof( uint32 ) ;
00249   cdir_offset      = ztohl( &( buf[ pos  ] ) ) ; pos += sizeof( uint32 ) ;
00250   zip_comment_len  = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
00251 //    cerr << "Zip comment length = " << zip_comment_len << endl ;
00252 //    cerr << "Length of remaining file = " << buf.size() - pos << endl ;
00253 
00254   return true ; // Dummy
00255 }
00256 
00257 bool EndOfCentralDirectory::checkSignature ( unsigned char *buf ) const {
00258 //    cerr << "potential header: " << ztohl( buf ) << endl ;
00259   return checkSignature( ztohl( buf ) ) ;
00260 }
00261 
00262 
00263 
00264 } // namespace
00265 
00266 
00267 
00273 /*
00274   Zipios++ - a small C++ library that provides easy access to .zip files.
00275   Copyright (C) 2000  Thomas Søndergaard
00276   
00277   This library is free software; you can redistribute it and/or
00278   modify it under the terms of the GNU Lesser General Public
00279   License as published by the Free Software Foundation; either
00280   version 2 of the License, or (at your option) any later version.
00281   
00282   This library is distributed in the hope that it will be useful,
00283   but WITHOUT ANY WARRANTY; without even the implied warranty of
00284   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00285   Lesser General Public License for more details.
00286   
00287   You should have received a copy of the GNU Lesser General Public
00288   License along with this library; if not, write to the Free Software
00289   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00290 */

Generated at Tue Aug 14 20:39:26 2001 for Zipios++ by doxygen1.2.0 written by Dimitri van Heesch, © 1997-2000