Add support to inject a uint64 symbol
Bug: 31559095 Test: m blueprint_tools Change-Id: Ifb46e21f5cac413abdf1dfe7fd6d31e06a7dbca5
This commit is contained in:
parent
2249dc892d
commit
0db18c239a
|
@ -88,7 +88,7 @@ func main() {
|
||||||
os.Exit(4)
|
os.Exit(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = symbol_inject.InjectSymbol(file, w, *symbol, *value, *from)
|
err = symbol_inject.InjectStringSymbol(file, w, *symbol, *value, *from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err.Error())
|
fmt.Fprintln(os.Stderr, err.Error())
|
||||||
os.Remove(*output)
|
os.Remove(*output)
|
||||||
|
|
|
@ -16,6 +16,7 @@ package symbol_inject
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
@ -50,7 +51,7 @@ func OpenFile(r io.ReaderAt) (*File, error) {
|
||||||
return file, err
|
return file, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func InjectSymbol(file *File, w io.Writer, symbol, value, from string) error {
|
func InjectStringSymbol(file *File, w io.Writer, symbol, value, from string) error {
|
||||||
offset, size, err := findSymbol(file, symbol)
|
offset, size, err := findSymbol(file, symbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -75,13 +76,29 @@ func InjectSymbol(file *File, w io.Writer, symbol, value, from string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return copyAndInject(file.r, w, offset, size, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func copyAndInject(r io.ReaderAt, w io.Writer, offset, size uint64, value string) (err error) {
|
|
||||||
buf := make([]byte, size)
|
buf := make([]byte, size)
|
||||||
copy(buf, value)
|
copy(buf, value)
|
||||||
|
|
||||||
|
return copyAndInject(file.r, w, offset, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func InjectUint64Symbol(file *File, w io.Writer, symbol string, value uint64) error {
|
||||||
|
offset, size, err := findSymbol(file, symbol)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if size != 8 {
|
||||||
|
return fmt.Errorf("symbol %q is not a uint64, it is %d bytes long", symbol, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 8)
|
||||||
|
binary.LittleEndian.PutUint64(buf, value)
|
||||||
|
|
||||||
|
return copyAndInject(file.r, w, offset, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyAndInject(r io.ReaderAt, w io.Writer, offset uint64, buf []byte) (err error) {
|
||||||
// Copy the first bytes up to the symbol offset
|
// Copy the first bytes up to the symbol offset
|
||||||
_, err = io.Copy(w, io.NewSectionReader(r, 0, int64(offset)))
|
_, err = io.Copy(w, io.NewSectionReader(r, 0, int64(offset)))
|
||||||
|
|
||||||
|
@ -91,7 +108,7 @@ func copyAndInject(r io.ReaderAt, w io.Writer, offset, size uint64, value string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the remainder of the file
|
// Write the remainder of the file
|
||||||
pos := int64(offset + size)
|
pos := int64(offset) + int64(len(buf))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_, err = io.Copy(w, io.NewSectionReader(r, pos, 1<<63-1-pos))
|
_, err = io.Copy(w, io.NewSectionReader(r, pos, 1<<63-1-pos))
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,32 +23,23 @@ import (
|
||||||
func TestCopyAndInject(t *testing.T) {
|
func TestCopyAndInject(t *testing.T) {
|
||||||
s := "abcdefghijklmnopqrstuvwxyz"
|
s := "abcdefghijklmnopqrstuvwxyz"
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
offset, size uint64
|
offset uint64
|
||||||
value string
|
buf string
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
offset: 0,
|
offset: 0,
|
||||||
size: 1,
|
buf: "A",
|
||||||
value: "A",
|
|
||||||
expected: "Abcdefghijklmnopqrstuvwxyz",
|
expected: "Abcdefghijklmnopqrstuvwxyz",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
offset: 1,
|
offset: 1,
|
||||||
size: 1,
|
buf: "B",
|
||||||
value: "B",
|
|
||||||
expected: "aBcdefghijklmnopqrstuvwxyz",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
offset: 1,
|
|
||||||
size: 1,
|
|
||||||
value: "BCD",
|
|
||||||
expected: "aBcdefghijklmnopqrstuvwxyz",
|
expected: "aBcdefghijklmnopqrstuvwxyz",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
offset: 25,
|
offset: 25,
|
||||||
size: 1,
|
buf: "Z",
|
||||||
value: "Z",
|
|
||||||
expected: "abcdefghijklmnopqrstuvwxyZ",
|
expected: "abcdefghijklmnopqrstuvwxyZ",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -57,7 +48,7 @@ func TestCopyAndInject(t *testing.T) {
|
||||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
in := bytes.NewReader([]byte(s))
|
in := bytes.NewReader([]byte(s))
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
copyAndInject(in, out, testCase.offset, testCase.size, testCase.value)
|
copyAndInject(in, out, testCase.offset, []byte(testCase.buf))
|
||||||
|
|
||||||
if out.String() != testCase.expected {
|
if out.String() != testCase.expected {
|
||||||
t.Errorf("expected %s, got %s", testCase.expected, out.String())
|
t.Errorf("expected %s, got %s", testCase.expected, out.String())
|
||||||
|
|
Loading…
Reference in New Issue