-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
70 lines (62 loc) · 2.07 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
package gomble
import (
"fmt"
"os"
"github.com/CodingVoid/gomble/logger"
)
// write int16 buffer in file of path with offset
func WriteInt16InFile(path string, buffer []int16) { // {{{
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
if err != nil {
logger.Fatalf(err.Error())
}
bufferByte := make([]byte, len(buffer)*2)
// Convert to little Endian ordering
for i := 0; i < len(buffer); i++ {
bufferByte[2*i] = byte(buffer[i] & 0xFF)
bufferByte[2*i+1] = byte((buffer[i] >> 8) & 0xFF)
}
if _, err := file.WriteAt(bufferByte[:], 0); err != nil {
logger.Fatalf(err.Error())
}
} // }}}
// write buffer in file of path with offset
func writeInFile(path string, offset int64, buffer []byte) { // {{{
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
logger.Fatalf(err.Error())
}
n := 0
if n, err = file.WriteAt(buffer[:], offset); err != nil {
logger.Fatalf(err.Error())
}
logger.Debugf("Wrote %d bytes\n", n)
file.Close()
} // }}}
// takes a prefix and a Byte Array and returns a string which contains the prefix + all byte array elements in a readable format
func formatByteArray(prefix string, data []byte) string { // {{{
out := prefix
for _, b := range data {
out += fmt.Sprintf("%02X ", b)
}
out += "\n"
return out
} // }}}
// takes a prefix and a uint32 Array and returns a string which contains the prefix + all uint32 array elements in a readable format
func formatUint32Array(prefix string, data []uint32) string { // {{{
out := prefix
for _, u := range data {
out += fmt.Sprintf("%d ", u)
}
out += "\n"
return out
} // }}}
// takes a prefix and a string Array and returns a string which contains the prefix + all string array elements in a readable format
func formatStringArray(prefix string, data []string) string { // {{{
out := prefix
for index, s := range data {
out += fmt.Sprintf("%i: %s", index, s)
}
out += "\n"
return out
} // }}}