make sure set bytes are not longer than byte field

* also little formatting issues
This commit is contained in:
gutmet 2020-04-27 17:58:34 +02:00
parent 6969cc9d96
commit 69114e6782

View File

@ -175,6 +175,9 @@ func (part *filePart) setHandling() {
writeTo := func(w io.Writer, bo binary.ByteOrder, s string) { writeTo := func(w io.Writer, bo binary.ByteOrder, s string) {
buf, err := hex.DecodeString(s) buf, err := hex.DecodeString(s)
optPanic("invalid string of hex values: "+s, err) optPanic("invalid string of hex values: "+s, err)
if int64(len(buf)) > bytes {
panic(fmt.Sprintf("string %s is too long for byte array %s : want %d bytes", s, part.name, bytes))
}
binaryWrite(w, bo, buf) binaryWrite(w, bo, buf)
} }
part.handling = handling{bytes, readFrom, writeTo} part.handling = handling{bytes, readFrom, writeTo}
@ -305,6 +308,18 @@ func backup(binpath string) {
fmt.Println("Created backup " + backuppath + "\n") fmt.Println("Created backup " + backuppath + "\n")
} }
func seekErr(i int, name string, inner error) string {
if inner != nil {
namepart := ""
if name != "" {
namepart = " name: " + name
}
return fmt.Sprintf("could not advance in file (stuck at part %d%s)", i, namepart)
} else {
return ""
}
}
func setValues(descr fileDescription, binpath string, vals map[string]string) { func setValues(descr fileDescription, binpath string, vals map[string]string) {
backup(binpath) backup(binpath)
f, err := os.OpenFile(binpath, os.O_RDWR, 0666) f, err := os.OpenFile(binpath, os.O_RDWR, 0666)
@ -320,7 +335,7 @@ func setValues(descr fileDescription, binpath string, vals map[string]string) {
} }
if needSeek { if needSeek {
_, err = f.Seek(part.handling.bytes, 1) _, err = f.Seek(part.handling.bytes, 1)
optPanic("set: could not advance in file (stuck at part "+strconv.Itoa(i)+"name: "+part.name+")", err) optPanic(seekErr(i, part.name, err), err)
} }
} }
} }
@ -333,7 +348,7 @@ func getValues(descr fileDescription, binpath string) map[string]string {
for i, part := range descr.parts { for i, part := range descr.parts {
if part.name == "" { if part.name == "" {
_, err = f.Seek(part.handling.bytes, 1) _, err = f.Seek(part.handling.bytes, 1)
optPanic("get: could not advance in file (stuck at part "+strconv.Itoa(i)+"name: "+part.name+")", err) optPanic(seekErr(i, part.name, err), err)
} else { } else {
vals[part.name] = part.handling.readFrom(f, descr.byteOrder) vals[part.name] = part.handling.readFrom(f, descr.byteOrder)
} }
@ -341,7 +356,7 @@ func getValues(descr fileDescription, binpath string) map[string]string {
return vals return vals
} }
func padding(descr fileDescription) string { func fmtPadding(descr fileDescription) string {
pad := 0 pad := 0
for _, part := range descr.parts { for _, part := range descr.parts {
if n := len(part.name); n > pad { if n := len(part.name); n > pad {
@ -354,7 +369,7 @@ func padding(descr fileDescription) string {
func printValues(descr fileDescription, vals map[string]string) { func printValues(descr fileDescription, vals map[string]string) {
for _, part := range descr.parts { for _, part := range descr.parts {
if part.name != "" { if part.name != "" {
fmt.Printf("%"+padding(descr)+"s = %s\n", part.name, vals[part.name]) fmt.Printf("%"+fmtPadding(descr)+"s = %s\n", part.name, vals[part.name])
} }
} }
} }