diff options
| author | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-09-07 19:40:16 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-16 21:39:36 +0300 |
| commit | 4e71a481884c0c43aea3d0ee695ae68cdec56478 (patch) | |
| tree | d75c96612526a7c686d9f4d85af6b89c7cd864e6 /utils/utils.go | |
| parent | final commit before rewrite (diff) | |
| download | sdp.go-4e71a481884c0c43aea3d0ee695ae68cdec56478.tar.gz sdp.go-4e71a481884c0c43aea3d0ee695ae68cdec56478.tar.bz2 sdp.go-4e71a481884c0c43aea3d0ee695ae68cdec56478.zip | |
first rewrite commit using bisaxa/bitreader
Diffstat (limited to 'utils/utils.go')
| -rw-r--r-- | utils/utils.go | 44 |
1 files changed, 2 insertions, 42 deletions
diff --git a/utils/utils.go b/utils/utils.go index 5226e80..46b707c 100644 --- a/utils/utils.go +++ b/utils/utils.go | |||
| @@ -1,55 +1,15 @@ | |||
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import ( | 3 | import "os" |
| 4 | "encoding/binary" | ||
| 5 | "log" | ||
| 6 | "math" | ||
| 7 | "math/bits" | ||
| 8 | "os" | ||
| 9 | "unsafe" | ||
| 10 | ) | ||
| 11 | 4 | ||
| 12 | func CheckError(e error) { | 5 | func CheckError(e error) { |
| 13 | if e != nil { | 6 | if e != nil { |
| 14 | log.Panic(e) | 7 | panic(e) |
| 15 | } | 8 | } |
| 16 | } | 9 | } |
| 17 | 10 | ||
| 18 | func ReverseByteArrayValues(byteArr []byte, size int) []byte { | ||
| 19 | arr := make([]byte, size) | ||
| 20 | for index, byteValue := range byteArr { | ||
| 21 | arr[index] = bits.Reverse8(byteValue) | ||
| 22 | } | ||
| 23 | return arr | ||
| 24 | } | ||
| 25 | |||
| 26 | func ReadByteFromFile(file *os.File, size int32) []byte { | 11 | func ReadByteFromFile(file *os.File, size int32) []byte { |
| 27 | tmp := make([]byte, size) | 12 | tmp := make([]byte, size) |
| 28 | file.Read(tmp) | 13 | file.Read(tmp) |
| 29 | return tmp | 14 | return tmp |
| 30 | } | 15 | } |
| 31 | |||
| 32 | func IntFromBytes(byteArr []byte) uint32 { | ||
| 33 | int := binary.LittleEndian.Uint32(byteArr) | ||
| 34 | return int | ||
| 35 | } | ||
| 36 | |||
| 37 | func FloatFromBytes(byteArr []byte) float32 { | ||
| 38 | bits := binary.LittleEndian.Uint32(byteArr) | ||
| 39 | float := math.Float32frombits(bits) | ||
| 40 | return float | ||
| 41 | } | ||
| 42 | |||
| 43 | func FloatArrFromBytes(byteArr []byte) []float32 { | ||
| 44 | if len(byteArr) == 0 { | ||
| 45 | return nil | ||
| 46 | } | ||
| 47 | |||
| 48 | l := len(byteArr) / 4 | ||
| 49 | ptr := unsafe.Pointer(&byteArr[0]) | ||
| 50 | // It is important to keep in mind that the Go garbage collector | ||
| 51 | // will not interact with this data, and that if src if freed, | ||
| 52 | // the behavior of any Go code using the slice is nondeterministic. | ||
| 53 | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
| 54 | return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] | ||
| 55 | } | ||