]> git.lizzy.rs Git - dragonfireclient.git/blob - src/serialization.cpp
license stuff
[dragonfireclient.git] / src / serialization.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "serialization.h"
25 #include "utility.h"
26
27 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
28 {
29         if(data.getSize() == 0)
30                 return;
31
32         // Write length (u32)
33
34         u8 tmp[4];
35         writeU32(tmp, data.getSize());
36         os.write((char*)tmp, 4);
37         
38         // We will be writing 8-bit pairs of more_count and byte
39         u8 more_count = 0;
40         u8 current_byte = data[0];
41         for(u32 i=1; i<data.getSize(); i++)
42         {
43                 if(
44                         data[i] != current_byte
45                         || more_count == 255
46                 )
47                 {
48                         // write count and byte
49                         os.write((char*)&more_count, 1);
50                         os.write((char*)&current_byte, 1);
51                         more_count = 0;
52                         current_byte = data[i];
53                 }
54                 else
55                 {
56                         more_count++;
57                 }
58         }
59         // write count and byte
60         os.write((char*)&more_count, 1);
61         os.write((char*)&current_byte, 1);
62 }
63
64 void decompress(std::istream &is, std::ostream &os, u8 version)
65 {
66         // Read length (u32)
67
68         u8 tmp[4];
69         is.read((char*)tmp, 4);
70         u32 len = readU32(tmp);
71         
72         // We will be reading 8-bit pairs of more_count and byte
73         u32 count = 0;
74         for(;;)
75         {
76                 u8 more_count=0;
77                 u8 byte=0;
78
79                 is.read((char*)&more_count, 1);
80                 
81                 is.read((char*)&byte, 1);
82
83                 if(is.eof())
84                         throw SerializationError("decompress: stream ended halfway");
85
86                 for(s32 i=0; i<(u16)more_count+1; i++)
87                         os.write((char*)&byte, 1);
88
89                 count += (u16)more_count+1;
90
91                 if(count == len)
92                         break;
93         }
94 }
95
96