mirror of
https://github.com/garraflavatra/go-fmp.git
synced 2025-06-28 12:35:12 +00:00
Store everything in uint64 to avoid overflow
This commit is contained in:
@ -1,9 +1,5 @@
|
|||||||
package fmp
|
package fmp
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
||||||
|
|
||||||
// Simple data
|
// Simple data
|
||||||
@ -34,7 +30,7 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_DATA,
|
Type: FMP_CHUNK_SIMPLE_DATA,
|
||||||
Value: payload[1 : 1+length],
|
Value: payload[1 : 1+length],
|
||||||
Length: 1 + uint32(length),
|
Length: 1 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if 0x12 <= payload[0] && payload[0] <= 0x15 {
|
if 0x12 <= payload[0] && payload[0] <= 0x15 {
|
||||||
@ -42,7 +38,7 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_DATA,
|
Type: FMP_CHUNK_SIMPLE_DATA,
|
||||||
Value: payload[1 : 1+length],
|
Value: payload[1 : 1+length],
|
||||||
Length: 1 + uint32(length),
|
Length: 1 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if 0x1A <= payload[0] && payload[0] <= 0x1D {
|
if 0x1A <= payload[0] && payload[0] <= 0x1D {
|
||||||
@ -50,7 +46,7 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_DATA,
|
Type: FMP_CHUNK_SIMPLE_DATA,
|
||||||
Value: payload[1 : 1+length],
|
Value: payload[1 : 1+length],
|
||||||
Length: 1 + uint32(length),
|
Length: 1 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +55,7 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
if payload[0] == 0x01 {
|
if payload[0] == 0x01 {
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
||||||
Key: uint32(payload[1]),
|
Key: uint64(payload[1]),
|
||||||
Value: payload[2 : 2+1],
|
Value: payload[2 : 2+1],
|
||||||
Length: 3,
|
Length: 3,
|
||||||
}, nil
|
}, nil
|
||||||
@ -68,24 +64,24 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
length := 2 * (payload[0] - 1)
|
length := 2 * (payload[0] - 1)
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
||||||
Key: uint32(payload[1]),
|
Key: uint64(payload[1]),
|
||||||
Value: payload[2 : 2+length],
|
Value: payload[2 : 2+length],
|
||||||
Length: 2 + uint32(length),
|
Length: 2 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x06 {
|
if payload[0] == 0x06 {
|
||||||
length := payload[2]
|
length := payload[2]
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
||||||
Key: uint32(payload[1]),
|
Key: uint64(payload[1]),
|
||||||
Value: payload[3 : 3+length], // docs say offset 2?
|
Value: payload[3 : 3+length], // docs say offset 2?
|
||||||
Length: 3 + uint32(length),
|
Length: 3 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x09 {
|
if payload[0] == 0x09 {
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
||||||
Key: uint32(binary.BigEndian.Uint16(payload[1:3])),
|
Key: parseVarUint64(payload[1:3]),
|
||||||
Value: payload[3 : 3+1], // docs say offset 2?
|
Value: payload[3 : 3+1], // docs say offset 2?
|
||||||
Length: 4,
|
Length: 4,
|
||||||
}, nil
|
}, nil
|
||||||
@ -94,18 +90,18 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
length := 2 * (payload[0] - 9)
|
length := 2 * (payload[0] - 9)
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
||||||
Key: uint32(binary.BigEndian.Uint16(payload[1:3])),
|
Key: parseVarUint64(payload[1:3]),
|
||||||
Value: payload[3 : 3+length], // docs say offset 2?
|
Value: payload[3 : 3+length], // docs say offset 2?
|
||||||
Length: 2 + uint32(length),
|
Length: 2 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x0E {
|
if payload[0] == 0x0E {
|
||||||
length := payload[2]
|
length := payload[2]
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
Type: FMP_CHUNK_SIMPLE_KEY_VALUE,
|
||||||
Key: uint32(binary.BigEndian.Uint16(payload[1:3])),
|
Key: parseVarUint64(payload[1:3]),
|
||||||
Value: payload[4 : 4+length], // docs say offset 2?
|
Value: payload[4 : 4+length], // docs say offset 2?
|
||||||
Length: 4 + uint32(length),
|
Length: 4 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,18 +111,18 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
length := payload[4]
|
length := payload[4]
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
||||||
Key: parseVarUint32(payload[1 : 1+3]),
|
Key: parseVarUint64(payload[1 : 1+3]),
|
||||||
Value: payload[5 : 5+length],
|
Value: payload[5 : 5+length],
|
||||||
Length: 5 + uint32(length),
|
Length: 5 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x17 {
|
if payload[0] == 0x17 {
|
||||||
length := uint32(binary.BigEndian.Uint16(payload[4 : 4+2]))
|
length := parseVarUint64(payload[4 : 4+2])
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
||||||
Key: uint32(binary.BigEndian.Uint16(payload[1 : 1+2])),
|
Key: parseVarUint64(payload[1 : 1+2]),
|
||||||
Value: payload[6 : 6+length],
|
Value: payload[6 : 6+length],
|
||||||
Length: 6 + uint32(length),
|
Length: 6 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x1E {
|
if payload[0] == 0x1E {
|
||||||
@ -134,40 +130,40 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
valueLength := payload[2+keyLength]
|
valueLength := payload[2+keyLength]
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
||||||
Key: parseVarUint32(payload[2 : 2+keyLength]),
|
Key: parseVarUint64(payload[2 : 2+keyLength]),
|
||||||
Value: payload[2+keyLength+1 : 2+keyLength+1+valueLength],
|
Value: payload[2+keyLength+1 : 2+keyLength+1+valueLength],
|
||||||
Length: 3 + uint32(keyLength) + uint32(valueLength),
|
Length: 3 + uint64(keyLength) + uint64(valueLength),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x1F {
|
if payload[0] == 0x1F {
|
||||||
keyLength := uint32(payload[1])
|
keyLength := uint64(payload[1])
|
||||||
valueLength := parseVarUint32(payload[2+keyLength : 2+keyLength+2+1])
|
valueLength := parseVarUint64(payload[2+keyLength : 2+keyLength+2+1])
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
Type: FMP_CHUNK_LONG_KEY_VALUE,
|
||||||
Key: parseVarUint32(payload[2 : 2+keyLength]),
|
Key: parseVarUint64(payload[2 : 2+keyLength]),
|
||||||
Value: payload[2+keyLength+2 : 2+keyLength+2+valueLength],
|
Value: payload[2+keyLength+2 : 2+keyLength+2+valueLength],
|
||||||
Length: 4 + uint32(keyLength) + uint32(valueLength),
|
Length: 4 + uint64(keyLength) + uint64(valueLength),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Segmented data
|
// Segmented data
|
||||||
|
|
||||||
if payload[0] == 0x07 {
|
if payload[0] == 0x07 {
|
||||||
length := binary.BigEndian.Uint16(payload[2 : 2+2])
|
length := parseVarUint64(payload[2 : 2+2])
|
||||||
payloadLimit := min(4+length, uint16(len(payload)))
|
payloadLimit := min(4+length, uint64(len(payload)))
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SEGMENTED_DATA,
|
Type: FMP_CHUNK_SEGMENTED_DATA,
|
||||||
Index: uint32(payload[1]),
|
Index: uint64(payload[1]),
|
||||||
Value: payload[4:payloadLimit],
|
Value: payload[4:payloadLimit],
|
||||||
Length: 4 + uint32(length),
|
Length: 4 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
if payload[0] == 0x0F {
|
if payload[0] == 0x0F {
|
||||||
length := uint32(binary.BigEndian.Uint16(payload[3 : 3+2]))
|
length := parseVarUint64(payload[3 : 3+2])
|
||||||
payloadLimit := min(5+length, uint32(len(payload)))
|
payloadLimit := min(5+length, uint64(len(payload)))
|
||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_SEGMENTED_DATA,
|
Type: FMP_CHUNK_SEGMENTED_DATA,
|
||||||
Index: uint32(binary.BigEndian.Uint16(payload[1 : 1+2])),
|
Index: parseVarUint64(payload[1 : 1+2]),
|
||||||
Value: payload[5:payloadLimit],
|
Value: payload[5:payloadLimit],
|
||||||
Length: 5 + length,
|
Length: 5 + length,
|
||||||
}, nil
|
}, nil
|
||||||
@ -208,7 +204,7 @@ func (ctx *FmpFile) readChunk(payload []byte) (*FmpChunk, error) {
|
|||||||
return &FmpChunk{
|
return &FmpChunk{
|
||||||
Type: FMP_CHUNK_PATH_PUSH,
|
Type: FMP_CHUNK_PATH_PUSH,
|
||||||
Value: payload[2 : 2+length],
|
Value: payload[2 : 2+length],
|
||||||
Length: 2 + uint32(length),
|
Length: 2 + uint64(length),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package fmp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -39,7 +38,7 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
ctx.NumSectors = ctx.FileSize / sectorSize
|
ctx.NumSectors = ctx.FileSize / sectorSize
|
||||||
ctx.Sectors = make([]*FmpSector, ctx.NumSectors)
|
ctx.Sectors = make([]*FmpSector, ctx.NumSectors)
|
||||||
|
|
||||||
currentPath := make([]uint16, 0)
|
currentPath := make([]uint64, 0)
|
||||||
|
|
||||||
for i := uint(0); i < ctx.NumSectors; i++ {
|
for i := uint(0); i < ctx.NumSectors; i++ {
|
||||||
sector, err := ctx.readSector()
|
sector, err := ctx.readSector()
|
||||||
@ -55,7 +54,7 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
for _, chunk := range sector.Chunks {
|
for _, chunk := range sector.Chunks {
|
||||||
switch chunk.Type {
|
switch chunk.Type {
|
||||||
case FMP_CHUNK_PATH_PUSH:
|
case FMP_CHUNK_PATH_PUSH:
|
||||||
currentPath = append(currentPath, uint16(chunk.Value[0]))
|
currentPath = append(currentPath, uint64(chunk.Value[0]))
|
||||||
|
|
||||||
case FMP_CHUNK_PATH_POP:
|
case FMP_CHUNK_PATH_POP:
|
||||||
if len(currentPath) > 0 {
|
if len(currentPath) > 0 {
|
||||||
@ -74,13 +73,13 @@ func OpenFile(path string) (*FmpFile, error) {
|
|||||||
|
|
||||||
case FMP_CHUNK_SIMPLE_KEY_VALUE:
|
case FMP_CHUNK_SIMPLE_KEY_VALUE:
|
||||||
ctx.Dictionary.set(
|
ctx.Dictionary.set(
|
||||||
append(currentPath, uint16(chunk.Key)),
|
append(currentPath, uint64(chunk.Key)),
|
||||||
chunk.Value,
|
chunk.Value,
|
||||||
)
|
)
|
||||||
|
|
||||||
case FMP_CHUNK_LONG_KEY_VALUE:
|
case FMP_CHUNK_LONG_KEY_VALUE:
|
||||||
ctx.Dictionary.set(
|
ctx.Dictionary.set(
|
||||||
append(currentPath, uint16(chunk.Key)), // todo: ??
|
append(currentPath, uint64(chunk.Key)), // todo: ??
|
||||||
chunk.Value,
|
chunk.Value,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -131,8 +130,8 @@ func (ctx *FmpFile) readSector() (*FmpSector, error) {
|
|||||||
sector := &FmpSector{
|
sector := &FmpSector{
|
||||||
Deleted: buf[0] != 0,
|
Deleted: buf[0] != 0,
|
||||||
Level: uint8(buf[1]),
|
Level: uint8(buf[1]),
|
||||||
PrevSectorID: binary.BigEndian.Uint32(buf[2:6]),
|
PrevSectorID: parseVarUint64(buf[2:6]),
|
||||||
NextSectorID: binary.BigEndian.Uint32(buf[6:10]),
|
NextSectorID: parseVarUint64(buf[6:10]),
|
||||||
}
|
}
|
||||||
|
|
||||||
payload := make([]byte, sectorSize-sectorHeaderSize)
|
payload := make([]byte, sectorSize-sectorHeaderSize)
|
||||||
@ -160,7 +159,7 @@ func (ctx *FmpFile) readSector() (*FmpSector, error) {
|
|||||||
panic("chunk length not set")
|
panic("chunk length not set")
|
||||||
}
|
}
|
||||||
sector.Chunks = append(sector.Chunks, chunk)
|
sector.Chunks = append(sector.Chunks, chunk)
|
||||||
payload = payload[min(chunk.Length, uint32(len(payload))):]
|
payload = payload[min(chunk.Length, uint64(len(payload))):]
|
||||||
if len(payload) == 0 || (len(payload) == 1 && payload[0] == 0x00) {
|
if len(payload) == 0 || (len(payload) == 1 && payload[0] == 0x00) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@ package fmp
|
|||||||
|
|
||||||
func (ctx *FmpFile) Tables() []*FmpTable {
|
func (ctx *FmpFile) Tables() []*FmpTable {
|
||||||
tables := make([]*FmpTable, 0)
|
tables := make([]*FmpTable, 0)
|
||||||
metaDict := ctx.Dictionary.get([]uint16{4, 1, 7})
|
metaDict := ctx.Dictionary.get([]uint64{4, 1, 7})
|
||||||
for _, meta := range *metaDict.Children {
|
for _, meta := range *metaDict.Children {
|
||||||
name := decodeByteSeq(meta.Children.get([]uint16{16}).Value)
|
name := decodeByteSeq(meta.Children.get([]uint64{16}).Value)
|
||||||
table := &FmpTable{Name: name}
|
table := &FmpTable{Name: name}
|
||||||
tables = append(tables, table)
|
tables = append(tables, table)
|
||||||
}
|
}
|
||||||
|
@ -19,20 +19,20 @@ type FmpFile struct {
|
|||||||
type FmpSector struct {
|
type FmpSector struct {
|
||||||
Deleted bool
|
Deleted bool
|
||||||
Level uint8
|
Level uint8
|
||||||
PrevSectorID uint32
|
PrevSectorID uint64
|
||||||
NextSectorID uint32
|
NextSectorID uint64
|
||||||
Chunks []*FmpChunk
|
Chunks []*FmpChunk
|
||||||
}
|
}
|
||||||
|
|
||||||
type FmpChunk struct {
|
type FmpChunk struct {
|
||||||
Type FmpChunkType
|
Type FmpChunkType
|
||||||
Length uint32
|
Length uint64
|
||||||
Key uint32 // If Type == FMP_CHUNK_SHORT_KEY_VALUE or FMP_CHUNK_LONG_KEY_VALUE
|
Key uint64 // If Type == FMP_CHUNK_SHORT_KEY_VALUE or FMP_CHUNK_LONG_KEY_VALUE
|
||||||
Index uint32 // Segment index, if Type == FMP_CHUNK_SEGMENTED_DATA
|
Index uint64 // Segment index, if Type == FMP_CHUNK_SEGMENTED_DATA
|
||||||
Value []byte
|
Value []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type FmpDict map[uint16]*FmpDictEntry
|
type FmpDict map[uint64]*FmpDictEntry
|
||||||
|
|
||||||
type FmpDictEntry struct {
|
type FmpDictEntry struct {
|
||||||
Value []byte
|
Value []byte
|
||||||
@ -43,7 +43,7 @@ type FmpTable struct {
|
|||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dict *FmpDict) get(path []uint16) *FmpDictEntry {
|
func (dict *FmpDict) get(path []uint64) *FmpDictEntry {
|
||||||
for i, key := range path {
|
for i, key := range path {
|
||||||
_, ok := (*dict)[key]
|
_, ok := (*dict)[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -59,7 +59,7 @@ func (dict *FmpDict) get(path []uint16) *FmpDictEntry {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dict *FmpDict) getValue(path []uint16) []byte {
|
func (dict *FmpDict) getValue(path []uint64) []byte {
|
||||||
ent := dict.get(path)
|
ent := dict.get(path)
|
||||||
if ent != nil {
|
if ent != nil {
|
||||||
return ent.Value
|
return ent.Value
|
||||||
@ -67,7 +67,7 @@ func (dict *FmpDict) getValue(path []uint16) []byte {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dict *FmpDict) set(path []uint16, value []byte) {
|
func (dict *FmpDict) set(path []uint64, value []byte) {
|
||||||
for i, key := range path {
|
for i, key := range path {
|
||||||
_, ok := (*dict)[key]
|
_, ok := (*dict)[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package fmp
|
package fmp
|
||||||
|
|
||||||
func parseVarUint32(payload []byte) uint32 {
|
func parseVarUint64(payload []byte) uint64 {
|
||||||
var length uint32
|
var length uint64
|
||||||
n := len(payload)
|
n := len(payload)
|
||||||
if n > 4 {
|
if n > 8 {
|
||||||
n = 4 // clamp to max uint32
|
n = 8 // clamp to max uint64
|
||||||
}
|
}
|
||||||
for i := range n {
|
for i := range n {
|
||||||
length <<= 8
|
length <<= 8
|
||||||
length |= uint32(payload[i])
|
length |= uint64(payload[i])
|
||||||
}
|
}
|
||||||
return length
|
return length
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user