zipheadio.h
Go to the documentation of this file.00001 #ifndef ZIPHEADIO_H
00002 #define ZIPHEADIO_H
00003
00004 #include "zipios-config.h"
00005
00006 #include "meta-iostreams.h"
00007 #include <string>
00008 #include <vector>
00009
00010 #include "ziphead.h"
00011 #include "zipios_defs.h"
00012
00013 namespace zipios {
00014
00015
00016
00017 #ifdef MY_BIG_ENDIAN
00018
00019 inline uint16 ztohs ( unsigned char *buf ) {
00020 uint16 out ;
00021
00022
00023 out = ( static_cast< uint16 >( buf[ 0 ] ) << 8 ) +
00024 ( static_cast< uint16 >( buf[ 1 ] ) ) ;
00025
00026 return out;
00027 }
00028
00029
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
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
00057
00058
00059
00060
00061 return out;
00062 }
00063
00064
00065 #endif
00066
00067
00068 inline uint32 htozl ( unsigned char *buf ) {
00069 return ztohl( buf ) ;
00070 }
00071
00072
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
00083 int cnt = 0;
00084 while ( rsf < buf_len && (cnt++ < buf_len) ) {
00085 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
00086 rsf += is.gcount () ;
00087 }
00088 return ztohl ( buf ) ;
00089 }
00090
00091 inline void writeUint32 ( uint32 host_val, ostream &os ) {
00092 uint32 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
00093 os.write( reinterpret_cast< char * >( &val ), sizeof( uint32 ) ) ;
00094 }
00095
00096 inline uint16 readUint16 ( istream &is ) {
00097 static const int buf_len = sizeof ( uint16 ) ;
00098 unsigned char buf [ buf_len ] ;
00099 int rsf = 0 ;
00100 while ( rsf < buf_len ) {
00101 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
00102 rsf += is.gcount () ;
00103 }
00104 return ztohs ( buf ) ;
00105 }
00106
00107 inline void writeUint16 ( uint16 host_val, ostream &os ) {
00108 uint16 val = (uint16)htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
00109 os.write( reinterpret_cast< char * >( &val ), sizeof( uint16 ) ) ;
00110 }
00111
00112 inline void readByteSeq ( istream &is, string &con, int count ) {
00113 char *buf = new char [ count + 1 ] ;
00114 int rsf = 0 ;
00115 while ( rsf < count && is ) {
00116 is.read ( buf + rsf, count - rsf ) ;
00117 rsf += is.gcount() ;
00118 }
00119 buf [ count ] = '\0' ;
00120
00121 con = buf ;
00122 delete [] buf ;
00123 }
00124
00125 inline void writeByteSeq( ostream &os, const string &con ) {
00126 os << con ;
00127 }
00128
00129 inline void readByteSeq ( istream &is, unsigned char *buf, int count ) {
00130 int rsf = 0 ;
00131
00132 while ( rsf < count && is ) {
00133 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
00134 rsf += is.gcount() ;
00135 }
00136 }
00137
00138 inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) {
00139 os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ;
00140 }
00141
00142 inline void readByteSeq ( istream &is, vector < unsigned char > &vec, int count ) {
00143 unsigned char *buf = new unsigned char [ count ] ;
00144 int rsf = 0 ;
00145 while ( rsf < count && is ) {
00146 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
00147 rsf += is.gcount() ;
00148 }
00149
00150 vec.insert ( vec.end (), buf, buf + count ) ;
00151 delete [] buf ;
00152 }
00153
00154 inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) {
00155 if(!vec.empty())
00156 os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ;
00157 }
00158
00159 istream& operator>> ( istream &is, ZipLocalEntry &zlh ) ;
00160 istream& operator>> ( istream &is, DataDescriptor &dd ) ;
00161 istream& operator>> ( istream &is, ZipCDirEntry &zcdh ) ;
00162
00163
00164 ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
00165 ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
00166 ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
00167
00168
00169 }
00170
00171 #endif
00172
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195