2
// ZipLocalFileHeader.h
4
// $Id: //poco/1.3/Zip/include/Poco/Zip/ZipLocalFileHeader.h#4 $
8
// Module: ZipLocalFileHeader
10
// Definition of the ZipLocalFileHeader class.
12
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
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:
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.
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.
39
#ifndef Zip_ZipLocalFileHeader_INCLUDED
40
#define Zip_ZipLocalFileHeader_INCLUDED
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"
58
class Zip_API ZipLocalFileHeader
59
/// Stores a Zip local file header
62
static const char HEADER[ZipCommon::HEADER_SIZE];
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
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)
73
virtual ~ZipLocalFileHeader();
74
/// Destroys the ZipLocalFileHeader.
76
ZipCommon::HostSystem getHostSystem() const;
78
int getMajorVersionNumber() const;
80
int getMinorVersionNumber() const;
82
void getRequiredVersion(int& major, int& minor);
83
/// The minimum version required to extract the data
85
Poco::UInt32 getHeaderSize() const;
86
/// Returns the total size of the header including filename + extra field size
88
void setStartPos(std::streamoff start);
89
/// Sets the start position to start and the end position to start+compressedSize
91
std::streamoff getStartPos() const;
92
/// Returns the position of the first byte of the header in the file stream
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)
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
100
std::streamoff getDataEndPos() const;
102
ZipCommon::CompressionMethod getCompressionMethod() const;
104
ZipCommon::CompressionLevel getCompressionLevel() const;
105
/// Returns the compression level used. Only valid when the compression method is CM_DEFLATE
107
bool isEncrypted() const;
109
const Poco::DateTime& lastModifiedAt() const;
111
Poco::UInt32 getCRC() const;
113
Poco::UInt32 getCompressedSize() const;
115
Poco::UInt32 getUncompressedSize() const;
117
void setCRC(Poco::UInt32 val);
119
void setCompressedSize(Poco::UInt32 val);
121
void setUncompressedSize(Poco::UInt32 val);
123
const std::string& getFileName() const;
127
bool isDirectory() const;
129
bool hasExtraField() const;
131
const std::string& getExtraField() const;
133
bool hasData() const;
135
bool searchCRCAndSizesAfterData() const;
137
void setSearchCRCAndSizesAfterData(bool val);
139
void setFileName(const std::string& fileName, bool isDirectory);
141
std::string createHeader() const;
145
void parse(std::istream& inp, bool assumeHeaderRead);
147
void parseDateTime();
149
void init(const Poco::Path& fileName, ZipCommon::CompressionMethod cm, ZipCommon::CompressionLevel cl);
151
Poco::UInt16 getFileNameLength() const;
153
Poco::UInt16 getExtraFieldLength() const;
155
Poco::UInt32 getCRCFromHeader() const;
157
Poco::UInt32 getCompressedSizeFromHeader() const;
159
Poco::UInt32 getUncompressedSizeFromHeader() const;
161
void setRequiredVersion(int major, int minor);
163
void setHostSystem(ZipCommon::HostSystem hs);
165
void setLastModifiedAt(const Poco::DateTime& dt);
167
void setEncryption(bool val);
169
void setFileNameLength(Poco::UInt16 size);
171
void setExtraFieldSize(Poco::UInt16 size);
173
void setCompressionMethod(ZipCommon::CompressionMethod cm);
175
void setCompressionLevel(ZipCommon::CompressionLevel cl);
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,
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,
198
FILELENGTH_POS = UNCOMPRESSEDSIZE_POS + UNCOMPRESSEDSIZE_SIZE,
199
EXTRAFIELD_LENGTH = 2,
200
EXTRAFIELD_POS = FILELENGTH_POS + FILELENGTH_SIZE,
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;
211
Poco::UInt32 _compressedSize;
212
Poco::UInt32 _uncompressedSize;
216
inline void ZipLocalFileHeader::setFileNameLength(Poco::UInt16 size)
218
ZipUtil::set16BitValue(size, _rawHeader, FILELENGTH_POS);
222
inline void ZipLocalFileHeader::setExtraFieldSize(Poco::UInt16 size)
224
ZipUtil::set16BitValue(size, _rawHeader, EXTRAFIELD_POS);
228
inline ZipCommon::HostSystem ZipLocalFileHeader::getHostSystem() const
230
return static_cast<ZipCommon::HostSystem>(_rawHeader[VERSION_POS + 1]);
234
inline void ZipLocalFileHeader::setHostSystem(ZipCommon::HostSystem hs)
236
_rawHeader[VERSION_POS + 1] = static_cast<char>(hs);
240
inline int ZipLocalFileHeader::getMajorVersionNumber() const
242
return (_rawHeader[VERSION_POS]/10);
246
inline int ZipLocalFileHeader::getMinorVersionNumber() const
248
return (_rawHeader[VERSION_POS]%10);
252
inline void ZipLocalFileHeader::getRequiredVersion(int& major, int& minor)
254
major = getMajorVersionNumber();
255
minor = getMinorVersionNumber();
259
inline void ZipLocalFileHeader::setRequiredVersion(int major, int minor)
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));
266
inline Poco::UInt16 ZipLocalFileHeader::getFileNameLength() const
268
return ZipUtil::get16BitValue(_rawHeader, FILELENGTH_POS);
272
inline Poco::UInt16 ZipLocalFileHeader::getExtraFieldLength() const
274
return ZipUtil::get16BitValue(_rawHeader, EXTRAFIELD_POS);
278
inline Poco::UInt32 ZipLocalFileHeader::getHeaderSize() const
280
return FULLHEADER_SIZE+getExtraFieldLength()+getFileNameLength();
284
inline std::streamoff ZipLocalFileHeader::getStartPos() const
290
inline void ZipLocalFileHeader::setStartPos(std::streamoff start)
293
_endPos = start + getHeaderSize()+getCompressedSize();
297
inline std::streamoff ZipLocalFileHeader::getEndPos() const
303
inline void ZipLocalFileHeader::parseDateTime()
305
_lastModifiedAt = ZipUtil::parseDateTime(_rawHeader, LASTMODEFILETIME_POS, LASTMODEFILEDATE_POS);
309
inline void ZipLocalFileHeader::setLastModifiedAt(const Poco::DateTime& dt)
311
_lastModifiedAt = dt;
312
ZipUtil::setDateTime(dt, _rawHeader, LASTMODEFILETIME_POS, LASTMODEFILEDATE_POS);
316
inline ZipCommon::CompressionMethod ZipLocalFileHeader::getCompressionMethod() const
318
return static_cast<ZipCommon::CompressionMethod>(ZipUtil::get16BitValue(_rawHeader, COMPR_METHOD_POS));
322
inline ZipCommon::CompressionLevel ZipLocalFileHeader::getCompressionLevel() const
324
// bit 1 and 2 indicate the level
325
return static_cast<ZipCommon::CompressionLevel>((ZipUtil::get16BitValue(_rawHeader, GENERAL_PURPOSE_POS)>>1) & 0x0003);
329
inline void ZipLocalFileHeader::setCompressionMethod(ZipCommon::CompressionMethod cm)
331
ZipUtil::set16BitValue(static_cast<Poco::UInt16>(cm), _rawHeader, COMPR_METHOD_POS);
335
inline void ZipLocalFileHeader::setCompressionLevel(ZipCommon::CompressionLevel cl)
337
// bit 1 and 2 indicate the level
338
Poco::UInt16 val = static_cast<Poco::UInt16>(cl);
340
Poco::UInt16 mask = 0xfff9;
341
_rawHeader[GENERAL_PURPOSE_POS] = ((_rawHeader[GENERAL_PURPOSE_POS] & mask) | val);
345
inline bool ZipLocalFileHeader::isEncrypted() const
347
// bit 0 indicates encryption
348
return ((ZipUtil::get16BitValue(_rawHeader, GENERAL_PURPOSE_POS) & 0x0001) != 0);
352
inline void ZipLocalFileHeader::setEncryption(bool val)
355
_rawHeader[GENERAL_PURPOSE_POS] |= 0x01;
357
_rawHeader[GENERAL_PURPOSE_POS] &= 0xfe;
361
inline void ZipLocalFileHeader::setSearchCRCAndSizesAfterData(bool val)
363
//set bit 3 of general purpose reg
365
_rawHeader[GENERAL_PURPOSE_POS] |= 0x08;
367
_rawHeader[GENERAL_PURPOSE_POS] &= 0xf7;
371
inline const Poco::DateTime& ZipLocalFileHeader::lastModifiedAt() const
373
return _lastModifiedAt;
377
inline Poco::UInt32 ZipLocalFileHeader::getCRC() const
383
inline Poco::UInt32 ZipLocalFileHeader::getCompressedSize() const
385
return _compressedSize;
389
inline Poco::UInt32 ZipLocalFileHeader::getUncompressedSize() const
391
return _uncompressedSize;
395
inline void ZipLocalFileHeader::setCRC(Poco::UInt32 val)
398
ZipUtil::set32BitValue(val, _rawHeader, CRC32_POS);
402
inline void ZipLocalFileHeader::setCompressedSize(Poco::UInt32 val)
404
_compressedSize = val;
405
ZipUtil::set32BitValue(val, _rawHeader, COMPRESSEDSIZE_POS);
409
inline void ZipLocalFileHeader::setUncompressedSize(Poco::UInt32 val)
411
_uncompressedSize = val;
412
ZipUtil::set32BitValue(val, _rawHeader, UNCOMPRESSEDSIZE_POS);
416
inline Poco::UInt32 ZipLocalFileHeader::getCRCFromHeader() const
418
return ZipUtil::get32BitValue(_rawHeader, CRC32_POS);
422
inline Poco::UInt32 ZipLocalFileHeader::getCompressedSizeFromHeader() const
424
return ZipUtil::get32BitValue(_rawHeader, COMPRESSEDSIZE_POS);
428
inline Poco::UInt32 ZipLocalFileHeader::getUncompressedSizeFromHeader() const
430
return ZipUtil::get32BitValue(_rawHeader, UNCOMPRESSEDSIZE_POS);
434
inline const std::string& ZipLocalFileHeader::getFileName() const
440
inline bool ZipLocalFileHeader::isFile() const
442
return !isDirectory();
446
inline bool ZipLocalFileHeader::isDirectory() const
448
poco_assert_dbg(!_fileName.empty());
449
return getUncompressedSize() == 0 && getCompressionMethod() == ZipCommon::CM_STORE && _fileName[_fileName.length()-1] == '/';
453
inline bool ZipLocalFileHeader::hasExtraField() const
455
return getExtraFieldLength() > 0;
459
inline const std::string& ZipLocalFileHeader::getExtraField() const
465
inline bool ZipLocalFileHeader::hasData() const
467
return (getCompressedSize() > 0);
471
inline std::streamoff ZipLocalFileHeader::getDataStartPos() const
473
return getStartPos() + getHeaderSize();
477
inline std::streamoff ZipLocalFileHeader::getDataEndPos() const
479
return getDataStartPos()+getCompressedSize();
483
} } // namespace Poco::Zip
486
#endif // Zip_ZipLocalFileHeader_INCLUDED