-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaders.go
More file actions
58 lines (52 loc) · 1.93 KB
/
Copy pathheaders.go
File metadata and controls
58 lines (52 loc) · 1.93 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
package pego
import (
"encoding/binary"
"io"
)
// readHeader reads a header of type T from reader at the given offset, advancing the offset by the size of the header.
// Returns a pointer to the newly read header or an error if reading failed.
func readHeader[T any](reader io.ReaderAt, offset *int64) (*T, error) {
h := new(T)
size := int64(binary.Size(h))
r := io.NewSectionReader(reader, *offset, size)
err := binary.Read(r, binary.LittleEndian, h)
if err != nil {
return nil, err
}
*offset += size
return h, nil
}
// writeHeader serializes the header data to the writer in little-endian byte order.
func writeHeader(writer io.Writer, h any) error {
return binary.Write(writer, binary.LittleEndian, h)
}
// DOSHeader contains the DOS header data.
type DOSHeader struct {
Magic uint16 // Magic number.
Cblp uint16 // Bytes on last page of file.
Cp uint16 // Pages in file.
Crlc uint16 // Relocations.
Cparhdr uint16 // Size of header in paragraphs.
Minalloc uint16 // Minimum extra paragraphs needed.
Maxalloc uint16 // Maximum extra paragraphs needed.
Ss uint16 // Initial (relative) SS value.
Sp uint16 // Initial SP value.
Csum uint16 // Checksum.
Ip uint16 // Initial IP value.
Cs uint16 // Initial (relative) CS value.
Lfarlc uint16 // File address of relocation table.
Ovno uint16 // Overlay number.
Res [4]uint16 // Reserved uint16s.
Oemid uint16 // OEM identifier (for e_oeminfo).
Oeminfo uint16 // OEM information; e_oemid specific.
Res2 [10]uint16 // Reserved uint16s.
Lfanew uint32 // File address of new exe header.
}
// PESignature is the PE file signature type.
type PESignature uint32
const (
DOSHeaderMagic uint16 = 0x5a4d // 'M', 'Z'
PESignatureMagic PESignature = 0x00004550 // 'P', 'E', 0, 0
PE32Magic = 0x10b
PE32PlusMagic = 0x20b
)