~ubuntu-branches/ubuntu/quantal/poco/quantal

« back to all changes in this revision

Viewing changes to Zip/include/Poco/Zip/ZipLocalFileHeader.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2008-11-15 11:39:15 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081115113915-7kauhm2c3m2i7oid
Tags: 1.3.3p1-2
* Fixed FTBFS with GCC 4.4 due to missing #include (Closes: #505619)
* Renamed 20_gcc43-missing-include.dpatch to 20_gcc44-missing-include.dpatch
* Downgraded dependencies on -dbg packages (Closes: #504342)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ZipLocalFileHeader.h
 
3
//
 
4
// $Id: //poco/1.3/Zip/include/Poco/Zip/ZipLocalFileHeader.h#4 $
 
5
//
 
6
// Library: Zip
 
7
// Package: Zip
 
8
// Module:  ZipLocalFileHeader
 
9
//
 
10
// Definition of the ZipLocalFileHeader class.
 
11
//
 
12
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
 
13
// and Contributors.
 
14
//
 
15
// Permission is hereby granted, free of charge, to any person or organization
 
16
// obtaining a copy of the software and accompanying documentation covered by
 
17
// this license (the "Software") to use, reproduce, display, distribute,
 
18
// execute, and transmit the Software, and to prepare derivative works of the
 
19
// Software, and to permit third-parties to whom the Software is furnished to
 
20
// do so, all subject to the following:
 
21
// 
 
22
// The copyright notices in the Software and this entire statement, including
 
23
// the above license grant, this restriction and the following disclaimer,
 
24
// must be included in all copies of the Software, in whole or in part, and
 
25
// all derivative works of the Software, unless such copies or derivative
 
26
// works are solely in the form of machine-executable object code generated by
 
27
// a source language processor.
 
28
// 
 
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
35
// DEALINGS IN THE SOFTWARE.
 
36
//
 
37
 
 
38
 
 
39
#ifndef Zip_ZipLocalFileHeader_INCLUDED
 
40
#define Zip_ZipLocalFileHeader_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Zip/Zip.h"
 
44
#include "Poco/Zip/ZipUtil.h"
 
45
#include "Poco/Zip/ZipCommon.h"
 
46
#include "Poco/DateTime.h"
 
47
#include "Poco/Path.h"
 
48
#include <iostream>
 
49
 
 
50
 
 
51
namespace Poco {
 
52
namespace Zip {
 
53
 
 
54
 
 
55
class ParseCallback;
 
56
 
 
57
 
 
58
class Zip_API ZipLocalFileHeader
 
59
        /// Stores a Zip local file header
 
60
{
 
61
public:
 
62
        static const char HEADER[ZipCommon::HEADER_SIZE];
 
63
 
 
64
        ZipLocalFileHeader(const Poco::Path& fileName, const Poco::DateTime& lastModifiedAt, ZipCommon::CompressionMethod cm, ZipCommon::CompressionLevel cl);
 
65
                /// Creates a zip file header from an absoluteFile. fileName is the name of the file in the zip, outputIsSeekable determines if we write
 
66
                /// CRC and file sizes to the LocalFileHeader or after data compression into a ZipDataInfo
 
67
 
 
68
        ZipLocalFileHeader(std::istream& inp, bool assumeHeaderRead, ParseCallback& callback);
 
69
                /// Creates the ZipLocalFileHeader by parsing the input stream.
 
70
                /// If assumeHeaderRead is true we assume that the first 4 bytes were already read outside.
 
71
                /// If skipOverDataBlock is true we position the stream after the data block (either at the next FileHeader or the Directory Entry)
 
72
 
 
73
        virtual ~ZipLocalFileHeader();
 
74
                /// Destroys the ZipLocalFileHeader.
 
75
 
 
76
        ZipCommon::HostSystem getHostSystem() const;
 
77
 
 
78
        int getMajorVersionNumber() const;
 
79
 
 
80
        int getMinorVersionNumber() const;
 
81
 
 
82
        void getRequiredVersion(int& major, int& minor);
 
83
                /// The minimum version required to extract the data
 
84
 
 
85
        Poco::UInt32 getHeaderSize() const;
 
86
                /// Returns the total size of the header including filename + extra field size
 
87
 
 
88
        void setStartPos(std::streamoff start);
 
89
                /// Sets the start position to start and the end position to start+compressedSize
 
90
 
 
91
        std::streamoff getStartPos() const;
 
92
                /// Returns the position of the first byte of the header in the file stream
 
93
 
 
94
        std::streamoff getEndPos() const;
 
95
                /// Points past the last byte of the file entry (ie. either the first byte of the next header, or the directory)
 
96
 
 
97
        std::streamoff getDataStartPos() const;
 
98
                /// Returns the streamoffset for the very first byte of data. Will be equal to DataEndPos if no data present
 
99
 
 
100
        std::streamoff getDataEndPos() const;
 
101
 
 
102
        ZipCommon::CompressionMethod getCompressionMethod() const;
 
103
 
 
104
        ZipCommon::CompressionLevel getCompressionLevel() const;
 
105
        /// Returns the compression level used. Only valid when the compression method is CM_DEFLATE
 
106
 
 
107
        bool isEncrypted() const;
 
108
 
 
109
        const Poco::DateTime& lastModifiedAt() const;
 
110
 
 
111
        Poco::UInt32 getCRC() const;
 
112
 
 
113
        Poco::UInt32 getCompressedSize() const;
 
114
 
 
115
        Poco::UInt32 getUncompressedSize() const;
 
116
 
 
117
        void setCRC(Poco::UInt32 val);
 
118
 
 
119
        void setCompressedSize(Poco::UInt32 val);
 
120
 
 
121
        void setUncompressedSize(Poco::UInt32 val);
 
122
 
 
123
        const std::string& getFileName() const;
 
124
 
 
125
        bool isFile() const;
 
126
 
 
127
        bool isDirectory() const;
 
128
 
 
129
        bool hasExtraField() const;
 
130
 
 
131
        const std::string& getExtraField() const;
 
132
 
 
133
        bool hasData() const;
 
134
 
 
135
        bool searchCRCAndSizesAfterData() const;
 
136
 
 
137
        void setSearchCRCAndSizesAfterData(bool val);
 
138
 
 
139
        void setFileName(const std::string& fileName, bool isDirectory);
 
140
 
 
141
        std::string createHeader() const;
 
142
                /// Creates a header
 
143
 
 
144
private:
 
145
        void parse(std::istream& inp, bool assumeHeaderRead);
 
146
 
 
147
        void parseDateTime();
 
148
 
 
149
        void init(const Poco::Path& fileName, ZipCommon::CompressionMethod cm, ZipCommon::CompressionLevel cl);
 
150
 
 
151
        Poco::UInt16 getFileNameLength() const;
 
152
        
 
153
        Poco::UInt16 getExtraFieldLength() const;
 
154
 
 
155
        Poco::UInt32 getCRCFromHeader() const;
 
156
 
 
157
        Poco::UInt32 getCompressedSizeFromHeader() const;
 
158
 
 
159
        Poco::UInt32 getUncompressedSizeFromHeader() const;
 
160
 
 
161
        void setRequiredVersion(int major, int minor);
 
162
 
 
163
        void setHostSystem(ZipCommon::HostSystem hs);
 
164
 
 
165
        void setLastModifiedAt(const Poco::DateTime& dt);
 
166
 
 
167
        void setEncryption(bool val);
 
168
 
 
169
        void setFileNameLength(Poco::UInt16 size);
 
170
 
 
171
        void setExtraFieldSize(Poco::UInt16 size);
 
172
 
 
173
        void setCompressionMethod(ZipCommon::CompressionMethod cm);
 
174
 
 
175
        void setCompressionLevel(ZipCommon::CompressionLevel cl);
 
176
 
 
177
private:
 
178
        enum
 
179
        {
 
180
                HEADER_POS = 0,
 
181
                VERSION_SIZE = 2,
 
182
                VERSION_POS = HEADER_POS+ZipCommon::HEADER_SIZE,
 
183
                GENERAL_PURPOSE_SIZE = 2,
 
184
                GENERAL_PURPOSE_POS = VERSION_POS + VERSION_SIZE,
 
185
                COMPR_METHOD_SIZE = 2,
 
186
                COMPR_METHOD_POS = GENERAL_PURPOSE_POS + GENERAL_PURPOSE_SIZE,
 
187
                LASTMODEFILETIME_SIZE = 2,
 
188
                LASTMODEFILETIME_POS = COMPR_METHOD_POS + COMPR_METHOD_SIZE,
 
189
                LASTMODEFILEDATE_SIZE = 2,
 
190
                LASTMODEFILEDATE_POS = LASTMODEFILETIME_POS + LASTMODEFILETIME_SIZE,
 
191
                CRC32_SIZE = 4,
 
192
                CRC32_POS = LASTMODEFILEDATE_POS + LASTMODEFILEDATE_SIZE,
 
193
                COMPRESSEDSIZE_SIZE = 4,
 
194
                COMPRESSEDSIZE_POS = CRC32_POS + CRC32_SIZE,
 
195
                UNCOMPRESSEDSIZE_SIZE = 4,
 
196
                UNCOMPRESSEDSIZE_POS = COMPRESSEDSIZE_POS + COMPRESSEDSIZE_SIZE,
 
197
                FILELENGTH_SIZE = 2,
 
198
                FILELENGTH_POS = UNCOMPRESSEDSIZE_POS + UNCOMPRESSEDSIZE_SIZE,
 
199
                EXTRAFIELD_LENGTH = 2,
 
200
                EXTRAFIELD_POS = FILELENGTH_POS + FILELENGTH_SIZE,
 
201
                FULLHEADER_SIZE = 30
 
202
        };
 
203
 
 
204
        char           _rawHeader[FULLHEADER_SIZE];
 
205
        std::streamoff _startPos;
 
206
        std::streamoff _endPos;
 
207
        std::string    _fileName;
 
208
        Poco::DateTime _lastModifiedAt;
 
209
        std::string    _extraField;
 
210
        Poco::UInt32   _crc32;
 
211
        Poco::UInt32   _compressedSize;
 
212
        Poco::UInt32   _uncompressedSize;
 
213
};
 
214
 
 
215
 
 
216
inline void ZipLocalFileHeader::setFileNameLength(Poco::UInt16 size)
 
217
{
 
218
        ZipUtil::set16BitValue(size, _rawHeader, FILELENGTH_POS);
 
219
}
 
220
 
 
221
 
 
222
inline void ZipLocalFileHeader::setExtraFieldSize(Poco::UInt16 size)
 
223
{
 
224
        ZipUtil::set16BitValue(size, _rawHeader, EXTRAFIELD_POS);
 
225
}
 
226
 
 
227
 
 
228
inline ZipCommon::HostSystem ZipLocalFileHeader::getHostSystem() const
 
229
{
 
230
        return static_cast<ZipCommon::HostSystem>(_rawHeader[VERSION_POS + 1]);
 
231
}
 
232
 
 
233
 
 
234
inline void ZipLocalFileHeader::setHostSystem(ZipCommon::HostSystem hs)
 
235
{
 
236
        _rawHeader[VERSION_POS + 1] = static_cast<char>(hs);
 
237
}
 
238
 
 
239
 
 
240
inline int ZipLocalFileHeader::getMajorVersionNumber() const
 
241
{
 
242
        return (_rawHeader[VERSION_POS]/10);
 
243
}
 
244
 
 
245
 
 
246
inline int ZipLocalFileHeader::getMinorVersionNumber() const
 
247
{
 
248
        return (_rawHeader[VERSION_POS]%10);
 
249
}
 
250
 
 
251
 
 
252
inline void ZipLocalFileHeader::getRequiredVersion(int& major, int& minor)
 
253
{
 
254
        major = getMajorVersionNumber();
 
255
        minor = getMinorVersionNumber();
 
256
}
 
257
 
 
258
 
 
259
inline void ZipLocalFileHeader::setRequiredVersion(int major, int minor)
 
260
{
 
261
        poco_assert (minor < 10);
 
262
        poco_assert (major < 24);
 
263
        _rawHeader[VERSION_POS] = static_cast<char>(static_cast<unsigned char>(major)*10+static_cast<unsigned char>(minor));
 
264
}
 
265
 
 
266
inline Poco::UInt16 ZipLocalFileHeader::getFileNameLength() const
 
267
{
 
268
        return ZipUtil::get16BitValue(_rawHeader, FILELENGTH_POS);
 
269
}
 
270
 
 
271
 
 
272
inline Poco::UInt16 ZipLocalFileHeader::getExtraFieldLength() const
 
273
{
 
274
        return ZipUtil::get16BitValue(_rawHeader, EXTRAFIELD_POS);
 
275
}
 
276
 
 
277
 
 
278
inline Poco::UInt32 ZipLocalFileHeader::getHeaderSize() const
 
279
{
 
280
        return FULLHEADER_SIZE+getExtraFieldLength()+getFileNameLength();
 
281
}
 
282
 
 
283
 
 
284
inline std::streamoff ZipLocalFileHeader::getStartPos() const
 
285
{
 
286
        return _startPos;
 
287
}
 
288
 
 
289
 
 
290
inline void ZipLocalFileHeader::setStartPos(std::streamoff start)
 
291
{
 
292
        _startPos = start;
 
293
        _endPos = start + getHeaderSize()+getCompressedSize();
 
294
}
 
295
 
 
296
 
 
297
inline std::streamoff ZipLocalFileHeader::getEndPos() const
 
298
{
 
299
        return _endPos;
 
300
}
 
301
 
 
302
 
 
303
inline void ZipLocalFileHeader::parseDateTime()
 
304
{
 
305
        _lastModifiedAt = ZipUtil::parseDateTime(_rawHeader, LASTMODEFILETIME_POS, LASTMODEFILEDATE_POS);
 
306
}
 
307
 
 
308
 
 
309
inline void ZipLocalFileHeader::setLastModifiedAt(const Poco::DateTime& dt)
 
310
{
 
311
        _lastModifiedAt = dt;
 
312
        ZipUtil::setDateTime(dt, _rawHeader, LASTMODEFILETIME_POS, LASTMODEFILEDATE_POS);
 
313
}
 
314
 
 
315
 
 
316
inline ZipCommon::CompressionMethod ZipLocalFileHeader::getCompressionMethod() const
 
317
{
 
318
        return static_cast<ZipCommon::CompressionMethod>(ZipUtil::get16BitValue(_rawHeader, COMPR_METHOD_POS));
 
319
}
 
320
 
 
321
 
 
322
inline ZipCommon::CompressionLevel ZipLocalFileHeader::getCompressionLevel() const
 
323
{
 
324
        // bit 1 and 2 indicate the level
 
325
        return static_cast<ZipCommon::CompressionLevel>((ZipUtil::get16BitValue(_rawHeader, GENERAL_PURPOSE_POS)>>1) & 0x0003);
 
326
}
 
327
 
 
328
 
 
329
inline void ZipLocalFileHeader::setCompressionMethod(ZipCommon::CompressionMethod cm)
 
330
{
 
331
        ZipUtil::set16BitValue(static_cast<Poco::UInt16>(cm), _rawHeader, COMPR_METHOD_POS);
 
332
}
 
333
 
 
334
 
 
335
inline void ZipLocalFileHeader::setCompressionLevel(ZipCommon::CompressionLevel cl)
 
336
{
 
337
        // bit 1 and 2 indicate the level
 
338
        Poco::UInt16 val = static_cast<Poco::UInt16>(cl);
 
339
        val <<= 1; 
 
340
        Poco::UInt16 mask = 0xfff9;
 
341
        _rawHeader[GENERAL_PURPOSE_POS] = ((_rawHeader[GENERAL_PURPOSE_POS] & mask) | val);
 
342
}
 
343
 
 
344
 
 
345
inline bool ZipLocalFileHeader::isEncrypted() const
 
346
{
 
347
        // bit 0 indicates encryption
 
348
        return ((ZipUtil::get16BitValue(_rawHeader, GENERAL_PURPOSE_POS) & 0x0001) != 0);
 
349
}
 
350
 
 
351
 
 
352
inline void ZipLocalFileHeader::setEncryption(bool val)
 
353
{
 
354
        if (val)
 
355
                _rawHeader[GENERAL_PURPOSE_POS] |= 0x01;
 
356
        else
 
357
                _rawHeader[GENERAL_PURPOSE_POS] &= 0xfe;
 
358
}
 
359
 
 
360
 
 
361
inline void ZipLocalFileHeader::setSearchCRCAndSizesAfterData(bool val)
 
362
{
 
363
        //set bit 3 of general purpose reg
 
364
        if (val)
 
365
                _rawHeader[GENERAL_PURPOSE_POS] |= 0x08;
 
366
        else
 
367
                _rawHeader[GENERAL_PURPOSE_POS] &= 0xf7;
 
368
}
 
369
 
 
370
 
 
371
inline const Poco::DateTime& ZipLocalFileHeader::lastModifiedAt() const
 
372
{
 
373
        return _lastModifiedAt;
 
374
}
 
375
 
 
376
 
 
377
inline Poco::UInt32 ZipLocalFileHeader::getCRC() const
 
378
{
 
379
        return _crc32;
 
380
}
 
381
 
 
382
 
 
383
inline Poco::UInt32 ZipLocalFileHeader::getCompressedSize() const
 
384
{
 
385
        return _compressedSize;
 
386
}
 
387
 
 
388
 
 
389
inline Poco::UInt32 ZipLocalFileHeader::getUncompressedSize() const
 
390
{
 
391
        return _uncompressedSize;
 
392
}
 
393
 
 
394
 
 
395
inline void ZipLocalFileHeader::setCRC(Poco::UInt32 val)
 
396
{
 
397
        _crc32 = val;
 
398
        ZipUtil::set32BitValue(val, _rawHeader, CRC32_POS);
 
399
}
 
400
 
 
401
 
 
402
inline void ZipLocalFileHeader::setCompressedSize(Poco::UInt32 val)
 
403
{
 
404
        _compressedSize = val;
 
405
        ZipUtil::set32BitValue(val, _rawHeader, COMPRESSEDSIZE_POS);
 
406
}
 
407
 
 
408
 
 
409
inline void ZipLocalFileHeader::setUncompressedSize(Poco::UInt32 val)
 
410
{
 
411
        _uncompressedSize = val;
 
412
        ZipUtil::set32BitValue(val, _rawHeader, UNCOMPRESSEDSIZE_POS);
 
413
}
 
414
 
 
415
 
 
416
inline Poco::UInt32 ZipLocalFileHeader::getCRCFromHeader() const
 
417
{
 
418
        return ZipUtil::get32BitValue(_rawHeader, CRC32_POS);
 
419
}
 
420
 
 
421
 
 
422
inline Poco::UInt32 ZipLocalFileHeader::getCompressedSizeFromHeader() const
 
423
{
 
424
        return ZipUtil::get32BitValue(_rawHeader, COMPRESSEDSIZE_POS);
 
425
}
 
426
 
 
427
 
 
428
inline Poco::UInt32 ZipLocalFileHeader::getUncompressedSizeFromHeader() const
 
429
{
 
430
        return ZipUtil::get32BitValue(_rawHeader, UNCOMPRESSEDSIZE_POS);
 
431
}
 
432
 
 
433
 
 
434
inline const std::string& ZipLocalFileHeader::getFileName() const
 
435
{
 
436
        return _fileName;
 
437
}
 
438
 
 
439
 
 
440
inline bool ZipLocalFileHeader::isFile() const
 
441
{
 
442
        return !isDirectory();
 
443
}
 
444
 
 
445
 
 
446
inline bool ZipLocalFileHeader::isDirectory() const
 
447
{
 
448
        poco_assert_dbg(!_fileName.empty());
 
449
        return getUncompressedSize() == 0 && getCompressionMethod() == ZipCommon::CM_STORE && _fileName[_fileName.length()-1] == '/';
 
450
}
 
451
 
 
452
 
 
453
inline bool ZipLocalFileHeader::hasExtraField() const
 
454
{
 
455
        return getExtraFieldLength() > 0;
 
456
}
 
457
 
 
458
 
 
459
inline const std::string& ZipLocalFileHeader::getExtraField() const
 
460
{
 
461
        return _extraField;
 
462
}
 
463
 
 
464
 
 
465
inline bool ZipLocalFileHeader::hasData() const
 
466
{
 
467
        return (getCompressedSize() > 0);
 
468
}
 
469
 
 
470
 
 
471
inline std::streamoff ZipLocalFileHeader::getDataStartPos() const
 
472
{
 
473
        return getStartPos() + getHeaderSize();
 
474
}
 
475
 
 
476
 
 
477
inline std::streamoff ZipLocalFileHeader::getDataEndPos() const
 
478
{
 
479
        return getDataStartPos()+getCompressedSize();
 
480
}
 
481
 
 
482
 
 
483
} } // namespace Poco::Zip
 
484
 
 
485
 
 
486
#endif // Zip_ZipLocalFileHeader_INCLUDED