/
varint.go
executable file
·57 lines (54 loc) · 1.56 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
package util
import "math"
// script parse
// 0-10
func DecodeVarIntForScript(buf []byte) (int, int) {
if buf[0] < 0x4c { //N/A 1-75
return int(buf[0]), 1
} else if buf[0] == 0x4c { //OP_PUSHDATA1 - 76
return int(buf[1]), 2
} else if buf[0] == 0x4d { //op_pushdata2 - 77
count :=
(int(buf[2]) * int(math.Pow(256, 1))) +
int(buf[1])
return count, 3
} else if buf[0] == 0x4e { //OP_PUSHDATA4 - 78
count :=
int(buf[4])*int(math.Pow(256, 3)) +
int(buf[3])*int(math.Pow(256, 2)) +
int(buf[2])*int(math.Pow(256, 1)) +
int(buf[1])
return count, 5
}
return -1, -1
}
// tx parse
// 0-9
func DecodeVarIntForTx(buf []byte) (int, int) {
//if len(buf) != 9 {
// return 0, 0
//}
if buf[0] <= 0xfc { //252 uint8_t
return int(buf[0]), 1
} else if buf[0] == 0xfd { //253 0xFD followed by the length as uint16_t
return (int(buf[2]) * int(math.Pow(256, 1))) + int(buf[1]), 3
} else if buf[0] == 0xfe { //254 0xFE followed by the length as uint32_t
count := (int(buf[4]) * int(math.Pow(256, 3))) +
(int(buf[3]) * int(math.Pow(256, 2))) +
(int(buf[2]) * int(math.Pow(256, 1))) +
int(buf[1])
return count, 5
} else if buf[0] == 0xff { //255 0xFF followed by the length as uint64_t
count := (int(buf[8]) * int(math.Pow(256, 7))) +
int(buf[7])*int(math.Pow(256, 6)) +
int(buf[6])*int(math.Pow(256, 5)) +
int(buf[5])*int(math.Pow(256, 4)) +
int(buf[4])*int(math.Pow(256, 3)) +
int(buf[3])*int(math.Pow(256, 2)) +
int(buf[2])*int(math.Pow(256, 1)) +
//int(buf[1])*int(math.Pow(256, 1))
int(buf[1])
return count, 9
}
return 0, 0
}