Skip to content

Commit

Permalink
improve item serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 18, 2020
1 parent 207d2ff commit c1d21ad
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 30 deletions.
21 changes: 9 additions & 12 deletions internal/item/bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ type Reader struct {
stream string
}

var ErrOutOfRange = errors.New("error: out of range")

func (r *Reader) ReadInt(n int) (uint64, error) {
if len(r.stream)-n < 0 {
n = len(r.stream)
if len(r.stream) < n {
return 0, ErrOutOfRange
}
val, err := strconv.ParseUint(r.stream[len(r.stream)-n:], 2, 64)
r.stream = r.stream[:len(r.stream)-n]
Expand Down Expand Up @@ -49,17 +51,12 @@ func (w *Writer) WriteInt(v, n int) error {

func (w *Writer) GetBytes() []byte {
bs := make([]byte, 0)
for i := len(*w); i > 8; i -= 8 {
p := (*w)[i-8 : i]
i, err := strconv.ParseUint(string(p), 2, 8)
if err != nil {
panic(err)
}
bs = append(bs, byte(i))
padding := (8 - len(*w)) % 8
for i := 0; i < padding; i++ {
*w = "0" + *w
}
if len(*w)%8 > 0 {
p := (*w)[:len(*w)%8]
i, err := strconv.ParseInt(string(p), 2, 8)
for i := len(*w)/8 - 1; i > -1; i-- {
i, err := strconv.ParseUint(string((*w)[i*8:i*8+8]), 2, 8)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/item/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ func Deserialize(data []byte) (item Item, err error) {
for i := 0; i < partCount; i++ {
item.Parts[i] = getPart(k, readNBits(r, bits)-1)
}
genericCount := int(readNBits(r, 4))
genericCount := readNBits(r, 4)
item.Generics = make([]string, genericCount)
bits = getBits("InventoryGenericPartData", item.Version)
for i := 0; i < genericCount; i++ {
for i := 0; i < int(genericCount); i++ {
// looks like the bits are the same
// for all the parts and generics
item.Generics[i] = getPart("InventoryGenericPartData", readNBits(r, bits)-1)
Expand Down
128 changes: 112 additions & 16 deletions internal/item/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,86 @@ package item

import (
"encoding/base64"
"encoding/hex"
"log"
"testing"
)

var checks = []string{
"A6cRHH+sfCuWGEZz2Lc5FWDbSfcQLmbaOV6SzgYP",
"AwAAAADFtIC3/mrBkEsaj5NM0xGVIBFDCAAAAAAAMAYA",
"AwAAAABLZ4A3RhkBkWMalJ8AEtSYWC1gJWYIAQAAAAAAyhgA",
"AwAAAADuCYA3RhkBkWMalJ8AEtSYWC1gJmYIAQAAAAAAyhgA",
"AwAAAACiM4A3rVMBk2tkjwhEwkRYO5cMpwkIAQAAAAAAyIAA",
"AwAAAAA+uIC3syBDllvs4u4gGyP7LLHEEkssMQAA",
"AwAAAADtqIC31oBBkWMEBcKAJnqQTAdOLWIIAQAAAAAAyIAA",
"AwAAAACGEoC36JCAkTsKGoSgBASiIgsA",
"AwAAAAAL94C3t9hAkysShLxMKmMLAA==",
"AwAAAAByhIC3A/pBkWMGBYLB+IDMbhnFMWYIAQAAAAAAzoAA",
"AwAAAACr6IC37xABkWsIqFPqeE0YjJJYUxUxhAAAAAAAAGQMAA==",
"AwAAAAB1WoC3t9hAkysShLxMKkMLAA==",
"AwAAAACaOoA3VJMAkSsQUhYFGGMLAA==",
"AwAAAADkl4A3VJMAkSsQUhYFGEMLAA==",
"AwAAAAAZzYA3VJMAkSsQUhYFGAMLAA==",
"AwAAAACd1YA3VJMAkSsQUhYFGKMLAA==",
"AwAAAAC754A3ElwAmCtYUlWPjAAAAA==",
"AwAAAADBk4A3ElwAmCtYUlWxjgAAAA==",
"AwAAAABM+oC33IBBkWMEA0LBZlmkGKfELb4IAQAAAAAAzoAA",
"AwAAAABDBIC3syBDllvs4u4gDG3MtcQVE0tsRQAA",
"AwAAAADT2YC3syBDllvs4u6gz2zcdcUVS0ysRAAA",
"AwAAAAB0xYA3pNNBkXMIKNMJSiplJhFOghEFhAAAAAAAAGUMAA==",
"AwAAAADh6oA3p+vCkHsiOIpkJRQgNB8QyRCcxAwhAAAAAABAGQMA",
"AwAAAACr94A3wNBBkWMIJxRMBMrEIJyELGIIAQAAAAAAyoAA",
"AwAAAAA2EYC3pGNBk2MaDghSkel4SJFSMnQIAQAAAAAAyhgA",
"AwAAAABvRoA38QgBk0sap9jjQvFwZDFDCAAAAAAAQAYA",
"AwAAAABRaYC3DUGBkGMGuXk40BtJSotjghAAAAAAAGAMAA==",
"AwAAAAAHiIC3NmvBkEsaD4dOwwlNchFDCAAAAAAAUAYA",
"AwAAAAC2soA31ECBkFOGteE+ViSvNkwIAQAAAAAA0oAA",
"AwAAAAB0hoA3a1IBk3MeMkhEkisIJhZQhqOLGUIAAAAAAIAzIAA=",
"AwAAAACxi4C3ZINBkXMEA0KBl9EoUowTAtQDhAAAAAAAAGNAAA==",
"AwAAAABMd4C3y+qAEmCaB3LONQrZ6stiihAAAAAAAGAJAA==",
"AwAAAABnloC3z5lBk1saN8zHFMEJCKlMzBACAAAAAACQAQA=",
"AwAAAACIAIC3t9hAkysShLxMKgMLAA==",
"AwAAAADjeIA3VJMAkSsQUhYFGIMLAA==",
"AwAAAADEyIA37wgBk1sap5fBcYmAShxYzBACAAAAAACkAQA=",
// potentially corrupt items
"AwAAAAAZDYC3/mrBkEsaj5NM0xGVIBFDCAAAAAAAMA==",
"AwAAAAA0qYA3RhkBkWMalJ8AEtSYWC1gJWYIAQAAAAAAyg==",
"AwAAAABknYA3RhkBkWMalJ8AEtSYWC1gJmYIAQAAAAAAyg==",
"AwAAAACrOoA3RhkBkWMalJ8AEtSYWC0gJmYIAQAAAAAAyg==",
"AwAAAADHT4A3rVMBk2tkjwhEwkRYO5cMpwkIAQAAAAAAyA==",
"AwAAAAAicYC3syBDllvs4u4gGyP7LLHEEkssMQ==",
"AwAAAAAXDIC31oBBkWMEBcKAJnqQTAdOLWIIAQAAAAAAyA==",
"AwAAAAD+8YC36JCAkTsKGoSgBASiIg==",
"AwAAAADDxoC3t9hAkysShLx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BACAAAAAACQ",
"AwAAAADvLIC3t9hAkysShLxMKgM=",
"AwAAAAB9BoA34pIAkSsQUgAFBIM=",
"AwAAAAAfmYA37wgBk1sap5fBcYmAShxYzBACAAAAAACk",
}

func TestDecryptSerial(t *testing.T) {
Expand Down Expand Up @@ -40,24 +114,46 @@ func TestDeserialize(t *testing.T) {

func TestSerialize(t *testing.T) {
for _, check := range checks {
bs, err := base64.StdEncoding.DecodeString(check)
if err != nil {
panic(err)
}
seed, err := GetSeedFromSerial(bs)
if err != nil {
panic(err)
}
item, err := Deserialize(bs)
if err != nil {
panic(err)
}
bs, err = Serialize(item, seed)
if err != nil {
panic(err)
var result = check
var history = make([]string, 10)
var item Item
var err error
for i := 0; i < 10; i++ {
bs, err := base64.StdEncoding.DecodeString(result)
if err != nil {
panic(err)
}
seed, err := GetSeedFromSerial(bs)
if err != nil {
panic(err)
}
item, err = Deserialize(bs)
if err != nil {
panic(err)
}
bs, err = Serialize(item, seed)
if err != nil {
panic(err)
}
result = base64.StdEncoding.EncodeToString(bs)
history[i] = result
}
result := base64.StdEncoding.EncodeToString(bs)
if result != check {
log.Println(err)
bs1, _ := base64.StdEncoding.DecodeString(check)
bs2, _ := base64.StdEncoding.DecodeString(result)
log.Println(hex.EncodeToString(bs1))
log.Println(hex.EncodeToString(bs2))
dec1, _ := DecryptSerial(bs1)
dec2, _ := DecryptSerial(bs2)
bs1, _ = base64.StdEncoding.DecodeString(check)
bs2, _ = base64.StdEncoding.DecodeString(result)
log.Println(hex.EncodeToString(dec1))
log.Println(hex.EncodeToString(dec2))
i1, _ := Deserialize(bs1)
i2, _ := Deserialize(bs2)
log.Println(i1.Version)
log.Println(i2.Version)
panic("invalid serial")
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/server/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func getItemsRequest(c *gin.Context) {
for _, data := range char.InventoryItems {
d := make([]byte, len(data.ItemSerialNumber))
copy(d, data.ItemSerialNumber)
log.Println(base64.StdEncoding.EncodeToString(data.ItemSerialNumber))
i, err := item.Deserialize(d)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit c1d21ad

Please sign in to comment.