diff options
| author | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-08-27 13:02:35 +0300 |
|---|---|---|
| committer | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-08-27 13:02:35 +0300 |
| commit | f108a577658c9aab8496da4ebd0fb4f0216093e8 (patch) | |
| tree | e484a8a8b54c92ca4a393f267ebc755ec6434c8d /utils/utils.go | |
| download | sdp.go-f108a577658c9aab8496da4ebd0fb4f0216093e8.tar.gz sdp.go-f108a577658c9aab8496da4ebd0fb4f0216093e8.tar.bz2 sdp.go-f108a577658c9aab8496da4ebd0fb4f0216093e8.zip | |
init
Diffstat (limited to 'utils/utils.go')
| -rw-r--r-- | utils/utils.go | 57 |
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 @@ | |||
| 1 | package utils | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/binary" | ||
| 5 | "log" | ||
| 6 | "math" | ||
| 7 | "os" | ||
| 8 | "unsafe" | ||
| 9 | |||
| 10 | "github.com/potterxu/bitreader" | ||
| 11 | ) | ||
| 12 | |||
| 13 | func ReadByteFromFile(file *os.File, size int32) []byte { | ||
| 14 | tmp := make([]byte, size) | ||
| 15 | file.Read(tmp) | ||
| 16 | return tmp | ||
| 17 | } | ||
| 18 | |||
| 19 | func CheckError(e error) { | ||
| 20 | if e != nil { | ||
| 21 | log.Panic(e) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | func 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 | |||
| 34 | func IntFromBytes(byteArr []byte) uint32 { | ||
| 35 | int := binary.LittleEndian.Uint32(byteArr) | ||
| 36 | return int | ||
| 37 | } | ||
| 38 | |||
| 39 | func FloatFromBytes(byteArr []byte) float32 { | ||
| 40 | bits := binary.LittleEndian.Uint32(byteArr) | ||
| 41 | float := math.Float32frombits(bits) | ||
| 42 | return float | ||
| 43 | } | ||
| 44 | |||
| 45 | func 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 | } | ||