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
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 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
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 }
00167
00168 #endif
00169
00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192