Zip64ExtraField.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Model\Extra\Fields;
  10. use PhpZip\Constants\ZipConstants;
  11. use PhpZip\Exception\RuntimeException;
  12. use PhpZip\Exception\ZipException;
  13. use PhpZip\Model\Extra\ZipExtraField;
  14. use PhpZip\Model\ZipEntry;
  15. /**
  16. * ZIP64 Extra Field.
  17. *
  18. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  19. */
  20. class Zip64ExtraField implements ZipExtraField
  21. {
  22. /** @var int The Header ID for a ZIP64 Extended Information Extra Field. */
  23. public const HEADER_ID = 0x0001;
  24. private ?int $uncompressedSize;
  25. private ?int $compressedSize;
  26. private ?int $localHeaderOffset;
  27. private ?int $diskStart;
  28. public function __construct(
  29. ?int $uncompressedSize = null,
  30. ?int $compressedSize = null,
  31. ?int $localHeaderOffset = null,
  32. ?int $diskStart = null
  33. ) {
  34. $this->uncompressedSize = $uncompressedSize;
  35. $this->compressedSize = $compressedSize;
  36. $this->localHeaderOffset = $localHeaderOffset;
  37. $this->diskStart = $diskStart;
  38. }
  39. /**
  40. * Returns the Header ID (type) of this Extra Field.
  41. * The Header ID is an unsigned short integer (two bytes)
  42. * which must be constant during the life cycle of this object.
  43. */
  44. public function getHeaderId(): int
  45. {
  46. return self::HEADER_ID;
  47. }
  48. /**
  49. * Populate data from this array as if it was in local file data.
  50. *
  51. * @param string $buffer the buffer to read data from
  52. * @param ?ZipEntry $entry
  53. *
  54. * @throws ZipException on error
  55. *
  56. * @return Zip64ExtraField
  57. */
  58. public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self
  59. {
  60. $length = \strlen($buffer);
  61. if ($length === 0) {
  62. // no local file data at all, may happen if an archive
  63. // only holds a ZIP64 extended information extra field
  64. // inside the central directory but not inside the local
  65. // file header
  66. return new self();
  67. }
  68. if ($length < 16) {
  69. throw new ZipException(
  70. 'Zip64 extended information must contain both size values in the local file header.'
  71. );
  72. }
  73. [
  74. 'uncompressedSize' => $uncompressedSize,
  75. 'compressedSize' => $compressedSize,
  76. ] = unpack('PuncompressedSize/PcompressedSize', substr($buffer, 0, 16));
  77. return new self($uncompressedSize, $compressedSize);
  78. }
  79. /**
  80. * Populate data from this array as if it was in central directory data.
  81. *
  82. * @param string $buffer the buffer to read data from
  83. * @param ?ZipEntry $entry
  84. *
  85. * @throws ZipException
  86. *
  87. * @return Zip64ExtraField
  88. */
  89. public static function unpackCentralDirData(string $buffer, ?ZipEntry $entry = null): self
  90. {
  91. if ($entry === null) {
  92. throw new RuntimeException('zipEntry is null');
  93. }
  94. $length = \strlen($buffer);
  95. $remaining = $length;
  96. $uncompressedSize = null;
  97. $compressedSize = null;
  98. $localHeaderOffset = null;
  99. $diskStart = null;
  100. if ($entry->getUncompressedSize() === ZipConstants::ZIP64_MAGIC) {
  101. if ($remaining < 8) {
  102. throw new ZipException('ZIP64 extension corrupt (no uncompressed size).');
  103. }
  104. $uncompressedSize = unpack('P', substr($buffer, $length - $remaining, 8))[1];
  105. $remaining -= 8;
  106. }
  107. if ($entry->getCompressedSize() === ZipConstants::ZIP64_MAGIC) {
  108. if ($remaining < 8) {
  109. throw new ZipException('ZIP64 extension corrupt (no compressed size).');
  110. }
  111. $compressedSize = unpack('P', substr($buffer, $length - $remaining, 8))[1];
  112. $remaining -= 8;
  113. }
  114. if ($entry->getLocalHeaderOffset() === ZipConstants::ZIP64_MAGIC) {
  115. if ($remaining < 8) {
  116. throw new ZipException('ZIP64 extension corrupt (no relative local header offset).');
  117. }
  118. $localHeaderOffset = unpack('P', substr($buffer, $length - $remaining, 8))[1];
  119. $remaining -= 8;
  120. }
  121. if ($remaining === 4) {
  122. $diskStart = unpack('V', substr($buffer, $length - $remaining, 4))[1];
  123. }
  124. return new self($uncompressedSize, $compressedSize, $localHeaderOffset, $diskStart);
  125. }
  126. /**
  127. * The actual data to put into local file data - without Header-ID
  128. * or length specifier.
  129. *
  130. * @return string the data
  131. */
  132. public function packLocalFileData(): string
  133. {
  134. if ($this->uncompressedSize !== null || $this->compressedSize !== null) {
  135. if ($this->uncompressedSize === null || $this->compressedSize === null) {
  136. throw new \InvalidArgumentException(
  137. 'Zip64 extended information must contain both size values in the local file header.'
  138. );
  139. }
  140. return $this->packSizes();
  141. }
  142. return '';
  143. }
  144. private function packSizes(): string
  145. {
  146. $data = '';
  147. if ($this->uncompressedSize !== null) {
  148. $data .= pack('P', $this->uncompressedSize);
  149. }
  150. if ($this->compressedSize !== null) {
  151. $data .= pack('P', $this->compressedSize);
  152. }
  153. return $data;
  154. }
  155. /**
  156. * The actual data to put into central directory - without Header-ID or
  157. * length specifier.
  158. *
  159. * @return string the data
  160. */
  161. public function packCentralDirData(): string
  162. {
  163. $data = $this->packSizes();
  164. if ($this->localHeaderOffset !== null) {
  165. $data .= pack('P', $this->localHeaderOffset);
  166. }
  167. if ($this->diskStart !== null) {
  168. $data .= pack('V', $this->diskStart);
  169. }
  170. return $data;
  171. }
  172. public function getUncompressedSize(): ?int
  173. {
  174. return $this->uncompressedSize;
  175. }
  176. public function setUncompressedSize(?int $uncompressedSize): void
  177. {
  178. $this->uncompressedSize = $uncompressedSize;
  179. }
  180. public function getCompressedSize(): ?int
  181. {
  182. return $this->compressedSize;
  183. }
  184. public function setCompressedSize(?int $compressedSize): void
  185. {
  186. $this->compressedSize = $compressedSize;
  187. }
  188. public function getLocalHeaderOffset(): ?int
  189. {
  190. return $this->localHeaderOffset;
  191. }
  192. public function setLocalHeaderOffset(?int $localHeaderOffset): void
  193. {
  194. $this->localHeaderOffset = $localHeaderOffset;
  195. }
  196. public function getDiskStart(): ?int
  197. {
  198. return $this->diskStart;
  199. }
  200. public function setDiskStart(?int $diskStart): void
  201. {
  202. $this->diskStart = $diskStart;
  203. }
  204. public function __toString(): string
  205. {
  206. $args = [self::HEADER_ID];
  207. $format = '0x%04x ZIP64: ';
  208. $formats = [];
  209. if ($this->uncompressedSize !== null) {
  210. $formats[] = 'SIZE=%d';
  211. $args[] = $this->uncompressedSize;
  212. }
  213. if ($this->compressedSize !== null) {
  214. $formats[] = 'COMP_SIZE=%d';
  215. $args[] = $this->compressedSize;
  216. }
  217. if ($this->localHeaderOffset !== null) {
  218. $formats[] = 'OFFSET=%d';
  219. $args[] = $this->localHeaderOffset;
  220. }
  221. if ($this->diskStart !== null) {
  222. $formats[] = 'DISK_START=%d';
  223. $args[] = $this->diskStart;
  224. }
  225. $format .= implode(' ', $formats);
  226. return vsprintf($format, $args);
  227. }
  228. }