1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpZip\Model\Extra\Fields; |
4
|
|
|
|
5
|
|
|
use PhpZip\Exception\ZipException; |
6
|
|
|
use PhpZip\Model\Extra\ZipExtraField; |
7
|
|
|
use PhpZip\Model\ZipEntry; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A common base class for Unicode extra information extra fields. |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractUnicodeExtraField implements ZipExtraField |
13
|
|
|
{ |
14
|
|
|
const DEFAULT_VERSION = 0x01; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
private $crc32; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $unicodeValue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int $crc32 |
24
|
|
|
* @param string $unicodeValue |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct($crc32, $unicodeValue) |
27
|
|
|
{ |
28
|
1 |
|
$this->crc32 = (int) $crc32; |
29
|
1 |
|
$this->unicodeValue = (string) $unicodeValue; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $unicodeValue |
34
|
|
|
* |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
1 |
|
public static function create($unicodeValue) |
38
|
|
|
{ |
39
|
1 |
|
return new static(crc32($unicodeValue), $unicodeValue); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int the CRC32 checksum of the filename or comment as |
44
|
|
|
* encoded in the central directory of the zip file |
45
|
|
|
*/ |
46
|
|
|
public function getCrc32() |
47
|
|
|
{ |
48
|
|
|
return $this->crc32; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getUnicodeValue() |
55
|
|
|
{ |
56
|
|
|
return $this->unicodeValue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $unicodeValue the UTF-8 encoded name to set |
61
|
|
|
*/ |
62
|
|
|
public function setUnicodeValue($unicodeValue) |
63
|
|
|
{ |
64
|
|
|
$this->unicodeValue = $unicodeValue; |
65
|
|
|
$this->crc32 = crc32($unicodeValue); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Populate data from this array as if it was in local file data. |
70
|
|
|
* |
71
|
|
|
* @param string $buffer the buffer to read data from |
72
|
|
|
* @param ZipEntry|null $entry |
73
|
|
|
* |
74
|
|
|
* @throws ZipException on error |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public static function unpackLocalFileData($buffer, ZipEntry $entry = null) |
79
|
|
|
{ |
80
|
|
|
if (\strlen($buffer) < 5) { |
81
|
|
|
throw new ZipException('UniCode path extra data must have at least 5 bytes.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$version = unpack('C', $buffer)[1]; |
85
|
|
|
|
86
|
|
|
if ($version !== self::DEFAULT_VERSION) { |
87
|
|
|
throw new ZipException(sprintf('Unsupported version [%d] for UniCode path extra data.', $version)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$crc32 = unpack('V', substr($buffer, 1))[1]; |
91
|
|
|
$unicodeValue = substr($buffer, 5); |
92
|
|
|
|
93
|
|
|
return new static($crc32, $unicodeValue); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Populate data from this array as if it was in central directory data. |
98
|
|
|
* |
99
|
|
|
* @param string $buffer the buffer to read data from |
100
|
|
|
* @param ZipEntry|null $entry |
101
|
|
|
* |
102
|
|
|
* @throws ZipException on error |
103
|
|
|
* |
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
|
|
public static function unpackCentralDirData($buffer, ZipEntry $entry = null) |
107
|
|
|
{ |
108
|
|
|
return self::unpackLocalFileData($buffer, $entry); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The actual data to put into local file data - without Header-ID |
113
|
|
|
* or length specifier. |
114
|
|
|
* |
115
|
|
|
* @return string the data |
116
|
|
|
*/ |
117
|
|
|
public function packLocalFileData() |
118
|
|
|
{ |
119
|
|
|
return pack( |
120
|
|
|
'CV', |
121
|
|
|
self::DEFAULT_VERSION, |
122
|
|
|
$this->crc32 |
123
|
|
|
) . |
124
|
|
|
$this->unicodeValue; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The actual data to put into central directory - without Header-ID or |
129
|
|
|
* length specifier. |
130
|
|
|
* |
131
|
|
|
* @return string the data |
132
|
|
|
*/ |
133
|
|
|
public function packCentralDirData() |
134
|
|
|
{ |
135
|
|
|
return $this->packLocalFileData(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|