2 (c) 2010 Perttu Ahola <celeron55@gmail.com>
5 #include "serialization.h"
8 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
10 if(data.getSize() == 0)
16 writeU32(tmp, data.getSize());
17 os.write((char*)tmp, 4);
19 // We will be writing 8-bit pairs of more_count and byte
21 u8 current_byte = data[0];
22 for(u32 i=1; i<data.getSize(); i++)
25 data[i] != current_byte
29 // write count and byte
30 os.write((char*)&more_count, 1);
31 os.write((char*)¤t_byte, 1);
33 current_byte = data[i];
40 // write count and byte
41 os.write((char*)&more_count, 1);
42 os.write((char*)¤t_byte, 1);
45 void decompress(std::istream &is, std::ostream &os, u8 version)
50 is.read((char*)tmp, 4);
51 u32 len = readU32(tmp);
53 // We will be reading 8-bit pairs of more_count and byte
60 is.read((char*)&more_count, 1);
62 is.read((char*)&byte, 1);
65 throw SerializationError("decompress: stream ended halfway");
67 for(s32 i=0; i<(u16)more_count+1; i++)
68 os.write((char*)&byte, 1);
70 count += (u16)more_count+1;