FreeCAD

Форк
0
/
ziphead.cpp 
290 строк · 8.3 Кб
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
void ZipLocalEntry::setDefaultExtract() {
58
  extract_version = 20 ; // version number
59
}
60

61
string ZipLocalEntry::getComment() const {
62
  return "" ; // No comment in a local entry
63
}
64

65
uint32 ZipLocalEntry::getCompressedSize() const {
66
  return compress_size ;
67
}
68

69
uint32 ZipLocalEntry::getCrc() const {
70
  return crc_32 ;
71
}
72

73
vector< unsigned char > ZipLocalEntry::getExtra() const {
74
  return extra_field ;
75
}
76

77
StorageMethod ZipLocalEntry::getMethod() const {
78
  return static_cast< StorageMethod >( compress_method ) ;
79
}
80

81
string ZipLocalEntry::getName() const {
82
  return filename ;
83
}
84

85
string ZipLocalEntry::getFileName() const {
86
  if ( isDirectory() )
87
    return string() ;
88
  string::size_type pos ;
89
  pos = filename.find_last_of( separator ) ;
90
  if ( pos != string::npos ) { // separator found!
91
    // isDirectory() check means pos should not be last, so pos+1 is ok 
92
    return filename.substr( pos + 1 ) ;
93
  } else {
94
    return filename ;
95
  }
96
}
97

98
uint32 ZipLocalEntry::getSize() const {
99
  return uncompress_size ;
100
}
101

102
int ZipLocalEntry::getTime() const {
103
  return ( last_mod_fdate << 16 ) + last_mod_ftime ; 
104
  // FIXME: what to do with this time date thing? (not only here?)
105
}
106

107
bool ZipLocalEntry::isValid() const {
108
  return _valid ;
109
}
110

111
bool ZipLocalEntry::isDirectory() const {
112
  //std::assert( filename.size() != 0 ) ;
113
  return  filename[ filename.size() - 1 ] == separator ;
114
}
115

116

117
void ZipLocalEntry::setComment( const string & ) {
118
  // A local entry cannot hold a comment
119
}
120

121
void ZipLocalEntry::setCompressedSize( uint32 size ) {
122
  compress_size = size ;
123
}
124

125
void ZipLocalEntry::setCrc( uint32 crc ) {
126
  crc_32 = crc ;
127
}
128

129
void ZipLocalEntry::setExtra( const vector< unsigned char > &extra ) {
130
  extra_field = extra ;
131
  extra_field_len = extra_field.size() ;
132
}
133

134
void ZipLocalEntry::setMethod( StorageMethod method ) {
135
  compress_method = static_cast< uint16 >( method ) ;
136
}
137

138
void ZipLocalEntry::setName( const string &name ) {
139
  filename = name ;
140
  filename_len = filename.size() ;
141
}
142

143
void ZipLocalEntry::setSize( uint32 size ) {
144
  uncompress_size = size ;
145
}
146

147
void ZipLocalEntry::setTime( int time ) {
148
  // FIXME: fix time setting here, and ind flist and elsewhere. Define the
149
  // date time semantics before mucking about - how surprising
150

151
  // Mark Donszelmann: added these lines to make zip work for winzip
152
  last_mod_fdate = (time >> 16) & 0x0000FFFF;
153
  last_mod_ftime = time & 0x0000FFFF;
154
}
155

156
string ZipLocalEntry::toString() const {
157
  OutputStringStream sout ;
158
  sout << filename << " (" << uncompress_size << " bytes, " ;
159
  sout << compress_size << " bytes compressed)" ;
160
  return sout.str() ;
161
}
162

163
int ZipLocalEntry::getLocalHeaderSize() const {
164
  return 30 + filename.size() + extra_field.size() ;
165
}
166

167
bool ZipLocalEntry::trailingDataDescriptor() const {
168
  // gp_bitfield bit 3 is one, if this entry uses a trailing data
169
  // descriptor to keep size, compressed size and crc-32
170
  // fields.
171
  return ( gp_bitfield & 4 ) != 0 ;
172
}
173

174
FileEntry *ZipLocalEntry::clone() const {
175
  return new ZipLocalEntry( *this ) ;
176
}
177

178

179
//
180
// ZipCDirEntry methods
181
//
182

183
const uint32 ZipCDirEntry::signature = 0x02014b50 ;
184

185
void ZipCDirEntry::setDefaultWriter() {
186
  writer_version = 0 ;
187
#ifdef WIN32
188
    writer_version |= static_cast< uint16 >( 0 ) << 8 ; // Windows, DOS
189
#else
190
    writer_version |= static_cast< uint16 >( 3 ) << 8 ; // Unix
191
#endif
192
    writer_version |= 20 ; // version number
193
}
194

195
string ZipCDirEntry::getComment() const {
196
  return file_comment ;
197
}
198

199
uint32 ZipCDirEntry::getLocalHeaderOffset() const {
200
  return rel_offset_loc_head ;
201
}
202

203
void ZipCDirEntry::setLocalHeaderOffset( uint32 offset ) {
204
  rel_offset_loc_head = offset ;
205
}
206

207

208
void ZipCDirEntry::setComment( const string &comment ) {
209
  file_comment = comment ;
210
  file_comment_len = file_comment.size() ;
211
}
212

213

214
string ZipCDirEntry::toString() const {
215
  OutputStringStream sout ;
216
  sout << filename << " (" << uncompress_size << " bytes, " ;
217
  sout << compress_size << " bytes compressed)" ;
218
  return sout.str() ;
219
}
220

221

222
int ZipCDirEntry::getCDirHeaderSize() const {
223
  return 46 + filename.size() + extra_field.size() + file_comment.size() ;
224
}
225

226

227
FileEntry *ZipCDirEntry::clone() const {
228
  return new ZipCDirEntry( *this ) ;
229
}
230

231

232
//
233
// EndOfCentralDirectory methods
234
//
235

236
const uint32 EndOfCentralDirectory::signature = 0x06054b50 ;
237

238
bool EndOfCentralDirectory::read( vector<unsigned char> &buf, int pos ) {
239
  if ( ( buf.size() - pos < sizeof( uint32 ) )   || 
240
       ( ! checkSignature( &( buf[ pos ] ) ) )     )
241
    return false ;
242

243
  eocd_offset_from_end = buf.size() - pos ;
244
  pos += sizeof( uint32 ) ;
245
  disk_num         = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
246
  cdir_disk_num    = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
247
  cdir_entries     = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
248
  cdir_tot_entries = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
249
  cdir_size        = ztohl( &( buf[ pos  ] ) ) ; pos += sizeof( uint32 ) ;
250
  cdir_offset      = ztohl( &( buf[ pos  ] ) ) ; pos += sizeof( uint32 ) ;
251
  zip_comment_len  = ztohs( &( buf[ pos  ] ) ) ; pos += sizeof( uint16 ) ;
252
//    cerr << "Zip comment length = " << zip_comment_len << endl ;
253
//    cerr << "Length of remaining file = " << buf.size() - pos << endl ;
254

255
  return true ; // Dummy
256
}
257

258
bool EndOfCentralDirectory::checkSignature ( unsigned char *buf ) const {
259
//    cerr << "potential header: " << ztohl( buf ) << endl ;
260
  return checkSignature( ztohl( buf ) ) ;
261
}
262

263

264

265
} // namespace
266

267

268

269
/** \file
270
    Implementation of routines for reading the central directory and 
271
    local headers of a zip archive. 
272
*/
273

274
/*
275
  Zipios++ - a small C++ library that provides easy access to .zip files.
276
  Copyright (C) 2000  Thomas Søndergaard
277
  
278
  This library is free software; you can redistribute it and/or
279
  modify it under the terms of the GNU Lesser General Public
280
  License as published by the Free Software Foundation; either
281
  version 2 of the License, or (at your option) any later version.
282
  
283
  This library is distributed in the hope that it will be useful,
284
  but WITHOUT ANY WARRANTY; without even the implied warranty of
285
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
286
  Lesser General Public License for more details.
287
  
288
  You should have received a copy of the GNU Lesser General Public
289
  License along with this library; if not, write to the Free Software
290
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
291
*/
292

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.