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

138 lines
2.7 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
)
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-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-13 20:40:34 +02:00
err = sector.processChunks(ctx.Dictionary, &currentPath)
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-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-13 22:17:32 +02:00
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-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
}