DeflatingStream.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // DeflatingStream.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/DeflatingStream.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: ZLibStream
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/DeflatingStream.h"
  16. #include "Poco/Exception.h"
  17. namespace Poco {
  18. DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, StreamType type, int level):
  19. BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
  20. _pIstr(&istr),
  21. _pOstr(0),
  22. _eof(false)
  23. {
  24. _zstr.zalloc = Z_NULL;
  25. _zstr.zfree = Z_NULL;
  26. _zstr.opaque = Z_NULL;
  27. _zstr.next_in = 0;
  28. _zstr.avail_in = 0;
  29. _zstr.next_out = 0;
  30. _zstr.avail_out = 0;
  31. _buffer = new char[DEFLATE_BUFFER_SIZE];
  32. int rc = deflateInit2(&_zstr, level, Z_DEFLATED, 15 + (type == STREAM_GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
  33. if (rc != Z_OK)
  34. {
  35. delete [] _buffer;
  36. throw IOException(zError(rc));
  37. }
  38. }
  39. DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, int windowBits, int level):
  40. BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
  41. _pIstr(&istr),
  42. _pOstr(0),
  43. _eof(false)
  44. {
  45. _zstr.zalloc = Z_NULL;
  46. _zstr.zfree = Z_NULL;
  47. _zstr.opaque = Z_NULL;
  48. _zstr.next_in = 0;
  49. _zstr.avail_in = 0;
  50. _zstr.next_out = 0;
  51. _zstr.avail_out = 0;
  52. _buffer = new char[DEFLATE_BUFFER_SIZE];
  53. int rc = deflateInit2(&_zstr, level, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY);
  54. if (rc != Z_OK)
  55. {
  56. delete [] _buffer;
  57. throw IOException(zError(rc));
  58. }
  59. }
  60. DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level):
  61. BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
  62. _pIstr(0),
  63. _pOstr(&ostr),
  64. _eof(false)
  65. {
  66. _zstr.zalloc = Z_NULL;
  67. _zstr.zfree = Z_NULL;
  68. _zstr.opaque = Z_NULL;
  69. _zstr.next_in = 0;
  70. _zstr.avail_in = 0;
  71. _zstr.next_out = 0;
  72. _zstr.avail_out = 0;
  73. _buffer = new char[DEFLATE_BUFFER_SIZE];
  74. int rc = deflateInit2(&_zstr, level, Z_DEFLATED, 15 + (type == STREAM_GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
  75. if (rc != Z_OK)
  76. {
  77. delete [] _buffer;
  78. throw IOException(zError(rc));
  79. }
  80. }
  81. DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, int windowBits, int level):
  82. BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
  83. _pIstr(0),
  84. _pOstr(&ostr),
  85. _eof(false)
  86. {
  87. _zstr.zalloc = Z_NULL;
  88. _zstr.zfree = Z_NULL;
  89. _zstr.opaque = Z_NULL;
  90. _zstr.next_in = 0;
  91. _zstr.avail_in = 0;
  92. _zstr.next_out = 0;
  93. _zstr.avail_out = 0;
  94. _buffer = new char[DEFLATE_BUFFER_SIZE];
  95. int rc = deflateInit2(&_zstr, level, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY);
  96. if (rc != Z_OK)
  97. {
  98. delete [] _buffer;
  99. throw IOException(zError(rc));
  100. }
  101. }
  102. DeflatingStreamBuf::~DeflatingStreamBuf()
  103. {
  104. try
  105. {
  106. close();
  107. }
  108. catch (...)
  109. {
  110. }
  111. delete [] _buffer;
  112. deflateEnd(&_zstr);
  113. }
  114. int DeflatingStreamBuf::close()
  115. {
  116. BufferedStreamBuf::sync();
  117. _pIstr = 0;
  118. if (_pOstr)
  119. {
  120. if (_zstr.next_out)
  121. {
  122. int rc = deflate(&_zstr, Z_FINISH);
  123. if (rc != Z_OK && rc != Z_STREAM_END) throw IOException(zError(rc));
  124. _pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
  125. if (!_pOstr->good()) throw IOException(zError(rc));
  126. _zstr.next_out = (unsigned char*) _buffer;
  127. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  128. while (rc != Z_STREAM_END)
  129. {
  130. rc = deflate(&_zstr, Z_FINISH);
  131. if (rc != Z_OK && rc != Z_STREAM_END) throw IOException(zError(rc));
  132. _pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
  133. if (!_pOstr->good()) throw IOException(zError(rc));
  134. _zstr.next_out = (unsigned char*) _buffer;
  135. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  136. }
  137. }
  138. _pOstr->flush();
  139. _pOstr = 0;
  140. }
  141. return 0;
  142. }
  143. int DeflatingStreamBuf::sync()
  144. {
  145. if (BufferedStreamBuf::sync())
  146. return -1;
  147. if (_pOstr && _zstr.next_out)
  148. {
  149. int rc = deflate(&_zstr, Z_SYNC_FLUSH);
  150. if (rc != Z_OK) throw IOException(zError(rc));
  151. _pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
  152. if (!_pOstr->good()) throw IOException(zError(rc));
  153. while (_zstr.avail_out == 0)
  154. {
  155. _zstr.next_out = (unsigned char*) _buffer;
  156. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  157. rc = deflate(&_zstr, Z_SYNC_FLUSH);
  158. if (rc != Z_OK) throw IOException(zError(rc));
  159. _pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
  160. if (!_pOstr->good()) throw IOException(zError(rc));
  161. };
  162. _zstr.next_out = (unsigned char*) _buffer;
  163. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  164. }
  165. _pOstr->flush();
  166. return 0;
  167. }
  168. int DeflatingStreamBuf::readFromDevice(char* buffer, std::streamsize length)
  169. {
  170. if (!_pIstr) return 0;
  171. if (_zstr.avail_in == 0 && !_eof)
  172. {
  173. int n = 0;
  174. if (_pIstr->good())
  175. {
  176. _pIstr->read(_buffer, DEFLATE_BUFFER_SIZE);
  177. n = static_cast<int>(_pIstr->gcount());
  178. }
  179. if (n > 0)
  180. {
  181. _zstr.next_in = (unsigned char*) _buffer;
  182. _zstr.avail_in = n;
  183. }
  184. else
  185. {
  186. _zstr.next_in = 0;
  187. _zstr.avail_in = 0;
  188. _eof = true;
  189. }
  190. }
  191. _zstr.next_out = (unsigned char*) buffer;
  192. _zstr.avail_out = static_cast<unsigned>(length);
  193. for (;;)
  194. {
  195. int rc = deflate(&_zstr, _eof ? Z_FINISH : Z_NO_FLUSH);
  196. if (_eof && rc == Z_STREAM_END)
  197. {
  198. _pIstr = 0;
  199. return static_cast<int>(length) - _zstr.avail_out;
  200. }
  201. if (rc != Z_OK) throw IOException(zError(rc));
  202. if (_zstr.avail_out == 0)
  203. {
  204. return static_cast<int>(length);
  205. }
  206. if (_zstr.avail_in == 0)
  207. {
  208. int n = 0;
  209. if (_pIstr->good())
  210. {
  211. _pIstr->read(_buffer, DEFLATE_BUFFER_SIZE);
  212. n = static_cast<int>(_pIstr->gcount());
  213. }
  214. if (n > 0)
  215. {
  216. _zstr.next_in = (unsigned char*) _buffer;
  217. _zstr.avail_in = n;
  218. }
  219. else
  220. {
  221. _zstr.next_in = 0;
  222. _zstr.avail_in = 0;
  223. _eof = true;
  224. }
  225. }
  226. }
  227. }
  228. int DeflatingStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
  229. {
  230. if (length == 0 || !_pOstr) return 0;
  231. _zstr.next_in = (unsigned char*) buffer;
  232. _zstr.avail_in = static_cast<unsigned>(length);
  233. _zstr.next_out = (unsigned char*) _buffer;
  234. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  235. for (;;)
  236. {
  237. int rc = deflate(&_zstr, Z_NO_FLUSH);
  238. if (rc != Z_OK) throw IOException(zError(rc));
  239. if (_zstr.avail_out == 0)
  240. {
  241. _pOstr->write(_buffer, DEFLATE_BUFFER_SIZE);
  242. if (!_pOstr->good()) throw IOException(zError(rc));
  243. _zstr.next_out = (unsigned char*) _buffer;
  244. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  245. }
  246. if (_zstr.avail_in == 0)
  247. {
  248. _pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
  249. if (!_pOstr->good()) throw IOException(zError(rc));
  250. _zstr.next_out = (unsigned char*) _buffer;
  251. _zstr.avail_out = DEFLATE_BUFFER_SIZE;
  252. break;
  253. }
  254. }
  255. return static_cast<int>(length);
  256. }
  257. DeflatingIOS::DeflatingIOS(std::ostream& ostr, DeflatingStreamBuf::StreamType type, int level):
  258. _buf(ostr, type, level)
  259. {
  260. poco_ios_init(&_buf);
  261. }
  262. DeflatingIOS::DeflatingIOS(std::ostream& ostr, int windowBits, int level):
  263. _buf(ostr, windowBits, level)
  264. {
  265. poco_ios_init(&_buf);
  266. }
  267. DeflatingIOS::DeflatingIOS(std::istream& istr, DeflatingStreamBuf::StreamType type, int level):
  268. _buf(istr, type, level)
  269. {
  270. poco_ios_init(&_buf);
  271. }
  272. DeflatingIOS::DeflatingIOS(std::istream& istr, int windowBits, int level):
  273. _buf(istr, windowBits, level)
  274. {
  275. poco_ios_init(&_buf);
  276. }
  277. DeflatingIOS::~DeflatingIOS()
  278. {
  279. }
  280. DeflatingStreamBuf* DeflatingIOS::rdbuf()
  281. {
  282. return &_buf;
  283. }
  284. DeflatingOutputStream::DeflatingOutputStream(std::ostream& ostr, DeflatingStreamBuf::StreamType type, int level):
  285. DeflatingIOS(ostr, type, level),
  286. std::ostream(&_buf)
  287. {
  288. }
  289. DeflatingOutputStream::DeflatingOutputStream(std::ostream& ostr, int windowBits, int level):
  290. DeflatingIOS(ostr, windowBits, level),
  291. std::ostream(&_buf)
  292. {
  293. }
  294. DeflatingOutputStream::~DeflatingOutputStream()
  295. {
  296. }
  297. int DeflatingOutputStream::close()
  298. {
  299. return _buf.close();
  300. }
  301. int DeflatingOutputStream::sync()
  302. {
  303. return _buf.pubsync();
  304. }
  305. DeflatingInputStream::DeflatingInputStream(std::istream& istr, DeflatingStreamBuf::StreamType type, int level):
  306. DeflatingIOS(istr, type, level),
  307. std::istream(&_buf)
  308. {
  309. }
  310. DeflatingInputStream::DeflatingInputStream(std::istream& istr, int windowBits, int level):
  311. DeflatingIOS(istr, windowBits, level),
  312. std::istream(&_buf)
  313. {
  314. }
  315. DeflatingInputStream::~DeflatingInputStream()
  316. {
  317. }
  318. } // namespace Poco