Prevent multiple newlines being emitted in encoding, newlines added as extra data in decoding

This commit is contained in:
DataHoarder 2023-10-10 08:25:19 +02:00
parent b4e6435498
commit 44ed702c86
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 7 additions and 4 deletions

View file

@ -26,7 +26,10 @@ func Decode(data []byte) (stream *Stream, err error) {
}
if d[0] != ':' {
currentRegion.Extra = append(currentRegion.Extra, strings.Trim(d, "\r\n"))
extra := strings.Trim(d, "\r\n")
if len(extra) > 0 {
currentRegion.Extra = append(currentRegion.Extra, extra)
}
continue
}

View file

@ -10,7 +10,7 @@ func Encode(stream *Stream) (hex []byte, err error) {
var entries []string
emitRecord := func(r Record) {
entries = append(entries, r.String()+"\r\n")
entries = append(entries, r.String())
}
emitAddress := func(addr uint32) error {
@ -43,7 +43,7 @@ func Encode(stream *Stream) (hex []byte, err error) {
// Add extra entries as needed
for _, e := range r.Extra {
entries = append(entries, e+"\r\n")
entries = append(entries, e)
}
endAddress := r.Address + uint32(len(r.Data))

View file

@ -41,7 +41,7 @@ func (r Record) String() string {
buf[3] = uint8(r.Code)
copy(buf[4:], r.Data)
buf[len(buf)-1] = Checksum(buf[:len(buf)-1])
return ":" + strings.ToUpper(hex.EncodeToString(buf)) + "\r\n"
return ":" + strings.ToUpper(hex.EncodeToString(buf))
}
func RecordFromString(data string) (Record, error) {