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