-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreader.go
More file actions
277 lines (235 loc) · 8.88 KB
/
reader.go
File metadata and controls
277 lines (235 loc) · 8.88 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package vtf
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/galaco/vtf/format"
"github.com/galaco/vtf/internal"
)
const (
vtfSignature = "VTF\x00"
)
var (
// ErrorVtfSignatureMismatch occurs when a stream does not start with the VTF magic signature
ErrorVtfSignatureMismatch = errors.New("header signature does not match: VTF\x00")
// ErrorTextureDepthNotSupported occurs when attempting to parse a stream with depth > 1
ErrorTextureDepthNotSupported = errors.New("only vtf textures with depth 1 are supported")
// ErrorMipmapSizeMismatch occurs when filesize does not match calculated mipmap size
ErrorMipmapSizeMismatch = errors.New("expected data size is smaller than actual")
// ErrorInvalidDimensions occurs when texture dimensions are invalid
ErrorInvalidDimensions = errors.New("invalid texture dimensions")
// ErrorInvalidMipmapCount occurs when mipmap count is unreasonable
ErrorInvalidMipmapCount = errors.New("invalid mipmap count")
// ErrorInvalidHeaderSize occurs when header size is invalid
ErrorInvalidHeaderSize = errors.New("invalid header size")
// ErrorUnsupportedVersion occurs when VTF version is not supported
ErrorUnsupportedVersion = errors.New("unsupported VTF version")
)
// Reader reads from a vtf stream
type Reader struct {
stream io.Reader
}
// ReadHeader reads the header of a texture only.
func (reader *Reader) ReadHeader() (*Header, error) {
data, err := io.ReadAll(reader.stream)
if err != nil {
return nil, err
}
// Header
return reader.parseHeader(data)
}
// Read parses vtf image from stream into a usable structure
// The only error to expect would be if mipmap data size overflows the total file size; normally
// due to tampered Header data.
func (reader *Reader) Read() (*Vtf, error) {
data, err := io.ReadAll(reader.stream)
if err != nil {
return nil, err
}
// Header
header, err := reader.parseHeader(data)
if err != nil {
return nil, err
}
// Validate header to prevent DoS attacks via malicious files
if err := reader.validateHeader(header, len(data)); err != nil {
return nil, err
}
// Resources - in vtf 7.3+ only
resourceData, err := reader.parseOtherResourceData(header, data)
if err != nil {
return nil, err
}
// Low resolution preview texture
lowResImage, err := reader.readLowResolutionMipmap(header, data)
if err != nil {
return nil, err
}
// Mipmaps
highResImage, err := reader.readMipmaps(header, data)
if err != nil {
return nil, err
}
return &Vtf{
header: *header,
resources: resourceData,
lowResolutionImageData: lowResImage,
highResolutionImageData: highResImage,
}, nil
}
// parseHeader reads vtf Header.
func (reader *Reader) parseHeader(buffer []byte) (*Header, error) {
// We know Header is 96 bytes maximum
// Note it *may* not be someday...
headerBytes := make([]byte, 96)
// Read bytes equal to Header size
byteReader := bytes.NewReader(buffer)
sectionReader := io.NewSectionReader(byteReader, 0, int64(len(headerBytes)))
_, err := sectionReader.Read(headerBytes)
if err != nil {
return nil, err
}
// Set Header data to read bytes
header := Header{}
err = binary.Read(bytes.NewBuffer(headerBytes[:]), binary.LittleEndian, &header)
if err != nil {
return nil, err
}
if string(header.Signature[:4]) != vtfSignature {
return nil, ErrorVtfSignatureMismatch
}
return &header, nil
}
// validateHeader performs security validation on header fields to prevent DoS attacks
func (reader *Reader) validateHeader(header *Header, fileSize int) error {
// Validate version - only support 7.1 to 7.5
majorVersion := header.Version[0]
minorVersion := header.Version[1]
version := majorVersion*10 + minorVersion
if majorVersion != 7 || version < 70 || version > 75 {
return fmt.Errorf("%w: %d.%d (only 7.0-7.5 supported)", ErrorUnsupportedVersion, majorVersion, minorVersion)
}
// Validate dimensions
if header.Width == 0 || header.Height == 0 {
return fmt.Errorf("%w: width=%d, height=%d (cannot be zero)", ErrorInvalidDimensions, header.Width, header.Height)
}
// Prevent excessive memory allocation - Source Engine typically uses max 4096x4096
const maxDimension = 16384 // Allow some headroom beyond typical max
if header.Width > maxDimension || header.Height > maxDimension {
return fmt.Errorf("%w: width=%d, height=%d (max %d)", ErrorInvalidDimensions, header.Width, header.Height, maxDimension)
}
// Validate mipmap count - should not exceed log2(max(width, height)) + 1
maxDim := header.Width
if header.Height > maxDim {
maxDim = header.Height
}
var maxMipmaps uint8 = 0
for d := maxDim; d > 0; d >>= 1 {
maxMipmaps++
}
if header.MipmapCount == 0 || header.MipmapCount > maxMipmaps {
return fmt.Errorf("%w: count=%d, expected 1-%d for %dx%d texture", ErrorInvalidMipmapCount, header.MipmapCount, maxMipmaps, header.Width, header.Height)
}
// Validate header size
if header.HeaderSize < 64 || header.HeaderSize > 1024 {
return fmt.Errorf("%w: %d bytes (expected 64-1024)", ErrorInvalidHeaderSize, header.HeaderSize)
}
// Validate header size doesn't exceed file size
if int(header.HeaderSize) > fileSize {
return fmt.Errorf("%w: header size %d exceeds file size %d", ErrorInvalidHeaderSize, header.HeaderSize, fileSize)
}
// Validate frame count is reasonable
if header.Frames == 0 || header.Frames > 1024 {
return fmt.Errorf("%w: frame count %d is invalid (expected 1-1024)", ErrorInvalidDimensions, header.Frames)
}
// Validate depth (if present in v7.2+)
if version >= 72 && header.Depth > 1 {
return ErrorTextureDepthNotSupported
}
// Validate low-res dimensions
if header.LowResImageWidth > 16 || header.LowResImageHeight > 16 {
return fmt.Errorf("%w: low-res dimensions %dx%d exceed expected maximum 16x16", ErrorInvalidDimensions, header.LowResImageWidth, header.LowResImageHeight)
}
return nil
}
// parseOtherResourceData reads resource data for 7.3+ images
func (reader *Reader) parseOtherResourceData(header *Header, buffer []byte) ([]byte, error) {
// validate Header version
if (header.Version[0]*10+header.Version[1] < 73) || header.NumResource == 0 {
header.Depth = 0
header.NumResource = 0
return []byte{}, nil
}
return []byte{}, nil
}
// readLowResolutionMipmap reads the low resolution texture information
// This is normally what you see previewed in Hammer texture browser.
// The largest axis should always be 16 wide/tall. The smallest can be any value,
// but is padded out to divisible by 4 for Dxt1 compressionn reasons
func (reader *Reader) readLowResolutionMipmap(header *Header, buffer []byte) ([]uint8, error) {
bufferSize := internal.ComputeSizeOfMipmapData(
int(header.LowResImageWidth),
int(header.LowResImageHeight),
format.Dxt1)
imgBuffer := make([]byte, bufferSize)
byteReader := bytes.NewReader(buffer[int(header.HeaderSize) : int(header.HeaderSize)+bufferSize])
sectionReader := io.NewSectionReader(byteReader, 0, int64(bufferSize))
_, err := sectionReader.Read(imgBuffer)
if err != nil {
return nil, err
}
return imgBuffer, nil
}
// readMipmaps reads all mipmaps
// Returned format is a bit odd, but is just a set of flat arrays containing arrays:
// mipmap[frame[face[slice[RGBA]]]
func (reader *Reader) readMipmaps(header *Header, buffer []byte) ([][][][][]uint8, error) {
if header.Depth > 1 {
return [][][][][]uint8{}, ErrorTextureDepthNotSupported
}
depth := header.Depth
// Occurs in texture versions before 7.2
if depth == 0 {
depth = 1
}
// Only support 1 ZSlice. No known Source game can use > 1 zslices
numZSlice := uint16(1)
bufferEnd := len(buffer)
storedFormat := format.Format(header.HighResImageFormat)
mipmapSizes := internal.ComputeMipmapSizes(int(header.MipmapCount), int(header.Width), int(header.Height))
// Iterate mipmap; smallest to largest
mipMaps := make([][][][][]uint8, header.MipmapCount)
for mipmapIdx := int8(header.MipmapCount - 1); mipmapIdx >= int8(0); mipmapIdx-- {
// Frame by frame; first to last
frames := make([][][][]uint8, header.Frames)
for frameIdx := uint16(0); frameIdx < header.Frames; frameIdx++ {
faces := make([][][]uint8, 1)
// Face by face; first to last
// @TODO is this correct to use depth? How to know how many faces there are
for faceIdx := uint16(0); faceIdx < depth; faceIdx++ {
zSlices := make([][]uint8, 1)
// Z Slice by Z Slice; first to last
// @TODO wtf is a z slice, and how do we know how many there are
for sliceIdx := uint16(0); sliceIdx < numZSlice; sliceIdx++ {
bufferSize := internal.ComputeSizeOfMipmapData(
mipmapSizes[mipmapIdx][0],
mipmapSizes[mipmapIdx][1],
storedFormat)
if len(buffer) < bufferEnd-bufferSize {
return mipMaps, ErrorMipmapSizeMismatch
}
img := buffer[bufferEnd-bufferSize : bufferEnd]
bufferEnd -= bufferSize
zSlices[sliceIdx] = img
}
faces[faceIdx] = zSlices
}
frames[frameIdx] = faces
}
mipMaps[mipmapIdx] = frames
// Ensure that we maintain aspect ratio when scaling up mipmaps
}
return mipMaps, nil
}