2025-06-11 16:01:49 +02:00
|
|
|
package fmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2025-06-11 16:19:31 +02:00
|
|
|
"encoding/binary"
|
2025-06-11 16:01:49 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
magicSequence = "\x00\x01\x00\x00\x00\x02\x00\x01\x00\x05\x00\x02\x00\x02\xC0"
|
|
|
|
hbamSequence = "HBAM7"
|
|
|
|
|
2025-06-11 21:52:52 +02:00
|
|
|
magicSize = len(magicSequence)
|
|
|
|
hbamSize = len(hbamSequence)
|
|
|
|
sectorSize = 4096
|
|
|
|
sectorHeaderSize = 20
|
2025-06-11 16:01:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type FmpFile struct {
|
2025-06-11 21:52:52 +02:00
|
|
|
VersionDate time.Time
|
|
|
|
CreatorName string
|
|
|
|
FileSize uint
|
|
|
|
NumSectors uint
|
|
|
|
Stream io.ReadSeeker
|
|
|
|
Sectors []*FmpSector
|
2025-06-12 09:27:47 +02:00
|
|
|
Chunks []*FmpChunk
|
2025-06-11 16:01:49 +02:00
|
|
|
}
|
|
|
|
|
2025-06-11 16:19:31 +02:00
|
|
|
type FmpSector struct {
|
|
|
|
Deleted bool
|
|
|
|
Level uint8
|
|
|
|
PrevSectorID uint32
|
|
|
|
NextSectorID uint32
|
2025-06-11 21:52:52 +02:00
|
|
|
Chunks []*FmpChunk
|
2025-06-11 16:19:31 +02:00
|
|
|
}
|
|
|
|
|
2025-06-11 16:01:49 +02:00
|
|
|
func OpenFile(path string) (*FmpFile, error) {
|
2025-06-11 16:19:31 +02:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
|
2025-06-11 16:01:49 +02:00
|
|
|
stream, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
defer stream.Close()
|
|
|
|
|
2025-06-11 16:01:49 +02:00
|
|
|
ctx := &FmpFile{Stream: stream}
|
|
|
|
if err := ctx.readHeader(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
|
2025-06-11 16:19:31 +02:00
|
|
|
ctx.FileSize = uint(info.Size())
|
|
|
|
ctx.NumSectors = ctx.FileSize / sectorSize
|
2025-06-11 21:52:52 +02:00
|
|
|
ctx.Sectors = make([]*FmpSector, ctx.NumSectors)
|
|
|
|
|
|
|
|
for i := uint(0); i < ctx.NumSectors; i++ {
|
|
|
|
sector, err := ctx.readSector()
|
2025-06-12 09:22:32 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx.Sectors[i] = sector
|
2025-06-12 09:27:47 +02:00
|
|
|
ctx.Chunks = append(ctx.Chunks, sector.Chunks...)
|
2025-06-11 21:52:52 +02:00
|
|
|
}
|
|
|
|
|
2025-06-11 16:01:49 +02:00
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *FmpFile) readHeader() error {
|
|
|
|
buf := make([]byte, sectorSize)
|
|
|
|
_, err := ctx.Stream.Read(buf)
|
|
|
|
if err != nil {
|
2025-06-11 21:52:52 +02:00
|
|
|
return err
|
2025-06-11 16:01:49 +02:00
|
|
|
}
|
|
|
|
if !bytes.Equal(buf[:magicSize], []byte(magicSequence)) {
|
|
|
|
return ErrBadMagic
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf[magicSize:magicSize+hbamSize], []byte(hbamSequence)) {
|
|
|
|
return ErrBadMagic
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.VersionDate, err = time.Parse("06JAN02", string(buf[531:538]))
|
|
|
|
if err != nil {
|
|
|
|
return ErrBadHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
appNameLength := int(buf[541])
|
2025-06-11 21:52:52 +02:00
|
|
|
ctx.CreatorName = string(buf[542 : 542+appNameLength])
|
2025-06-11 16:01:49 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2025-06-11 16:19:31 +02:00
|
|
|
|
|
|
|
func (ctx *FmpFile) readSector() (*FmpSector, error) {
|
2025-06-11 21:52:52 +02:00
|
|
|
buf := make([]byte, sectorHeaderSize)
|
|
|
|
n, err := ctx.Stream.Read(buf)
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2025-06-11 16:19:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrRead
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
|
2025-06-11 16:19:31 +02:00
|
|
|
sector := &FmpSector{
|
|
|
|
Deleted: buf[0] != 0,
|
|
|
|
Level: uint8(buf[1]),
|
|
|
|
PrevSectorID: binary.BigEndian.Uint32(buf[2:6]),
|
|
|
|
NextSectorID: binary.BigEndian.Uint32(buf[6:10]),
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
|
|
|
|
payload := make([]byte, sectorSize-sectorHeaderSize)
|
|
|
|
n, err = ctx.Stream.Read(payload)
|
|
|
|
if n != sectorSize-sectorHeaderSize {
|
|
|
|
return nil, ErrRead
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrRead
|
|
|
|
}
|
|
|
|
sector.Chunks = make([]*FmpChunk, 0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
chunk, err := ctx.readChunk(payload)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if chunk == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if chunk.Length == 0 {
|
|
|
|
panic("chunk length not set")
|
|
|
|
}
|
2025-06-12 09:27:47 +02:00
|
|
|
sector.Chunks = append(sector.Chunks, chunk)
|
2025-06-12 08:58:23 +02:00
|
|
|
payload = payload[min(chunk.Length, uint32(len(payload))):]
|
2025-06-12 09:22:32 +02:00
|
|
|
if len(payload) == 0 || (len(payload) == 1 && payload[0] == 0x00) {
|
2025-06-12 08:58:23 +02:00
|
|
|
break
|
|
|
|
}
|
2025-06-11 21:52:52 +02:00
|
|
|
}
|
|
|
|
|
2025-06-11 16:19:31 +02:00
|
|
|
return sector, nil
|
|
|
|
}
|