-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmtbytes.go
192 lines (157 loc) · 4.11 KB
/
fmtbytes.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package fmtbytes
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"unicode/utf16"
)
//TODO: make these return errors instead of panicing
/*ReadBytesNInt reads length bytes into a new buffer
and returns the result as a []byte
*/
func ReadBytesNInt(file *os.File, length uint32) []byte {
byteBuffer := make([]byte, length)
_, err := file.Read(byteBuffer)
if err != nil {
fmt.Println(err)
panic(err)
}
return byteBuffer
}
/*ReadBytesLongLong reads 8 bytes into a new buffer
and returns the result as a uint64*/
func ReadBytesLongLong(file *os.File) uint64 {
byteBuffer := make([]byte, 8)
_, err := file.Read(byteBuffer)
if err != nil {
fmt.Println(err)
panic(err)
}
return binary.BigEndian.Uint64(byteBuffer)
}
/*ReadBytesLong reads 4 bytes into a new buffer
and returns the result as a uint32*/
func ReadBytesLong(file *os.File) uint32 {
byteBuffer := make([]byte, 4)
_, err := file.Read(byteBuffer)
if err != nil {
fmt.Println(err)
panic(err)
}
return binary.BigEndian.Uint32(byteBuffer)
}
/*ReadBytesShort reads 2 bytes into a new buffer
and returns the result as an uint16*/
func ReadBytesShort(file *os.File) uint16 {
byteBuffer := make([]byte, 2)
_, err := file.Read(byteBuffer)
if err != nil {
panic(err)
}
return binary.BigEndian.Uint16(byteBuffer)
}
/*ReadSingleByte reads 1 bytes into a new buffer
and returns the result as an uint16*/
func ReadSingleByte(file *os.File) int {
byteBuffer := make([]byte, 1)
_, err := file.Read(byteBuffer)
if err != nil {
panic(err)
}
return int(byteBuffer[0])
}
/*ReadBytesString reads length number of bytes into a new buffer
and returns the result as a string*/
func ReadBytesString(file *os.File, length int) string {
byteBuffer := make([]byte, length)
_, err := file.Read(byteBuffer)
if err != nil {
panic(err)
}
return string(byteBuffer)
}
/*ReadIntoArray16 takes a []byte and creates a new slice containing the values in uint16 form*/
func ReadIntoArray16(file *os.File, length uint32) []uint16 {
newBufferLength := length / 2
newBuffer := make([]uint16, newBufferLength)
var i uint32
for i = 0; i < newBufferLength; i++ {
newBuffer = append(newBuffer, ReadBytesShort(file))
}
return newBuffer
}
/*ReadIntoArray8 takes a []byte and creates a new slice containing the values in uint8 form*/
func ReadIntoArray8(file *os.File, length uint32) []uint8 {
newBufferLength := length / 2
newBuffer := make([]uint8, newBufferLength)
var i uint32
for i = 0; i < newBufferLength; i++ {
newBuffer = append(newBuffer, uint8(ReadSingleByte(file)))
}
return newBuffer
}
//ReadRawBytes returns a buffer containing length bytes at the current file pointer index
func ReadRawBytes(file *os.File, length int) []byte {
buffer := make([]byte, length)
_, err := file.Read(buffer)
if err != nil {
panic(err)
}
return buffer
}
/*ParseUnicodeString parses a unicode string from the
photoshop file into a string*/
func ParseUnicodeString(file *os.File) string {
length := ReadBytesLong(file)
if length == 0 {
return ""
}
stringBuffer := make([]uint16, length)
for i := range stringBuffer {
stringBuffer[i] = ReadBytesShort(file)
}
return string(utf16.Decode(stringBuffer))
}
/*ParsePascalString parses a pascal string directly from file*/
func ParsePascalString(file *os.File) string {
b := ReadSingleByte(file)
if b == 0 {
ReadSingleByte(file)
return ""
}
s := ReadBytesString(file, b)
if b%2 != 0 {
ReadSingleByte(file)
}
return s
}
/*ReadDouble reads a 64bit int*/
func ReadDouble(file *os.File) (float64, error) {
buffer := make([]byte, 8)
_, err := file.Read(buffer)
if err != nil {
return 0, err
}
reader := bytes.NewReader(buffer)
var res float64
binary.Read(reader, binary.BigEndian, &res)
return res, nil
}
/*ReadFloat reads 4bytes into a float*/
func ReadFloat(file *os.File) float32 {
buffer := make([]byte, 4)
_, err := file.Read(buffer)
if err != nil {
return 0
}
reader := bytes.NewReader(buffer)
var res float32
binary.Read(reader, binary.BigEndian, &res)
return res
}
/*GetFileLength gets the full size in bytes of the file given*/
func GetFileLength(file *os.File) int64 {
fi, _ := file.Stat()
return fi.Size()
}