mirror of
https://github.com/garraflavatra/go-fmp.git
synced 2025-06-28 04:25:11 +00:00
Add sector reader
This commit is contained in:
37
fmp/fmp.go
37
fmp/fmp.go
@ -2,6 +2,7 @@ package fmp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -17,12 +18,28 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FmpFile struct {
|
type FmpFile struct {
|
||||||
Stream io.ReadSeeker
|
Stream io.ReadSeeker
|
||||||
|
|
||||||
|
FileSize uint
|
||||||
|
NumSectors uint
|
||||||
|
|
||||||
VersionDate time.Time
|
VersionDate time.Time
|
||||||
ApplicationName string
|
ApplicationName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FmpSector struct {
|
||||||
|
Deleted bool
|
||||||
|
Level uint8
|
||||||
|
PrevSectorID uint32
|
||||||
|
NextSectorID uint32
|
||||||
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
func OpenFile(path string) (*FmpFile, error) {
|
func OpenFile(path string) (*FmpFile, error) {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
stream, err := os.Open(path)
|
stream, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if stream != nil {
|
if stream != nil {
|
||||||
@ -35,6 +52,8 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
stream.Close()
|
stream.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
ctx.FileSize = uint(info.Size())
|
||||||
|
ctx.NumSectors = ctx.FileSize / sectorSize
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,3 +80,19 @@ func (ctx *FmpFile) readHeader() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *FmpFile) readSector() (*FmpSector, error) {
|
||||||
|
buf := make([]byte, sectorSize)
|
||||||
|
_, err := ctx.Stream.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrRead
|
||||||
|
}
|
||||||
|
sector := &FmpSector{
|
||||||
|
Deleted: buf[0] != 0,
|
||||||
|
Level: uint8(buf[1]),
|
||||||
|
PrevSectorID: binary.BigEndian.Uint32(buf[2:6]),
|
||||||
|
NextSectorID: binary.BigEndian.Uint32(buf[6:10]),
|
||||||
|
Payload: buf[6:4076],
|
||||||
|
}
|
||||||
|
return sector, nil
|
||||||
|
}
|
||||||
|
@ -7,6 +7,12 @@ func TestOpenFile(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if f.FileSize != 393216 {
|
||||||
|
t.Errorf("expected file size to be 393216, got %d", f.FileSize)
|
||||||
|
}
|
||||||
|
if f.NumSectors != 96 {
|
||||||
|
t.Errorf("expected number of sectors to be 96, got %d", f.NumSectors)
|
||||||
|
}
|
||||||
if f.ApplicationName != "Pro 12.0" {
|
if f.ApplicationName != "Pro 12.0" {
|
||||||
t.Errorf("expected application name to be 'Pro 12.0', got '%s'", f.ApplicationName)
|
t.Errorf("expected application name to be 'Pro 12.0', got '%s'", f.ApplicationName)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user