1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // cstring converts ASCII byte sequence b to string.
15 // It stops once it finds 0 or reaches end of b.
16 func cstring(b
[]byte) string {
17 i
:= bytes
.IndexByte(b
, 0)
24 // StringTable is a COFF string table.
25 type StringTable
[]byte
27 func readStringTable(fh
*FileHeader
, r io
.ReadSeeker
) (StringTable
, error
) {
28 // COFF string table is located right after COFF symbol table.
29 if fh
.PointerToSymbolTable
<= 0 {
32 offset
:= fh
.PointerToSymbolTable
+ COFFSymbolSize
*fh
.NumberOfSymbols
33 _
, err
:= r
.Seek(int64(offset
), seekStart
)
35 return nil, fmt
.Errorf("fail to seek to string table: %v", err
)
38 err
= binary
.Read(r
, binary
.LittleEndian
, &l
)
40 return nil, fmt
.Errorf("fail to read string table length: %v", err
)
42 // string table length includes itself
47 buf
:= make([]byte, l
)
48 _
, err
= io
.ReadFull(r
, buf
)
50 return nil, fmt
.Errorf("fail to read string table: %v", err
)
52 return StringTable(buf
), nil
55 // TODO(brainman): decide if start parameter should be int instead of uint32
57 // String extracts string from COFF string table st at offset start.
58 func (st StringTable
) String(start
uint32) (string, error
) {
59 // start includes 4 bytes of string table length
61 return "", fmt
.Errorf("offset %d is before the start of string table", start
)
64 if int(start
) > len(st
) {
65 return "", fmt
.Errorf("offset %d is beyond the end of string table", start
)
67 return cstring(st
[start
:]), nil