]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/zipheadio.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / zipheadio.cpp
1
2 #include "zipios++/zipios-config.h"
3
4 #include "zipios++/meta-iostreams.h"
5 #include <iterator>
6 #include <string>
7
8 #include "zipios_common.h"
9 #include "zipios++/zipheadio.h"
10
11 #include "outputstringstream.h"
12
13 namespace zipios {
14
15 std::istream& operator>> ( std::istream &is, ZipLocalEntry &zlh         ) {
16   zlh._valid = false ; // set to true upon successful completion.
17   if ( ! is )
18     return is ;
19
20 //    // Before reading anything we record the position in the stream
21 //    // This is a field in the central directory entry, but not
22 //    // in the local entry. After all, we know where we are, anyway.
23 //    zlh.rel_offset_loc_head  = is.tellg() ;
24
25   if ( zlh.signature != readUint32( is ) ) {
26     // put stream in error state and return
27     is.setstate ( std::ios::failbit ) ;
28     return is ;
29   }
30   
31   zlh.extract_version = readUint16( is ) ;
32   zlh.gp_bitfield     = readUint16( is ) ;
33   zlh.compress_method = readUint16( is ) ;
34   zlh.last_mod_ftime  = readUint16( is ) ;
35   zlh.last_mod_fdate  = readUint16( is ) ;
36   zlh.crc_32          = readUint32( is ) ;
37   zlh.compress_size   = readUint32( is ) ;
38   zlh.uncompress_size = readUint32( is ) ;
39   zlh.filename_len    = readUint16( is ) ;
40   zlh.extra_field_len = readUint16( is ) ;
41
42   // Read filename and extra_field
43   readByteSeq( is, zlh.filename, zlh.filename_len ) ;
44   readByteSeq( is, zlh.extra_field, zlh.extra_field_len ) ; 
45
46   if ( is )
47     zlh._valid = true ;
48   return is ;
49 }
50
51
52 std::istream& operator>> ( std::istream &is, DataDescriptor & ) {
53   return is ;
54 }
55
56
57 std::istream& operator>> ( std::istream &is, ZipCDirEntry &zcdh ) {
58   zcdh._valid = false ; // set to true upon successful completion.
59   if ( ! is ) 
60     return is ;
61
62   if ( zcdh.signature != readUint32( is ) ) {
63     // put stream in error state and return
64     is.setstate ( std::ios::failbit ) ;
65     return is ;
66   }
67   
68   zcdh.writer_version       = readUint16( is ) ;
69   zcdh.extract_version      = readUint16( is ) ;
70   zcdh.gp_bitfield          = readUint16( is ) ;
71   zcdh.compress_method      = readUint16( is ) ;
72   zcdh.last_mod_ftime       = readUint16( is ) ;
73   zcdh.last_mod_fdate       = readUint16( is ) ;
74   zcdh.crc_32               = readUint32( is ) ;
75   zcdh.compress_size        = readUint32( is ) ;
76   zcdh.uncompress_size      = readUint32( is ) ;
77   zcdh.filename_len         = readUint16( is ) ;
78   zcdh.extra_field_len      = readUint16( is ) ;
79   zcdh.file_comment_len     = readUint16( is ) ; 
80   zcdh.disk_num_start       = readUint16( is ) ;
81   zcdh.intern_file_attr     = readUint16( is ) ;
82   zcdh.extern_file_attr     = readUint32( is ) ;
83   zcdh.rel_offset_loc_head  = readUint32( is ) ;
84
85   // Read filename and extra_field
86   readByteSeq( is, zcdh.filename, zcdh.filename_len ) ;
87   readByteSeq( is, zcdh.extra_field, zcdh.extra_field_len ) ; 
88   readByteSeq( is, zcdh.file_comment, zcdh.file_comment_len ) ;
89
90   if ( is )
91     zcdh._valid = true ;
92   return is ;
93 }
94
95 std::ostream &operator<< ( std::ostream &os, const ZipLocalEntry &zlh ) {
96   if ( ! os )
97     return os ;
98
99   writeUint32( zlh.signature      , os ) ;
100   writeUint16( zlh.extract_version, os ) ;
101   writeUint16( zlh.gp_bitfield    , os ) ;
102   writeUint16( zlh.compress_method, os ) ;
103   writeUint16( zlh.last_mod_ftime , os ) ;
104   writeUint16( zlh.last_mod_fdate , os ) ;
105   writeUint32( zlh.crc_32         , os ) ;
106   writeUint32( zlh.compress_size  , os ) ;
107   writeUint32( zlh.uncompress_size, os ) ;
108   writeUint16( zlh.filename_len   , os ) ;
109   writeUint16( zlh.extra_field_len, os ) ;
110  
111
112   // Write filename and extra_field
113   writeByteSeq( os, zlh.filename ) ;
114   writeByteSeq( os, zlh.extra_field ) ; 
115
116   return os ;
117 }
118
119 std::ostream &operator<< ( std::ostream &os, const ZipCDirEntry &zcdh ) {
120   if ( ! os ) 
121     return os ;
122
123   writeUint32( zcdh.signature          , os ) ;
124   writeUint16( zcdh.writer_version     , os ) ;
125   writeUint16( zcdh.extract_version    , os ) ;
126   writeUint16( zcdh.gp_bitfield        , os ) ;
127   writeUint16( zcdh.compress_method    , os ) ;
128   writeUint16( zcdh.last_mod_ftime     , os ) ;
129   writeUint16( zcdh.last_mod_fdate     , os ) ;
130   writeUint32( zcdh.crc_32             , os ) ;
131   writeUint32( zcdh.compress_size      , os ) ;
132   writeUint32( zcdh.uncompress_size    , os ) ;
133   writeUint16( zcdh.filename_len       , os ) ;
134   writeUint16( zcdh.extra_field_len    , os ) ;
135   writeUint16( zcdh.file_comment_len   , os ) ;
136   writeUint16( zcdh.disk_num_start     , os ) ;
137   writeUint16( zcdh.intern_file_attr   , os ) ;
138   writeUint32( zcdh.extern_file_attr   , os ) ;
139   writeUint32( zcdh.rel_offset_loc_head, os ) ;
140
141   // Write filename and extra_field
142   writeByteSeq( os, zcdh.filename ) ;
143   writeByteSeq( os, zcdh.extra_field ) ; 
144   writeByteSeq( os, zcdh.file_comment ) ;
145
146   return os ;
147 }
148
149 std::ostream &operator<< ( std::ostream &os, const EndOfCentralDirectory &eocd ) {
150   if ( ! os ) 
151     return os ;
152
153   writeUint32( eocd.signature       , os ) ;
154   writeUint16( eocd.disk_num        , os ) ;
155   writeUint16( eocd.cdir_disk_num   , os ) ;
156   writeUint16( eocd.cdir_entries    , os ) ;
157   writeUint16( eocd.cdir_tot_entries, os ) ;
158   writeUint32( eocd.cdir_size       , os ) ;
159   writeUint32( eocd.cdir_offset     , os ) ;
160   writeUint16( eocd.zip_comment_len , os ) ;
161   
162   writeByteSeq( os, eocd.zip_comment ) ;
163
164   return os ;
165 }
166
167
168
169 } // namespace
170
171
172
173 /** \file
174     Implementation of I/O functions for the header structures
175     defined in ziphead.h.
176 */
177
178 /*
179   Zipios++ - a small C++ library that provides easy access to .zip files.
180   Copyright (C) 2000  Thomas Søndergaard
181   
182   This library is free software; you can redistribute it and/or
183   modify it under the terms of the GNU Lesser General Public
184   License as published by the Free Software Foundation; either
185   version 2 of the License, or (at your option) any later version.
186   
187   This library is distributed in the hope that it will be useful,
188   but WITHOUT ANY WARRANTY; without even the implied warranty of
189   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
190   Lesser General Public License for more details.
191   
192   You should have received a copy of the GNU Lesser General Public
193   License along with this library; if not, write to the Free Software
194   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
195 */