summaryrefslogtreecommitdiffstats
path: root/src/utility/utility.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-05-18 16:03:05 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-05-18 16:03:05 +0200
commit4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79 (patch)
tree548911d4e341fd5717acff3b5bc752c7b5650bf4 /src/utility/utility.cpp
parent2017-05-13 (diff)
downloadAltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.gz
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.bz2
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.lz
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.xz
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.tar.zst
AltCraft-4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79.zip
Diffstat (limited to 'src/utility/utility.cpp')
-rw-r--r--src/utility/utility.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/utility/utility.cpp b/src/utility/utility.cpp
new file mode 100644
index 0000000..aa50e9f
--- /dev/null
+++ b/src/utility/utility.cpp
@@ -0,0 +1,66 @@
+#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