| 1 |
package pktline |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/utils/trace" |
| 10 |
) |
| 11 |
|
| 12 |
const ( |
| 13 |
lenSize = 4 |
| 14 |
) |
| 15 |
|
| 16 |
// ErrInvalidPktLen is returned by Err() when an invalid pkt-len is found. |
| 17 |
var ErrInvalidPktLen = errors.New("invalid pkt-len found") |
| 18 |
|
| 19 |
// Scanner provides a convenient interface for reading the payloads of a |
| 20 |
// series of pkt-lines. It takes an io.Reader providing the source, |
| 21 |
// which then can be tokenized through repeated calls to the Scan |
| 22 |
// method. |
| 23 |
// |
| 24 |
// After each Scan call, the Bytes method will return the payload of the |
| 25 |
// corresponding pkt-line on a shared buffer, which will be 65516 bytes |
| 26 |
// or smaller. Flush pkt-lines are represented by empty byte slices. |
| 27 |
// |
| 28 |
// Scanning stops at EOF or the first I/O error. |
| 29 |
type Scanner struct { |
| 30 |
r io.Reader // The reader provided by the client |
| 31 |
err error // Sticky error |
| 32 |
payload []byte // Last pkt-payload |
| 33 |
len [lenSize]byte // Last pkt-len |
| 34 |
} |
| 35 |
|
| 36 |
// NewScanner returns a new Scanner to read from r. |
| 37 |
func NewScanner(r io.Reader) *Scanner { |
| 38 |
return &Scanner{ |
| 39 |
r: r, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Err returns the first error encountered by the Scanner. |
| 44 |
func (s *Scanner) Err() error { |
| 45 |
return s.err |
| 46 |
} |
| 47 |
|
| 48 |
// Scan advances the Scanner to the next pkt-line, whose payload will |
| 49 |
// then be available through the Bytes method. Scanning stops at EOF |
| 50 |
// or the first I/O error. After Scan returns false, the Err method |
| 51 |
// will return any error that occurred during scanning, except that if |
| 52 |
// it was io.EOF, Err will return nil. |
| 53 |
func (s *Scanner) Scan() bool { |
| 54 |
var l int |
| 55 |
l, s.err = s.readPayloadLen() |
| 56 |
if s.err == io.EOF { |
| 57 |
s.err = nil |
| 58 |
return false |
| 59 |
} |
| 60 |
if s.err != nil { |
| 61 |
return false |
| 62 |
} |
| 63 |
|
| 64 |
if cap(s.payload) < l { |
| 65 |
s.payload = make([]byte, 0, l) |
| 66 |
} |
| 67 |
|
| 68 |
if _, s.err = io.ReadFull(s.r, s.payload[:l]); s.err != nil { |
| 69 |
return false |
| 70 |
} |
| 71 |
s.payload = s.payload[:l] |
| 72 |
trace.Packet.Printf("packet: < %04x %s", l, s.payload) |
| 73 |
|
| 74 |
if bytes.HasPrefix(s.payload, errPrefix) { |
| 75 |
s.err = &ErrorLine{ |
| 76 |
Text: strings.TrimSpace(string(s.payload[4:])), |
| 77 |
} |
| 78 |
return false |
| 79 |
} |
| 80 |
|
| 81 |
return true |
| 82 |
} |
| 83 |
|
| 84 |
// Bytes returns the most recent payload generated by a call to Scan. |
| 85 |
// The underlying array may point to data that will be overwritten by a |
| 86 |
// subsequent call to Scan. It does no allocation. |
| 87 |
func (s *Scanner) Bytes() []byte { |
| 88 |
return s.payload |
| 89 |
} |
| 90 |
|
| 91 |
// Method readPayloadLen returns the payload length by reading the |
| 92 |
// pkt-len and subtracting the pkt-len size. |
| 93 |
func (s *Scanner) readPayloadLen() (int, error) { |
| 94 |
if _, err := io.ReadFull(s.r, s.len[:]); err != nil { |
| 95 |
if err == io.ErrUnexpectedEOF { |
| 96 |
return 0, ErrInvalidPktLen |
| 97 |
} |
| 98 |
|
| 99 |
return 0, err |
| 100 |
} |
| 101 |
|
| 102 |
n, err := hexDecode(s.len) |
| 103 |
if err != nil { |
| 104 |
return 0, err |
| 105 |
} |
| 106 |
|
| 107 |
switch { |
| 108 |
case n == 0: |
| 109 |
return 0, nil |
| 110 |
case n <= lenSize: |
| 111 |
return 0, ErrInvalidPktLen |
| 112 |
case n > OversizePayloadMax+lenSize: |
| 113 |
return 0, ErrInvalidPktLen |
| 114 |
default: |
| 115 |
return n - lenSize, nil |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Turns the hexadecimal representation of a number in a byte slice into |
| 120 |
// a number. This function substitute strconv.ParseUint(string(buf), 16, |
| 121 |
// 16) and/or hex.Decode, to avoid generating new strings, thus helping the |
| 122 |
// GC. |
| 123 |
func hexDecode(buf [lenSize]byte) (int, error) { |
| 124 |
var ret int |
| 125 |
for i := 0; i < lenSize; i++ { |
| 126 |
n, err := asciiHexToByte(buf[i]) |
| 127 |
if err != nil { |
| 128 |
return 0, ErrInvalidPktLen |
| 129 |
} |
| 130 |
ret = 16*ret + int(n) |
| 131 |
} |
| 132 |
return ret, nil |
| 133 |
} |
| 134 |
|
| 135 |
// turns the hexadecimal ascii representation of a byte into its |
| 136 |
// numerical value. Example: from 'b' to 11 (0xb). |
| 137 |
func asciiHexToByte(b byte) (byte, error) { |
| 138 |
switch { |
| 139 |
case b >= '0' && b <= '9': |
| 140 |
return b - '0', nil |
| 141 |
case b >= 'a' && b <= 'f': |
| 142 |
return b - 'a' + 10, nil |
| 143 |
case b >= 'A' && b <= 'F': |
| 144 |
return b - 'A' + 10, nil |
| 145 |
default: |
| 146 |
return 0, ErrInvalidPktLen |
| 147 |
} |
| 148 |
} |