summaryrefslogtreecommitdiff
path: root/src/serialization.cpp
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2010-11-27 01:02:21 +0200
committerPerttu Ahola <celeron55@gmail.com>2010-11-27 01:02:21 +0200
commit4e249fb3fbf75f0359758760d88e22aa5b14533c (patch)
tree323087d05efbd2ace27b316d4f017cf812a31992 /src/serialization.cpp
downloadminetest-4e249fb3fbf75f0359758760d88e22aa5b14533c.tar.gz
minetest-4e249fb3fbf75f0359758760d88e22aa5b14533c.tar.bz2
minetest-4e249fb3fbf75f0359758760d88e22aa5b14533c.zip
Initial files
Diffstat (limited to 'src/serialization.cpp')
-rw-r--r--src/serialization.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/serialization.cpp b/src/serialization.cpp
new file mode 100644
index 000000000..72f675594
--- /dev/null
+++ b/src/serialization.cpp
@@ -0,0 +1,77 @@
+/*
+(c) 2010 Perttu Ahola <celeron55@gmail.com>
+*/
+
+#include "serialization.h"
+#include "utility.h"
+
+void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
+{
+ if(data.getSize() == 0)
+ return;
+
+ // Write length (u32)
+
+ u8 tmp[4];
+ writeU32(tmp, data.getSize());
+ os.write((char*)tmp, 4);
+
+ // We will be writing 8-bit pairs of more_count and byte
+ u8 more_count = 0;
+ u8 current_byte = data[0];
+ for(u32 i=1; i<data.getSize(); i++)
+ {
+ if(
+ data[i] != current_byte
+ || more_count == 255
+ )
+ {
+ // write count and byte
+ os.write((char*)&more_count, 1);
+ os.write((char*)&current_byte, 1);
+ more_count = 0;
+ current_byte = data[i];
+ }
+ else
+ {
+ more_count++;
+ }
+ }
+ // write count and byte
+ os.write((char*)&more_count, 1);
+ os.write((char*)&current_byte, 1);
+}
+
+void decompress(std::istream &is, std::ostream &os, u8 version)
+{
+ // Read length (u32)
+
+ u8 tmp[4];
+ is.read((char*)tmp, 4);
+ u32 len = readU32(tmp);
+
+ // We will be reading 8-bit pairs of more_count and byte
+ u32 count = 0;
+ for(;;)
+ {
+ u8 more_count=0;
+ u8 byte=0;
+
+ is.read((char*)&more_count, 1);
+
+ is.read((char*)&byte, 1);
+
+ if(is.eof())
+ throw SerializationError("decompress: stream ended halfway");
+
+ for(s32 i=0; i<(u16)more_count+1; i++)
+ os.write((char*)&byte, 1);
+
+ count += (u16)more_count+1;
+
+ if(count == len)
+ break;
+ }
+}
+
+