InflatingStream.cpp 6.6 KB

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