]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/zipios++/zipheadio.h
debian/patches/*: fix quilt-patch-missing-description lintian tag
[flightcrew.git] / src / zipios / zipios++ / zipheadio.h
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"
12
13 namespace zipios {
14
15 // byte order conversion functions. 
16 // ztohs (zip-to-host-short)
17 #ifdef MY_BIG_ENDIAN
18
19 inline 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)
30 inline 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
42 inline 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)
50 inline 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)
68 inline uint32 htozl ( unsigned char *buf ) {
69   return ztohl( buf ) ;
70 }
71
72 // htozs (host-to-zip-short)
73 inline uint16 htozs ( unsigned char *buf ) {
74   return ztohs( buf ) ;
75 }
76
77
78 inline 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 += static_cast< int >( is.gcount () ) ;
85   }
86   return  ztohl ( buf ) ;
87 }
88
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 ) ) ;
92 }
93
94 inline 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 += static_cast< int >( is.gcount () ) ;
101   }
102   return  ztohs ( buf ) ;
103 }
104
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 ) ) ;
108 }
109
110 inline 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 += static_cast< int >( is.gcount() ) ;
116   }
117   buf [ count ] = '\0' ;
118
119   con = buf ;
120   delete [] buf ;
121 }
122
123 inline void writeByteSeq( ostream &os, const string &con ) {
124   os << con ;
125 }
126
127 inline 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 += static_cast< int >( is.gcount() ) ;
133   }
134 }
135
136 inline void writeByteSeq ( ostream &os, const unsigned char *buf, int count ) {
137   os.rdbuf()->sputn( reinterpret_cast< const char * >( buf ), count ) ;
138 }
139
140 inline 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 += static_cast< int >( is.gcount() ) ;
146   }
147   
148   vec.insert ( vec.end (), buf, buf + count ) ;
149   delete [] buf ;
150 }
151
152 inline void writeByteSeq ( ostream &os, const vector < unsigned char > &vec ) {
153   os.rdbuf()->sputn( reinterpret_cast< const char * >( &( vec[ 0 ] ) ), vec.size() ) ;
154 }
155
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 ) ;
160
161 ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
162 ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
163 ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
164
165
166 } // namespace
167
168 #endif
169
170 /** \file
171     Header file that defines I/O functions for the header structures
172     defined in ziphead.h.
173 */
174
175 /*
176   Zipios++ - a small C++ library that provides easy access to .zip files.
177   Copyright (C) 2000  Thomas Søndergaard
178   
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.
183   
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.
188   
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
192 */