Debian Codegraph @

classes/ReportFile/ReportGZFileCutFilter.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2022 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 namespace Liuch\DmarcSrg\ReportFile;
24
25 use php_user_filter;
26
27 class ReportGZFileCutFilter extends php_user_filter
28 {
29 private $head = true;
30 private $header_data = '';
31 private $tail_data = '';
32
33 public function filter($in, $out, &$consumed, $closing): int
34 {
35 $b_cnt = 0;
36 while ($bucket = stream_bucket_make_writeable($in)) {
37 $consumed += $bucket->datalen;
38 $data = null;
39 if ($this->head) {
40 $data = $this->skipGzHeader($bucket->data);
41 } else {
42 $data = $bucket->data;
43 }
44 $data = $this->cutGzTail($data);
45 if (strlen($data) > 0) {
46 $bucket->data = $data;
47 stream_bucket_append($out, $bucket);
48 $b_cnt += 1;
49 }
50 }
51 return ($b_cnt > 0) ? PSFS_PASS_ON : PSFS_FEED_ME;
52 }
53
54 private function skipGzHeader($data)
55 {
56 // https://tools.ietf.org/html/rfc1952
57 $this->header_data .= $data;
58 $len = strlen($this->header_data);
59 if ($len < 10) { // minimal gz header
60 return '';
61 }
62
63 $pos = 10;
64 $flags = ord($this->header_data[3]);
65 if ($flags & 4) { // FLG.FEXTRA
66 $pos += (ord($this->header_data[$pos + 1]) | (ord($this->header_data[$pos + 2]) << 8)) + 2;
67 if ($pos > $len) {
68 return '';
69 }
70 }
71 if ($flags & 8) { // FLG.FNAME
72 $pos = $this->skipZeroTerminatedString($this->header_data, $len, $pos);
73 if ($pos > $len) {
74 return '';
75 }
76 }
77 if ($flags & 16) { // FLG.FCOMMENT
78 $pos = $this->skipZeroTerminatedString($this->header_data, $len, $pos);
79 if ($pos > $len) {
80 return '';
81 }
82 }
83 if ($flags & 2) { // FLG.FHCRC
84 $pos += 2;
85 if ($pos > $len) {
86 return '';
87 }
88 }
89 $res = substr($this->header_data, $pos);
90 $this->head = false;
91 $this->header_data = '';
92 return $res;
93 }
94
95 private function cutGzTail($data)
96 {
97 $res = $this->tail_data . $data;
98 $this->tail_data = substr($res, -8);
99 if (strlen($res) <= 8) {
100 return '';
101 }
102 return substr($res, 0, -8);
103 }
104
105 private function skipZeroTerminatedString($str, $len, $pos)
106 {
107 for ($i = $pos; $i < $len; ++$i) {
108 if ($str[$i] === "\0") {
109 return $i + 1;
110 }
111 }
112 return $len + 1;
113 }
114 }