github.com/bir3/gocompiler@v0.9.2202/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  	"bytes"
     9  	"encoding/binary"
    10  	"fmt"
    11  	"github.com/bir3/gocompiler/src/internal/saferio"
    12  	"io"
    13  )
    14  
    15  // cstring converts ASCII byte sequence b to string.
    16  // It stops once it finds 0 or reaches end of b.
    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  // StringTable is a COFF string table.
    26  type StringTable []byte
    27  
    28  func readStringTable(fh *FileHeader, r io.ReadSeeker) (StringTable, error) {
    29  	// COFF string table is located right after COFF symbol table.
    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  	// string table length includes itself
    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  // TODO(brainman): decide if start parameter should be int instead of uint32
    57  
    58  // String extracts string from COFF string table st at offset start.
    59  func (st StringTable) String(start uint32) (string, error) {
    60  	// start includes 4 bytes of string table length
    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  }