1<?php
2/**
3 * Copyright 2002-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @see      https://tools.ietf.org/html/rfc1952
9 * @author   Michael Cochrane <mike@graftonhall.co.nz>
10 * @author   Michael Slusarz <slusarz@horde.org>
11 * @category Horde
12 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
13 * @package  Compress
14 */
15
16/**
17 * The Horde_Compress_Gzip class allows gzip files to be read.
18 *
19 * @author    Michael Cochrane <mike@graftonhall.co.nz>
20 * @author    Michael Slusarz <slusarz@horde.org>
21 * @category  Horde
22 * @copyright 2002-2017 Horde LLC
23 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
24 * @package   Compress
25 */
26class Horde_Compress_Gzip extends Horde_Compress_Base
27{
28    /**
29     */
30    public $canDecompress = true;
31
32    /**
33     * Gzip file flags.
34     *
35     * @var array
36     */
37    protected $_flags = array(
38        'FTEXT'     =>  0x01,
39        'FHCRC'     =>  0x02,
40        'FEXTRA'    =>  0x04,
41        'FNAME'     =>  0x08,
42        'FCOMMENT'  =>  0x10
43    );
44
45    /**
46     * @return string  The uncompressed data.
47     */
48    public function decompress($data, array $params = array())
49    {
50        /* If gzip is not compiled into PHP, return now. */
51        if (!Horde_Util::extensionExists('zlib')) {
52            throw new Horde_Compress_Exception(Horde_Compress_Translation::t("This server can't uncompress gzip files."));
53        }
54
55        /* Gzipped File - decompress it first. */
56        $position = 0;
57        $info = @unpack('CCM/CFLG/VTime/CXFL/COS', substr($data, $position + 2));
58        if (!$info) {
59            throw new Horde_Compress_Exception(Horde_Compress_Translation::t("Unable to decompress data."));
60        }
61        $position += 10;
62
63        if ($info['FLG'] & $this->_flags['FEXTRA']) {
64            $XLEN = unpack('vLength', substr($data, $position + 0, 2));
65            $XLEN = $XLEN['Length'];
66            $position += $XLEN + 2;
67        }
68
69        if ($info['FLG'] & $this->_flags['FNAME']) {
70            $filenamePos = strpos($data, "\x0", $position);
71            // Filename
72            // substr($data, $position, $filenamePos - $position);
73            $position = $filenamePos + 1;
74        }
75
76        if ($info['FLG'] & $this->_flags['FCOMMENT']) {
77            $commentPos = strpos($data, "\x0", $position);
78            // Comment
79            // substr($data, $position, $commentPos - $position);
80            $position = $commentPos + 1;
81        }
82
83        if ($info['FLG'] & $this->_flags['FHCRC']) {
84            $hcrc = unpack('vCRC', substr($data, $position + 0, 2));
85            $hcrc = $hcrc['CRC'];
86            $position += 2;
87        }
88
89        $result = @gzinflate(substr($data, $position, strlen($data) - $position));
90        if (empty($result)) {
91            throw new Horde_Compress_Exception(Horde_Compress_Translation::t("Unable to decompress data."));
92        }
93
94        return $result;
95    }
96
97}
98