windows: add resource extraction functions

These functions make it possible to read executable resource information
at runtime.

Change-Id: I00f260199ecda8daeb3417eaa9c02198663063b7
Reviewed-on: https://go-review.googlesource.com/c/sys/+/298173
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Jason A. Donenfeld
2021-03-05 02:33:59 +00:00
parent 7b4935edff
commit f9bc61c02a
4 changed files with 163 additions and 0 deletions
+24
View File
@@ -5,6 +5,7 @@
package windows_test
import (
"bytes"
"debug/pe"
"errors"
"fmt"
@@ -562,3 +563,26 @@ func TestPEBFilePath(t *testing.T) {
t.Errorf("expected os.Executable() to return same value as peb.Ldr.{entry}.FullDllName - want %#q; got %#q", osPath, pebPath)
}
}
func TestResourceExtraction(t *testing.T) {
system32, err := windows.GetSystemDirectory()
if err != nil {
t.Errorf("unable to find system32 directory: %v", err)
}
cmd, err := windows.LoadLibrary(filepath.Join(system32, "cmd.exe"))
if err != nil {
t.Errorf("unable to load cmd.exe: %v", err)
}
defer windows.FreeLibrary(cmd)
rsrc, err := windows.FindResource(cmd, windows.CREATEPROCESS_MANIFEST_RESOURCE_ID, windows.RT_MANIFEST)
if err != nil {
t.Errorf("unable to find cmd.exe manifest resource: %v", err)
}
manifest, err := windows.LoadResourceData(cmd, rsrc)
if err != nil {
t.Errorf("unable to load cmd.exe manifest resource data: %v", err)
}
if !bytes.Contains(manifest, []byte("</assembly>")) {
t.Errorf("did not find </assembly> in manifest")
}
}