2
0

vnc-enc-zlib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * QEMU VNC display driver: zlib encoding
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2006 Fabrice Bellard
  6. * Copyright (C) 2009 Red Hat, Inc
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "vnc.h"
  28. #define ZALLOC_ALIGNMENT 16
  29. void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
  30. {
  31. void *p;
  32. size *= items;
  33. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  34. p = g_malloc0(size);
  35. return (p);
  36. }
  37. void vnc_zlib_zfree(void *x, void *addr)
  38. {
  39. g_free(addr);
  40. }
  41. static void vnc_zlib_start(VncState *vs)
  42. {
  43. buffer_reset(&vs->zlib.zlib);
  44. // make the output buffer be the zlib buffer, so we can compress it later
  45. vs->zlib.tmp = vs->output;
  46. vs->output = vs->zlib.zlib;
  47. }
  48. static int vnc_zlib_stop(VncState *vs)
  49. {
  50. z_streamp zstream = &vs->zlib.stream;
  51. int previous_out;
  52. // switch back to normal output/zlib buffers
  53. vs->zlib.zlib = vs->output;
  54. vs->output = vs->zlib.tmp;
  55. // compress the zlib buffer
  56. // initialize the stream
  57. // XXX need one stream per session
  58. if (zstream->opaque != vs) {
  59. int err;
  60. VNC_DEBUG("VNC: initializing zlib stream\n");
  61. VNC_DEBUG("VNC: opaque = %p | vs = %p\n", zstream->opaque, vs);
  62. zstream->zalloc = vnc_zlib_zalloc;
  63. zstream->zfree = vnc_zlib_zfree;
  64. err = deflateInit2(zstream, vs->tight->compression, Z_DEFLATED,
  65. MAX_WBITS,
  66. MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  67. if (err != Z_OK) {
  68. fprintf(stderr, "VNC: error initializing zlib\n");
  69. return -1;
  70. }
  71. vs->zlib.level = vs->tight->compression;
  72. zstream->opaque = vs;
  73. }
  74. if (vs->tight->compression != vs->zlib.level) {
  75. if (deflateParams(zstream, vs->tight->compression,
  76. Z_DEFAULT_STRATEGY) != Z_OK) {
  77. return -1;
  78. }
  79. vs->zlib.level = vs->tight->compression;
  80. }
  81. // reserve memory in output buffer
  82. buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
  83. // set pointers
  84. zstream->next_in = vs->zlib.zlib.buffer;
  85. zstream->avail_in = vs->zlib.zlib.offset;
  86. zstream->next_out = vs->output.buffer + vs->output.offset;
  87. zstream->avail_out = vs->output.capacity - vs->output.offset;
  88. previous_out = zstream->avail_out;
  89. zstream->data_type = Z_BINARY;
  90. // start encoding
  91. if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
  92. fprintf(stderr, "VNC: error during zlib compression\n");
  93. return -1;
  94. }
  95. vs->output.offset = vs->output.capacity - zstream->avail_out;
  96. return previous_out - zstream->avail_out;
  97. }
  98. int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
  99. {
  100. int old_offset, new_offset, bytes_written;
  101. vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_ZLIB);
  102. // remember where we put in the follow-up size
  103. old_offset = vs->output.offset;
  104. vnc_write_s32(vs, 0);
  105. // compress the stream
  106. vnc_zlib_start(vs);
  107. vnc_raw_send_framebuffer_update(vs, x, y, w, h);
  108. bytes_written = vnc_zlib_stop(vs);
  109. if (bytes_written == -1)
  110. return 0;
  111. // hack in the size
  112. new_offset = vs->output.offset;
  113. vs->output.offset = old_offset;
  114. vnc_write_u32(vs, bytes_written);
  115. vs->output.offset = new_offset;
  116. return 1;
  117. }
  118. void vnc_zlib_clear(VncState *vs)
  119. {
  120. if (vs->zlib.stream.opaque) {
  121. deflateEnd(&vs->zlib.stream);
  122. }
  123. buffer_free(&vs->zlib.zlib);
  124. }