diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-04-15 12:17:53 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-04-15 12:17:53 +0200 |
commit | 2ee0f834487cfe4b6bd9424ca2715685a8db16e4 (patch) | |
tree | 6867718d6bfcd42f6a82c86dd8f149d6ea6cab68 /utility.cpp | |
download | AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.tar AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.tar.gz AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.tar.bz2 AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.tar.lz AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.tar.xz AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.tar.zst AltCraft-2ee0f834487cfe4b6bd9424ca2715685a8db16e4.zip |
Diffstat (limited to 'utility.cpp')
-rw-r--r-- | utility.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/utility.cpp b/utility.cpp new file mode 100644 index 0000000..fffe2f8 --- /dev/null +++ b/utility.cpp @@ -0,0 +1,69 @@ +// +// Created by lag1924 on 24.03.17. +// +#include "utility.h" + +int VarIntRead(unsigned char *data, size_t &readed) { + readed = 0; + int result = 0; + char read; + do { + read = data[readed]; + int value = (read & 0b01111111); + result |= (value << (7 * readed)); + + readed++; + if (readed > 5) { + throw "VarInt is too big"; + } + } while ((read & 0b10000000) != 0); + + return result; +} + +size_t VarIntWrite(unsigned int value, unsigned char *data) { + size_t len = 0; + do { + unsigned char temp = (unsigned char) (value & 0b01111111); + value >>= 7; + if (value != 0) { + temp |= 0b10000000; + } + data[len] = temp; + len++; + } while (value != 0); + return len; +} + +long long int ReadVarLong(unsigned char *data, int &readed) { + readed = 0; + long long result = 0; + unsigned char read; + do { + read = data[readed]; + long long value = (read & 0b01111111); + result |= (value << (7 * readed)); + + readed++; + if (readed > 10) { + throw "VarLong is too big"; + } + } while ((read & 0b10000000) != 0); + return result; +} + +unsigned char *WriteVarLong(unsigned long long int value, int &len) { + unsigned char *data = new unsigned char[10]; + len = 0; + do { + unsigned char temp = (unsigned char) (value & 0b01111111); + value >>= 7; + if (value != 0) { + temp |= 0b10000000; + } + data[len] = temp; + len++; + } while (value != 0); + + return data; +}
\ No newline at end of file |