4 #include "zipios++/zipios-config.h"
6 #include "zipios++/meta-iostreams.h"
10 #include "zipios++/ziphead.h"
11 #include "zipios++/zipios_defs.h"
15 // byte order conversion functions.
16 // ztohs (zip-to-host-short)
19 inline uint16 ztohs ( unsigned char *buf ) {
21 // *( reinterpret_cast<unsigned char *>( &out ) ) = *( buf + 1 );
22 // *( reinterpret_cast<unsigned char *>( &out ) + 1 ) = *( buf );
23 out = ( static_cast< uint16 >( buf[ 0 ] ) << 8 ) +
24 ( static_cast< uint16 >( buf[ 1 ] ) ) ;
29 // ztohl (zip-to-host-long)
30 inline uint32 ztohl ( unsigned char *buf ) {
32 out = ( static_cast< uint32 >( buf[ 0 ] ) << 24 ) +
33 ( static_cast< uint32 >( buf[ 1 ] ) << 16 ) +
34 ( static_cast< uint32 >( buf[ 2 ] ) << 8 ) +
35 ( static_cast< uint32 >( buf[ 3 ] ) ) ;
42 inline uint16 ztohs ( unsigned char *buf ) {
44 out = ( static_cast< uint16 >( buf[ 1 ] ) << 8 ) +
45 ( static_cast< uint16 >( buf[ 0 ] ) ) ;
49 // ztohl (zip-to-host-long)
50 inline uint32 ztohl ( unsigned char *buf ) {
52 out = ( static_cast< uint32 >( buf[ 3 ] ) << 24 ) +
53 ( static_cast< uint32 >( buf[ 2 ] ) << 16 ) +
54 ( static_cast< uint32 >( buf[ 1 ] ) << 8 ) +
55 ( static_cast< uint32 >( buf[ 0 ] ) ) ;
56 // cerr << "buf : " << static_cast< int >( buf[ 0 ] ) ;
57 // cerr << " " << static_cast< int >( buf[ 1 ] ) ;
58 // cerr << " " << static_cast< int >( buf[ 2 ] ) ;
59 // cerr << " " << static_cast< int >( buf[ 3 ] ) << endl ;
60 // cerr << "uint32 " << out << endl ;
67 // htozl (host-to-zip-long)
68 inline uint32 htozl ( unsigned char *buf ) {
72 // htozs (host-to-zip-short)
73 inline uint16 htozs ( unsigned char *buf ) {
78 inline uint32 readUint32 ( istream &is ) {
79 static const int buf_len = sizeof ( uint32 ) ;
80 unsigned char buf [ buf_len ] ;
82 while ( rsf < buf_len ) {
83 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
84 rsf += static_cast< int >( is.gcount () ) ;
86 return ztohl ( buf ) ;
89 inline void writeUint32 ( uint32 host_val, ostream &os ) {
90 uint32 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
91 os.write( reinterpret_cast< char * >( &val ), sizeof( uint32 ) ) ;
94 inline uint16 readUint16 ( istream &is ) {
95 static const int buf_len = sizeof ( uint16 ) ;
96 unsigned char buf [ buf_len ] ;
98 while ( rsf < buf_len ) {
99 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
100 rsf += static_cast< int >( is.gcount () ) ;
102 return ztohs ( buf ) ;
105 inline void writeUint16 ( uint16 host_val, ostream &os ) {
106 uint16 val = static_cast< uint16 >( htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ) ;
107 os.write( reinterpret_cast< char * >( &val ), sizeof( uint16 ) ) ;
110 inline void readByteSeq ( istream &is, string &con, int count ) {
111 char *buf = new char [ count + 1 ] ;
113 while ( rsf < count && is ) {
114 is.read ( buf + rsf, count - rsf ) ;
115 rsf += static_cast< int >( is.gcount() ) ;
117 buf [ count ] = '\0' ;
123 inline void writeByteSeq( ostream &os, const string &con ) {
127 inline void readByteSeq ( istream &is, unsigned char *buf, int count ) {
130 while ( rsf < count && is ) {
131 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
132 rsf += static_cast< int >( is.gcount() ) ;
136 inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) {
137 os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ;
140 inline void readByteSeq ( istream &is, vector < unsigned char > &vec, int count ) {
141 unsigned char *buf = new unsigned char [ count ] ;
143 while ( rsf < count && is ) {
144 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
145 rsf += static_cast< int >( is.gcount() ) ;
148 vec.insert ( vec.end (), buf, buf + count ) ;
152 inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) {
153 os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ;
156 istream& operator>> ( istream &is, ZipLocalEntry &zlh ) ;
157 istream& operator>> ( istream &is, DataDescriptor &dd ) ;
158 istream& operator>> ( istream &is, ZipCDirEntry &zcdh ) ;
159 // istream& operator>> ( istream &is, EndOfCentralDirectory &eocd ) ;
161 ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
162 ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
163 ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
171 Header file that defines I/O functions for the header structures
172 defined in ziphead.h.
176 Zipios++ - a small C++ library that provides easy access to .zip files.
177 Copyright (C) 2000 Thomas Søndergaard
179 This library is free software; you can redistribute it and/or
180 modify it under the terms of the GNU Lesser General Public
181 License as published by the Free Software Foundation; either
182 version 2 of the License, or (at your option) any later version.
184 This library is distributed in the hope that it will be useful,
185 but WITHOUT ANY WARRANTY; without even the implied warranty of
186 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
187 Lesser General Public License for more details.
189 You should have received a copy of the GNU Lesser General Public
190 License along with this library; if not, write to the Free Software
191 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA