This repository was archived by the owner on Feb 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetstring.go
More file actions
248 lines (215 loc) · 5.45 KB
/
netstring.go
File metadata and controls
248 lines (215 loc) · 5.45 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package socketmap
import (
"errors"
"io"
"strconv"
)
// Netstring errors
var (
// ErrNetstringIncomplete is returned when the netstring data is incomplete.
ErrNetstringIncomplete = errors.New("netstring: incomplete data")
// ErrNetstringInvalid is returned when the netstring format is invalid.
ErrNetstringInvalid = errors.New("netstring: invalid format")
// ErrNetstringNotReady is returned when trying to read data before it's ready.
ErrNetstringNotReady = errors.New("netstring: data not ready")
)
// Compile-time interface checks
var (
_ io.ReaderFrom = (*Netstring)(nil)
_ io.WriterTo = (*Netstring)(nil)
)
// Netstring represents a netstring-encoded message.
// Format: <length>:<data>,
type Netstring struct {
data []byte
ready bool
reading bool
lenBuf []byte
dataLen int
dataRead int
}
// NetstringForReading creates a new Netstring ready for reading from a stream.
func NetstringForReading() *Netstring {
return &Netstring{
reading: true,
lenBuf: make([]byte, 0, 16),
}
}
// NetstringFrom creates a new Netstring from the given data.
func NetstringFrom(data []byte) *Netstring {
return &Netstring{
data: data,
ready: true,
}
}
// ReadFrom reads a netstring from the given reader.
// Implements io.ReaderFrom interface.
// Returns the number of bytes read and nil when complete,
// ErrNetstringIncomplete if more data is needed,
// or another error if the format is invalid or an I/O error occurs.
func (ns *Netstring) ReadFrom(r io.Reader) (int64, error) {
if !ns.reading {
return 0, ErrNetstringInvalid
}
var totalRead int64
buf := make([]byte, 1)
// Read length prefix until we find ':'
if ns.dataLen == 0 && !ns.ready {
for {
n, err := r.Read(buf)
totalRead += int64(n)
if err != nil {
if err == io.EOF && len(ns.lenBuf) == 0 {
return totalRead, io.EOF
}
if err == io.EOF {
return totalRead, io.ErrUnexpectedEOF
}
return totalRead, err
}
if n == 0 {
return totalRead, ErrNetstringIncomplete
}
c := buf[0]
if c == ':' {
if len(ns.lenBuf) == 0 {
return totalRead, ErrNetstringInvalid
}
length, err := strconv.Atoi(string(ns.lenBuf))
if err != nil {
return totalRead, ErrNetstringInvalid
}
ns.dataLen = length
ns.data = make([]byte, length)
ns.dataRead = 0
break
}
if c < '0' || c > '9' {
return totalRead, ErrNetstringInvalid
}
ns.lenBuf = append(ns.lenBuf, c)
if len(ns.lenBuf) > 10 {
return totalRead, ErrNetstringInvalid
}
}
}
// Read data
for ns.dataRead < ns.dataLen {
n, err := r.Read(ns.data[ns.dataRead:])
totalRead += int64(n)
if err != nil {
if err == io.EOF {
return totalRead, io.ErrUnexpectedEOF
}
return totalRead, err
}
if n == 0 {
return totalRead, ErrNetstringIncomplete
}
ns.dataRead += n
}
// Read trailing comma
n, err := r.Read(buf)
totalRead += int64(n)
if err != nil {
if err == io.EOF {
return totalRead, io.ErrUnexpectedEOF
}
return totalRead, err
}
if n == 0 {
return totalRead, ErrNetstringIncomplete
}
if buf[0] != ',' {
return totalRead, ErrNetstringInvalid
}
ns.ready = true
return totalRead, nil
}
// WriteTo writes the netstring-encoded data to the given writer.
// Implements io.WriterTo interface.
func (ns *Netstring) WriteTo(w io.Writer) (int64, error) {
if !ns.ready {
return 0, ErrNetstringNotReady
}
data, err := ns.Marshal()
if err != nil {
return 0, err
}
n, err := w.Write(data)
return int64(n), err
}
// String returns a string representation of the netstring data.
// Implements fmt.Stringer interface.
func (ns *Netstring) String() string {
if !ns.ready {
return "<not ready>"
}
return string(ns.data)
}
// MarshalBinary returns the netstring-encoded representation.
// Implements encoding.BinaryMarshaler interface.
func (ns *Netstring) MarshalBinary() ([]byte, error) {
return ns.Marshal()
}
// UnmarshalBinary decodes a netstring from the given data.
// Implements encoding.BinaryUnmarshaler interface.
func (ns *Netstring) UnmarshalBinary(data []byte) error {
if len(data) < 3 {
return ErrNetstringInvalid
}
// Find the colon separator
colonIdx := -1
for i := 0; i < len(data) && i < 11; i++ {
if data[i] == ':' {
colonIdx = i
break
}
if data[i] < '0' || data[i] > '9' {
return ErrNetstringInvalid
}
}
if colonIdx == -1 || colonIdx == 0 {
return ErrNetstringInvalid
}
// Parse length
length, err := strconv.Atoi(string(data[:colonIdx]))
if err != nil {
return ErrNetstringInvalid
}
// Verify we have enough data: length prefix + ':' + data + ','
expectedLen := colonIdx + 1 + length + 1
if len(data) < expectedLen {
return ErrNetstringIncomplete
}
// Verify trailing comma
if data[colonIdx+1+length] != ',' {
return ErrNetstringInvalid
}
// Extract data
ns.data = make([]byte, length)
copy(ns.data, data[colonIdx+1:colonIdx+1+length])
ns.ready = true
ns.reading = false
return nil
}
// Bytes returns the data contained in the netstring.
func (ns *Netstring) Bytes() ([]byte, error) {
if !ns.ready {
return nil, ErrNetstringNotReady
}
return ns.data, nil
}
// Marshal returns the netstring-encoded representation.
func (ns *Netstring) Marshal() ([]byte, error) {
if !ns.ready {
return nil, ErrNetstringNotReady
}
length := strconv.Itoa(len(ns.data))
result := make([]byte, 0, len(length)+1+len(ns.data)+1)
result = append(result, length...)
result = append(result, ':')
result = append(result, ns.data...)
result = append(result, ',')
return result, nil
}