| 1 |
// Package pktline implements reading payloads form pkt-lines and encoding |
| 2 |
// pkt-lines from payloads. |
| 3 |
package pktline |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
|
| 11 |
"github.com/go-git/go-git/v5/utils/trace" |
| 12 |
) |
| 13 |
|
| 14 |
// An Encoder writes pkt-lines to an output stream. |
| 15 |
type Encoder struct { |
| 16 |
w io.Writer |
| 17 |
} |
| 18 |
|
| 19 |
const ( |
| 20 |
// MaxPayloadSize is the maximum payload size of a pkt-line in bytes. |
| 21 |
MaxPayloadSize = 65516 |
| 22 |
|
| 23 |
// For compatibility with canonical Git implementation, accept longer pkt-lines |
| 24 |
OversizePayloadMax = 65520 |
| 25 |
) |
| 26 |
|
| 27 |
var ( |
| 28 |
// FlushPkt are the contents of a flush-pkt pkt-line. |
| 29 |
FlushPkt = []byte{'0', '0', '0', '0'} |
| 30 |
// Flush is the payload to use with the Encode method to encode a flush-pkt. |
| 31 |
Flush = []byte{} |
| 32 |
// FlushString is the payload to use with the EncodeString method to encode a flush-pkt. |
| 33 |
FlushString = "" |
| 34 |
// ErrPayloadTooLong is returned by the Encode methods when any of the |
| 35 |
// provided payloads is bigger than MaxPayloadSize. |
| 36 |
ErrPayloadTooLong = errors.New("payload is too long") |
| 37 |
) |
| 38 |
|
| 39 |
// NewEncoder returns a new encoder that writes to w. |
| 40 |
func NewEncoder(w io.Writer) *Encoder { |
| 41 |
return &Encoder{ |
| 42 |
w: w, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Flush encodes a flush-pkt to the output stream. |
| 47 |
func (e *Encoder) Flush() error { |
| 48 |
defer trace.Packet.Print("packet: > 0000") |
| 49 |
_, err := e.w.Write(FlushPkt) |
| 50 |
return err |
| 51 |
} |
| 52 |
|
| 53 |
// Encode encodes a pkt-line with the payload specified and write it to |
| 54 |
// the output stream. If several payloads are specified, each of them |
| 55 |
// will get streamed in their own pkt-lines. |
| 56 |
func (e *Encoder) Encode(payloads ...[]byte) error { |
| 57 |
for _, p := range payloads { |
| 58 |
if err := e.encodeLine(p); err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
func (e *Encoder) encodeLine(p []byte) error { |
| 67 |
if len(p) > MaxPayloadSize { |
| 68 |
return ErrPayloadTooLong |
| 69 |
} |
| 70 |
|
| 71 |
if bytes.Equal(p, Flush) { |
| 72 |
return e.Flush() |
| 73 |
} |
| 74 |
|
| 75 |
n := len(p) + 4 |
| 76 |
defer trace.Packet.Printf("packet: > %04x %s", n, p) |
| 77 |
if _, err := e.w.Write(asciiHex16(n)); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
_, err := e.w.Write(p) |
| 81 |
return err |
| 82 |
} |
| 83 |
|
| 84 |
// Returns the hexadecimal ascii representation of the 16 less |
| 85 |
// significant bits of n. The length of the returned slice will always |
| 86 |
// be 4. Example: if n is 1234 (0x4d2), the return value will be |
| 87 |
// []byte{'0', '4', 'd', '2'}. |
| 88 |
func asciiHex16(n int) []byte { |
| 89 |
var ret [4]byte |
| 90 |
ret[0] = byteToASCIIHex(byte(n & 0xf000 >> 12)) |
| 91 |
ret[1] = byteToASCIIHex(byte(n & 0x0f00 >> 8)) |
| 92 |
ret[2] = byteToASCIIHex(byte(n & 0x00f0 >> 4)) |
| 93 |
ret[3] = byteToASCIIHex(byte(n & 0x000f)) |
| 94 |
|
| 95 |
return ret[:] |
| 96 |
} |
| 97 |
|
| 98 |
// turns a byte into its hexadecimal ascii representation. Example: |
| 99 |
// from 11 (0xb) to 'b'. |
| 100 |
func byteToASCIIHex(n byte) byte { |
| 101 |
if n < 10 { |
| 102 |
return '0' + n |
| 103 |
} |
| 104 |
|
| 105 |
return 'a' - 10 + n |
| 106 |
} |
| 107 |
|
| 108 |
// EncodeString works similarly as Encode but payloads are specified as strings. |
| 109 |
func (e *Encoder) EncodeString(payloads ...string) error { |
| 110 |
for _, p := range payloads { |
| 111 |
if err := e.Encode([]byte(p)); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
// Encodef encodes a single pkt-line with the payload formatted as |
| 120 |
// the format specifier. The rest of the arguments will be used in |
| 121 |
// the format string. |
| 122 |
func (e *Encoder) Encodef(format string, a ...interface{}) error { |
| 123 |
return e.EncodeString( |
| 124 |
fmt.Sprintf(format, a...), |
| 125 |
) |
| 126 |
} |