Skip to content

Commit

Permalink
add: add header checks in hfe reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Le Saux committed Oct 16, 2023
1 parent 2af2fb9 commit 2c2a88b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 29 additions & 1 deletion hfe/hfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

var (
NoHFEFormatFound = errors.New("no hfe format found")
NoHFEFormatFound = errors.New("no hfe format found")
HFERevisionUnsupported = errors.New("hfe revision format no supported")
)

type PicFileFormatHeader struct {
Expand Down Expand Up @@ -51,6 +52,26 @@ const (
DISABLE_FLOPPYMODE FloppyInterfaceMode = 0xFE
)

var floppyInterfaceModes map[FloppyInterfaceMode]string = map[FloppyInterfaceMode]string{
IBMPC_DD_FLOPPYMODE: "IBMPC_DD_FLOPPYMODE",
IBMPC_HD_FLOPPYMODE: "IBMPC_HD_FLOPPYMODE",
ATARIST_DD_FLOPPYMODE: "ATARIST_DD_FLOPPYMODE",
ATARIST_HD_FLOPPYMODE: "ATARIST_HD_FLOPPYMODE",
CPC_DD_FLOPPYMODE: "CPC_DD_FLOPPYMODE",
GENERIC_SHUGGART_DD_FLOPPYMODE: "GENERIC_SHUGGART_DD_FLOPPYMODE",
IBMPC_ED_FLOPPYMODE: "IBMPC_ED_FLOPPYMODE",
MSX2_DD_FLOPPYMODE: "MSX2_DD_FLOPPYMODE",
C64_DD_FLOPPYMODE: "C64_DD_FLOPPYMODE",
EMU_SHUGART_FLOPPYMODE: "EMU_SHUGART_FLOPPYMODE",
S950_DD_FLOPPYMODE: "S950_DD_FLOPPYMODE",
S950_HD_FLOPPYMODE: "S950_HD_FLOPPYMODE",
DISABLE_FLOPPYMODE: "DISABLE_FLOPPYMODE",
}

func String(f FloppyInterfaceMode) string {
return floppyInterfaceModes[f]
}

type TrackEncoding = uint8

const (
Expand Down Expand Up @@ -99,6 +120,13 @@ func ReadHeader(r io.Reader) (PicFileFormatHeader, error) {
return PicFileFormatHeader{}, NoHFEFormatFound
}

if h.FormatRevision > 3 {
return PicFileFormatHeader{}, HFERevisionUnsupported
}

if h.NbSide < 1 || h.NbSide > 2 || h.NbTracks == 0 || h.BitRate == 0 {
return PicFileFormatHeader{}, HFERevisionUnsupported
}
return h, nil
}

Expand Down
3 changes: 3 additions & 0 deletions hfe/hfe_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hfe_test

import (
"fmt"
"os"
"testing"

Expand All @@ -26,6 +27,8 @@ func TestReadHfe(t *testing.T) {
h, err := hfe.Read(f)
assert.NoError(t, err)
assert.NotEmpty(t, h)
assert.Equal(t, hfe.CPC_DD_FLOPPYMODE, h.Header.FloppyInterfaceMode)
fmt.Printf("format:%v\n", hfe.String(h.Header.FloppyInterfaceMode))
})

}

0 comments on commit 2c2a88b

Please sign in to comment.