github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/debug/pe/string.go (about) 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. 4 5 package pe 6 7 import ( 8 "encoding/binary" 9 "fmt" 10 "io" 11 ) 12 13 // cstring converts ASCII byte sequence b to string. 14 // It stops once it finds 0 or reaches end of b. 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 // StringTable is a COFF string table. 23 type StringTable []byte 24 25 func readStringTable(fh *FileHeader, r io.ReadSeeker) (StringTable, error) { 26 // COFF string table is located right after COFF symbol table. 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 // string table length includes itself 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 // TODO(brainman): decide if start parameter should be int instead of uint32 54 55 // String extracts string from COFF string table st at offset start. 56 func (st StringTable) String(start uint32) (string, error) { 57 // start includes 4 bytes of string table length 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 }