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

211 lines
4.4 KiB
Go
Raw Normal View History

2025-06-11 16:01:49 +02:00
package fmp
import (
"bytes"
"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
)
2025-06-14 16:28:41 +02:00
type FmpFile struct {
VersionDate time.Time
CreatorName string
FileSize uint
Sectors []*FmpSector
Chunks []*FmpChunk
Dictionary *FmpDict
2025-06-22 19:14:22 +02:00
tables []*FmpTable
2025-06-14 16:28:41 +02:00
numSectors uint64 // Excludes the header sector
currentSectorID uint64
stream io.ReadSeeker
}
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)
2025-06-13 22:17:32 +02:00
ctx.stream.Seek(2*sectorSize, io.SeekStart)
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 19:54:17 +02:00
2025-06-13 12:09:39 +02:00
ctx.Sectors = append(ctx.Sectors, sector)
2025-06-13 20:40:34 +02:00
2025-06-13 19:54:17 +02:00
if sector.ID != 0 {
2025-06-14 15:19:36 +02:00
err = sector.processChunks(ctx.Dictionary)
2025-06-13 20:40:34 +02:00
if err != nil {
return nil, err
}
2025-06-13 19:54:17 +02:00
ctx.Chunks = append(ctx.Chunks, sector.Chunks...)
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-22 19:14:22 +02:00
ctx.readTables()
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) {
2025-06-14 14:24:35 +02:00
debug("---------- Reading sector %d", 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]),
2025-06-22 19:14:22 +02:00
PrevID: decodeVarUint64(buf[4 : 4+4]),
NextID: decodeVarUint64(buf[8 : 8+4]),
2025-06-13 19:54:17 +02:00
Chunks: make([]*FmpChunk, 0),
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 19:54:17 +02:00
sector.Payload = make([]byte, sectorPayloadSize)
n, err = ctx.stream.Read(sector.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-11 16:19:31 +02:00
return sector, nil
}
2025-06-22 19:14:22 +02:00
func (ctx *FmpFile) readTables() {
tables := make([]*FmpTable, 0)
ent := ctx.Dictionary.GetEntry(3, 16, 5)
for path, tableEnt := range *ent.Children {
if path < 128 {
continue
}
table := &FmpTable{
ID: path,
Name: decodeString(tableEnt.Children.GetValue(16)),
Columns: map[uint64]*FmpColumn{},
Records: map[uint64]*FmpRecord{},
}
tables = append(tables, table)
for colPath, colEnt := range *ctx.Dictionary.GetChildren(table.ID, 3, 5) {
name := decodeString(colEnt.Children.GetValue(16))
flags := colEnt.Children.GetValue(2)
column := &FmpColumn{
Index: colPath,
Name: name,
Type: FmpFieldType(flags[0]),
DataType: FmpDataType(flags[1]),
StorageType: FmpFieldStorageType(flags[9]),
Repetitions: flags[25],
Indexed: flags[8] == 128,
}
if flags[11] == 1 {
column.AutoEnter = autoEnterPresetMap[flags[4]]
} else {
column.AutoEnter = autoEnterOptionMap[flags[11]]
}
table.Columns[column.Index] = column
}
for recPath, recEnt := range *ctx.Dictionary.GetChildren(table.ID, 5) {
record := &FmpRecord{Index: recPath, Values: make(map[uint64]string)}
table.Records[record.Index] = record
if recPath > table.lastRecordID {
table.lastRecordID = recPath
}
for colIndex, value := range *recEnt.Children {
record.Values[colIndex] = decodeString(value.Value)
}
}
}
ctx.tables = tables
}