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"
12
13namespace zipios {
14
15// byte order conversion functions.
16// ztohs (zip-to-host-short)
17#ifdef MY_BIG_ENDIAN
18
19inline uint16 ztohs ( unsigned char *buf ) {
20 uint16 out ;
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 ] ) ) ;
25
26 return out;
27}
28
29// ztohl (zip-to-host-long)
30inline uint32 ztohl ( unsigned char *buf ) {
31 uint32 out;
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 ] ) ) ;
36
37 return out;
38}
39
40#else
41
42inline uint16 ztohs ( unsigned char *buf ) {
43 uint16 out ;
44 out = ( static_cast< uint16 >( buf[ 1 ] ) << 8 ) +
45 ( static_cast< uint16 >( buf[ 0 ] ) ) ;
46 return out;
47}
48
49// ztohl (zip-to-host-long)
50inline uint32 ztohl ( unsigned char *buf ) {
51 uint32 out;
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 ;
61 return out;
62}
63
64
65#endif
66
67// htozl (host-to-zip-long)
68inline uint32 htozl ( unsigned char *buf ) {
69 return ztohl( buf ) ;
70}
71
72// htozs (host-to-zip-short)
73inline uint16 htozs ( unsigned char *buf ) {
74 return ztohs( buf ) ;
75}
76
77
78inline uint32 readUint32 ( istream &is ) {
79 static const int buf_len = sizeof ( uint32 ) ;
80 unsigned char buf [ buf_len ] ;
81 int rsf = 0 ;
82 while ( rsf < buf_len ) {
83 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
84 rsf += is.gcount () ;
85 }
86 return ztohl ( buf ) ;
87}
88
89inline 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 ) ) ;
92}
93
94inline uint16 readUint16 ( istream &is ) {
95 static const int buf_len = sizeof ( uint16 ) ;
96 unsigned char buf [ buf_len ] ;
97 int rsf = 0 ;
98 while ( rsf < buf_len ) {
99 is.read ( reinterpret_cast< char * >( buf ) + rsf, buf_len - rsf ) ;
100 rsf += is.gcount () ;
101 }
102 return ztohs ( buf ) ;
103}
104
105inline void writeUint16 ( uint16 host_val, ostream &os ) {
106 uint16 val = htozl( reinterpret_cast< unsigned char * >( &host_val ) ) ;
107 os.write( reinterpret_cast< char * >( &val ), sizeof( uint16 ) ) ;
108}
109
110inline void readByteSeq ( istream &is, string &con, int count ) {
111 char *buf = new char [ count + 1 ] ;
112 int rsf = 0 ;
113 while ( rsf < count && is ) {
114 is.read ( buf + rsf, count - rsf ) ;
115 rsf += is.gcount() ;
116 }
117 buf [ count ] = '\0' ;
118
119 con = buf ;
120 delete [] buf ;
121}
122
123inline void writeByteSeq( ostream &os, const string &con ) {
124 os << con ;
125}
126
127inline void readByteSeq ( istream &is, unsigned char *buf, int count ) {
128 int rsf = 0 ;
129
130 while ( rsf < count && is ) {
131 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
132 rsf += is.gcount() ;
133 }
134}
135
136inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) {
137 os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ;
138}
139
140inline void readByteSeq ( istream &is, vector < unsigned char > &vec, int count ) {
141 unsigned char *buf = new unsigned char [ count ] ;
142 int rsf = 0 ;
143 while ( rsf < count && is ) {
144 is.read ( reinterpret_cast< char * >( buf ) + rsf, count - rsf ) ;
145 rsf += is.gcount() ;
146 }
147
148 vec.insert ( vec.end (), buf, buf + count ) ;
149 delete [] buf ;
150}
151
152inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) {
153 if (vec.size() > 0)
154 os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ;
155}
156
157istream& operator>> ( istream &is, ZipLocalEntry &zlh ) ;
158istream& operator>> ( istream &is, DataDescriptor &dd ) ;
159istream& operator>> ( istream &is, ZipCDirEntry &zcdh ) ;
160// istream& operator>> ( istream &is, EndOfCentralDirectory &eocd ) ;
161
162ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
163ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
164ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
165
166
167} // namespace
168
169#endif
170
175
176/*
177 Zipios++ - a small C++ library that provides easy access to .zip files.
178 Copyright (C) 2000 Thomas Søndergaard
179
180 This library is free software; you can redistribute it and/or
181 modify it under the terms of the GNU Lesser General Public
182 License as published by the Free Software Foundation; either
183 version 2 of the License, or (at your option) any later version.
184
185 This library is distributed in the hope that it will be useful,
186 but WITHOUT ANY WARRANTY; without even the implied warranty of
187 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
188 Lesser General Public License for more details.
189
190 You should have received a copy of the GNU Lesser General Public
191 License along with this library; if not, write to the Free Software
192 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
193*/
The end of the Central directory structure.
Definition ziphead.h:159
Specialization of ZipLocalEntry, that add fields for storing the extra information,...
Definition ziphead.h:102
A concrete implementation of the abstract FileEntry base class for ZipFile entries,...
Definition ziphead.h:22
A struct containing fields for the entries in a zip file data descriptor, that trails the compressed ...
Definition ziphead.h:93
Header file containing classes and functions for reading the central directory and local header field...
Header file that defines some simple data types.