Zipios++
|
00001 #ifndef ZIPHEADIO_H 00002 #define ZIPHEADIO_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++/ziphead.h" 00011 #include "zipios++/zipios_defs.h" 00012 00013 namespace zipios { 00014 00015 // byte order conversion functions. 00016 // ztohs (zip-to-host-short) 00017 #ifdef MY_BIG_ENDIAN 00018 00019 inline uint16 ztohs ( unsigned char *buf ) { 00020 uint16 out ; 00021 // *( reinterpret_cast<unsigned char *>( &out ) ) = *( buf + 1 ); 00022 // *( reinterpret_cast<unsigned char *>( &out ) + 1 ) = *( buf ); 00023 out = ( static_cast< uint16 >( buf[ 0 ] ) << 8 ) + 00024 ( static_cast< uint16 >( buf[ 1 ] ) ) ; 00025 00026 return out; 00027 } 00028 00029 // ztohl (zip-to-host-long) 00030 inline uint32 ztohl ( unsigned char *buf ) { 00031 uint32 out; 00032 out = ( static_cast< uint32 >( buf[ 0 ] ) << 24 ) + 00033 ( static_cast< uint32 >( buf[ 1 ] ) << 16 ) + 00034 ( static_cast< uint32 >( buf[ 2 ] ) << 8 ) + 00035 ( static_cast< uint32 >( buf[ 3 ] ) ) ; 00036 00037 return out; 00038 } 00039 00040 #else 00041 00042 inline uint16 ztohs ( unsigned char *buf ) { 00043 uint16 out ; 00044 out = ( static_cast< uint16 >( buf[ 1 ] ) << 8 ) + 00045 ( static_cast< uint16 >( buf[ 0 ] ) ) ; 00046 return out; 00047 } 00048 00049 // ztohl (zip-to-host-long) 00050 inline uint32 ztohl ( unsigned char *buf ) { 00051 uint32 out; 00052 out = ( static_cast< uint32 >( buf[ 3 ] ) << 24 ) + 00053 ( static_cast< uint32 >( buf[ 2 ] ) << 16 ) + 00054 ( static_cast< uint32 >( buf[ 1 ] ) << 8 ) + 00055 ( static_cast< uint32 >( buf[ 0 ] ) ) ; 00056 // cerr << "buf : " << static_cast< int >( buf[ 0 ] ) ; 00057 // cerr << " " << static_cast< int >( buf[ 1 ] ) ; 00058 // cerr << " " << static_cast< int >( buf[ 2 ] ) ; 00059 // cerr << " " << static_cast< int >( buf[ 3 ] ) << endl ; 00060 // cerr << "uint32 " << out << endl ; 00061 return out; 00062 } 00063 00064 00065 #endif 00066 00067 // htozl (host-to-zip-long) 00068 inline uint32 htozl ( unsigned char *buf ) { 00069 return ztohl( buf ) ; 00070 } 00071 00072 // htozs (host-to-zip-short) 00073 inline uint16 htozs ( unsigned char *buf ) { 00074 return ztohs( buf ) ; 00075 } 00076 00077 00078 inline uint32 readUint32 ( istream &is ) { 00079 static const int buf_len = sizeof ( uint32 ) ; 00080 unsigned char buf [ buf_len ] ; 00081 int rsf = 0 ; 00082 while ( rsf < buf_len ) { 00083 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ; 00084 rsf += is.gcount () ; 00085 } 00086 return ztohl ( buf ) ; 00087 } 00088 00089 inline void writeUint32 ( uint32 host_val, ostream &os ) { 00090 uint32 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ; 00091 os.write( reinterpret_cast< char * >( &val ), sizeof( uint32 ) ) ; 00092 } 00093 00094 inline uint16 readUint16 ( istream &is ) { 00095 static const int buf_len = sizeof ( uint16 ) ; 00096 unsigned char buf [ buf_len ] ; 00097 int rsf = 0 ; 00098 while ( rsf < buf_len ) { 00099 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ; 00100 rsf += is.gcount () ; 00101 } 00102 return ztohs ( buf ) ; 00103 } 00104 00105 inline void writeUint16 ( uint16 host_val, ostream &os ) { 00106 uint16 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ; 00107 os.write( reinterpret_cast< char * >( &val ), sizeof( uint16 ) ) ; 00108 } 00109 00110 inline void readByteSeq ( istream &is, string &con, int count ) { 00111 char *buf = new char [ count + 1 ] ; 00112 int rsf = 0 ; 00113 while ( rsf < count && is ) { 00114 is.read ( buf + rsf, count - rsf ) ; 00115 rsf += is.gcount() ; 00116 } 00117 buf [ count ] = '\0' ; 00118 00119 con = buf ; 00120 delete [] buf ; 00121 } 00122 00123 inline void writeByteSeq( ostream &os, const string &con ) { 00124 os << con ; 00125 } 00126 00127 inline void readByteSeq ( istream &is, unsigned char *buf, int count ) { 00128 int rsf = 0 ; 00129 00130 while ( rsf < count && is ) { 00131 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ; 00132 rsf += is.gcount() ; 00133 } 00134 } 00135 00136 inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) { 00137 os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ; 00138 } 00139 00140 inline void readByteSeq ( istream &is, vector < unsigned char > &vec, int count ) { 00141 unsigned char *buf = new unsigned char [ count ] ; 00142 int rsf = 0 ; 00143 while ( rsf < count && is ) { 00144 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ; 00145 rsf += is.gcount() ; 00146 } 00147 00148 vec.insert ( vec.end (), buf, buf + count ) ; 00149 delete [] buf ; 00150 } 00151 00152 inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) { 00153 os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ; 00154 } 00155 00156 istream& operator>> ( istream &is, ZipLocalEntry &zlh ) ; 00157 istream& operator>> ( istream &is, DataDescriptor &dd ) ; 00158 istream& operator>> ( istream &is, ZipCDirEntry &zcdh ) ; 00159 // istream& operator>> ( istream &is, EndOfCentralDirectory &eocd ) ; 00160 00161 ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ; 00162 ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ; 00163 ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ; 00164 00165 00166 } // namespace 00167 00168 #endif 00169 00175 /* 00176 Zipios++ - a small C++ library that provides easy access to .zip files. 00177 Copyright (C) 2000 Thomas Søndergaard 00178 00179 This library is free software; you can redistribute it and/or 00180 modify it under the terms of the GNU Lesser General Public 00181 License as published by the Free Software Foundation; either 00182 version 2 of the License, or (at your option) any later version. 00183 00184 This library is distributed in the hope that it will be useful, 00185 but WITHOUT ANY WARRANTY; without even the implied warranty of 00186 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00187 Lesser General Public License for more details. 00188 00189 You should have received a copy of the GNU Lesser General Public 00190 License along with this library; if not, write to the Free Software 00191 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00192 */