Repository navigation
Need help converting this VarInt type into a struct or function. #2869
|
This value is used everywhere in mc packets to denote array lengths. Best I can tell, it requires for loops to be processed, but I can't seem to figure out how best to implement it. We can assume that the varints are of valid length. This is the available documentation on how the VarInts function But the crucial spot is this: Reading: int readVarInt() {
int value = 0;
for (int position = 0; position < 32; position += 7) {
byte currentByte = readByte();
// Note: In C this must be performed on an unsigned type to avoid undefined overflow
// behavior with negative VarInts.
value |= (int)(currentByte & 0x7F) << position;
if ((currentByte & 0x80) == 0)
return value;
}
error("VarInt too big");
}Writing: void writeVarInt(int value) {
while ((value & ~0x7F) != 0) {
writeByte((value & 0x7F) | 0x80);
// Note: >>> means that the leftmost bits are filled with zeroes regardless of the sign,
// rather than being filled with copies of the sign bit to preserve the sign.
// In languages that don't have a ">>>" operator, This behavior can often be selected by
// performing the shift on an unsigned type.
value >>>= 7;
}
writeByte(value);
}I'm probably missing some easy trick, but the methods I've seen so far don't leave the ability to use it as the length for an array, kinda like: struct array{
VarInt length;
char data[length];
};Would that even be possible? Or would I need to just make a function which reads the value, then manually offset the reader by a calculated number of bytes. |
Replies: 2 comments 1 reply
|
you may want to use leb128 type instead. It seems to be the same thing with a different name. |
|
Hey! |
Hey!
This looks very very similar to LEB128 for which we have an implementation in the standard library. You can either import that using
import type.leb128or maybe copy-paste it and adapt it for your needs: https://github.com/WerWolv/ImHex-Patterns/blob/master/includes/type/leb128.pat