-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
74 lines (67 loc) · 1.7 KB
/
util.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
package ntgo
import (
"bytes"
"io"
"encoding/binary"
"math"
"errors"
)
var (
ErrULEBDecodeDataInvalid = errors.New("uleb: could not decode invalid uleb encoding")
)
// DecodeULEB128 returns the ULEB128-encoded Value.
func DecodeULEB128(r io.Reader) (uint32, error) {
var result uint32 = 0
var shift uint32 = 0
var currByte = [1]byte{0x80}
for currByte[0]&0x80 == 0x80 {
_, readErr := io.ReadFull(r, currByte[:])
if readErr != nil {
return 0, ErrULEBDecodeDataInvalid
}
result |= uint32(currByte[0]&0x7f) << shift
shift+=7
}
return result, nil
}
// DecodeSaveULEB128 returns the ULEB128-encoded Value and the ULEB128 data.
func DecodeAndSaveULEB128(r io.Reader) (uint32, []byte, error) {
data := []byte{}
var result uint32 = 0
var shift uint32 = 0
currByte := [1]byte{0x80}
for currByte[0]&0x80 == 0x80 {
_, readErr := io.ReadFull(r, currByte[:])
if readErr != nil {
return 0, data, ErrULEBDecodeDataInvalid
}
result |= uint32(currByte[0]&0x7f) << shift
shift+=7
data = append(data, currByte[0])
}
return result, data, nil
}
// Encodes data into ULEB128 Value as a byte array
func EncodeULEB128(data uint32) []byte {
remaining := data >> 7
var buf = new(bytes.Buffer)
for remaining != 0 {
buf.WriteByte(byte(data&0x7f | 0x80))
data = remaining
remaining >>= 7
}
buf.WriteByte(byte(data & 0x7f))
return buf.Bytes()
}
// BytesToFloat64 converts bytes to Float64
func BytesToFloat64(bytes []byte) float64 {
bits := binary.BigEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}
func Float64ToBytes(value float64) []byte {
bits := math.Float64bits(value)
floatBytes := make([]byte, 8)
binary.BigEndian.PutUint64(floatBytes, bits)
return floatBytes
}