openpgp-php/lib/OpenPgP/CompressedDataPacket.php

102 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2020-06-07 06:25:59 +00:00
<?php
namespace Leenooks\OpenPGP;
/**
* OpenPGP Compressed Data packet (tag 8).
*
* @see http://tools.ietf.org/html/rfc4880#section-5.6
*/
class CompressedDataPacket extends Packet implements \IteratorAggregate, \ArrayAccess
{
protected $tag = 8;
public $algorithm;
/* see http://tools.ietf.org/html/rfc4880#section-9.3 */
static $algorithms = [
0 => 'Uncompressed',
1 => 'ZIP',
2 => 'ZLIB',
3 => 'BZip2',
];
// IteratorAggregate interface
function getIterator()
{
return new \ArrayIterator($this->data->packets);
}
// ArrayAccess interface
function offsetExists($offset) {
return isset($this->data[$offset]);
}
function offsetGet($offset) {
return $this->data[$offset];
}
function offsetSet($offset, $value) {
return is_null($offset) ? $this->data[] = $value : $this->data[$offset] = $value;
}
function offsetUnset($offset) {
unset($this->data[$offset]);
}
function body()
{
$body = chr($this->algorithm);
switch($this->algorithm) {
case 0:
2020-06-24 12:46:20 +00:00
$body .= (string)$this->data;
2020-06-07 06:25:59 +00:00
break;
case 1:
2020-06-24 12:46:20 +00:00
$body .= gzdeflate((string)$this->data);
2020-06-07 06:25:59 +00:00
break;
case 2:
2020-06-24 12:46:20 +00:00
$body .= gzcompress((string)$this->data);
2020-06-07 06:25:59 +00:00
break;
case 3:
2020-06-24 12:46:20 +00:00
$body .= bzcompress((string)$this->data);
2020-06-07 06:25:59 +00:00
break;
default:
/* @todo error? */
}
return $body;
}
function read()
{
$this->algorithm = ord($this->read_byte());
$this->data = $this->read_bytes($this->length);
switch($this->algorithm) {
case 0:
2020-06-18 12:03:56 +00:00
$this->data = Message::parse($this->data);
2020-06-07 06:25:59 +00:00
break;
case 1:
2020-06-18 12:03:56 +00:00
$this->data = Message::parse(gzinflate($this->data));
2020-06-07 06:25:59 +00:00
break;
case 2:
2020-06-18 12:03:56 +00:00
$this->data = Message::parse(gzuncompress($this->data));
2020-06-07 06:25:59 +00:00
break;
case 3:
2020-06-18 12:03:56 +00:00
$this->data = Message::parse(bzdecompress($this->data));
2020-06-07 06:25:59 +00:00
break;
default:
/* TODO error? */
}
}
}