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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (C) 2002-2003 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 <rdr/ZlibInStream.h>
  19. #include <rdr/Exception.h>
  20. #include <zlib.h>
  21. using namespace rdr;
  22. enum { DEFAULT_BUF_SIZE = 16384 };
  23. ZlibInStream::ZlibInStream(int bufSize_)
  24. : underlying(0), bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0),
  25. bytesIn(0)
  26. {
  27. zs = new z_stream;
  28. zs->zalloc = Z_NULL;
  29. zs->zfree = Z_NULL;
  30. zs->opaque = Z_NULL;
  31. zs->next_in = Z_NULL;
  32. zs->avail_in = 0;
  33. if (inflateInit(zs) != Z_OK) {
  34. delete zs;
  35. throw Exception("ZlibInStream: inflateInit failed");
  36. }
  37. ptr = end = start = new U8[bufSize];
  38. }
  39. ZlibInStream::~ZlibInStream()
  40. {
  41. delete [] start;
  42. inflateEnd(zs);
  43. delete zs;
  44. }
  45. void ZlibInStream::setUnderlying(InStream* is, int bytesIn_)
  46. {
  47. underlying = is;
  48. bytesIn = bytesIn_;
  49. ptr = end = start;
  50. }
  51. int ZlibInStream::pos()
  52. {
  53. return offset + ptr - start;
  54. }
  55. void ZlibInStream::reset()
  56. {
  57. ptr = end = start;
  58. if (!underlying) return;
  59. while (bytesIn > 0) {
  60. decompress(true);
  61. end = start; // throw away any data
  62. }
  63. underlying = 0;
  64. }
  65. int ZlibInStream::overrun(int itemSize, int nItems, bool wait)
  66. {
  67. if (itemSize > bufSize)
  68. throw Exception("ZlibInStream overrun: max itemSize exceeded");
  69. if (!underlying)
  70. throw Exception("ZlibInStream overrun: no underlying stream");
  71. if (end - ptr != 0)
  72. memmove(start, ptr, end - ptr);
  73. offset += ptr - start;
  74. end -= ptr - start;
  75. ptr = start;
  76. while (end - ptr < itemSize) {
  77. if (!decompress(wait))
  78. return 0;
  79. }
  80. if (itemSize * nItems > end - ptr)
  81. nItems = (end - ptr) / itemSize;
  82. return nItems;
  83. }
  84. // decompress() calls the decompressor once. Note that this won't necessarily
  85. // generate any output data - it may just consume some input data. Returns
  86. // false if wait is false and we would block on the underlying stream.
  87. bool ZlibInStream::decompress(bool wait)
  88. {
  89. zs->next_out = (U8*)end;
  90. zs->avail_out = start + bufSize - end;
  91. int n = underlying->check(1, 1, wait);
  92. if (n == 0) return false;
  93. zs->next_in = (U8*)underlying->getptr();
  94. zs->avail_in = underlying->getend() - underlying->getptr();
  95. if ((int)zs->avail_in > bytesIn)
  96. zs->avail_in = bytesIn;
  97. int rc = inflate(zs, Z_SYNC_FLUSH);
  98. if (rc != Z_OK) {
  99. throw Exception("ZlibInStream: inflate failed");
  100. }
  101. bytesIn -= zs->next_in - underlying->getptr();
  102. end = zs->next_out;
  103. underlying->setptr(zs->next_in);
  104. return true;
  105. }