mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Initial files
This commit is contained in:
commit
4e249fb3fb
71 changed files with 22664 additions and 0 deletions
77
src/serialization.cpp
Normal file
77
src/serialization.cpp
Normal file
|
@ -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*)¤t_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*)¤t_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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue