Skip to content

Commit 07d07d2

Browse files
committed
extract PK signatures into class constants
Three ZIP record signatures were sprinkled through the file as raw byte literals (one as a uint32 in the EOCD scan, three more as "\x50\x4b..." strings on write). Pull them out as SIG_* class constants and convert the EOCD scan to a 4-byte sliding string buffer so reads and writes share a single source of truth.
1 parent ed2fd8b commit 07d07d2

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/Zip.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class Zip extends Archive
1717
{
1818
const LOCAL_FILE_HEADER_CRC_OFFSET = 14;
1919

20+
const SIG_LOCAL_FILE_HEADER = "\x50\x4b\x03\x04";
21+
const SIG_CENTRAL_FILE_HEADER = "\x50\x4b\x01\x02";
22+
const SIG_END_OF_CENTRAL_DIR = "\x50\x4b\x05\x06";
23+
2024
protected $file = '';
2125
protected $fh;
2226
protected $memory = '';
@@ -480,7 +484,7 @@ public function close()
480484
$this->writebytes($ctrldir);
481485

482486
// write end of central directory record
483-
$this->writebytes("\x50\x4b\x05\x06"); // end of central dir signature
487+
$this->writebytes(self::SIG_END_OF_CENTRAL_DIR);
484488
$this->writebytes(pack('v', 0)); // number of this disk
485489
$this->writebytes(pack('v', 0)); // number of the disk with the start of the central directory
486490
$this->writebytes(pack('v',
@@ -553,18 +557,17 @@ protected function readCentralDir()
553557

554558
@fseek($this->fh, $size - $maximum_size);
555559
$pos = ftell($this->fh);
556-
$bytes = 0x00000000;
560+
$bytes = '';
557561

558562
while ($pos < $size) {
559-
$byte = @fread($this->fh, 1);
560-
$bytes = (($bytes << 8) & 0xFFFFFFFF) | ord($byte);
561-
if ($bytes == 0x504b0506) {
563+
$bytes = substr($bytes . (string)@fread($this->fh, 1), -4);
564+
if ($bytes === self::SIG_END_OF_CENTRAL_DIR) {
562565
break;
563566
}
564567
$pos++;
565568
}
566569

567-
if ($bytes != 0x504b0506) {
570+
if ($bytes !== self::SIG_END_OF_CENTRAL_DIR) {
568571
throw new ArchiveCorruptedException(
569572
'End of central directory signature not found - not a valid ZIP file'
570573
);
@@ -926,7 +929,7 @@ protected function makeCentralFileRecord($offset, $ts, $crc, $len, $clen, $name,
926929

927930
list($name, $extra) = $this->encodeFilename($name);
928931

929-
$header = "\x50\x4b\x01\x02"; // central file header signature
932+
$header = self::SIG_CENTRAL_FILE_HEADER;
930933
$header .= pack('v', 14); // version made by - VFAT
931934
$header .= pack('v', 20); // version needed to extract - 2.0
932935
$header .= pack('v', 0); // general purpose flag - no flags set
@@ -973,7 +976,7 @@ protected function makeLocalFileHeader($ts, $crc, $len, $clen, $name, $comp = nu
973976

974977
list($name, $extra) = $this->encodeFilename($name);
975978

976-
$header = "\x50\x4b\x03\x04"; // local file header signature
979+
$header = self::SIG_LOCAL_FILE_HEADER;
977980
$header .= pack('v', 20); // version needed to extract - 2.0
978981
$header .= pack('v', 0); // general purpose flag - no flags set
979982
$header .= pack('v', $comp); // compression method - deflate|none

0 commit comments

Comments
 (0)