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.4KB

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