serialization.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "serialization.h"
  17. #include "util/serialize.h"
  18. #include "zlib.h"
  19. /* report a zlib or i/o error */
  20. void zerr(int ret)
  21. {
  22. dstream<<"zerr: ";
  23. switch (ret) {
  24. case Z_ERRNO:
  25. if (ferror(stdin))
  26. dstream<<"error reading stdin"<<std::endl;
  27. if (ferror(stdout))
  28. dstream<<"error writing stdout"<<std::endl;
  29. break;
  30. case Z_STREAM_ERROR:
  31. dstream<<"invalid compression level"<<std::endl;
  32. break;
  33. case Z_DATA_ERROR:
  34. dstream<<"invalid or incomplete deflate data"<<std::endl;
  35. break;
  36. case Z_MEM_ERROR:
  37. dstream<<"out of memory"<<std::endl;
  38. break;
  39. case Z_VERSION_ERROR:
  40. dstream<<"zlib version mismatch!"<<std::endl;
  41. break;
  42. default:
  43. dstream<<"return value = "<<ret<<std::endl;
  44. }
  45. }
  46. void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level)
  47. {
  48. z_stream z;
  49. const s32 bufsize = 16384;
  50. char output_buffer[bufsize];
  51. int status = 0;
  52. int ret;
  53. z.zalloc = Z_NULL;
  54. z.zfree = Z_NULL;
  55. z.opaque = Z_NULL;
  56. ret = deflateInit(&z, level);
  57. if(ret != Z_OK)
  58. throw SerializationError("compressZlib: deflateInit failed");
  59. // Point zlib to our input buffer
  60. z.next_in = (Bytef*)&data[0];
  61. z.avail_in = data_size;
  62. // And get all output
  63. for(;;)
  64. {
  65. z.next_out = (Bytef*)output_buffer;
  66. z.avail_out = bufsize;
  67. status = deflate(&z, Z_FINISH);
  68. if(status == Z_NEED_DICT || status == Z_DATA_ERROR
  69. || status == Z_MEM_ERROR)
  70. {
  71. zerr(status);
  72. throw SerializationError("compressZlib: deflate failed");
  73. }
  74. int count = bufsize - z.avail_out;
  75. if(count)
  76. os.write(output_buffer, count);
  77. // This determines zlib has given all output
  78. if(status == Z_STREAM_END)
  79. break;
  80. }
  81. deflateEnd(&z);
  82. }
  83. void compressZlib(const std::string &data, std::ostream &os, int level)
  84. {
  85. compressZlib((u8*)data.c_str(), data.size(), os, level);
  86. }
  87. void decompressZlib(std::istream &is, std::ostream &os)
  88. {
  89. z_stream z;
  90. const s32 bufsize = 16384;
  91. char input_buffer[bufsize];
  92. char output_buffer[bufsize];
  93. int status = 0;
  94. int ret;
  95. int bytes_read = 0;
  96. int input_buffer_len = 0;
  97. z.zalloc = Z_NULL;
  98. z.zfree = Z_NULL;
  99. z.opaque = Z_NULL;
  100. ret = inflateInit(&z);
  101. if(ret != Z_OK)
  102. throw SerializationError("dcompressZlib: inflateInit failed");
  103. z.avail_in = 0;
  104. //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
  105. for(;;)
  106. {
  107. z.next_out = (Bytef*)output_buffer;
  108. z.avail_out = bufsize;
  109. if(z.avail_in == 0)
  110. {
  111. z.next_in = (Bytef*)input_buffer;
  112. is.read(input_buffer, bufsize);
  113. input_buffer_len = is.gcount();
  114. z.avail_in = input_buffer_len;
  115. //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
  116. }
  117. if(z.avail_in == 0)
  118. {
  119. //dstream<<"z.avail_in == 0"<<std::endl;
  120. break;
  121. }
  122. //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
  123. status = inflate(&z, Z_NO_FLUSH);
  124. //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
  125. bytes_read += is.gcount() - z.avail_in;
  126. //dstream<<"bytes_read="<<bytes_read<<std::endl;
  127. if(status == Z_NEED_DICT || status == Z_DATA_ERROR
  128. || status == Z_MEM_ERROR)
  129. {
  130. zerr(status);
  131. throw SerializationError("decompressZlib: inflate failed");
  132. }
  133. int count = bufsize - z.avail_out;
  134. //dstream<<"count="<<count<<std::endl;
  135. if(count)
  136. os.write(output_buffer, count);
  137. if(status == Z_STREAM_END)
  138. {
  139. //dstream<<"Z_STREAM_END"<<std::endl;
  140. //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
  141. //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
  142. // Unget all the data that inflate didn't take
  143. is.clear(); // Just in case EOF is set
  144. for(u32 i=0; i < z.avail_in; i++)
  145. {
  146. is.unget();
  147. if(is.fail() || is.bad())
  148. {
  149. dstream<<"unget #"<<i<<" failed"<<std::endl;
  150. dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
  151. throw SerializationError("decompressZlib: unget failed");
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. inflateEnd(&z);
  158. }
  159. void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version)
  160. {
  161. if(version >= 11)
  162. {
  163. compressZlib(*data ,data.getSize(), os);
  164. return;
  165. }
  166. if(data.getSize() == 0)
  167. return;
  168. // Write length (u32)
  169. u8 tmp[4];
  170. writeU32(tmp, data.getSize());
  171. os.write((char*)tmp, 4);
  172. // We will be writing 8-bit pairs of more_count and byte
  173. u8 more_count = 0;
  174. u8 current_byte = data[0];
  175. for(u32 i=1; i<data.getSize(); i++)
  176. {
  177. if(
  178. data[i] != current_byte
  179. || more_count == 255
  180. )
  181. {
  182. // write count and byte
  183. os.write((char*)&more_count, 1);
  184. os.write((char*)&current_byte, 1);
  185. more_count = 0;
  186. current_byte = data[i];
  187. }
  188. else
  189. {
  190. more_count++;
  191. }
  192. }
  193. // write count and byte
  194. os.write((char*)&more_count, 1);
  195. os.write((char*)&current_byte, 1);
  196. }
  197. void decompress(std::istream &is, std::ostream &os, u8 version)
  198. {
  199. if(version >= 11)
  200. {
  201. decompressZlib(is, os);
  202. return;
  203. }
  204. // Read length (u32)
  205. u8 tmp[4];
  206. is.read((char*)tmp, 4);
  207. u32 len = readU32(tmp);
  208. // We will be reading 8-bit pairs of more_count and byte
  209. u32 count = 0;
  210. for(;;)
  211. {
  212. u8 more_count=0;
  213. u8 byte=0;
  214. is.read((char*)&more_count, 1);
  215. is.read((char*)&byte, 1);
  216. if(is.eof())
  217. throw SerializationError("decompress: stream ended halfway");
  218. for(s32 i=0; i<(u16)more_count+1; i++)
  219. os.write((char*)&byte, 1);
  220. count += (u16)more_count+1;
  221. if(count == len)
  222. break;
  223. }
  224. }