3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
24 #include "serialization.h"
27 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
29 if(data.getSize() == 0)
35 writeU32(tmp, data.getSize());
36 os.write((char*)tmp, 4);
38 // We will be writing 8-bit pairs of more_count and byte
40 u8 current_byte = data[0];
41 for(u32 i=1; i<data.getSize(); i++)
44 data[i] != current_byte
48 // write count and byte
49 os.write((char*)&more_count, 1);
50 os.write((char*)¤t_byte, 1);
52 current_byte = data[i];
59 // write count and byte
60 os.write((char*)&more_count, 1);
61 os.write((char*)¤t_byte, 1);
64 void decompress(std::istream &is, std::ostream &os, u8 version)
69 is.read((char*)tmp, 4);
70 u32 len = readU32(tmp);
72 // We will be reading 8-bit pairs of more_count and byte
79 is.read((char*)&more_count, 1);
81 is.read((char*)&byte, 1);
84 throw SerializationError("decompress: stream ended halfway");
86 for(s32 i=0; i<(u16)more_count+1; i++)
87 os.write((char*)&byte, 1);
89 count += (u16)more_count+1;