mirror of
https://github.com/garraflavatra/go-fmp.git
synced 2025-07-09 00:14:05 +00:00
Parse doubly linked sectors in order
This commit is contained in:
@ -6,6 +6,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *FmpFile) ToDebugFile(fname string) {
|
func (f *FmpFile) ToDebugFile(fname string) {
|
||||||
|
f_sectors, err := os.Create(fname + ".sectors")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := f_sectors.Close(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
for _, sect := range f.Sectors {
|
||||||
|
fmt.Fprintf(f_sectors, "%s\n", sect.String())
|
||||||
|
}
|
||||||
|
|
||||||
f_chunks, err := os.Create(fname + ".chunks")
|
f_chunks, err := os.Create(fname + ".chunks")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -31,8 +44,12 @@ func (f *FmpFile) ToDebugFile(fname string) {
|
|||||||
fmt.Fprint(f_dicts, f.Dictionary.String())
|
fmt.Fprint(f_dicts, f.Dictionary.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sect *FmpSector) String() string {
|
||||||
|
return fmt.Sprintf("<Sector id=%v del=%v, lev=%v, prevID=%v, nextID=%v>", sect.ID, sect.Deleted, sect.Level, sect.PrevID, sect.NextID)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *FmpChunk) String() string {
|
func (c *FmpChunk) String() string {
|
||||||
return fmt.Sprintf("<%v(%v)>", c.Type, c.Length)
|
return fmt.Sprintf("<Chunk type=%v len=%v>", c.Type, c.Length)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dict *FmpDict) string(parentPath string) string {
|
func (dict *FmpDict) string(parentPath string) string {
|
||||||
|
@ -2,7 +2,6 @@ package fmp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -10,13 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
sectorSize = 4096
|
||||||
|
sectorHeaderSize = 20
|
||||||
|
sectorPayloadSize = sectorSize - sectorHeaderSize
|
||||||
|
|
||||||
magicSequence = "\x00\x01\x00\x00\x00\x02\x00\x01\x00\x05\x00\x02\x00\x02\xC0"
|
magicSequence = "\x00\x01\x00\x00\x00\x02\x00\x01\x00\x05\x00\x02\x00\x02\xC0"
|
||||||
hbamSequence = "HBAM7"
|
hbamSequence = "HBAM7"
|
||||||
|
|
||||||
magicSize = len(magicSequence)
|
headerSize = sectorSize
|
||||||
hbamSize = len(hbamSequence)
|
magicSize = len(magicSequence)
|
||||||
sectorSize = 4096
|
hbamSize = len(hbamSequence)
|
||||||
sectorHeaderSize = 20
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func OpenFile(path string) (*FmpFile, error) {
|
func OpenFile(path string) (*FmpFile, error) {
|
||||||
@ -37,12 +39,11 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.FileSize = uint(info.Size())
|
ctx.FileSize = uint(info.Size())
|
||||||
ctx.numSectors = ctx.FileSize / sectorSize
|
ctx.numSectors = uint64((ctx.FileSize / sectorSize) - 1)
|
||||||
ctx.Sectors = make([]*FmpSector, ctx.numSectors)
|
ctx.Sectors = make([]*FmpSector, 0)
|
||||||
|
|
||||||
currentPath := make([]uint64, 0)
|
currentPath := make([]uint64, 0)
|
||||||
|
|
||||||
for i := uint(0); i < ctx.numSectors; i++ {
|
for {
|
||||||
sector, err := ctx.readSector()
|
sector, err := ctx.readSector()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
@ -50,7 +51,7 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ctx.Sectors[i] = sector
|
ctx.Sectors = append(ctx.Sectors, sector)
|
||||||
ctx.Chunks = append(ctx.Chunks, sector.Chunks...)
|
ctx.Chunks = append(ctx.Chunks, sector.Chunks...)
|
||||||
|
|
||||||
for _, chunk := range sector.Chunks {
|
for _, chunk := range sector.Chunks {
|
||||||
@ -89,13 +90,22 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *FmpFile) readHeader() error {
|
func (ctx *FmpFile) readHeader() error {
|
||||||
buf := make([]byte, sectorSize)
|
buf := make([]byte, headerSize)
|
||||||
_, err := ctx.stream.Read(buf)
|
_, err := ctx.stream.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -130,26 +140,30 @@ func (ctx *FmpFile) readSector() (*FmpSector, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sector := &FmpSector{
|
sector := &FmpSector{
|
||||||
Deleted: buf[0] != 0,
|
ID: ctx.currentSectorID,
|
||||||
Level: uint8(buf[1]),
|
Deleted: buf[0] > 0,
|
||||||
PrevSectorID: parseVarUint64(buf[2:6]),
|
Level: uint8(buf[1]),
|
||||||
NextSectorID: parseVarUint64(buf[6:10]),
|
PrevID: parseVarUint64(buf[4 : 4+4]),
|
||||||
|
NextID: parseVarUint64(buf[8 : 8+4]),
|
||||||
}
|
}
|
||||||
|
|
||||||
payload := make([]byte, sectorSize-sectorHeaderSize)
|
payload := make([]byte, sectorPayloadSize)
|
||||||
n, err = ctx.stream.Read(payload)
|
n, err = ctx.stream.Read(payload)
|
||||||
if n != sectorSize-sectorHeaderSize {
|
|
||||||
|
if n != sectorPayloadSize {
|
||||||
return nil, ErrRead
|
return nil, ErrRead
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrRead
|
return nil, ErrRead
|
||||||
}
|
}
|
||||||
|
|
||||||
sector.Chunks = make([]*FmpChunk, 0)
|
sector.Chunks = make([]*FmpChunk, 0)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
chunk, err := ctx.readChunk(payload)
|
chunk, err := ctx.readChunk(payload)
|
||||||
|
fmt.Printf("0x%02X", payload[0])
|
||||||
if chunk != nil {
|
if chunk != nil {
|
||||||
fmt.Printf("%s - (type%v)\n", hex.EncodeToString([]byte{payload[0]}), int(chunk.Type))
|
fmt.Printf(" (type %v)\n", int(chunk.Type))
|
||||||
}
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
|
@ -10,8 +10,8 @@ func TestOpenFile(t *testing.T) {
|
|||||||
if f.FileSize != 393216 {
|
if f.FileSize != 393216 {
|
||||||
t.Errorf("expected file size to be 393216, got %d", f.FileSize)
|
t.Errorf("expected file size to be 393216, got %d", f.FileSize)
|
||||||
}
|
}
|
||||||
if f.numSectors != 96 {
|
if f.numSectors != 95 {
|
||||||
t.Errorf("expected number of sectors to be 96, got %d", f.numSectors)
|
t.Errorf("expected number of sectors to be 95, got %d", f.numSectors)
|
||||||
}
|
}
|
||||||
if f.CreatorName != "Pro 12.0" {
|
if f.CreatorName != "Pro 12.0" {
|
||||||
t.Errorf("expected application name to be 'Pro 12.0', got '%s'", f.CreatorName)
|
t.Errorf("expected application name to be 'Pro 12.0', got '%s'", f.CreatorName)
|
||||||
|
@ -13,16 +13,21 @@ type FmpFile struct {
|
|||||||
Chunks []*FmpChunk
|
Chunks []*FmpChunk
|
||||||
Dictionary *FmpDict
|
Dictionary *FmpDict
|
||||||
|
|
||||||
numSectors uint
|
numSectors uint64 // Excludes the header sector
|
||||||
stream io.ReadSeeker
|
currentSectorID uint64
|
||||||
|
|
||||||
|
stream io.ReadSeeker
|
||||||
}
|
}
|
||||||
|
|
||||||
type FmpSector struct {
|
type FmpSector struct {
|
||||||
Deleted bool
|
ID uint64
|
||||||
Level uint8
|
Level uint8
|
||||||
PrevSectorID uint64
|
Deleted bool
|
||||||
NextSectorID uint64
|
PrevID uint64
|
||||||
Chunks []*FmpChunk
|
NextID uint64
|
||||||
|
Prev *FmpSector
|
||||||
|
Next *FmpSector
|
||||||
|
Chunks []*FmpChunk
|
||||||
}
|
}
|
||||||
|
|
||||||
type FmpChunk struct {
|
type FmpChunk struct {
|
||||||
|
Reference in New Issue
Block a user