Zipios++
zipheadio.h
Go to the documentation of this file.
1 #ifndef ZIPHEADIO_H
2 #define ZIPHEADIO_H
3 
4 #include "zipios++/zipios-config.h"
5 
6 #include "zipios++/meta-iostreams.h"
7 #include <string>
8 #include <vector>
9 
10 #include "zipios++/ziphead.h"
11 #include "zipios++/zipios_defs.h"
13 
14 namespace zipios {
15 
16 // byte order conversion functions.
17 // ztohs (zip-to-host-short)
18 #ifdef MY_BIG_ENDIAN
19 
20 inline uint16 ztohs ( unsigned char *buf ) {
21  uint16 out ;
22 // *( reinterpret_cast<unsigned char *>( &out ) ) = *( buf + 1 );
23 // *( reinterpret_cast<unsigned char *>( &out ) + 1 ) = *( buf );
24  out = ( static_cast< uint16 >( buf[ 0 ] ) << 8 ) +
25  ( static_cast< uint16 >( buf[ 1 ] ) ) ;
26 
27  return out;
28 }
29 
30 // ztohl (zip-to-host-long)
31 inline uint32 ztohl ( unsigned char *buf ) {
32  uint32 out;
33  out = ( static_cast< uint32 >( buf[ 0 ] ) << 24 ) +
34  ( static_cast< uint32 >( buf[ 1 ] ) << 16 ) +
35  ( static_cast< uint32 >( buf[ 2 ] ) << 8 ) +
36  ( static_cast< uint32 >( buf[ 3 ] ) ) ;
37 
38  return out;
39 }
40 
41 #else
42 
43 inline uint16 ztohs ( unsigned char *buf ) {
44  uint16 out ;
45  out = ( static_cast< uint16 >( buf[ 1 ] ) << 8 ) +
46  ( static_cast< uint16 >( buf[ 0 ] ) ) ;
47  return out;
48 }
49 
50 // ztohl (zip-to-host-long)
51 inline uint32 ztohl ( unsigned char *buf ) {
52  uint32 out;
53  out = ( static_cast< uint32 >( buf[ 3 ] ) << 24 ) +
54  ( static_cast< uint32 >( buf[ 2 ] ) << 16 ) +
55  ( static_cast< uint32 >( buf[ 1 ] ) << 8 ) +
56  ( static_cast< uint32 >( buf[ 0 ] ) ) ;
57 // cerr << "buf : " << static_cast< int >( buf[ 0 ] ) ;
58 // cerr << " " << static_cast< int >( buf[ 1 ] ) ;
59 // cerr << " " << static_cast< int >( buf[ 2 ] ) ;
60 // cerr << " " << static_cast< int >( buf[ 3 ] ) << endl ;
61 // cerr << "uint32 " << out << endl ;
62  return out;
63 }
64 
65 
66 #endif
67 
68 // htozl (host-to-zip-long)
69 inline uint32 htozl ( unsigned char *buf ) {
70  return ztohl( buf ) ;
71 }
72 
73 // htozs (host-to-zip-short)
74 inline uint16 htozs ( unsigned char *buf ) {
75  return ztohs( buf ) ;
76 }
77 
78 
79 inline uint32 readUint32 ( istream &is ) {
80  static const int buf_len = sizeof ( uint32 ) ;
81  unsigned char buf [ buf_len ] ;
82  int rsf = 0 ;
83  std::streampos original_pos = is.tellg() ;
84  while ( rsf < buf_len && !is.eof() ) {
85  is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
86  rsf += is.gcount () ;
87  }
88  if ( rsf != buf_len ) {
89  is.seekg( original_pos ) ;
90  throw InvalidStateException( "Reached end-of-file while trying to read a"
91  "Uint32; the zip archive may be corrupt." ) ;
92  }
93  return ztohl ( buf ) ;
94 }
95 
96 inline void writeUint32 ( uint32 host_val, ostream &os ) {
97  uint32 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
98  os.write( reinterpret_cast< char * >( &val ), sizeof( uint32 ) ) ;
99 }
100 
101 inline uint16 readUint16 ( istream &is ) {
102  static const int buf_len = sizeof ( uint16 ) ;
103  unsigned char buf [ buf_len ] ;
104  int rsf = 0 ;
105  std::streampos original_pos = is.tellg() ;
106  while ( rsf < buf_len && !is.eof() ) {
107  is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
108  rsf += is.gcount () ;
109  }
110  if ( rsf != buf_len ) {
111  is.seekg( original_pos ) ;
112  throw InvalidStateException( "Reached end-of-file while trying to read a"
113  "Uint16; the zip archive may be corrupt." ) ;
114  }
115  return ztohs ( buf ) ;
116 }
117 
118 inline void writeUint16 ( uint16 host_val, ostream &os ) {
119  uint16 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
120  os.write( reinterpret_cast< char * >( &val ), sizeof( uint16 ) ) ;
121 }
122 
123 inline void readByteSeq ( istream &is, string &con, int count ) {
124  char *buf = new char [ count + 1 ] ;
125  int rsf = 0 ;
126  while ( rsf < count && is ) {
127  is.read ( buf + rsf, count - rsf ) ;
128  rsf += is.gcount() ;
129  }
130  buf [ count ] = '\0' ;
131 
132  con = buf ;
133  delete [] buf ;
134 }
135 
136 inline void writeByteSeq( ostream &os, const string &con ) {
137  os << con ;
138 }
139 
140 inline void readByteSeq ( istream &is, unsigned char *buf, int count ) {
141  int rsf = 0 ;
142 
143  while ( rsf < count && is ) {
144  is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
145  rsf += is.gcount() ;
146  }
147 }
148 
149 inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) {
150  os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ;
151 }
152 
153 inline void readByteSeq ( istream &is, vector < unsigned char > &vec, int count ) {
154  unsigned char *buf = new unsigned char [ count ] ;
155  int rsf = 0 ;
156  while ( rsf < count && is ) {
157  is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
158  rsf += is.gcount() ;
159  }
160 
161  vec.insert ( vec.end (), buf, buf + count ) ;
162  delete [] buf ;
163 }
164 
165 inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) {
166  os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ;
167 }
168 
169 istream& operator>> ( istream &is, ZipLocalEntry &zlh ) ;
170 istream& operator>> ( istream &is, DataDescriptor &dd ) ;
171 istream& operator>> ( istream &is, ZipCDirEntry &zcdh ) ;
172 // istream& operator>> ( istream &is, EndOfCentralDirectory &eocd ) ;
173 
174 ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
175 ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
176 ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
177 
178 
179 } // namespace
180 
181 #endif
182 
188 /*
189  Zipios++ - a small C++ library that provides easy access to .zip files.
190  Copyright (C) 2000 Thomas Søndergaard
191 
192  This library is free software; you can redistribute it and/or
193  modify it under the terms of the GNU Lesser General Public
194  License as published by the Free Software Foundation; either
195  version 2 of the License, or (at your option) any later version.
196 
197  This library is distributed in the hope that it will be useful,
198  but WITHOUT ANY WARRANTY; without even the implied warranty of
199  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
200  Lesser General Public License for more details.
201 
202  You should have received a copy of the GNU Lesser General Public
203  License along with this library; if not, write to the Free Software
204  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
205 */
Header file that defines some simple data types.
Header file containing classes and functions for reading the central directory and local header field...
Header file that defines a number of exceptions used by FileCollection and its subclasses.