Source file src/debug/pe/string.go

     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  	"io"
    12  )
    13  
    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)
    18  	if i == -1 {
    19  		i = len(b)
    20  	}
    21  	return string(b[:i])
    22  }
    23  
    24  // StringTable is a COFF string table.
    25  type StringTable []byte
    26  
    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 {
    30  		return nil, nil
    31  	}
    32  	offset := fh.PointerToSymbolTable + COFFSymbolSize*fh.NumberOfSymbols
    33  	_, err := r.Seek(int64(offset), seekStart)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("fail to seek to string table: %v", err)
    36  	}
    37  	var l uint32
    38  	err = binary.Read(r, binary.LittleEndian, &l)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("fail to read string table length: %v", err)
    41  	}
    42  	// string table length includes itself
    43  	if l <= 4 {
    44  		return nil, nil
    45  	}
    46  	l -= 4
    47  
    48  	// If the string table is large, the file may be corrupt.
    49  	// Read in chunks to avoid crashing due to out of memory.
    50  	const chunk = 10 << 20 // 10M
    51  	var buf []byte
    52  	if l < chunk {
    53  		buf = make([]byte, l)
    54  		_, err = io.ReadFull(r, buf)
    55  	} else {
    56  		for l > 0 {
    57  			n := l
    58  			if n > chunk {
    59  				n = chunk
    60  			}
    61  			buf1 := make([]byte, n)
    62  			_, err = io.ReadFull(r, buf1)
    63  			if err != nil {
    64  				break
    65  			}
    66  			buf = append(buf, buf1...)
    67  			l -= n
    68  		}
    69  	}
    70  	if err != nil {
    71  		return nil, fmt.Errorf("fail to read string table: %v", err)
    72  	}
    73  	return StringTable(buf), nil
    74  }
    75  
    76  // TODO(brainman): decide if start parameter should be int instead of uint32
    77  
    78  // String extracts string from COFF string table st at offset start.
    79  func (st StringTable) String(start uint32) (string, error) {
    80  	// start includes 4 bytes of string table length
    81  	if start < 4 {
    82  		return "", fmt.Errorf("offset %d is before the start of string table", start)
    83  	}
    84  	start -= 4
    85  	if int(start) > len(st) {
    86  		return "", fmt.Errorf("offset %d is beyond the end of string table", start)
    87  	}
    88  	return cstring(st[start:]), nil
    89  }
    90  

View as plain text