This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
/
helpers.go
93 lines (78 loc) · 1.86 KB
/
helpers.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
package dbf
import (
"io"
"os"
//"fmt"
)
// appendSlice data[]byte to slice and returns the new byte slice.
// Change this function if you want to tweak memory allocation for the database.
func appendSlice(slice, data []byte) []byte {
slice = append(slice, data...)
return slice
}
func readFile(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
d, err := f.Stat()
if err != nil {
return nil, err
}
buf := make([]byte, d.Size())
_, err = io.ReadFull(f, buf)
if err != nil {
return nil, err
}
return buf, err
}
func uint32ToBytes(x uint32) []byte {
var buf [4]byte
buf[0] = byte(x >> 0)
buf[1] = byte(x >> 8)
buf[2] = byte(x >> 16)
buf[3] = byte(x >> 24)
return buf[:]
}
func int32ToBytes(x int32) []byte {
var buf [4]byte
buf[0] = byte(x >> 0)
buf[1] = byte(x >> 8)
buf[2] = byte(x >> 16)
buf[3] = byte(x >> 24)
return buf[:]
}
func uint64ToBytes(x uint64) []byte {
var buf [8]byte
buf[0] = byte(x >> 0)
buf[1] = byte(x >> 8)
buf[2] = byte(x >> 16)
buf[3] = byte(x >> 24)
buf[4] = byte(x >> 32)
buf[5] = byte(x >> 40)
buf[6] = byte(x >> 48)
buf[7] = byte(x >> 56)
return buf[:]
}
func int64ToBytes(x uint64) []byte {
var buf [8]byte
buf[0] = byte(x >> 0)
buf[1] = byte(x >> 8)
buf[2] = byte(x >> 16)
buf[3] = byte(x >> 24)
buf[4] = byte(x >> 32)
buf[5] = byte(x >> 40)
buf[6] = byte(x >> 48)
buf[7] = byte(x >> 56)
return buf[:]
}
func bytesToInt32le(b []byte) int32 {
return int32(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
}
func bytesToInt32be(b []byte) int32 {
return int32(uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24)
}
func bytesToUint64le(b []byte) uint64 {
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}