| 1 | // |
| 2 | // ZipStream.cpp |
| 3 | // |
| 4 | // Library: Zip |
| 5 | // Package: Zip |
| 6 | // Module: ZipStream |
| 7 | // |
| 8 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Zip/ZipStream.h" |
| 16 | #include "Poco/Zip/ZipArchive.h" |
| 17 | #include "Poco/Zip/AutoDetectStream.h" |
| 18 | #include "Poco/Zip/PartialStream.h" |
| 19 | #include "Poco/Zip/ZipDataInfo.h" |
| 20 | #include "Poco/Zip/ZipException.h" |
| 21 | #include "Poco/Exception.h" |
| 22 | #include "Poco/InflatingStream.h" |
| 23 | #include "Poco/DeflatingStream.h" |
| 24 | #if defined(POCO_UNBUNDLED) |
| 25 | #include <zlib.h> |
| 26 | #else |
| 27 | #include "Poco/zlib.h" |
| 28 | #endif |
| 29 | |
| 30 | |
| 31 | namespace Poco { |
| 32 | namespace Zip { |
| 33 | |
| 34 | |
| 35 | ZipStreamBuf::(std::istream& istr, const ZipLocalFileHeader& fileEntry, bool reposition): |
| 36 | Poco::BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in), |
| 37 | _pIstr(&istr), |
| 38 | _pOstr(0), |
| 39 | _ptrBuf(), |
| 40 | _ptrOBuf(), |
| 41 | _ptrHelper(), |
| 42 | _ptrOHelper(), |
| 43 | _crc32(Poco::Checksum::TYPE_CRC32), |
| 44 | _expectedCrc32(0), |
| 45 | _checkCRC(true), |
| 46 | _bytesWritten(0), |
| 47 | _pHeader(0) |
| 48 | { |
| 49 | if (fileEntry.isDirectory()) |
| 50 | return; |
| 51 | _expectedCrc32 = fileEntry.getCRC(); |
| 52 | std::streamoff start = fileEntry.getDataStartPos(); |
| 53 | std::streamoff end = fileEntry.getDataEndPos(); |
| 54 | _checkCRC = !fileEntry.searchCRCAndSizesAfterData(); |
| 55 | if (fileEntry.getCompressionMethod() == ZipCommon::CM_DEFLATE) |
| 56 | { |
| 57 | // Fake init bytes at beginning of stream |
| 58 | std::string init = ZipUtil::fakeZLibInitString(fileEntry.getCompressionLevel()); |
| 59 | |
| 60 | // Fake adler at end of stream: just some dummy value, not checked anway |
| 61 | std::string crc(4, ' '); |
| 62 | if (fileEntry.searchCRCAndSizesAfterData()) |
| 63 | { |
| 64 | _ptrHelper = new AutoDetectInputStream(istr, init, crc, reposition, static_cast<Poco::UInt32>(start), fileEntry.needsZip64()); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | _ptrHelper = new PartialInputStream(istr, start, end, reposition, init, crc); |
| 69 | } |
| 70 | _ptrBuf = new Poco::InflatingInputStream(*_ptrHelper, Poco::InflatingStreamBuf::STREAM_ZIP); |
| 71 | } |
| 72 | else if (fileEntry.getCompressionMethod() == ZipCommon::CM_STORE) |
| 73 | { |
| 74 | if (fileEntry.searchCRCAndSizesAfterData()) |
| 75 | { |
| 76 | _ptrBuf = new AutoDetectInputStream(istr, "" , "" , reposition, static_cast<Poco::UInt32>(start), fileEntry.needsZip64()); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | _ptrBuf = new PartialInputStream(istr, start, end, reposition); |
| 81 | } |
| 82 | } |
| 83 | else throw Poco::NotImplementedException("Unsupported compression method" ); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | ZipStreamBuf::(std::ostream& ostr, ZipLocalFileHeader& fileEntry, bool reposition): |
| 88 | Poco::BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out), |
| 89 | _pIstr(0), |
| 90 | _pOstr(&ostr), |
| 91 | _ptrBuf(), |
| 92 | _ptrOBuf(), |
| 93 | _ptrHelper(), |
| 94 | _ptrOHelper(), |
| 95 | _crc32(Poco::Checksum::TYPE_CRC32), |
| 96 | _expectedCrc32(0), |
| 97 | _checkCRC(false), |
| 98 | _bytesWritten(0), |
| 99 | _pHeader(&fileEntry) |
| 100 | { |
| 101 | if (fileEntry.isEncrypted()) |
| 102 | throw Poco::NotImplementedException("Encryption not supported" ); |
| 103 | |
| 104 | if (fileEntry.isDirectory()) |
| 105 | { |
| 106 | // only header, no payload, zero crc |
| 107 | fileEntry.setSearchCRCAndSizesAfterData(false); |
| 108 | fileEntry.setCompressedSize(0); |
| 109 | fileEntry.setUncompressedSize(0); |
| 110 | fileEntry.setCRC(0); |
| 111 | std::string = fileEntry.createHeader(); |
| 112 | ostr.write(header.c_str(), static_cast<std::streamsize>(header.size())); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | fileEntry.setSearchCRCAndSizesAfterData(!reposition); |
| 117 | if (fileEntry.getCompressionMethod() == ZipCommon::CM_DEFLATE) |
| 118 | { |
| 119 | int level = Z_DEFAULT_COMPRESSION; |
| 120 | if (fileEntry.getCompressionLevel() == ZipCommon::CL_FAST || fileEntry.getCompressionLevel() == ZipCommon::CL_SUPERFAST) |
| 121 | level = Z_BEST_SPEED; |
| 122 | else if (fileEntry.getCompressionLevel() == ZipCommon::CL_MAXIMUM) |
| 123 | level = Z_BEST_COMPRESSION; |
| 124 | // ignore the zlib init string which is of size 2 and also ignore the 4 byte adler32 value at the end of the stream! |
| 125 | _ptrOHelper = new PartialOutputStream(*_pOstr, 2, 4, false); |
| 126 | _ptrOBuf = new Poco::DeflatingOutputStream(*_ptrOHelper, DeflatingStreamBuf::STREAM_ZLIB, level); |
| 127 | } |
| 128 | else if (fileEntry.getCompressionMethod() == ZipCommon::CM_STORE) |
| 129 | { |
| 130 | _ptrOHelper = new PartialOutputStream(*_pOstr, 0, 0, false); |
| 131 | _ptrOBuf = new PartialOutputStream(*_ptrOHelper, 0, 0, false); |
| 132 | } |
| 133 | else throw Poco::NotImplementedException("Unsupported compression method" ); |
| 134 | |
| 135 | // now write the header to the ostr! |
| 136 | if (fileEntry.needsZip64()) |
| 137 | fileEntry.setZip64Data(); |
| 138 | std::string = fileEntry.createHeader(); |
| 139 | ostr.write(header.c_str(), static_cast<std::streamsize>(header.size())); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | ZipStreamBuf::~ZipStreamBuf() |
| 145 | { |
| 146 | // make sure destruction of streams happens in correct order |
| 147 | _ptrOBuf = 0; |
| 148 | _ptrOHelper = 0; |
| 149 | _ptrBuf = 0; |
| 150 | _ptrHelper = 0; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | int ZipStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
| 155 | { |
| 156 | if (!_ptrBuf) return 0; // directory entry |
| 157 | _ptrBuf->read(buffer, length); |
| 158 | int cnt = static_cast<int>(_ptrBuf->gcount()); |
| 159 | if (cnt > 0) |
| 160 | { |
| 161 | _crc32.update(buffer, cnt); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | if (_crc32.checksum() != _expectedCrc32) |
| 166 | { |
| 167 | if (_checkCRC) |
| 168 | throw ZipException("CRC failure" ); |
| 169 | else |
| 170 | { |
| 171 | // the CRC value is written directly after the data block |
| 172 | // parse it directly from the input stream |
| 173 | ZipDataInfo nfo(*_pIstr, false); |
| 174 | // now push back the header to the stream, so that the ZipLocalFileHeader can read it |
| 175 | Poco::Int32 size = static_cast<Poco::Int32>(nfo.getFullHeaderSize()); |
| 176 | _expectedCrc32 = nfo.getCRC32(); |
| 177 | _pIstr->seekg(-size, std::ios::cur); |
| 178 | if (!_pIstr->good()) throw Poco::IOException("Failed to seek on input stream" ); |
| 179 | if (!crcValid()) |
| 180 | throw ZipException("CRC failure" ); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | return cnt; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | int ZipStreamBuf::writeToDevice(const char* buffer, std::streamsize length) |
| 189 | { |
| 190 | if (!_ptrOBuf) return 0; // directory entry |
| 191 | if (length == 0) |
| 192 | return 0; |
| 193 | _bytesWritten += length; |
| 194 | _ptrOBuf->write(buffer, length); |
| 195 | _crc32.update(buffer, static_cast<unsigned int>(length)); |
| 196 | return static_cast<int>(length); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | void ZipStreamBuf::close(Poco::UInt64& ) |
| 201 | { |
| 202 | extraDataSize = 0; |
| 203 | if (_ptrOBuf && _pHeader) |
| 204 | { |
| 205 | _ptrOBuf->flush(); |
| 206 | DeflatingOutputStream* pDO = dynamic_cast<DeflatingOutputStream*>(_ptrOBuf.get()); |
| 207 | if (pDO) |
| 208 | pDO->close(); |
| 209 | if (_ptrOHelper) |
| 210 | { |
| 211 | _ptrOHelper->flush(); |
| 212 | _ptrOHelper->close(); |
| 213 | } |
| 214 | _ptrOBuf = 0; |
| 215 | if (!*_pOstr) throw Poco::IOException("Bad output stream" ); |
| 216 | |
| 217 | // write an extra datablock if required |
| 218 | // or fix the crc entries |
| 219 | poco_check_ptr(_pHeader); |
| 220 | _pHeader->setCRC(_crc32.checksum()); |
| 221 | _pHeader->setUncompressedSize(_bytesWritten); |
| 222 | _pHeader->setCompressedSize(_ptrOHelper->bytesWritten()); |
| 223 | if (_bytesWritten == 0) |
| 224 | { |
| 225 | poco_assert (_ptrOHelper->bytesWritten() == 0); |
| 226 | // Empty files must use CM_STORE, otherwise unzipping will fail |
| 227 | _pHeader->setCompressionMethod(ZipCommon::CM_STORE); |
| 228 | _pHeader->setCompressionLevel(ZipCommon::CL_NORMAL); |
| 229 | } |
| 230 | _pHeader->setStartPos(_pHeader->getStartPos()); // This resets EndPos now that compressed Size is known |
| 231 | |
| 232 | if (_pHeader->searchCRCAndSizesAfterData()) |
| 233 | { |
| 234 | if (_pHeader->needsZip64()) |
| 235 | { |
| 236 | ZipDataInfo64 info; |
| 237 | info.setCRC32(_crc32.checksum()); |
| 238 | info.setUncompressedSize(_bytesWritten); |
| 239 | info.setCompressedSize(_ptrOHelper->bytesWritten()); |
| 240 | extraDataSize = info.getFullHeaderSize(); |
| 241 | _pOstr->write(info.getRawHeader(), static_cast<std::streamsize>(extraDataSize)); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | ZipDataInfo info; |
| 246 | info.setCRC32(_crc32.checksum()); |
| 247 | info.setUncompressedSize(static_cast<Poco::UInt32>(_bytesWritten)); |
| 248 | info.setCompressedSize(static_cast<Poco::UInt32>(_ptrOHelper->bytesWritten())); |
| 249 | extraDataSize = info.getFullHeaderSize(); |
| 250 | _pOstr->write(info.getRawHeader(), static_cast<std::streamsize>(extraDataSize)); |
| 251 | } |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | _pOstr->seekp(_pHeader->getStartPos(), std::ios_base::beg); |
| 256 | if (!*_pOstr) throw Poco::IOException("Bad output stream" ); |
| 257 | |
| 258 | if (_pHeader->hasExtraField()) // Update sizes in header extension. |
| 259 | _pHeader->setZip64Data(); |
| 260 | std::string = _pHeader->createHeader(); |
| 261 | _pOstr->write(header.c_str(), static_cast<std::streamsize>(header.size())); |
| 262 | _pOstr->seekp(0, std::ios_base::end); |
| 263 | if (!*_pOstr) throw Poco::IOException("Bad output stream" ); |
| 264 | } |
| 265 | _pHeader = 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | bool ZipStreamBuf::crcValid() const |
| 271 | { |
| 272 | if (!_ptrBuf) return true; // directory entry |
| 273 | return _crc32.checksum() == _expectedCrc32; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | ZipIOS::(std::istream& istr, const ZipLocalFileHeader& fileEntry, bool reposition): _buf(istr, fileEntry, reposition) |
| 278 | { |
| 279 | poco_ios_init(&_buf); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | ZipIOS::(std::ostream& ostr, ZipLocalFileHeader& fileEntry, bool reposition): _buf(ostr, fileEntry, reposition) |
| 284 | { |
| 285 | poco_ios_init(&_buf); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | ZipIOS::~ZipIOS() |
| 290 | { |
| 291 | } |
| 292 | |
| 293 | |
| 294 | ZipStreamBuf* ZipIOS::rdbuf() |
| 295 | { |
| 296 | return &_buf; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | ZipInputStream::(std::istream& istr, const ZipLocalFileHeader& fileEntry, bool reposition): ZipIOS(istr, fileEntry, reposition), std::istream(&_buf) |
| 301 | { |
| 302 | } |
| 303 | |
| 304 | |
| 305 | ZipInputStream::~ZipInputStream() |
| 306 | { |
| 307 | } |
| 308 | |
| 309 | |
| 310 | bool ZipInputStream::crcValid() const |
| 311 | { |
| 312 | return _buf.crcValid(); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | ZipOutputStream::(std::ostream& ostr, ZipLocalFileHeader& fileEntry, bool seekableOutput): ZipIOS(ostr, fileEntry, seekableOutput), std::ostream(&_buf) |
| 317 | { |
| 318 | } |
| 319 | |
| 320 | |
| 321 | ZipOutputStream::~ZipOutputStream() |
| 322 | { |
| 323 | } |
| 324 | |
| 325 | |
| 326 | void ZipOutputStream::close(Poco::UInt64& ) |
| 327 | { |
| 328 | flush(); |
| 329 | _buf.close(extraDataSize); |
| 330 | } |
| 331 | |
| 332 | |
| 333 | } } // namespace Poco::Zip |
| 334 | |