| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#include "ZipHeader.h" |
| 27 |
#include <zipios++/backbuffer.h> |
| 28 |
#include <zipios++/zipinputstream.h> |
| 29 |
|
| 30 |
using zipios::ConstEntryPointer; |
| 31 |
using zipios::FileCollection; |
| 32 |
using zipios::ZipHeader; |
| 33 |
|
| 34 |
|
| 35 |
ZipHeader::ZipHeader(std::istream& inp, int s_off, int e_off) |
| 36 |
: _input(inp) |
| 37 |
, _vs(s_off, e_off) |
| 38 |
{ |
| 39 |
init(_input); |
| 40 |
} |
| 41 |
|
| 42 |
/** Create a copy of this instance. */ |
| 43 |
FileCollection* ZipHeader::clone() const |
| 44 |
{ |
| 45 |
return new ZipHeader(*this); |
| 46 |
} |
| 47 |
|
| 48 |
void ZipHeader::close() |
| 49 |
{ |
| 50 |
_valid = false; |
| 51 |
} |
| 52 |
|
| 53 |
std::istream* ZipHeader::getInputStream(const ConstEntryPointer& entry) |
| 54 |
{ |
| 55 |
if (!_valid) { |
| 56 |
throw zipios::InvalidStateException("Attempt to use an invalid FileCollection"); |
| 57 |
} |
| 58 |
return getInputStream(entry->getName()); |
| 59 |
} |
| 60 |
|
| 61 |
std::istream* ZipHeader::getInputStream(const std::string& entry_name, MatchPath matchpath) |
| 62 |
{ |
| 63 |
if (!_valid) { |
| 64 |
throw zipios::InvalidStateException("Attempt to use an invalid ZipHeader"); |
| 65 |
} |
| 66 |
|
| 67 |
zipios::ConstEntryPointer ent = getEntry(entry_name, matchpath); |
| 68 |
|
| 69 |
if (!ent) { |
| 70 |
return nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
return new zipios::ZipInputStream( |
| 74 |
_input, |
| 75 |
static_cast<const zipios::ZipCDirEntry*>(ent.get())->getLocalHeaderOffset() + _vs.startOffset() |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
bool ZipHeader::init(std::istream& _zipfile) |
| 80 |
{ |
| 81 |
// Check stream error state |
| 82 |
if (!_zipfile) { |
| 83 |
setError("Error reading from file"); |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
_valid = readCentralDirectory(_zipfile); |
| 88 |
return _valid; |
| 89 |
} |
| 90 |
|
| 91 |
bool ZipHeader::readCentralDirectory(std::istream& _zipfile) |
| 92 |
{ |
| 93 |
// Find and read eocd. |
| 94 |
if (!readEndOfCentralDirectory(_zipfile)) { |
| 95 |
throw zipios::FCollException("Unable to find zip structure: End-of-central-directory"); |
| 96 |
} |
| 97 |
|
| 98 |
// Position read pointer to start of first entry in central dir. |
| 99 |
_vs.vseekg(_zipfile, _eocd.offset(), std::ios::beg); |
| 100 |
|
| 101 |
int entry_num = 0; |
| 102 |
// Giving the default argument in the next line to keep Visual C++ quiet |
| 103 |
_entries.resize(_eocd.totalCount(), nullptr); |
| 104 |
while ((entry_num < _eocd.totalCount())) { |
| 105 |
zipios::ZipCDirEntry* ent = new zipios::ZipCDirEntry; |
| 106 |
_entries[entry_num] = ent; |
| 107 |
_zipfile >> *ent; |
| 108 |
if (!_zipfile) { |
| 109 |
if (_zipfile.bad()) { |
| 110 |
throw zipios::IOException( |
| 111 |
"Error reading zip file while reading zip file central directory" |
| 112 |
); |
| 113 |
} |
| 114 |
if (_zipfile.fail()) { |
| 115 |
throw zipios::FCollException( |
| 116 |
"Zip file consistency problem. Failure while reading " |
| 117 |
"zip file central directory" |
| 118 |
); |
| 119 |
} |
| 120 |
if (_zipfile.eof()) { |
| 121 |
throw zipios::IOException( |
| 122 |
"Premature end of file while reading zip file central directory" |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
++entry_num; |
| 127 |
} |
| 128 |
|
| 129 |
// Consistency check. eocd should start here |
| 130 |
|
| 131 |
int pos = _vs.vtellg(_zipfile); |
| 132 |
_vs.vseekg(_zipfile, 0, std::ios::end); |
| 133 |
int remaining = static_cast<int>(_vs.vtellg(_zipfile)) - pos; |
| 134 |
if (remaining != _eocd.eocdOffSetFromEnd()) { |
| 135 |
throw zipios::FCollException( |
| 136 |
"Zip file consistency problem. Zip file data fields are " |
| 137 |
"inconsistent with zip file layout" |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
// Consistency check 2, are local headers consistent with |
| 142 |
// cd headers |
| 143 |
if (!confirmLocalHeaders(_zipfile)) { |
| 144 |
throw zipios::FCollException( |
| 145 |
"Zip file consistency problem. Zip file data fields are " |
| 146 |
"inconsistent with zip file layout" |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
bool ZipHeader::readEndOfCentralDirectory(std::istream& _zipfile) |
| 154 |
{ |
| 155 |
zipios::BackBuffer bb(_zipfile, _vs); |
| 156 |
int read_p = -1; |
| 157 |
bool found = false; |
| 158 |
while (!found) { |
| 159 |
if (read_p < 0) { |
| 160 |
if (!bb.readChunk(read_p)) { |
| 161 |
found = false; |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
if (_eocd.read(bb, read_p)) { |
| 166 |
found = true; |
| 167 |
break; |
| 168 |
} |
| 169 |
--read_p; |
| 170 |
} |
| 171 |
|
| 172 |
return found; |
| 173 |
} |
| 174 |
|
| 175 |
bool ZipHeader::confirmLocalHeaders(std::istream& _zipfile) |
| 176 |
{ |
| 177 |
zipios::Entries::const_iterator it; |
| 178 |
zipios::ZipCDirEntry* ent {}; |
| 179 |
int inconsistencies = 0; |
| 180 |
zipios::ZipLocalEntry zlh; |
| 181 |
for (it = _entries.begin(); it != _entries.end(); ++it) { |
| 182 |
ent = static_cast<zipios::ZipCDirEntry*>((*it).get()); |
| 183 |
_vs.vseekg(_zipfile, ent->getLocalHeaderOffset(), std::ios::beg); |
| 184 |
_zipfile >> zlh; |
| 185 |
if (!_zipfile || zlh != *ent) { |
| 186 |
inconsistencies++; |
| 187 |
_zipfile.clear(); |
| 188 |
} |
| 189 |
} |
| 190 |
return !inconsistencies; |
| 191 |
} |
| 192 |
|
| 193 |
void ZipHeader::setError(std::string /*error_str*/) |
| 194 |
{ |
| 195 |
_valid = false; |
| 196 |
} |