| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2012 Fridrich Strba ([email protected]) |
| 11 | * |
| 12 | * For minor contributions see the git repository. |
| 13 | * |
| 14 | * Alternatively, the contents of this file may be used under the terms |
| 15 | * of the GNU Lesser General Public License Version 2.1 or later |
| 16 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 17 | * applicable instead of those above. |
| 18 | */ |
| 19 | |
| 20 | #include "RVNGZipStream.h" |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <string> |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | #include <utility> |
| 28 | |
| 29 | #include <zlib.h> |
| 30 | #include <librevenge-stream/librevenge-stream.h> |
| 31 | |
| 32 | namespace librevenge |
| 33 | { |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | class StreamException |
| 38 | { |
| 39 | }; |
| 40 | |
| 41 | struct LocalFileHeader |
| 42 | { |
| 43 | unsigned short min_version; |
| 44 | unsigned short general_flag; |
| 45 | unsigned short compression; |
| 46 | unsigned short lastmod_time; |
| 47 | unsigned short lastmod_date; |
| 48 | unsigned crc32; |
| 49 | unsigned compressed_size; |
| 50 | unsigned uncompressed_size; |
| 51 | unsigned short filename_size; |
| 52 | unsigned short extra_field_size; |
| 53 | std::string filename; |
| 54 | std::string extra_field; |
| 55 | LocalFileHeader() |
| 56 | : min_version(0), general_flag(0), compression(0), lastmod_time(0), lastmod_date(0), |
| 57 | crc32(0), compressed_size(0), uncompressed_size(0), filename_size(0), extra_field_size(0), |
| 58 | filename(), extra_field() {} |
| 59 | ~LocalFileHeader() {} |
| 60 | }; |
| 61 | |
| 62 | struct CentralDirectoryEntry |
| 63 | { |
| 64 | unsigned short creator_version; |
| 65 | unsigned short min_version; |
| 66 | unsigned short general_flag; |
| 67 | unsigned short compression; |
| 68 | unsigned short lastmod_time; |
| 69 | unsigned short lastmod_date; |
| 70 | unsigned crc32; |
| 71 | unsigned compressed_size; |
| 72 | unsigned uncompressed_size; |
| 73 | unsigned short filename_size; |
| 74 | unsigned short extra_field_size; |
| 75 | unsigned short file_comment_size; |
| 76 | unsigned short disk_num; |
| 77 | unsigned short internal_attr; |
| 78 | unsigned external_attr; |
| 79 | unsigned offset; |
| 80 | std::string filename; |
| 81 | std::string extra_field; |
| 82 | std::string file_comment; |
| 83 | CentralDirectoryEntry() |
| 84 | : creator_version(0), min_version(0), general_flag(0), compression(0), lastmod_time(0), |
| 85 | lastmod_date(0), crc32(0), compressed_size(0), uncompressed_size(0), filename_size(0), |
| 86 | extra_field_size(0), file_comment_size(0), disk_num(0), internal_attr(0), |
| 87 | external_attr(0), offset(0), filename(), extra_field(), file_comment() {} |
| 88 | ~CentralDirectoryEntry() {} |
| 89 | }; |
| 90 | |
| 91 | struct CentralDirectoryEnd |
| 92 | { |
| 93 | unsigned short disk_num; |
| 94 | unsigned short cdir_disk; |
| 95 | unsigned short disk_entries; |
| 96 | unsigned short cdir_entries; |
| 97 | unsigned cdir_size; |
| 98 | unsigned cdir_offset; |
| 99 | unsigned short comment_size; |
| 100 | std::string comment; |
| 101 | CentralDirectoryEnd() |
| 102 | : disk_num(0), cdir_disk(0), disk_entries(0), cdir_entries(0), |
| 103 | cdir_size(0), cdir_offset(0), comment_size(0), comment() {} |
| 104 | ~CentralDirectoryEnd() {} |
| 105 | }; |
| 106 | |
| 107 | #define CDIR_ENTRY_SIG 0x02014b50 |
| 108 | #define LOC_FILE_HEADER_SIG 0x04034b50 |
| 109 | #define CDIR_END_SIG 0x06054b50 |
| 110 | |
| 111 | static unsigned short getShort(RVNGInputStream *input) |
| 112 | { |
| 113 | unsigned long numBytesRead = 0; |
| 114 | const unsigned char *ret = input->read(2, numBytesRead); |
| 115 | if (numBytesRead != 2) |
| 116 | throw StreamException(); |
| 117 | return (unsigned short)(ret[0]|((unsigned short)ret[1]<<8)); |
| 118 | } |
| 119 | |
| 120 | static unsigned getInt(RVNGInputStream *input) |
| 121 | { |
| 122 | unsigned long numBytesRead = 0; |
| 123 | const unsigned char *ret = input->read(4, numBytesRead); |
| 124 | if (numBytesRead != 4) |
| 125 | throw StreamException(); |
| 126 | return (unsigned)(ret[0]|((unsigned)ret[1]<<8)|((unsigned)ret[2]<<16)|((unsigned)ret[3]<<24)); |
| 127 | } |
| 128 | |
| 129 | static bool readCentralDirectoryEnd(RVNGInputStream *input, CentralDirectoryEnd &end) |
| 130 | { |
| 131 | try |
| 132 | { |
| 133 | unsigned signature = getInt(input); |
| 134 | if (signature != CDIR_END_SIG) |
| 135 | return false; |
| 136 | |
| 137 | end.disk_num = getShort(input); |
| 138 | end.cdir_disk = getShort(input); |
| 139 | end.disk_entries = getShort(input); |
| 140 | end.cdir_entries = getShort(input); |
| 141 | end.cdir_size = getInt(input); |
| 142 | end.cdir_offset = getInt(input); |
| 143 | end.comment_size = getShort(input); |
| 144 | |
| 145 | end.comment.clear(); |
| 146 | if (end.comment_size) |
| 147 | { |
| 148 | // check if the comment field size seems coherent |
| 149 | long pos=input->tell(); |
| 150 | if (input->seek(long(end.comment_size), librevenge::RVNG_SEEK_CUR)!=0) |
| 151 | return false; |
| 152 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 153 | |
| 154 | unsigned long read; |
| 155 | const unsigned char *data=input->read((unsigned long) end.comment_size, read); |
| 156 | if (!data || read!=(unsigned long) end.comment_size) |
| 157 | return false; |
| 158 | end.comment.append((const char *) data, size_t(end.comment_size)); |
| 159 | } |
| 160 | } |
| 161 | catch (...) |
| 162 | { |
| 163 | return false; |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | static bool readCentralDirectoryEntry(RVNGInputStream *input, CentralDirectoryEntry &entry) |
| 169 | { |
| 170 | try |
| 171 | { |
| 172 | unsigned signature = getInt(input); |
| 173 | if (signature != CDIR_ENTRY_SIG) |
| 174 | return false; |
| 175 | |
| 176 | entry.creator_version = getShort(input); |
| 177 | entry.min_version = getShort(input); |
| 178 | entry.general_flag = getShort(input); |
| 179 | entry.compression = getShort(input); |
| 180 | entry.lastmod_time = getShort(input); |
| 181 | entry.lastmod_date = getShort(input); |
| 182 | entry.crc32 = getInt(input); |
| 183 | entry.compressed_size = getInt(input); |
| 184 | entry.uncompressed_size = getInt(input); |
| 185 | entry.filename_size = getShort(input); |
| 186 | entry.extra_field_size = getShort(input); |
| 187 | entry.file_comment_size = getShort(input); |
| 188 | entry.disk_num = getShort(input); |
| 189 | entry.internal_attr = getShort(input); |
| 190 | entry.external_attr = getInt(input); |
| 191 | entry.offset = getInt(input); |
| 192 | |
| 193 | // check if the field sizes seem coherent |
| 194 | long pos=input->tell(); |
| 195 | if (input->seek(long(entry.filename_size+entry.extra_field_size+entry.file_comment_size), |
| 196 | librevenge::RVNG_SEEK_CUR)!=0) |
| 197 | return false; |
| 198 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 199 | |
| 200 | entry.filename.clear(); |
| 201 | if (entry.filename_size) |
| 202 | { |
| 203 | unsigned long read; |
| 204 | const unsigned char *data=input->read((unsigned long) entry.filename_size, read); |
| 205 | if (!data || read!=(unsigned long) entry.filename_size) |
| 206 | return false; |
| 207 | entry.filename.append((const char *) data, size_t(entry.filename_size)); |
| 208 | } |
| 209 | entry.extra_field.clear(); |
| 210 | if (entry.extra_field_size) |
| 211 | { |
| 212 | unsigned long read; |
| 213 | const unsigned char *data=input->read((unsigned long) entry.extra_field_size, read); |
| 214 | if (!data || read!=(unsigned long) entry.extra_field_size) |
| 215 | return false; |
| 216 | entry.extra_field.append((const char *) data, size_t(entry.extra_field_size)); |
| 217 | } |
| 218 | entry.file_comment.clear(); |
| 219 | if (entry.file_comment_size) |
| 220 | { |
| 221 | unsigned long read; |
| 222 | const unsigned char *data=input->read((unsigned long) entry.file_comment_size, read); |
| 223 | if (!data || read!=(unsigned long) entry.file_comment_size) |
| 224 | return false; |
| 225 | entry.file_comment.append((const char *) data, size_t(entry.file_comment_size)); |
| 226 | } |
| 227 | } |
| 228 | catch (...) |
| 229 | { |
| 230 | return false; |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | static bool readLocalFileHeader(RVNGInputStream *input, LocalFileHeader &header) |
| 236 | { |
| 237 | try |
| 238 | { |
| 239 | unsigned signature = getInt(input); |
| 240 | if (signature != LOC_FILE_HEADER_SIG) |
| 241 | return false; |
| 242 | |
| 243 | header.min_version = getShort(input); |
| 244 | header.general_flag = getShort(input); |
| 245 | header.compression = getShort(input); |
| 246 | header.lastmod_time = getShort(input); |
| 247 | header.lastmod_date = getShort(input); |
| 248 | header.crc32 = getInt(input); |
| 249 | header.compressed_size = getInt(input); |
| 250 | header.uncompressed_size = getInt(input); |
| 251 | header.filename_size = getShort(input); |
| 252 | header.extra_field_size = getShort(input); |
| 253 | // check if the field sizes seem coherent |
| 254 | long pos=input->tell(); |
| 255 | if (input->seek(long(header.filename_size+header.extra_field_size), librevenge::RVNG_SEEK_CUR)!=0) |
| 256 | return false; |
| 257 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 258 | header.filename.clear(); |
| 259 | if (header.filename_size) |
| 260 | { |
| 261 | unsigned long read; |
| 262 | const unsigned char *data=input->read((unsigned long) header.filename_size, read); |
| 263 | if (!data || read!=(unsigned long) header.filename_size) |
| 264 | return false; |
| 265 | header.filename.append((const char *) data, size_t(header.filename_size)); |
| 266 | } |
| 267 | header.extra_field.clear(); |
| 268 | if (header.extra_field_size) |
| 269 | { |
| 270 | unsigned long read; |
| 271 | const unsigned char *data=input->read((unsigned long) header.extra_field_size, read); |
| 272 | if (!data || read!=(unsigned long) header.extra_field_size) |
| 273 | return false; |
| 274 | header.extra_field.append((const char *) data, size_t(header.extra_field_size)); |
| 275 | } |
| 276 | } |
| 277 | catch (...) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | static bool areHeadersConsistent(const LocalFileHeader &header, const CentralDirectoryEntry &entry) |
| 285 | { |
| 286 | if (header.min_version != entry.min_version) |
| 287 | return false; |
| 288 | if (header.general_flag != entry.general_flag) |
| 289 | return false; |
| 290 | if (header.compression != entry.compression) |
| 291 | return false; |
| 292 | if (!(header.general_flag & 0x08)) |
| 293 | { |
| 294 | if (header.crc32 != entry.crc32) |
| 295 | return false; |
| 296 | if (header.compressed_size != entry.compressed_size) |
| 297 | return false; |
| 298 | if (header.uncompressed_size != entry.uncompressed_size) |
| 299 | return false; |
| 300 | } |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | static bool findCentralDirectoryEnd(RVNGInputStream *input) |
| 305 | { |
| 306 | try |
| 307 | { |
| 308 | input->seek(0, RVNG_SEEK_END); |
| 309 | long size = input->tell(); |
| 310 | |
| 311 | // CentralDirectoryEnd is CDIR_END_SIG:4+at least 18 other bytes |
| 312 | if (size < 22) return false; |
| 313 | if (input->seek(size>1024 ? size-1024 : 0, RVNG_SEEK_SET)) |
| 314 | return false; |
| 315 | long pos=input->tell(); |
| 316 | long toCheck=(size-18)-pos; |
| 317 | unsigned long numBytesRead = 0; |
| 318 | unsigned char const *ret = |
| 319 | input->read((unsigned long) toCheck, numBytesRead); |
| 320 | if (!ret || long(numBytesRead)!=toCheck) |
| 321 | return false; |
| 322 | unsigned const sigRev= |
| 323 | ((CDIR_END_SIG&0xFF)<<24)| |
| 324 | ((CDIR_END_SIG&0xFF00)<<8)| |
| 325 | ((CDIR_END_SIG&0xFF0000)>>8)| |
| 326 | ((CDIR_END_SIG&0xFF000000)>>24); |
| 327 | unsigned signature=0; |
| 328 | for (long p=0; p < toCheck; p++) |
| 329 | { |
| 330 | signature=((signature&0xFFFFFF)<<8)|*(ret++); |
| 331 | if (signature == sigRev) |
| 332 | { |
| 333 | input->seek(pos+p-3, RVNG_SEEK_SET); |
| 334 | return true; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | catch (...) |
| 340 | { |
| 341 | return false; |
| 342 | } |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | static bool findDataStream(RVNGInputStream *input, CentralDirectoryEntry &entry, const char *name) |
| 347 | { |
| 348 | size_t name_size = strlen(name); |
| 349 | if (!findCentralDirectoryEnd(input)) |
| 350 | return false; |
| 351 | CentralDirectoryEnd end; |
| 352 | if (!readCentralDirectoryEnd(input, end)) |
| 353 | return false; |
| 354 | input->seek(end.cdir_offset, RVNG_SEEK_SET); |
| 355 | while (!input->isEnd() && (unsigned)input->tell() < end.cdir_offset + end.cdir_size) |
| 356 | { |
| 357 | if (!readCentralDirectoryEntry(input, entry)) |
| 358 | return false; |
| 359 | if (name_size == entry.filename_size && entry.filename == name) |
| 360 | break; |
| 361 | } |
| 362 | if (name_size != entry.filename_size) |
| 363 | return false; |
| 364 | if (entry.filename != name) |
| 365 | return false; |
| 366 | input->seek(entry.offset, RVNG_SEEK_SET); |
| 367 | LocalFileHeader header; |
| 368 | if (!readLocalFileHeader(input, header)) |
| 369 | return false; |
| 370 | if (!areHeadersConsistent(header, entry)) |
| 371 | return false; |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | static std::vector<std::string> getSubStreamNamesInZip(RVNGInputStream *input, bool all=false) |
| 376 | { |
| 377 | std::vector<std::string> res; |
| 378 | if (!input || !findCentralDirectoryEnd(input)) |
| 379 | return res; |
| 380 | CentralDirectoryEnd end; |
| 381 | if (!readCentralDirectoryEnd(input, end)) |
| 382 | return res; |
| 383 | input->seek(long(end.cdir_offset), RVNG_SEEK_SET); |
| 384 | while (!input->isEnd() && (unsigned)input->tell() < end.cdir_offset + end.cdir_size) |
| 385 | { |
| 386 | CentralDirectoryEntry entry; |
| 387 | if (!readCentralDirectoryEntry(input, entry)) |
| 388 | break; |
| 389 | if (!entry.filename_size || (!all && entry.filename[entry.filename.size()-1]=='/')) continue; |
| 390 | // A file name with a \0 char in it is likely broken. Anyway, our char*-based |
| 391 | // interface cannot handle such names, so we just ignore them. |
| 392 | if (entry.filename.find('\0') != std::string::npos) |
| 393 | continue; |
| 394 | res.push_back(entry.filename); |
| 395 | } |
| 396 | return res; |
| 397 | } |
| 398 | } // anonymous namespace |
| 399 | |
| 400 | bool RVNGZipStream::isZipFile(RVNGInputStream *input) |
| 401 | { |
| 402 | // look for central directory end |
| 403 | if (!findCentralDirectoryEnd(input)) |
| 404 | return false; |
| 405 | CentralDirectoryEnd end; |
| 406 | if (!readCentralDirectoryEnd(input, end)) |
| 407 | return false; |
| 408 | input->seek(end.cdir_offset, RVNG_SEEK_SET); |
| 409 | // read first entry in the central directory |
| 410 | CentralDirectoryEntry entry; |
| 411 | if (!readCentralDirectoryEntry(input, entry)) |
| 412 | return false; |
| 413 | input->seek(entry.offset, RVNG_SEEK_SET); |
| 414 | // read the local file header and compare with the central directory information |
| 415 | LocalFileHeader header; |
| 416 | if (!readLocalFileHeader(input, header)) |
| 417 | return false; |
| 418 | if (!areHeadersConsistent(header, entry)) |
| 419 | return false; |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | std::vector<std::string> RVNGZipStream::getSubStreamNamesList(RVNGInputStream *input) |
| 424 | { |
| 425 | return getSubStreamNamesInZip(input,false); |
| 426 | } |
| 427 | |
| 428 | RVNGInputStream *RVNGZipStream::getSubstream(RVNGInputStream *input, const char *name) |
| 429 | { |
| 430 | CentralDirectoryEntry entry; |
| 431 | if (!findDataStream(input, entry, name)) |
| 432 | return nullptr; |
| 433 | if (!entry.compressed_size) |
| 434 | return nullptr; |
| 435 | unsigned long numBytesRead = 0; |
| 436 | auto *compressedData = const_cast<unsigned char *>(input->read(entry.compressed_size, numBytesRead)); |
| 437 | if (numBytesRead != entry.compressed_size) |
| 438 | return nullptr; |
| 439 | if (!entry.compression) |
| 440 | return new RVNGStringStream(compressedData, (unsigned)numBytesRead); |
| 441 | else |
| 442 | { |
| 443 | int ret; |
| 444 | z_stream strm; |
| 445 | |
| 446 | /* allocate inflate state */ |
| 447 | strm.zalloc = Z_NULL; |
| 448 | strm.zfree = Z_NULL; |
| 449 | strm.opaque = Z_NULL; |
| 450 | strm.avail_in = 0; |
| 451 | strm.next_in = Z_NULL; |
| 452 | ret = inflateInit2(&strm,-MAX_WBITS); |
| 453 | if (ret != Z_OK) |
| 454 | return nullptr; |
| 455 | |
| 456 | const unsigned long blockSize = (std::max)(4096ul, 2 * numBytesRead); |
| 457 | |
| 458 | std::vector<unsigned char> data(blockSize); |
| 459 | |
| 460 | strm.avail_in = (unsigned)numBytesRead; |
| 461 | strm.next_in = (Bytef *)compressedData; |
| 462 | strm.next_out = data.data(); |
| 463 | |
| 464 | bool done = false; |
| 465 | while (!done) |
| 466 | { |
| 467 | const std::ptrdiff_t nextOutIndex = strm.next_out - data.data(); |
| 468 | assert(nextOutIndex >= 0); |
| 469 | data.resize(data.size() + blockSize); |
| 470 | assert(data.size() > std::size_t(nextOutIndex)); |
| 471 | strm.avail_out = unsigned(data.size() - std::size_t(nextOutIndex)); |
| 472 | strm.next_out = reinterpret_cast<Bytef *>(&data[nextOutIndex]); |
| 473 | |
| 474 | ret = inflate(&strm, Z_SYNC_FLUSH); |
| 475 | |
| 476 | switch (ret) |
| 477 | { |
| 478 | case Z_OK: |
| 479 | break; |
| 480 | // TODO: return partial result on Z_BUF_ERROR/Z_DATA_ERROR? |
| 481 | default: |
| 482 | strm.total_out = 0; // abandon partial result |
| 483 | /* FALLTHRU */ |
| 484 | case Z_STREAM_END: |
| 485 | done = true; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | (void)inflateEnd(&strm); |
| 491 | |
| 492 | if (strm.total_out == 0) |
| 493 | return nullptr; |
| 494 | return new RVNGStringStream(data.data(), (unsigned int) strm.total_out); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | } |
| 499 | |
| 500 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |