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.

ZlibOutStream.cxx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 2002-2004 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/ZlibOutStream.h>
  19. #include <rdr/Exception.h>
  20. #include <zlib.h>
  21. using namespace rdr;
  22. enum { DEFAULT_BUF_SIZE = 16384 };
  23. ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel)
  24. : underlying(os), bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
  25. {
  26. zs = new z_stream;
  27. zs->zalloc = Z_NULL;
  28. zs->zfree = Z_NULL;
  29. zs->opaque = Z_NULL;
  30. if (deflateInit(zs, compressLevel) != Z_OK) {
  31. delete zs;
  32. throw Exception("ZlibOutStream: deflateInit failed");
  33. }
  34. ptr = start = new U8[bufSize];
  35. end = start + bufSize;
  36. }
  37. ZlibOutStream::~ZlibOutStream()
  38. {
  39. try {
  40. flush();
  41. } catch (Exception&) {
  42. }
  43. delete [] start;
  44. deflateEnd(zs);
  45. delete zs;
  46. }
  47. void ZlibOutStream::setUnderlying(OutStream* os)
  48. {
  49. underlying = os;
  50. }
  51. int ZlibOutStream::length()
  52. {
  53. return offset + ptr - start;
  54. }
  55. void ZlibOutStream::flush()
  56. {
  57. zs->next_in = start;
  58. zs->avail_in = ptr - start;
  59. // fprintf(stderr,"zos flush: avail_in %d\n",zs->avail_in);
  60. while (zs->avail_in != 0) {
  61. do {
  62. underlying->check(1);
  63. zs->next_out = underlying->getptr();
  64. zs->avail_out = underlying->getend() - underlying->getptr();
  65. // fprintf(stderr,"zos flush: calling deflate, avail_in %d, avail_out %d\n",
  66. // zs->avail_in,zs->avail_out);
  67. int rc = deflate(zs, Z_SYNC_FLUSH);
  68. if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
  69. // fprintf(stderr,"zos flush: after deflate: %d bytes\n",
  70. // zs->next_out-underlying->getptr());
  71. underlying->setptr(zs->next_out);
  72. } while (zs->avail_out == 0);
  73. }
  74. offset += ptr - start;
  75. ptr = start;
  76. }
  77. int ZlibOutStream::overrun(int itemSize, int nItems)
  78. {
  79. // fprintf(stderr,"ZlibOutStream overrun\n");
  80. if (itemSize > bufSize)
  81. throw Exception("ZlibOutStream overrun: max itemSize exceeded");
  82. while (end - ptr < itemSize) {
  83. zs->next_in = start;
  84. zs->avail_in = ptr - start;
  85. do {
  86. underlying->check(1);
  87. zs->next_out = underlying->getptr();
  88. zs->avail_out = underlying->getend() - underlying->getptr();
  89. // fprintf(stderr,"zos overrun: calling deflate, avail_in %d, avail_out %d\n",
  90. // zs->avail_in,zs->avail_out);
  91. int rc = deflate(zs, 0);
  92. if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
  93. // fprintf(stderr,"zos overrun: after deflate: %d bytes\n",
  94. // zs->next_out-underlying->getptr());
  95. underlying->setptr(zs->next_out);
  96. } while (zs->avail_out == 0);
  97. // output buffer not full
  98. if (zs->avail_in == 0) {
  99. offset += ptr - start;
  100. ptr = start;
  101. } else {
  102. // but didn't consume all the data? try shifting what's left to the
  103. // start of the buffer.
  104. fprintf(stderr,"z out buf not full, but in data not consumed\n");
  105. memmove(start, zs->next_in, ptr - zs->next_in);
  106. offset += zs->next_in - start;
  107. ptr -= zs->next_in - start;
  108. }
  109. }
  110. if (itemSize * nItems > end - ptr)
  111. nItems = (end - ptr) / itemSize;
  112. return nItems;
  113. }