From e704042403b4b34a64eb5fd7eec474d4c4902a35 Mon Sep 17 00:00:00 2001 From: Justin T <37750742+jtieri@users.noreply.github.com> Date: Tue, 3 Dec 2019 21:14:51 -0600 Subject: [PATCH] add support for 3 byte slices Previous implementation didn't support the three byte encoded headers found in incoming packets --- habbgo/utils/encoding/base64.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/habbgo/utils/encoding/base64.go b/habbgo/utils/encoding/base64.go index 634daf2..50a8075 100644 --- a/habbgo/utils/encoding/base64.go +++ b/habbgo/utils/encoding/base64.go @@ -7,12 +7,31 @@ This implementation is a Golang port of the examples from Puomi's wiki for Base6 */ package encoding -// EncodeB64 takes an integer, encodes it in FUSE-Base64 & returns a slice of bytes that should contain two char's. -func EncodeB64(i int) []byte { - return []byte{byte(i/64 + 64), byte(i%64 + 64)} +import "math" + +// EncodeB64 takes an integer, encodes it in FUSE-Base64 & returns a slice of length number of bytes. +func EncodeB64(i int, length int) []byte { + bytes := make([]byte, length) + for j := 1; j <= length; j++ { + k := uint((length - j) * 6) + bytes[j-1] = byte(0x40 + ((i >> k) & 0x3f)) + } + return bytes } // DecodeB64 take a slice of bytes, decodes it from FUSE-Base64 & returns the decoded bytes as an integer. func DecodeB64(bytes []byte) int { - return 64 * (int(bytes[0]%64) + int(bytes[1]%64)) + decodedVal := 0 + counter := 0 + + for i := len(bytes) - 1; i >= 0; i-- { + x := int(bytes[i] - 0x40) + if counter > 0 { + x *= int(math.Pow(64.0, float64(counter))) + } + decodedVal += x + counter++ + } + + return decodedVal }