1
0
mirror of https://github.com/garraflavatra/go-fmp.git synced 2025-06-28 04:25:11 +00:00
Files
go-fmp/fmp/fmp_file.go

171 lines
3.4 KiB
Go
Raw Normal View History

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
)
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-12 10:43:02 +02:00
ctx := &FmpFile{Stream: stream, Dictionary: &FmpDict{}}
2025-06-11 16:01:49 +02:00
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)
2025-06-12 10:43:02 +02:00
currentPath := make([]uint16, 0)
2025-06-11 21:52:52 +02:00
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-12 10:43:02 +02:00
for _, chunk := range sector.Chunks {
switch chunk.Type {
case FMP_CHUNK_PATH_PUSH:
currentPath = append(currentPath, uint16(chunk.Value[0]))
case FMP_CHUNK_PATH_POP:
if len(currentPath) > 0 {
currentPath = currentPath[:len(currentPath)-1]
}
case FMP_CHUNK_SIMPLE_DATA:
ctx.Dictionary.set(currentPath, chunk.Value)
case FMP_CHUNK_SEGMENTED_DATA:
// Todo: take index into account
ctx.Dictionary.set(
currentPath,
2025-06-12 11:44:57 +02:00
append(ctx.Dictionary.getValue(currentPath), chunk.Value...),
2025-06-12 10:43:02 +02:00
)
case FMP_CHUNK_SIMPLE_KEY_VALUE:
ctx.Dictionary.set(
append(currentPath, uint16(chunk.Key)),
chunk.Value,
)
case FMP_CHUNK_LONG_KEY_VALUE:
ctx.Dictionary.set(
append(currentPath, uint16(chunk.Key)), // todo: ??
chunk.Value,
)
case FMP_CHUNK_NOOP:
// noop
}
}
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
}