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

202 lines
4.1 KiB
Go
Raw Normal View History

2025-06-11 16:01:49 +02:00
package fmp
import (
"bytes"
2025-06-12 21:21:55 +02:00
"fmt"
2025-06-11 16:01:49 +02:00
"io"
"os"
"time"
)
const (
2025-06-13 12:09:39 +02:00
sectorSize = 4096
sectorHeaderSize = 20
sectorPayloadSize = sectorSize - sectorHeaderSize
2025-06-11 16:01:49 +02:00
magicSequence = "\x00\x01\x00\x00\x00\x02\x00\x01\x00\x05\x00\x02\x00\x02\xC0"
hbamSequence = "HBAM7"
2025-06-13 12:09:39 +02:00
headerSize = sectorSize
magicSize = len(magicSequence)
hbamSize = len(hbamSequence)
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 21:21:55 +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())
2025-06-13 12:09:39 +02:00
ctx.numSectors = uint64((ctx.FileSize / sectorSize) - 1)
ctx.Sectors = make([]*FmpSector, 0)
currentPath := make([]uint64, 0)
2025-06-12 10:43:02 +02:00
2025-06-13 12:09:39 +02:00
for {
2025-06-11 21:52:52 +02:00
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
}
2025-06-13 12:09:39 +02:00
ctx.Sectors = append(ctx.Sectors, 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, uint64(chunk.Value[0]))
2025-06-12 10:43:02 +02:00
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, uint64(chunk.Key)),
2025-06-12 10:43:02 +02:00
chunk.Value,
)
case FMP_CHUNK_LONG_KEY_VALUE:
ctx.Dictionary.set(
append(currentPath, uint64(chunk.Key)), // todo: ??
2025-06-12 10:43:02 +02:00
chunk.Value,
)
case FMP_CHUNK_NOOP:
// noop
}
if chunk.Delayed {
if len(currentPath) == 0 {
println("warning: delayed pop without path")
} else {
currentPath = currentPath[:len(currentPath)-1]
}
}
2025-06-12 10:43:02 +02:00
}
2025-06-13 12:09:39 +02:00
ctx.currentSectorID = sector.NextID
if sector.NextID == 0 {
break
} else if sector.NextID > ctx.numSectors {
return nil, ErrBadHeader
} else {
ctx.stream.Seek(int64(sector.NextID*sectorSize), 0)
}
2025-06-11 21:52:52 +02:00
}
2025-06-11 16:01:49 +02:00
return ctx, nil
}
func (ctx *FmpFile) readHeader() error {
2025-06-13 12:09:39 +02:00
buf := make([]byte, headerSize)
2025-06-12 21:21:55 +02:00
_, err := ctx.stream.Read(buf)
2025-06-11 16:01:49 +02:00
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) {
println("------- Reading sector", ctx.currentSectorID)
2025-06-11 21:52:52 +02:00
buf := make([]byte, sectorHeaderSize)
2025-06-12 21:21:55 +02:00
n, err := ctx.stream.Read(buf)
2025-06-11 21:52:52 +02:00
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{
2025-06-13 12:09:39 +02:00
ID: ctx.currentSectorID,
Deleted: buf[0] > 0,
Level: uint8(buf[1]),
PrevID: parseVarUint64(buf[4 : 4+4]),
NextID: parseVarUint64(buf[8 : 8+4]),
2025-06-11 16:19:31 +02:00
}
2025-06-11 21:52:52 +02:00
2025-06-13 12:19:40 +02:00
if ctx.currentSectorID == 0 && sector.PrevID > 0 {
return nil, ErrBadSectorHeader
}
2025-06-13 12:09:39 +02:00
payload := make([]byte, sectorPayloadSize)
2025-06-12 21:21:55 +02:00
n, err = ctx.stream.Read(payload)
2025-06-13 12:09:39 +02:00
if n != sectorPayloadSize {
2025-06-11 21:52:52 +02:00
return nil, ErrRead
}
if err != nil {
return nil, ErrRead
}
2025-06-13 12:09:39 +02:00
2025-06-11 21:52:52 +02:00
sector.Chunks = make([]*FmpChunk, 0)
for {
chunk, err := ctx.readChunk(payload)
2025-06-13 12:09:39 +02:00
fmt.Printf("0x%02X", payload[0])
2025-06-12 21:21:55 +02:00
if chunk != nil {
2025-06-13 12:09:39 +02:00
fmt.Printf(" (type %v)\n", int(chunk.Type))
2025-06-12 21:21:55 +02:00
}
2025-06-11 21:52:52 +02:00
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)
payload = payload[min(chunk.Length, uint64(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
}