forked from Mirrorlandia_minetest/minetest
78 lines
1.3 KiB
C++
78 lines
1.3 KiB
C++
|
/*
|
||
|
(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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|