Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated the VarInt function in mbinary package #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions mbinary/mbinary.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
package mbinary

import (
"errors"
"log"
)

const SEGMENT_BITS = 0x7f
const CONTINUE_BIT = 0x80

// VarInt decodes an int64 from buf and returns that value and the
// number of bytes read (> 0). If an error occurred, the value is 0
// and the number of bytes n is <= 0 with the following meaning:
//
// n == 0: buf too small
// n < 0: value larger than 64 bits (overflow)
// and -n is the number of bytes read
//
// number of bytes read (> 0). If an error occurred, the value is -1
// and the number of bytes n is -1
// Important: Minecraft is coding bytes in a little bit different way than
// protocol buffer so binary is useless.
func VarInt(b []byte) (int64, int) {
var value int64 = 0
var bitOffset byte = 0
var currIndx = 0
value := int64(0)
position := 0
var currentByte byte

for {
if bitOffset == 35 {
return 0, 0
}

currentByte = b[currIndx]
value |= int64(currentByte&0x7F) << uint(bitOffset)

currIndx++
bitOffset += 7

if currentByte&0x80 != 0x80 {
currentByte = b[position]
value |= (int64(currentByte) & SEGMENT_BITS) << int64(position)
if (currentByte & CONTINUE_BIT) == 0 {
break
}
position += 7
if position >= 32 {
err := errors.New("VarInt to big")
log.Println(err)
return -1, -1
}
}

return int64(value), currIndx
return value, position
}

// VarText pulls text from given array
Expand Down