master
ブランチ master
LittleBeast / src / Std / Lib / Git / PktLine.php
4.3 KB、116行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Lib\Git;
39
40/**
41 * Gitのプロトコルのpkt-line(4桁の16進数の長さ+データ、0000はflush)
42 */
43final class PktLine {
44 public const FLUSH = 0;
45 public const DELIM = 1;
46 public const END = 2;
47 /** pkt-lineの最大の長さ */
48 public const MAX = 65520;
49
50 /** @param resource $in */
51 public function __construct(private $in) {}
52
53 /** 1行(文字列、又はFLUSH・DELIM・END、終わりならnull)。行の最後の改行は消す */
54 public function read(bool $keepNewline = false): string|int|null {
55 $head = $this->exact(4, true);
56 if ($head === null) return null;
57 if (!ctype_xdigit($head)) throw new GitException('不正なpkt-line:'.bin2hex($head));
58 $len = hexdec($head);
59 if ($len < 4) return $len;
60 if ($len > self::MAX) throw new GitException('pkt-lineが長過ぎます');
61 $data = $this->exact($len - 4, false);
62 return $keepNewline ? $data : (str_ends_with($data, "\n") ? substr($data, 0, -1) : $data);
63 }
64
65 /** 次のflushまでの行 */
66 public function readUntilFlush(): array {
67 $out = [];
68 while (true) {
69 $l = $this->read();
70 if ($l === null || $l === self::FLUSH) return $out;
71 $out[] = $l;
72 }
73 }
74
75 private function exact(int $n, bool $eofOk): ?string {
76 $s = '';
77 while (strlen($s) < $n) {
78 $chunk = fread($this->in, $n - strlen($s));
79 if ($chunk === false || $chunk === '') {
80 if (feof($this->in)) {
81 if ($s === '' && $eofOk) return null;
82 throw new GitException('接続が途中で切れました');
83 }
84 continue;
85 }
86 $s .= $chunk;
87 }
88 return $s;
89 }
90
91 /** pkt-lineにする */
92 public static function encode(string $data): string {
93 if (strlen($data) + 4 > self::MAX) throw new GitException('pkt-lineが長過ぎます');
94 return sprintf('%04x', strlen($data) + 4).$data;
95 }
96
97 public static function flush(): string {
98 return '0000';
99 }
100
101 public static function delim(): string {
102 return '0001';
103 }
104
105 /**
106 * サイドバンドのpkt-line(1:データ、2:進捗、3:エラー)
107 *
108 * @param int $max 1つの最大(side-band-64kは65520、side-bandは1000)
109 */
110 public static function band(int $band, string $data, int $max = self::MAX): string {
111 $out = '';
112 $step = $max - 5;
113 for ($i = 0, $n = strlen($data); $i < $n; $i += $step) $out .= self::encode(chr($band).substr($data, $i, $step));
114 return $out;
115 }
116}