forked from mit-dci/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.go
85 lines (75 loc) · 1.84 KB
/
stdlib.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
package lnutil
import (
"bytes"
"encoding/binary"
"fmt"
)
// I shouldn't even have to write these...
// int32 to 4 bytes. Always works.
func I32tB(i int32) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, i)
return buf.Bytes()
}
// uint32 to 4 bytes. Always works.
func U32tB(i uint32) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, i)
return buf.Bytes()
}
// 4 byte slice to uint32. Returns ffffffff if something doesn't work.
func BtU32(b []byte) uint32 {
if len(b) != 4 {
fmt.Printf("Got %x to BtU32 (%d bytes)\n", b, len(b))
return 0xffffffff
}
var i uint32
buf := bytes.NewBuffer(b)
binary.Read(buf, binary.BigEndian, &i)
return i
}
// 4 byte slice to int32. Returns 7fffffff if something doesn't work.
func BtI32(b []byte) int32 {
if len(b) != 4 {
fmt.Printf("Got %x to BtI32 (%d bytes)\n", b, len(b))
return 0x7fffffff
}
var i int32
buf := bytes.NewBuffer(b)
binary.Read(buf, binary.BigEndian, &i)
return i
}
// uint64 to 8 bytes. Always works.
func U64tB(i uint64) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, i)
return buf.Bytes()
}
// int64 to 8 bytes. Always works.
func I64tB(i int64) []byte {
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, i)
return buf.Bytes()
}
// 8 bytes to int64 (bitcoin amounts). returns 7fff... if it doesn't work.
func BtI64(b []byte) int64 {
if len(b) != 8 {
fmt.Printf("Got %x to BtI64 (%d bytes)\n", b, len(b))
return 0x7fffffffffffffff
}
var i int64
buf := bytes.NewBuffer(b)
binary.Read(buf, binary.BigEndian, &i)
return i
}
// 8 bytes to uint64. returns ffff. if it doesn't work.
func BtU64(b []byte) uint64 {
if len(b) != 8 {
fmt.Printf("Got %x to BtU64 (%d bytes)\n", b, len(b))
return 0xffffffffffffffff
}
var i uint64
buf := bytes.NewBuffer(b)
binary.Read(buf, binary.BigEndian, &i)
return i
}