-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.go
47 lines (41 loc) · 947 Bytes
/
common.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
package socks5
import (
"bytes"
"errors"
"io"
)
var errUnexpectMinusLength = errors.New("arg number should not be minus")
// ReadNBytes wrap io.ReadFull. read n bytes.
// The error is EOF only if no bytes were read.
// If an EOF happens after reading some but not all the bytes,
// ReadFull returns ErrUnexpectedEOF.
func ReadNBytes(reader io.Reader, n int) ([]byte, error) {
if n < 0 {
return nil, errUnexpectMinusLength
}
data := make([]byte, n)
_, err := io.ReadFull(reader, data)
if err != nil {
return nil, err
}
return data, nil
}
// ReadUntilNULL Read all not Null byte.
// Until read first Null byte(all zero bits)
func ReadUntilNULL(reader io.Reader) ([]byte, error) {
data := &bytes.Buffer{}
b := make([]byte, 1)
for {
_, err := reader.Read(b)
if err != nil {
if err == io.EOF {
return nil, nil
}
return nil, err
}
if b[0] == NULL {
return data.Bytes(), nil
}
data.WriteByte(b[0])
}
}