-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarint.go
138 lines (126 loc) · 2.1 KB
/
varint.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
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
//
// varint.go
// Copyright (C) 2017 weirdgiraffe <giraffe@cyberzoo.xyz>
//
// Distributed under terms of the MIT license.
//
package bitcoin
import (
"encoding/binary"
"fmt"
"io"
)
type Varint uint64
func (v Varint) OutSize() int {
switch {
case v < 0xfd:
return 1
case v < 0xffff:
return 3
case v < 0xffffffff:
return 5
default:
return 9
}
}
func WriteVarint(w io.Writer, u Varint) (err error) {
switch {
case u < 0xfd:
v := byte(0xff & u)
_, err = w.Write([]byte{v})
if err != nil {
return
}
case u < 0xffff:
v := uint16(u)
err = binary.Write(w, binary.LittleEndian, &v)
if err != nil {
return
}
case u < 0xffffffff:
v := uint32(u)
err = binary.Write(w, binary.LittleEndian, &v)
if err != nil {
return
}
default:
v := uint64(u)
err = binary.Write(w, binary.LittleEndian, &v)
if err != nil {
return
}
}
return nil
}
func ReadVarint(r io.Reader, u *Varint) (err error) {
b := []byte{0}
_, err = r.Read(b)
if err != nil {
return
}
switch b[0] {
case 0xfd:
var s uint16
err = binary.Read(r, binary.LittleEndian, &s)
if err != nil {
return
}
*u = Varint(s)
case 0xfe:
var w uint32
err = binary.Read(r, binary.LittleEndian, &w)
if err != nil {
return
}
*u = Varint(w)
case 0xff:
var dw uint64
err = binary.Read(r, binary.LittleEndian, &dw)
if err != nil {
return
}
*u = Varint(dw)
default:
*u = Varint(b[0])
}
return nil
}
type ScriptInt struct {
val int64
}
func ScriptIntFromSlice(b []byte) *ScriptInt {
if len(b) > 8 {
panic(fmt.Errorf("ScriptInt is bigger than int64"))
}
s := &ScriptInt{}
var offt uint = 0
for i := range b {
s.val |= (int64(b[i]) << offt)
offt += 8
}
if ((int64(0x80)<<offt)&s.val) > 0 && (s.val > 0) {
s.val = -s.val
}
return s
}
func (s ScriptInt) Int64() int64 {
return s.val
}
func (s ScriptInt) Int() int {
return int(s.val)
}
func (s ScriptInt) Bytes() []byte {
b := make([]byte, 8)
var offt uint = 0
for i := range b {
b[i] = byte((s.val >> offt) & 0xff)
offt += 8
if b[i] == 0 {
if i == 0 {
return []byte{0}
}
return b[:i]
}
}
return b
}