Source file
src/debug/pe/string.go
1
2
3
4
5 package pe
6
7 import (
8 "bytes"
9 "encoding/binary"
10 "fmt"
11 "internal/saferio"
12 "io"
13 )
14
15
16
17 func cstring(b []byte) string {
18 i := bytes.IndexByte(b, 0)
19 if i == -1 {
20 i = len(b)
21 }
22 return string(b[:i])
23 }
24
25
26 type StringTable []byte
27
28 func readStringTable(fh *FileHeader, r io.ReadSeeker) (StringTable, error) {
29
30 if fh.PointerToSymbolTable <= 0 {
31 return nil, nil
32 }
33 offset := fh.PointerToSymbolTable + COFFSymbolSize*fh.NumberOfSymbols
34 _, err := r.Seek(int64(offset), io.SeekStart)
35 if err != nil {
36 return nil, fmt.Errorf("fail to seek to string table: %v", err)
37 }
38 var l uint32
39 err = binary.Read(r, binary.LittleEndian, &l)
40 if err != nil {
41 return nil, fmt.Errorf("fail to read string table length: %v", err)
42 }
43
44 if l <= 4 {
45 return nil, nil
46 }
47 l -= 4
48
49 buf, err := saferio.ReadData(r, uint64(l))
50 if err != nil {
51 return nil, fmt.Errorf("fail to read string table: %v", err)
52 }
53 return StringTable(buf), nil
54 }
55
56
57
58
59 func (st StringTable) String(start uint32) (string, error) {
60
61 if start < 4 {
62 return "", fmt.Errorf("offset %d is before the start of string table", start)
63 }
64 start -= 4
65 if int(start) > len(st) {
66 return "", fmt.Errorf("offset %d is beyond the end of string table", start)
67 }
68 return cstring(st[start:]), nil
69 }
70
View as plain text