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_util.go

91 lines
1.5 KiB
Go
Raw Normal View History

2025-06-12 11:44:57 +02:00
package fmp
2025-06-14 16:28:41 +02:00
import "slices"
type FmpDict map[uint64]*FmpDictEntry
type FmpDictEntry struct {
Value []byte
Children *FmpDict
}
func (dict *FmpDict) GetEntry(path []uint64) *FmpDictEntry {
for i, key := range path {
_, ok := (*dict)[key]
if !ok {
return nil
}
if i == len(path)-1 {
return (*dict)[key]
} else {
dict = (*dict)[key].Children
if dict == nil {
return nil
}
}
}
return nil
}
func (dict *FmpDict) GetValue(path []uint64) []byte {
ent := dict.GetEntry(path)
if ent != nil {
return ent.Value
}
return nil
}
func (dict *FmpDict) SetValue(path []uint64, value []byte) {
for i, key := range path {
_, ok := (*dict)[key]
if !ok {
(*dict)[key] = &FmpDictEntry{Children: &FmpDict{}}
}
if i == len(path)-1 {
(*dict)[key].Value = value
} else {
dict = (*dict)[key].Children
}
}
}
func parseVarUint64(payload []byte) uint64 {
var length uint64
2025-06-12 21:21:55 +02:00
n := min(len(payload), 8) // clamp to uint64
2025-06-12 11:44:57 +02:00
for i := range n {
length <<= 8
length |= uint64(payload[i])
2025-06-12 11:44:57 +02:00
}
return length
}
func decodeByteSeq(payload []byte) string {
result := ""
for i := range payload {
result += string(payload[i] ^ 0x5A)
}
return result
}
2025-06-14 16:28:41 +02:00
func addIf(cond bool, val uint64) uint64 {
if cond {
return val
}
return 0
}
func slicesHaveSameElements[Type comparable](a, b []Type) bool {
if len(a) != len(b) {
return false
}
for _, av := range a {
found := slices.Contains(b, av)
if !found {
return false
}
}
return true
}