-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-encoder.go
More file actions
104 lines (85 loc) · 2.51 KB
/
binary-encoder.go
File metadata and controls
104 lines (85 loc) · 2.51 KB
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
package ssh
import (
"encoding/binary"
"errors"
)
var bigendian = binary.BigEndian
// Encoder type. Don't touch the internals.
type Encoder struct {
buf []byte
commit func([]byte) (int, error)
}
// Create a new printer with empty output.
func NewEncoder() *Encoder {
return &Encoder{make([]byte, 0, 32), nil}
}
// Create a new printer with output prefixed with the given byte slice.
func NewEncoderWith(data []byte) *Encoder {
return &Encoder{data, nil}
}
func NewEncoderWithCommit(data []byte, commit func([]byte) (int, error)) *Encoder {
return &Encoder{data, commit}
}
// Output a byte.
func (p *Encoder) Byte(d byte) *Encoder {
p.buf = append(p.buf, d)
return p
}
// Output 2 bigendian bytes.
func (p *Encoder) U16(d uint16) *Encoder {
p.buf = append(p.buf, byte(d>>8), byte(d))
return p
}
// Output 4 bigendian bytes.
func (p *Encoder) U32(d uint32) *Encoder {
p.buf = append(p.buf, byte(d>>24), byte(d>>16), byte(d>>8), byte(d))
return p
}
// Output 4 bigendian bytes.
func (p *Encoder) U64(d uint64) *Encoder {
p.buf = append(p.buf, byte(d>>56), byte(d>>48), byte(d>>40), byte(d>>32), byte(d>>24), byte(d>>16), byte(d>>8), byte(d))
return p
}
// Output a raw byte slice with no length prefix.
func (p *Encoder) Bytes(d []byte) *Encoder {
p.buf = append(p.buf, d...)
return p
}
// Output a raw string with no length prefix.
func (p *Encoder) String(d string) *Encoder {
p.buf = append(p.buf, []byte(d)...)
return p
}
// Output a string with a 4 byte bigendian length prefix and no trailing null.
func (p *Encoder) U32String(d string) *Encoder {
return p.U32(uint32(len(d))).String(d)
}
// Output bytes with a 4 byte bigendian length prefix and no trailing null.
func (p *Encoder) U32Bytes(d []byte) *Encoder {
return p.U32(uint32(len(d))).Bytes(d)
}
// Output a string with a 2 byte bigendian length prefix and no trailing null.
func (p *Encoder) U16String(d string) *Encoder {
if len(d) > 0xffff {
panic(errors.New("string length overflows uint16"))
}
return p.U16(uint16(len(d))).String(d)
}
// Output a string with a 1 byte bigendian length prefix and no trailing null.
func (p *Encoder) U8String(d string) *Encoder {
if len(d) > 0xff {
panic(errors.New("string length overflows uint8"))
}
return p.Byte(byte(len(d))).String(d)
}
// Output a string terminated by a null-byte
func (p *Encoder)String0(d string) *Encoder {
return p.String(d).Byte(0)
}
// Get the output as a byte slice.
func (p *Encoder) Out() []byte {
return p.buf
}
func (p *Encoder) Commit() (int, error) {
return p.commit(p.buf)
}