Skip to content

Commit

Permalink
Added magic number to binary encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Oct 3, 2024
1 parent 05c9574 commit dfc362c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/arduino/libraries/librarieslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package libraries

import (
"encoding/binary"
"errors"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -45,7 +46,18 @@ func (list *List) Add(libs ...*Library) {
}
}

func (list *List) binaryMagicNumber() uint32 {
return 0xAD000001
}

func (list *List) UnmarshalBinary(in io.Reader, prefix *paths.Path) error {
var magic uint32
if err := binary.Read(in, binary.NativeEndian, &magic); err != nil {
return err
}
if magic != list.binaryMagicNumber() {
return errors.New("invalid cache version")
}
var n int32
if err := binary.Read(in, binary.NativeEndian, &n); err != nil {
return err
Expand All @@ -63,6 +75,9 @@ func (list *List) UnmarshalBinary(in io.Reader, prefix *paths.Path) error {
}

func (list *List) MarshalBinary(out io.Writer, prefix *paths.Path) error {
if err := binary.Write(out, binary.NativeEndian, list.binaryMagicNumber()); err != nil {
return err
}
if err := binary.Write(out, binary.NativeEndian, int32(len(*list))); err != nil {
return err
}
Expand Down

0 comments on commit dfc362c

Please sign in to comment.