1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
|
#include "lc_global.h"
#include "lc_zipfile.h"
#include "lc_file.h"
#include "lc_math.h"
#include <zlib.h>
#include <time.h>
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
lcZipFile::lcZipFile()
{
mModified = false;
mFile = nullptr;
}
lcZipFile::~lcZipFile()
{
delete mFile;
}
bool lcZipFile::OpenRead(const QString& FileName)
{
lcDiskFile* File = new lcDiskFile(FileName);
mFile = File;
if (!File->Open(QIODevice::ReadOnly) || !Open())
{
delete File;
mFile = nullptr;
return false;
}
return true;
}
bool lcZipFile::OpenRead(lcFile* File)
{
mFile = File;
if (!Open())
{
mFile = nullptr;
return false;
}
return true;
}
bool lcZipFile::OpenWrite(const QString& FileName)
{
lcDiskFile* File = new lcDiskFile(FileName);
mFile = File;
mNumEntries = 0;
mCentralDirSize = 0;
mCentralDirOffset = 0;
mBytesBeforeZipFile = 0;
mCentralPos = 0;
if (!File->Open(QIODevice::WriteOnly))
{
delete File;
mFile = nullptr;
return false;
}
return true;
}
quint64 lcZipFile::SearchCentralDir()
{
quint64 SizeFile, MaxBack, BackRead, PosFound;
const int CommentBufferSize = 1024;
quint8 buf[CommentBufferSize + 4];
SizeFile = mFile->GetLength();
MaxBack = lcMin(SizeFile, 0xffffULL);
BackRead = 4;
PosFound = 0;
while (BackRead < MaxBack)
{
quint64 ReadPos, ReadSize;
if (BackRead + CommentBufferSize > MaxBack)
BackRead = MaxBack;
else
BackRead += CommentBufferSize;
ReadPos = SizeFile - BackRead;
ReadSize = ((CommentBufferSize + 4) < (SizeFile - ReadPos)) ? (CommentBufferSize + 4) : (SizeFile - ReadPos);
mFile->Seek((long)ReadPos, SEEK_SET);
if (mFile->ReadBuffer(buf, (long)ReadSize) != ReadSize)
break;
for (int i = (int)ReadSize - 3; (i--) > 0;)
{
if (((*(buf+i)) == 0x50) && ((*(buf+i+1)) == 0x4b) && ((*(buf+i+2)) == 0x05) && ((*(buf+i+3)) == 0x06))
{
PosFound = ReadPos + i;
break;
}
}
if (PosFound != 0)
break;
}
return PosFound;
}
quint64 lcZipFile::SearchCentralDir64()
{
quint64 SizeFile, MaxBack, BackRead, PosFound;
const int CommentBufferSize = 1024;
quint8 buf[CommentBufferSize + 4];
SizeFile = mFile->GetLength();
MaxBack = lcMin(SizeFile, 0xffffULL);
BackRead = 4;
PosFound = 0;
while (BackRead < MaxBack)
{
quint64 ReadPos, ReadSize;
if (BackRead + CommentBufferSize > MaxBack)
BackRead = MaxBack;
else
BackRead += CommentBufferSize;
ReadPos = SizeFile - BackRead;
ReadSize = ((CommentBufferSize + 4) < (SizeFile - ReadPos)) ? (CommentBufferSize + 4) : (SizeFile - ReadPos);
mFile->Seek((long)ReadPos, SEEK_SET);
if (mFile->ReadBuffer(buf, (long)ReadSize) != ReadSize)
break;
for (int i=(int)ReadSize - 3; (i--) > 0; )
{
if (((*(buf+i)) == 0x50) && ((*(buf+i+1)) == 0x4b) && ((*(buf+i+2)) == 0x06) && ((*(buf+i+3)) == 0x07))
{
PosFound = ReadPos + i;
break;
}
}
if (PosFound != 0)
break;
}
if (PosFound == 0)
return 0;
mFile->Seek((long)PosFound, SEEK_SET);
quint32 Number;
quint64 RelativeOffset;
// Signature.
if (mFile->ReadU32(&Number, 1) != 1)
return 0;
// Number of the disk with the start of the zip64 end of central directory.
if (mFile->ReadU32(&Number, 1) != 1)
return 0;
if (Number != 0)
return 0;
// Relative offset of the zip64 end of central directory record.
if (mFile->ReadU64(&RelativeOffset, 1) != 1)
return 0;
// Total number of disks.
if (mFile->ReadU32(&Number, 1) != 1)
return 0;
if (Number != 0)
return 0;
// Go to end of central directory record.
mFile->Seek((long)RelativeOffset, SEEK_SET);
// The signature.
if (mFile->ReadU32(&Number, 1) != 1)
return 0;
if (Number != 0x06064b50)
return 0;
return RelativeOffset;
}
bool lcZipFile::CheckFileCoherencyHeader(int FileIndex, quint32* SizeVar, quint64* OffsetLocalExtraField, quint32* SizeLocalExtraField)
{
quint16 Number16, Flags;
quint32 Number32, Magic;
quint16 SizeFilename, SizeExtraField;
const lcZipFileInfo& FileInfo = mFiles[FileIndex];
*SizeVar = 0;
*OffsetLocalExtraField = 0;
*SizeLocalExtraField = 0;
mFile->Seek((long)(FileInfo.offset_curfile + mBytesBeforeZipFile), SEEK_SET);
if (mFile->ReadU32(&Magic, 1) != 1 || Magic != 0x04034b50)
return false;
if (mFile->ReadU16(&Number16, 1) != 1)
return false;
if (mFile->ReadU16(&Flags, 1) != 1)
return false;
if (mFile->ReadU16(&Number16, 1) != 1 || Number16 != FileInfo.compression_method)
return false;
if (FileInfo.compression_method != 0 && FileInfo.compression_method != Z_DEFLATED)
return false;
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
if (mFile->ReadU32(&Number32, 1) != 1 || ((Number32 != FileInfo.crc) && ((Flags & 8)==0)))
return false;
if (mFile->ReadU32(&Number32, 1) != 1 || (Number32 != 0xffffffffU && (Number32 != FileInfo.compressed_size) && ((Flags & 8)==0)))
return false;
if (mFile->ReadU32(&Number32, 1) != 1 || (Number32 != 0xffffffffU && (Number32 != FileInfo.uncompressed_size) && ((Flags & 8)==0)))
return false;
if (mFile->ReadU16(&SizeFilename, 1) != 1 || SizeFilename != FileInfo.size_filename)
return false;
*SizeVar += SizeFilename;
if (mFile->ReadU16(&SizeExtraField, 1) != 1)
return false;
*OffsetLocalExtraField= FileInfo.offset_curfile + 0x1e + SizeFilename;
*SizeLocalExtraField = SizeExtraField;
*SizeVar += SizeExtraField;
return true;
}
bool lcZipFile::Open()
{
quint64 NumberEntriesCD, CentralPos;
CentralPos = SearchCentralDir64();
if (CentralPos)
{
quint32 NumberDisk, NumberDiskWithCD;
mZip64 = true;
// Skip signature, size and versions.
mFile->Seek((long)CentralPos + 4 + 8 + 2 + 2, SEEK_SET);
// Number of this disk.
if (mFile->ReadU32(&NumberDisk, 1) != 1)
return false;
// Number of the disk with the start of the central directory.
if (mFile->ReadU32(&NumberDiskWithCD, 1) != 1)
return false;
// Total number of entries in the central directory on this disk.
if (mFile->ReadU64(&mNumEntries, 1) != 1)
return false;
// Total number of entries in the central directory.
if (mFile->ReadU64(&NumberEntriesCD, 1) != 1)
return false;
if ((NumberEntriesCD != mNumEntries) || (NumberDiskWithCD != 0) || (NumberDisk != 0))
return false;
// Size of the central directory.
if (mFile->ReadU64(&mCentralDirSize, 1) != 1)
return false;
// Offset of start of central directory with respect to the starting disk number.
if (mFile->ReadU64(&mCentralDirOffset, 1) != 1)
return false;
// us.gi.size_comment = 0;
}
else
{
quint16 NumberDisk, NumberDiskWithCD;
quint16 Number16;
quint32 Number32;
CentralPos = SearchCentralDir();
if (CentralPos == 0)
return false;
mZip64 = false;
// Skip signature.
mFile->Seek((long)CentralPos + 4, SEEK_SET);
// Number of this disk.
if (mFile->ReadU16(&NumberDisk, 1) != 1)
return false;
// Number of the disk with the start of the central directory.
if (mFile->ReadU16(&NumberDiskWithCD, 1) != 1)
return false;
// Total number of entries in the central dir on this disk.
if (mFile->ReadU16(&Number16, 1) != 1)
return false;
mNumEntries = Number16;
// Total number of entries in the central dir.
if (mFile->ReadU16(&Number16, 1) != 1)
return false;
NumberEntriesCD = Number16;
if ((NumberEntriesCD != mNumEntries) || (NumberDiskWithCD != 0) || (NumberDisk != 0))
return false;
// Size of the central directory.
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
mCentralDirSize = Number32;
// Offset of start of central directory with respect to the starting disk number.
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
mCentralDirOffset= Number32;
// zipfile comment length.
// if (mFile->ReadU16(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=1)
// return false;
}
if (CentralPos < mCentralDirOffset + mCentralDirSize)
return false;
mBytesBeforeZipFile = CentralPos - (mCentralDirOffset + mCentralDirSize);
mCentralPos = CentralPos;
return ReadCentralDir();
}
bool lcZipFile::ReadCentralDir()
{
quint64 PosInCentralDir = mCentralDirOffset;
mFile->Seek((long)(PosInCentralDir + mBytesBeforeZipFile), SEEK_SET);
mFiles.AllocGrow((int)mNumEntries);
for (quint64 FileNum = 0; FileNum < mNumEntries; FileNum++)
{
quint32 Magic, Number32;
lcZipFileInfo& FileInfo = mFiles.Add();
long Seek = 0;
FileInfo.write_buffer = nullptr;
FileInfo.deleted = false;
if (mFile->ReadU32(&Magic, 1) != 1 || Magic != 0x02014b50)
return false;
if (mFile->ReadU16(&FileInfo.version, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.version_needed, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.flag, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.compression_method, 1) != 1)
return false;
if (mFile->ReadU32(&FileInfo.dosDate, 1) != 1)
return false;
quint32 Date = FileInfo.dosDate >> 16;
FileInfo.tmu_date.tm_mday = (quint32)(Date & 0x1f);
FileInfo.tmu_date.tm_mon = (quint32)((((Date) & 0x1E0) / 0x20) - 1);
FileInfo.tmu_date.tm_year = (quint32)(((Date & 0x0FE00) / 0x0200) + 1980);
FileInfo.tmu_date.tm_hour = (quint32)((FileInfo.dosDate & 0xF800) / 0x800);
FileInfo.tmu_date.tm_min = (quint32)((FileInfo.dosDate & 0x7E0) / 0x20);
FileInfo.tmu_date.tm_sec = (quint32)(2*(FileInfo.dosDate & 0x1f));
if (mFile->ReadU32(&FileInfo.crc, 1) != 1)
return false;
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
FileInfo.compressed_size = Number32;
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
FileInfo.uncompressed_size = Number32;
if (mFile->ReadU16(&FileInfo.size_filename, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.size_file_extra, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.size_file_comment, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.disk_num_start, 1) != 1)
return false;
if (mFile->ReadU16(&FileInfo.internal_fa, 1) != 1)
return false;
if (mFile->ReadU32(&FileInfo.external_fa, 1) != 1)
return false;
// relative offset of local header
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
FileInfo.offset_curfile = Number32;
Seek += FileInfo.size_filename;
quint32 SizeRead;
if (FileInfo.size_filename < sizeof(FileInfo.file_name) - 1)
{
*(FileInfo.file_name + FileInfo.size_filename) = '\0';
SizeRead = FileInfo.size_filename;
}
else
{
*(FileInfo.file_name + sizeof(FileInfo.file_name) - 1) = '\0';
SizeRead = sizeof(FileInfo.file_name) - 1;
}
if (FileInfo.size_filename > 0)
if (mFile->ReadBuffer(FileInfo.file_name, SizeRead) != SizeRead)
return false;
Seek -= SizeRead;
/*
// Read extrafield
if ((err==UNZ_OK) && (extraField!=nullptr))
{
ZPOS64_T uSizeRead ;
if (file_info.size_file_extra<extraFieldBufferSize)
uSizeRead = file_info.size_file_extra;
else
uSizeRead = extraFieldBufferSize;
if (lSeek!=0)
{
if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
if (ZREAD64(s->z_filefunc, s->filestream,extraField,(uLong)uSizeRead)!=uSizeRead)
err=UNZ_ERRNO;
lSeek += file_info.size_file_extra - (uLong)uSizeRead;
}
else
lSeek += file_info.size_file_extra;
*/
Seek += FileInfo.size_file_extra;
if (FileInfo.size_file_extra != 0)
{
quint32 acc = 0;
// since lSeek now points to after the extra field we need to move back
Seek -= FileInfo.size_file_extra;
if (Seek != 0)
{
mFile->Seek(Seek, SEEK_CUR);
Seek = 0;
}
while (acc < FileInfo.size_file_extra)
{
quint16 HeaderId, DataSize;
if (mFile->ReadU16(&HeaderId, 1) != 1)
return false;
if (mFile->ReadU16(&DataSize, 1) != 1)
return false;
// ZIP64 extra fields.
if (HeaderId == 0x0001)
{
if (FileInfo.uncompressed_size == (quint64)(unsigned long)-1)
{
if (mFile->ReadU64(&FileInfo.uncompressed_size, 1) != 1)
return false;
}
if (FileInfo.compressed_size == (quint64)(unsigned long)-1)
{
if (mFile->ReadU64(&FileInfo.compressed_size, 1) != 1)
return false;
}
if (FileInfo.offset_curfile == (quint64)-1)
{
// Relative Header offset.
if (mFile->ReadU64(&FileInfo.offset_curfile, 1) != 1)
return false;
}
if (FileInfo.disk_num_start == (quint16)-1)
{
// Disk Start Number.
if (mFile->ReadU32(&Number32, 1) != 1)
return false;
}
}
else
{
mFile->Seek(DataSize, SEEK_CUR);
}
acc += 2 + 2 + DataSize;
}
}
/*
if ((err==UNZ_OK) && (szComment!=nullptr))
{
uLong uSizeRead ;
if (file_info.size_file_comment<commentBufferSize)
{
*(szComment+file_info.size_file_comment)='\0';
uSizeRead = file_info.size_file_comment;
}
else
uSizeRead = commentBufferSize;
if (lSeek!=0)
{
if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
lSeek=0;
else
err=UNZ_ERRNO;
}
if ((file_info.size_file_comment>0) && (commentBufferSize>0))
if (ZREAD64(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
err=UNZ_ERRNO;
lSeek+=file_info.size_file_comment - uSizeRead;
}
else
lSeek+=file_info.size_file_comment;
*/
Seek += FileInfo.size_file_comment;
mFile->Seek(Seek, SEEK_CUR);
}
return true;
}
bool lcZipFile::ExtractFile(const char* FileName, lcMemFile& File, quint32 MaxLength)
{
for (int FileIdx = 0; FileIdx < mFiles.GetSize(); FileIdx++)
{
lcZipFileInfo& FileInfo = mFiles[FileIdx];
if (!qstricmp(FileInfo.file_name, FileName))
return ExtractFile(FileIdx, File, MaxLength);
}
return false;
}
bool lcZipFile::ExtractFile(int FileIndex, lcMemFile& File, quint32 MaxLength)
{
QMutexLocker Lock(&mMutex);
quint32 SizeVar;
quint64 OffsetLocalExtraField;
quint32 SizeLocalExtraField;
const lcZipFileInfo& FileInfo = mFiles[FileIndex];
if (!CheckFileCoherencyHeader(FileIndex, &SizeVar, &OffsetLocalExtraField, &SizeLocalExtraField))
return false;
const int BufferSize = 16384;
char ReadBuffer[BufferSize];
z_stream Stream;
quint32 Crc32;
quint64 PosInZipfile;
quint64 RestReadCompressed;
quint64 RestReadUncompressed;
Crc32 = 0;
Stream.total_out = 0;
if (FileInfo.compression_method == Z_DEFLATED)
{
Stream.zalloc = (alloc_func)0;
Stream.zfree = (free_func)0;
Stream.opaque = (voidpf)0;
Stream.next_in = 0;
Stream.avail_in = 0;
int err = inflateInit2(&Stream, -MAX_WBITS);
if (err != Z_OK)
return false;
}
RestReadCompressed = FileInfo.compressed_size;
RestReadUncompressed = FileInfo.uncompressed_size;
PosInZipfile = FileInfo.offset_curfile + 0x1e + SizeVar;
Stream.avail_in = (uInt)0;
quint32 Length = lcMin((quint32)FileInfo.uncompressed_size, MaxLength);
File.SetLength(Length);
File.Seek(0, SEEK_SET);
Stream.next_out = (Bytef*)File.mBuffer;
Stream.avail_out = Length;
quint32 Read = 0;
while (Stream.avail_out > 0)
{
if ((Stream.avail_in == 0) && (RestReadCompressed > 0))
{
quint32 ReadThis = BufferSize;
if (RestReadCompressed < ReadThis)
ReadThis = (quint32)RestReadCompressed;
if (ReadThis == 0)
return false;
mFile->Seek((long)(PosInZipfile + mBytesBeforeZipFile), SEEK_SET);
if (mFile->ReadBuffer(ReadBuffer, ReadThis) != ReadThis)
return false;
PosInZipfile += ReadThis;
RestReadCompressed -= ReadThis;
Stream.next_in = (Bytef*)ReadBuffer;
Stream.avail_in = (uInt)ReadThis;
}
if (FileInfo.compression_method == 0)
{
quint32 DoCopy, i;
if ((Stream.avail_in == 0) && (RestReadCompressed == 0))
return (Read == 0) ? false : true;
if (Stream.avail_out < Stream.avail_in)
DoCopy = Stream.avail_out;
else
DoCopy = Stream.avail_in;
for (i = 0; i < DoCopy; i++)
*(Stream.next_out+i) = *(Stream.next_in+i);
Crc32 = crc32(Crc32, Stream.next_out, DoCopy);
RestReadUncompressed -= DoCopy;
Stream.avail_in -= DoCopy;
Stream.avail_out -= DoCopy;
Stream.next_out += DoCopy;
Stream.next_in += DoCopy;
Stream.total_out += DoCopy;
Read += DoCopy;
}
else
{
quint64 TotalOutBefore, TotalOutAfter;
const Bytef *bufBefore;
quint64 OutThis;
int flush = Z_SYNC_FLUSH;
TotalOutBefore = Stream.total_out;
bufBefore = Stream.next_out;
int err = inflate(&Stream,flush);
if ((err >= 0) && (Stream.msg != nullptr))
err = Z_DATA_ERROR;
TotalOutAfter = Stream.total_out;
OutThis = TotalOutAfter - TotalOutBefore;
Crc32 = crc32(Crc32, bufBefore, (uInt)(OutThis));
RestReadUncompressed -= OutThis;
Read += (uInt)(TotalOutAfter - TotalOutBefore);
if (err != Z_OK)
{
inflateEnd(&Stream);
if (RestReadUncompressed == 0)
{
if (Crc32 != FileInfo.crc)
return false;
}
if (err == Z_STREAM_END)
return (Read == 0) ? false : true;
return false;
}
}
}
inflateEnd(&Stream);
return true;
}
|