~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Base/zipios/ziphead.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "zipios-config.h"
 
3
 
 
4
#include "meta-iostreams.h"
 
5
#include <iterator>
 
6
#include <string>
 
7
#include <cassert>
 
8
 
 
9
#include "zipios_common.h"
 
10
#include "ziphead.h"
 
11
#include "zipheadio.h"
 
12
#include "zipios_defs.h"
 
13
 
 
14
#include "outputstringstream.h"
 
15
 
 
16
namespace zipios {
 
17
 
 
18
using std::ios ;
 
19
 
 
20
bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) {
 
21
  // Not all fields need to be identical. Some of the information
 
22
  // may be put in a data descriptor that trails the compressed
 
23
  // data, according to the specs (The trailing data descriptor
 
24
  // can contain crc_32, compress_size and uncompress_size.)
 
25
 
 
26
  // Experience has shown that extra_field and extra_field_len
 
27
  // can differ too.
 
28
 
 
29
//    cerr << "----- BEGIN -----" << endl ;
 
30
//    cerr << ( zlh.extract_version == ze.extract_version     ) << endl ; 
 
31
//    cerr << ( zlh.gp_bitfield     == ze.gp_bitfield         ) << endl ; 
 
32
//    cerr << ( zlh.compress_method == ze.compress_method     ) << endl ; 
 
33
//    cerr << ( zlh.last_mod_ftime  == ze.last_mod_ftime      ) << endl ; 
 
34
//    cerr << ( zlh.last_mod_fdate  == ze.last_mod_fdate      ) << endl ; 
 
35
 
 
36
//    cerr << ( zlh.filename_len    == ze.filename_len        ) << endl ; 
 
37
  
 
38
//    cerr << ( zlh.filename        == ze.filename            ) << endl ; 
 
39
//    cerr << "----- END -----" << endl ;
 
40
  return ( zlh.extract_version == ze.extract_version     &&
 
41
           zlh.gp_bitfield     == ze.gp_bitfield         &&
 
42
           zlh.compress_method == ze.compress_method     &&
 
43
           zlh.last_mod_ftime  == ze.last_mod_ftime      &&
 
44
           zlh.last_mod_fdate  == ze.last_mod_fdate      &&
 
45
           zlh.filename_len    == ze.filename_len        &&
 
46
           
 
47
           zlh.filename        == ze.filename               ) ;
 
48
}
 
49
 
 
50
//
 
51
// ZipLocalEntry methods
 
52
//
 
53
 
 
54
const uint32 ZipLocalEntry::signature = 0x04034b50 ;
 
55
 
 
56
 
 
57
 
 
58
ZipCDirEntry &ZipCDirEntry::operator=( const class ZipCDirEntry &src ) {
 
59
  writer_version      = src.writer_version      ;
 
60
  extract_version     = src.extract_version     ;
 
61
  gp_bitfield         = src.gp_bitfield         ;
 
62
  compress_method     = src.compress_method     ;
 
63
  last_mod_ftime      = src.last_mod_ftime      ;
 
64
  last_mod_fdate      = src.last_mod_fdate      ;
 
65
  crc_32              = src.crc_32              ;
 
66
  compress_size       = src.compress_size       ; 
 
67
  uncompress_size     = src.uncompress_size     ;
 
68
  filename_len        = src.filename_len        ;
 
69
  extra_field_len     = src.extra_field_len     ;
 
70
  file_comment_len    = src.file_comment_len    ; 
 
71
  disk_num_start      = src.disk_num_start      ;
 
72
  intern_file_attr    = src.intern_file_attr    ;
 
73
  extern_file_attr    = src.extern_file_attr    ;
 
74
  rel_offset_loc_head = src.rel_offset_loc_head ;
 
75
 
 
76
  filename     = src.filename     ;
 
77
  extra_field  = src.extra_field  ; 
 
78
  file_comment = src.file_comment ;
 
79
 
 
80
  return *this ;
 
81
}
 
82
 
 
83
bool EndOfCentralDirectory::checkSignature ( uint32 sig ) const {
 
84
  return signature == sig ;
 
85
}
 
86
 
 
87
void ZipLocalEntry::setDefaultExtract() {
 
88
  extract_version = 20 ; // version number
 
89
}
 
90
 
 
91
string ZipLocalEntry::getComment() const {
 
92
  return "" ; // No comment in a local entry
 
93
}
 
94
 
 
95
uint32 ZipLocalEntry::getCompressedSize() const {
 
96
  return compress_size ;
 
97
}
 
98
 
 
99
uint32 ZipLocalEntry::getCrc() const {
 
100
  return crc_32 ;
 
101
}
 
102
 
 
103
vector< unsigned char > ZipLocalEntry::getExtra() const {
 
104
  return extra_field ;
 
105
}
 
106
 
 
107
StorageMethod ZipLocalEntry::getMethod() const {
 
108
  return static_cast< StorageMethod >( compress_method ) ;
 
109
}
 
110
 
 
111
string ZipLocalEntry::getName() const {
 
112
  return filename ;
 
113
}
 
114
 
 
115
string ZipLocalEntry::getFileName() const {
 
116
  if ( isDirectory() )
 
117
    return string() ;
 
118
  string::size_type pos ;
 
119
  pos = filename.find_last_of( separator ) ;
 
120
  if ( pos != string::npos ) { // separator found!
 
121
    // isDirectory() check means pos should not be last, so pos+1 is ok 
 
122
    return filename.substr( pos + 1 ) ;
 
123
  } else {
 
124
    return filename ;
 
125
  }
 
126
}
 
127
 
 
128
uint32 ZipLocalEntry::getSize() const {
 
129
  return uncompress_size ;
 
130
}
 
131
 
 
132
int ZipLocalEntry::getTime() const {
 
133
  return ( last_mod_fdate << 16 ) + last_mod_ftime ; 
 
134
  // FIXME: what to do with this time date thing? (not only here?)
 
135
}
 
136
 
 
137
bool ZipLocalEntry::isValid() const {
 
138
  return _valid ;
 
139
}
 
140
 
 
141
bool ZipLocalEntry::isDirectory() const {
 
142
  //std::assert( filename.size() != 0 ) ;
 
143
  return  filename[ filename.size() - 1 ] == separator ;
 
144
}
 
145
 
 
146
 
 
147
void ZipLocalEntry::setComment( const string & ) {
 
148
  // A local entry cannot hold a comment
 
149
}
 
150
 
 
151
void ZipLocalEntry::setCompressedSize( uint32 size ) {
 
152
  compress_size = size ;
 
153
}
 
154
 
 
155
void ZipLocalEntry::setCrc( uint32 crc ) {
 
156
  crc_32 = crc ;
 
157
}
 
158
 
 
159
void ZipLocalEntry::setExtra( const vector< unsigned char > &extra ) {
 
160
  extra_field = extra ;
 
161
  extra_field_len = extra_field.size() ;
 
162
}
 
163
 
 
164
void ZipLocalEntry::setMethod( StorageMethod method ) {
 
165
  compress_method = static_cast< uint16 >( method ) ;
 
166
}
 
167
 
 
168
void ZipLocalEntry::setName( const string &name ) {
 
169
  filename = name ;
 
170
  filename_len = filename.size() ;
 
171
}
 
172
 
 
173
void ZipLocalEntry::setSize( uint32 size ) {
 
174
  uncompress_size = size ;
 
175
}
 
176
 
 
177
void ZipLocalEntry::setTime( int time ) {
 
178
  // FIXME: fix time setting here, and ind flist and elsewhere. Define the
 
179
  // date time semantics before mucking about - how surprising
 
180
 
 
181
  // Mark Donszelmann: added these lines to make zip work for winzip
 
182
  last_mod_fdate = (time >> 16) & 0x0000FFFF;
 
183
  last_mod_ftime = time & 0x0000FFFF;
 
184
}
 
185
 
 
186
string ZipLocalEntry::toString() const {
 
187
  OutputStringStream sout ;
 
188
  sout << filename << " (" << uncompress_size << " bytes, " ;
 
189
  sout << compress_size << " bytes compressed)" ;
 
190
  return sout.str() ;
 
191
}
 
192
 
 
193
int ZipLocalEntry::getLocalHeaderSize() const {
 
194
  return 30 + filename.size() + extra_field.size() ;
 
195
}
 
196
 
 
197
bool ZipLocalEntry::trailingDataDescriptor() const {
 
198
  // gp_bitfield bit 3 is one, if this entry uses a trailing data
 
199
  // descriptor to keep size, compressed size and crc-32
 
200
  // fields.
 
201
  if ( ( gp_bitfield & 4 ) == 1 )
 
202
    return true ;
 
203
  else
 
204
    return false ;
 
205
}
 
206
 
 
207
FileEntry *ZipLocalEntry::clone() const {
 
208
  return new ZipLocalEntry( *this ) ;
 
209
}
 
210
 
 
211
 
 
212
//
 
213
// ZipCDirEntry methods
 
214
//
 
215
 
 
216
const uint32 ZipCDirEntry::signature = 0x02014b50 ;
 
217
 
 
218
void ZipCDirEntry::setDefaultWriter() {
 
219
  writer_version = 0 ;
 
220
#ifdef WIN32
 
221
    writer_version |= static_cast< uint16 >( 0 ) << 8 ; // Windows, DOS
 
222
#else
 
223
    writer_version |= static_cast< uint16 >( 3 ) << 8 ; // Unix
 
224
#endif
 
225
    writer_version |= 20 ; // version number
 
226
}
 
227
 
 
228
string ZipCDirEntry::getComment() const {
 
229
  return file_comment ;
 
230
}
 
231
 
 
232
uint32 ZipCDirEntry::getLocalHeaderOffset() const {
 
233
  return rel_offset_loc_head ;
 
234
}
 
235
 
 
236
void ZipCDirEntry::setLocalHeaderOffset( uint32 offset ) {
 
237
  rel_offset_loc_head = offset ;
 
238
}
 
239
 
 
240
 
 
241
void ZipCDirEntry::setComment( const string &comment ) {
 
242
  file_comment = comment ;
 
243
  file_comment_len = file_comment.size() ;
 
244
}
 
245
 
 
246
 
 
247
string ZipCDirEntry::toString() const {
 
248
  OutputStringStream sout ;
 
249
  sout << filename << " (" << uncompress_size << " bytes, " ;
 
250
  sout << compress_size << " bytes compressed)" ;
 
251
  return sout.str() ;
 
252
}
 
253
 
 
254
 
 
255
int ZipCDirEntry::getCDirHeaderSize() const {
 
256
  return 46 + filename.size() + extra_field.size() + file_comment.size() ;
 
257
}
 
258
 
 
259
 
 
260
FileEntry *ZipCDirEntry::clone() const {
 
261
  return new ZipCDirEntry( *this ) ;
 
262
}
 
263
 
 
264
 
 
265
//
 
266
// EndOfCentralDirectory methods
 
267
//
 
268
 
 
269
const uint32 EndOfCentralDirectory::signature = 0x06054b50 ;
 
270
 
 
271
bool EndOfCentralDirectory::read( vector<unsigned char> &buf, int pos ) {
 
272
  if ( ( buf.size() - pos < sizeof( uint32 ) )   || 
 
273
       ( ! checkSignature( &( buf[ pos ] ) ) )     )
 
274
    return false ;
 
275
 
 
276
  eocd_offset_from_end = buf.size() - pos ;
 
277
  pos += sizeof( uint32 ) ;
 
278
  disk_num         = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
 
279
  cdir_disk_num    = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
 
280
  cdir_entries     = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
 
281
  cdir_tot_entries = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
 
282
  cdir_size        = ztohl( &( buf[ pos  ] ) ) ; pos += sizeof( uint32 ) ;
 
283
  cdir_offset      = ztohl( &( buf[ pos  ] ) ) ; pos += sizeof( uint32 ) ;
 
284
  zip_comment_len  = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
 
285
//    cerr << "Zip comment length = " << zip_comment_len << endl ;
 
286
//    cerr << "Length of remaining file = " << buf.size() - pos << endl ;
 
287
 
 
288
  return true ; // Dummy
 
289
}
 
290
 
 
291
bool EndOfCentralDirectory::checkSignature ( unsigned char *buf ) const {
 
292
//    cerr << "potential header: " << ztohl( buf ) << endl ;
 
293
  return checkSignature( ztohl( buf ) ) ;
 
294
}
 
295
 
 
296
 
 
297
 
 
298
} // namespace
 
299
 
 
300
 
 
301
 
 
302
/** \file
 
303
    Implementation of routines for reading the central directory and 
 
304
    local headers of a zip archive. 
 
305
*/
 
306
 
 
307
/*
 
308
  Zipios++ - a small C++ library that provides easy access to .zip files.
 
309
  Copyright (C) 2000  Thomas S�ndergaard
 
310
  
 
311
  This library is free software; you can redistribute it and/or
 
312
  modify it under the terms of the GNU Lesser General Public
 
313
  License as published by the Free Software Foundation; either
 
314
  version 2 of the License, or (at your option) any later version.
 
315
  
 
316
  This library is distributed in the hope that it will be useful,
 
317
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
318
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
319
  Lesser General Public License for more details.
 
320
  
 
321
  You should have received a copy of the GNU Lesser General Public
 
322
  License along with this library; if not, write to the Free Software
 
323
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
324
*/