You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZlibInStream.cxx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software 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 General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <assert.h>
  19. #include <rdr/ZlibInStream.h>
  20. #include <rdr/Exception.h>
  21. #include <zlib.h>
  22. using namespace rdr;
  23. ZlibInStream::ZlibInStream()
  24. : underlying(0), zs(NULL), bytesIn(0)
  25. {
  26. init();
  27. }
  28. ZlibInStream::~ZlibInStream()
  29. {
  30. deinit();
  31. }
  32. void ZlibInStream::setUnderlying(InStream* is, size_t bytesIn_)
  33. {
  34. underlying = is;
  35. bytesIn = bytesIn_;
  36. skip(avail());
  37. }
  38. void ZlibInStream::flushUnderlying()
  39. {
  40. while (bytesIn > 0) {
  41. if (!hasData(1))
  42. throw Exception("ZlibInStream: failed to flush remaining stream data");
  43. skip(avail());
  44. }
  45. setUnderlying(NULL, 0);
  46. }
  47. void ZlibInStream::reset()
  48. {
  49. deinit();
  50. init();
  51. }
  52. void ZlibInStream::init()
  53. {
  54. assert(zs == NULL);
  55. zs = new z_stream;
  56. zs->zalloc = Z_NULL;
  57. zs->zfree = Z_NULL;
  58. zs->opaque = Z_NULL;
  59. zs->next_in = Z_NULL;
  60. zs->avail_in = 0;
  61. if (inflateInit(zs) != Z_OK) {
  62. delete zs;
  63. zs = NULL;
  64. throw Exception("ZlibInStream: inflateInit failed");
  65. }
  66. }
  67. void ZlibInStream::deinit()
  68. {
  69. assert(zs != NULL);
  70. setUnderlying(NULL, 0);
  71. inflateEnd(zs);
  72. delete zs;
  73. zs = NULL;
  74. }
  75. bool ZlibInStream::fillBuffer(size_t maxSize)
  76. {
  77. if (!underlying)
  78. throw Exception("ZlibInStream overrun: no underlying stream");
  79. zs->next_out = (U8*)end;
  80. zs->avail_out = maxSize;
  81. if (!underlying->hasData(1))
  82. return false;
  83. size_t length = underlying->avail();
  84. if (length > bytesIn)
  85. length = bytesIn;
  86. zs->next_in = (U8*)underlying->getptr(length);
  87. zs->avail_in = length;
  88. int rc = inflate(zs, Z_SYNC_FLUSH);
  89. if (rc < 0) {
  90. throw Exception("ZlibInStream: inflate failed");
  91. }
  92. bytesIn -= length - zs->avail_in;
  93. end = zs->next_out;
  94. underlying->setptr(length - zs->avail_in);
  95. return true;
  96. }