ussd/debug/db.go

85 lines
1.5 KiB
Go
Raw Normal View History

2024-12-01 18:32:06 +01:00
package debug
import (
"fmt"
"encoding/binary"
"git.grassecon.net/urdt/ussd/common"
2024-12-08 00:06:03 +01:00
"git.defalsify.org/vise.git/db"
2024-12-01 18:32:06 +01:00
)
2024-12-02 00:12:58 +01:00
var (
dbTypStr map[common.DataTyp]string = make(map[common.DataTyp]string)
)
2024-12-01 18:32:06 +01:00
type KeyInfo struct {
SessionId string
Typ uint8
SubTyp common.DataTyp
Label string
Description string
}
2024-12-08 23:48:39 +01:00
func (k KeyInfo) String() string {
v := uint16(k.SubTyp)
s := subTypToString(k.SubTyp)
if s == "" {
v = uint16(k.Typ)
s = typToString(k.Typ)
}
return fmt.Sprintf("Session Id: %s\nTyp: %s (%d)\n", k.SessionId, s, v)
}
2024-12-01 18:32:06 +01:00
func ToKeyInfo(k []byte, sessionId string) (KeyInfo, error) {
o := KeyInfo{}
b := []byte(sessionId)
2024-12-02 00:12:58 +01:00
2024-12-01 18:32:06 +01:00
if len(k) <= len(b) {
return o, fmt.Errorf("storage key missing")
}
o.SessionId = sessionId
2024-12-08 00:06:03 +01:00
o.Typ = uint8(k[0])
2024-12-01 18:32:06 +01:00
k = k[1:]
2024-12-08 00:06:03 +01:00
o.SessionId = string(k[:len(b)])
k = k[len(b):]
2024-12-01 18:32:06 +01:00
2024-12-08 00:06:03 +01:00
if o.Typ == db.DATATYPE_USERDATA {
2024-12-01 18:32:06 +01:00
if len(k) == 0 {
return o, fmt.Errorf("missing subtype key")
}
v := binary.BigEndian.Uint16(k[:2])
o.SubTyp = common.DataTyp(v)
2024-12-02 00:12:58 +01:00
o.Label = subTypToString(o.SubTyp)
2024-12-01 18:32:06 +01:00
k = k[2:]
2024-12-02 00:12:58 +01:00
} else {
o.Label = typToString(o.Typ)
2024-12-01 18:32:06 +01:00
}
if len(k) != 0 {
return o, fmt.Errorf("excess key information")
}
return o, nil
}
2024-12-02 00:12:58 +01:00
2024-12-08 00:06:03 +01:00
func FromKey(k []byte) (KeyInfo, error) {
o := KeyInfo{}
if len(k) < 4 {
return o, fmt.Errorf("insufficient key length")
}
sessionIdBytes := k[1:len(k)-2]
return ToKeyInfo(k, string(sessionIdBytes))
}
2024-12-02 00:12:58 +01:00
func subTypToString(v common.DataTyp) string {
2024-12-08 00:06:03 +01:00
return dbTypStr[v + db.DATATYPE_USERDATA + 1]
2024-12-02 00:12:58 +01:00
}
func typToString(v uint8) string {
return dbTypStr[common.DataTyp(uint16(v))]
}