-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
104 lines (90 loc) · 1.6 KB
/
util.go
File metadata and controls
104 lines (90 loc) · 1.6 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 outscript
import (
"encoding/binary"
"errors"
"io"
)
type readHelper struct {
N int64
R io.Reader
Err error
}
func (rc *readHelper) readByte() byte {
if rc.Err != nil {
return 0
}
if r, ok := rc.R.(io.ByteReader); ok {
v, err := r.ReadByte()
if err != nil {
rc.Err = err
return 0
}
rc.N += 1
return v
}
var res [1]byte
n, err := io.ReadFull(rc.R, res[:])
rc.N += int64(n)
if err != nil {
rc.Err = err
}
return res[0]
}
func (rc *readHelper) readUint16le() uint16 {
var res [2]byte
rc.readFull(res[:])
return binary.LittleEndian.Uint16(res[:])
}
func (rc *readHelper) readUint32le() uint32 {
var res [4]byte
rc.readFull(res[:])
return binary.LittleEndian.Uint32(res[:])
}
func (rc *readHelper) readUint64le() uint64 {
var res [8]byte
rc.readFull(res[:])
return binary.LittleEndian.Uint64(res[:])
}
func (rc *readHelper) readFull(buf []byte) {
if rc.Err != nil {
return
}
n, err := io.ReadFull(rc.R, buf)
rc.N += int64(n)
if err != nil {
rc.Err = err
}
}
func (rc *readHelper) readTo(d io.ReaderFrom) {
if rc.Err != nil {
return
}
n, err := d.ReadFrom(rc.R)
rc.N += n
if err != nil {
rc.Err = err
}
}
func (rc *readHelper) readVarBuf() []byte {
if rc.Err != nil {
return nil
}
var ln BtcVarInt
rc.readTo(&ln)
if ln == 0 {
return nil
}
if ln > 100000 {
rc.Err = errors.New("error: buffer larger than maximum allowed length")
return nil
}
buf := make([]byte, ln)
rc.readFull(buf)
return buf
}
func (rc *readHelper) err(err error) (int64, error) {
return rc.N, err
}
func (rc *readHelper) ret() (int64, error) {
return rc.N, rc.Err
}