aboutsummaryrefslogtreecommitdiff
path: root/utils/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/utils.go')
-rw-r--r--utils/utils.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/utils/utils.go b/utils/utils.go
new file mode 100644
index 0000000..62924c2
--- /dev/null
+++ b/utils/utils.go
@@ -0,0 +1,57 @@
1package utils
2
3import (
4 "encoding/binary"
5 "log"
6 "math"
7 "os"
8 "unsafe"
9
10 "github.com/potterxu/bitreader"
11)
12
13func ReadByteFromFile(file *os.File, size int32) []byte {
14 tmp := make([]byte, size)
15 file.Read(tmp)
16 return tmp
17}
18
19func CheckError(e error) {
20 if e != nil {
21 log.Panic(e)
22 }
23}
24
25func CheckFirstBit(byteArr []byte) bool {
26 reader := bitreader.BitReader(byteArr)
27 state, err := reader.ReadBit()
28 if err != nil {
29 state = false
30 }
31 return state
32}
33
34func IntFromBytes(byteArr []byte) uint32 {
35 int := binary.LittleEndian.Uint32(byteArr)
36 return int
37}
38
39func FloatFromBytes(byteArr []byte) float32 {
40 bits := binary.LittleEndian.Uint32(byteArr)
41 float := math.Float32frombits(bits)
42 return float
43}
44
45func FloatArrFromBytes(byteArr []byte) []float32 {
46 if len(byteArr) == 0 {
47 return nil
48 }
49
50 l := len(byteArr) / 4
51 ptr := unsafe.Pointer(&byteArr[0])
52 // It is important to keep in mind that the Go garbage collector
53 // will not interact with this data, and that if src if freed,
54 // the behavior of any Go code using the slice is nondeterministic.
55 // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
56 return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l]
57}