// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pe

import (
	
	
	
	
	
)

// cstring converts ASCII byte sequence b to string.
// It stops once it finds 0 or reaches end of b.
func cstring( []byte) string {
	 := bytes.IndexByte(, 0)
	if  == -1 {
		 = len()
	}
	return string([:])
}

// StringTable is a COFF string table.
type StringTable []byte

func readStringTable( *FileHeader,  io.ReadSeeker) (StringTable, error) {
	// COFF string table is located right after COFF symbol table.
	if .PointerToSymbolTable <= 0 {
		return nil, nil
	}
	 := .PointerToSymbolTable + COFFSymbolSize*.NumberOfSymbols
	,  := .Seek(int64(), io.SeekStart)
	if  != nil {
		return nil, fmt.Errorf("fail to seek to string table: %v", )
	}
	var  uint32
	 = binary.Read(, binary.LittleEndian, &)
	if  != nil {
		return nil, fmt.Errorf("fail to read string table length: %v", )
	}
	// string table length includes itself
	if  <= 4 {
		return nil, nil
	}
	 -= 4

	,  := saferio.ReadData(, uint64())
	if  != nil {
		return nil, fmt.Errorf("fail to read string table: %v", )
	}
	return StringTable(), nil
}

// TODO(brainman): decide if start parameter should be int instead of uint32

// String extracts string from COFF string table st at offset start.
func ( StringTable) ( uint32) (string, error) {
	// start includes 4 bytes of string table length
	if  < 4 {
		return "", fmt.Errorf("offset %d is before the start of string table", )
	}
	 -= 4
	if int() > len() {
		return "", fmt.Errorf("offset %d is beyond the end of string table", )
	}
	return cstring([:]), nil
}