2
// ZipLocalFileHeader.cpp
4
// $Id: //poco/1.3/Zip/src/ZipLocalFileHeader.cpp#4 $
8
// Module: ZipLocalFileHeader
10
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/Zip/ZipLocalFileHeader.h"
38
#include "Poco/Zip/ZipDataInfo.h"
39
#include "Poco/Zip/ParseCallback.h"
40
#include "Poco/Buffer.h"
41
#include "Poco/Exception.h"
42
#include "Poco/File.h"
50
const char ZipLocalFileHeader::HEADER[ZipCommon::HEADER_SIZE] = {'\x50', '\x4b', '\x03', '\x04'};
53
ZipLocalFileHeader::ZipLocalFileHeader(const Poco::Path& fileName,
54
const Poco::DateTime& lastModifiedAt,
55
ZipCommon::CompressionMethod cm,
56
ZipCommon::CompressionLevel cl):
67
std::memcpy(_rawHeader, HEADER, ZipCommon::HEADER_SIZE);
68
std::memset(_rawHeader+ZipCommon::HEADER_SIZE, 0, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
69
ZipCommon::HostSystem hs = ZipCommon::HS_FAT;
71
#if (POCO_OS == POCO_OS_CYGWIN)
72
hs = ZipCommon::HS_UNIX;
74
#if (POCO_OS == POCO_OS_VMS)
75
hs = ZipCommon::HS_VMS;
77
#if defined(POCO_OS_FAMILY_UNIX)
78
hs = ZipCommon::HS_UNIX;
83
setLastModifiedAt(lastModifiedAt);
84
init(fileName, cm, cl);
88
ZipLocalFileHeader::ZipLocalFileHeader(std::istream& inp, bool assumeHeaderRead, ParseCallback& callback):
90
_startPos(inp.tellg()),
99
poco_assert_dbg( (EXTRAFIELD_POS+EXTRAFIELD_LENGTH) == FULLHEADER_SIZE);
101
if (assumeHeaderRead)
102
_startPos -= ZipCommon::HEADER_SIZE;
104
parse(inp, assumeHeaderRead);
106
bool ok = callback.handleZipEntry(inp, *this);
110
if (searchCRCAndSizesAfterData())
112
ZipDataInfo nfo(inp, false);
113
setCRC(nfo.getCRC32());
114
setCompressedSize(nfo.getCompressedSize());
115
setUncompressedSize(nfo.getUncompressedSize());
120
poco_assert_dbg(!searchCRCAndSizesAfterData());
123
_endPos = _startPos + getHeaderSize() + _compressedSize; // exclude the data block!
127
ZipLocalFileHeader::~ZipLocalFileHeader()
132
void ZipLocalFileHeader::parse(std::istream& inp, bool assumeHeaderRead)
134
if (!assumeHeaderRead)
136
inp.read(_rawHeader, ZipCommon::HEADER_SIZE);
140
std::memcpy(_rawHeader, HEADER, ZipCommon::HEADER_SIZE);
142
poco_assert (std::memcmp(_rawHeader, HEADER, ZipCommon::HEADER_SIZE) == 0);
143
// read the rest of the header
144
inp.read(_rawHeader + ZipCommon::HEADER_SIZE, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
145
poco_assert (_rawHeader[VERSION_POS + 1]>= ZipCommon::HS_FAT && _rawHeader[VERSION_POS + 1] < ZipCommon::HS_UNUSED);
146
poco_assert (getMajorVersionNumber() <= 2);
147
poco_assert (ZipUtil::get16BitValue(_rawHeader, COMPR_METHOD_POS) < ZipCommon::CM_UNUSED);
149
Poco::UInt16 len = getFileNameLength();
150
Poco::Buffer<char> buf(len);
151
inp.read(buf.begin(), len);
152
_fileName = std::string(buf.begin(), len);
155
len = getExtraFieldLength();
156
Poco::Buffer<char> xtra(len);
157
inp.read(xtra.begin(), len);
158
_extraField = std::string(xtra.begin(), len);
160
if (!searchCRCAndSizesAfterData())
162
_crc32 = getCRCFromHeader();
163
_compressedSize = getCompressedSizeFromHeader();
164
_uncompressedSize = getUncompressedSizeFromHeader();
169
bool ZipLocalFileHeader::searchCRCAndSizesAfterData() const
171
if (getCompressionMethod() == ZipCommon::CM_DEFLATE)
174
return ((ZipUtil::get16BitValue(_rawHeader, GENERAL_PURPOSE_POS) & 0x0008) != 0);
180
void ZipLocalFileHeader::setFileName(const std::string& fileName, bool isDirectory)
182
poco_assert (!fileName.empty());
183
Poco::Path aPath(fileName);
187
aPath.makeDirectory();
189
setCompressedSize(0);
190
setUncompressedSize(0);
191
setCompressionMethod(ZipCommon::CM_STORE);
192
setCompressionLevel(ZipCommon::CL_NORMAL);
198
_fileName = aPath.toString(Poco::Path::PATH_UNIX);
199
if (_fileName[0] == '/')
200
_fileName = _fileName.substr(1);
203
poco_assert_dbg (_fileName[_fileName.size()-1] == '/');
205
setFileNameLength(static_cast<Poco::UInt16>(_fileName.size()));
209
void ZipLocalFileHeader::init( const Poco::Path& fName,
210
ZipCommon::CompressionMethod cm,
211
ZipCommon::CompressionLevel cl)
213
poco_assert (_fileName.empty());
214
setSearchCRCAndSizesAfterData(false);
215
Poco::Path fileName(fName);
216
fileName.setDevice(""); // clear device!
217
setFileName(fileName.toString(Poco::Path::PATH_UNIX), fileName.isDirectory());
218
setRequiredVersion(2, 0);
219
if (fileName.isFile())
221
setCompressionMethod(cm);
222
setCompressionLevel(cl);
225
setCompressionMethod(ZipCommon::CM_STORE);
229
std::string ZipLocalFileHeader::createHeader() const
231
std::string result(_rawHeader, FULLHEADER_SIZE);
232
result.append(_fileName);
233
result.append(_extraField);
238
} } // namespace Poco::Zip