-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathpkt.go
141 lines (128 loc) · 3.57 KB
/
pkt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitfs
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
// A pktLineReader reads Git pkt-line-formatted packets.
//
// Each n-byte packet is preceded by a 4-digit hexadecimal length
// encoding n+4 (the length counts its own bytes), like "0006a\n" for "a\n".
//
// A packet starting with 0000 is a so-called flush packet.
// A packet starting with 0001 is a delimiting marker,
// which usually marks the end of a sequence in the stream.
//
// See https://git-scm.com/docs/protocol-common#_pkt_line_format
// for the official documentation, although it fails to mention the 0001 packets.
type pktLineReader struct {
b *bufio.Reader
size [4]byte
}
// newPktLineReader returns a new pktLineReader reading from r.
func newPktLineReader(r io.Reader) *pktLineReader {
return &pktLineReader{b: bufio.NewReader(r)}
}
// Next returns the payload of the next packet from the stream.
// If the next packet is a flush packet (length 0000), Next returns nil, io.EOF.
// If the next packet is a delimiter packet (length 0001), Next returns nil, nil.
// If the data stream has ended, Next returns nil, io.ErrUnexpectedEOF.
func (r *pktLineReader) Next() ([]byte, error) {
_, err := io.ReadFull(r.b, r.size[:])
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
n, err := strconv.ParseUint(string(r.size[:]), 16, 0)
if err != nil || n == 2 || n == 3 {
return nil, fmt.Errorf("malformed pkt-line")
}
if n == 1 {
return nil, nil // delimiter
}
if n == 0 {
return nil, io.EOF
}
buf := make([]byte, n-4)
_, err = io.ReadFull(r.b, buf)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
return buf, nil
}
// Lines reads packets from r until a flush packet.
// It returns a string for each packet, with any trailing newline trimmed.
func (r *pktLineReader) Lines() ([]string, error) {
var lines []string
for {
line, err := r.Next()
if err != nil {
if err == io.EOF {
err = nil
}
return lines, err
}
lines = append(lines, strings.TrimSuffix(string(line), "\n"))
}
}
// A pktLineWriter writes Git pkt-line-formatted packets.
// See pktLineReader for a description of the packet format.
type pktLineWriter struct {
b *bufio.Writer
size [4]byte
}
// newPktLineWriter returns a new pktLineWriter writing to w.
func newPktLineWriter(w io.Writer) *pktLineWriter {
return &pktLineWriter{b: bufio.NewWriter(w)}
}
// writeSize writes a four-digit hexadecimal length packet for n.
// Typically n is len(data)+4.
func (w *pktLineWriter) writeSize(n int) {
hex := "0123456789abcdef"
w.size[0] = hex[n>>12]
w.size[1] = hex[(n>>8)&0xf]
w.size[2] = hex[(n>>4)&0xf]
w.size[3] = hex[(n>>0)&0xf]
w.b.Write(w.size[:])
}
// Write writes b as a single packet.
func (w *pktLineWriter) Write(b []byte) (int, error) {
n := len(b)
if n+4 > 0xffff {
return 0, fmt.Errorf("write too large")
}
w.writeSize(n + 4)
w.b.Write(b)
return n, nil
}
// WriteString writes s as a single packet.
func (w *pktLineWriter) WriteString(s string) (int, error) {
n := len(s)
if n+4 > 0xffff {
return 0, fmt.Errorf("write too large")
}
w.writeSize(n + 4)
w.b.WriteString(s)
return n, nil
}
// Close writes a terminating flush packet
// and flushes buffered data to the underlying writer.
func (w *pktLineWriter) Close() error {
w.b.WriteString("0000")
w.b.Flush()
return nil
}
// Delim writes a delimiter packet.
func (w *pktLineWriter) Delim() {
w.b.WriteString("0001")
}