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.

ZRLEDecoder.cxx 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2022 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rdr/InStream.h>
  23. #include <rdr/MemInStream.h>
  24. #include <rdr/OutStream.h>
  25. #include <rfb/Exception.h>
  26. #include <rfb/ServerParams.h>
  27. #include <rfb/PixelBuffer.h>
  28. #include <rfb/ZRLEDecoder.h>
  29. using namespace rfb;
  30. static inline uint32_t readOpaque24A(rdr::InStream* is)
  31. {
  32. uint32_t r=0;
  33. ((uint8_t*)&r)[0] = is->readU8();
  34. ((uint8_t*)&r)[1] = is->readU8();
  35. ((uint8_t*)&r)[2] = is->readU8();
  36. return r;
  37. }
  38. static inline uint32_t readOpaque24B(rdr::InStream* is)
  39. {
  40. uint32_t r=0;
  41. ((uint8_t*)&r)[1] = is->readU8();
  42. ((uint8_t*)&r)[2] = is->readU8();
  43. ((uint8_t*)&r)[3] = is->readU8();
  44. return r;
  45. }
  46. template<class T>
  47. static inline T readPixel(rdr::ZlibInStream* zis)
  48. {
  49. if (sizeof(T) == 1)
  50. return zis->readOpaque8();
  51. if (sizeof(T) == 2)
  52. return zis->readOpaque16();
  53. if (sizeof(T) == 4)
  54. return zis->readOpaque32();
  55. }
  56. static inline void zlibHasData(rdr::ZlibInStream* zis, size_t length)
  57. {
  58. if (!zis->hasData(length))
  59. throw Exception("ZRLE decode error");
  60. }
  61. ZRLEDecoder::ZRLEDecoder() : Decoder(DecoderOrdered)
  62. {
  63. }
  64. ZRLEDecoder::~ZRLEDecoder()
  65. {
  66. }
  67. bool ZRLEDecoder::readRect(const Rect& /*r*/, rdr::InStream* is,
  68. const ServerParams& /*server*/,
  69. rdr::OutStream* os)
  70. {
  71. uint32_t len;
  72. if (!is->hasData(4))
  73. return false;
  74. is->setRestorePoint();
  75. len = is->readU32();
  76. os->writeU32(len);
  77. if (!is->hasDataOrRestore(len))
  78. return false;
  79. is->clearRestorePoint();
  80. os->copyBytes(is, len);
  81. return true;
  82. }
  83. void ZRLEDecoder::decodeRect(const Rect& r, const uint8_t* buffer,
  84. size_t buflen, const ServerParams& server,
  85. ModifiablePixelBuffer* pb)
  86. {
  87. rdr::MemInStream is(buffer, buflen);
  88. const rfb::PixelFormat& pf = server.pf();
  89. switch (pf.bpp) {
  90. case 8: zrleDecode<uint8_t>(r, &is, &zis, pf, pb); break;
  91. case 16: zrleDecode<uint16_t>(r, &is, &zis, pf, pb); break;
  92. case 32: zrleDecode<uint32_t>(r, &is, &zis, pf, pb); break;
  93. }
  94. }
  95. template<class T>
  96. void ZRLEDecoder::zrleDecode(const Rect& r, rdr::InStream* is,
  97. rdr::ZlibInStream* zis,
  98. const PixelFormat& pf,
  99. ModifiablePixelBuffer* pb)
  100. {
  101. int length = is->readU32();
  102. zis->setUnderlying(is, length);
  103. Rect t;
  104. T buf[64 * 64];
  105. Pixel maxPixel = pf.pixelFromRGB((uint16_t)-1, (uint16_t)-1, (uint16_t)-1);
  106. bool fitsInLS3Bytes = maxPixel < (1<<24);
  107. bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
  108. bool isLowCPixel = (sizeof(T) == 4) &&
  109. ((fitsInLS3Bytes && pf.isLittleEndian()) ||
  110. (fitsInMS3Bytes && pf.isBigEndian()));
  111. bool isHighCPixel = (sizeof(T) == 4) &&
  112. ((fitsInLS3Bytes && pf.isBigEndian()) ||
  113. (fitsInMS3Bytes && pf.isLittleEndian()));
  114. for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 64) {
  115. t.br.y = __rfbmin(r.br.y, t.tl.y + 64);
  116. for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 64) {
  117. t.br.x = __rfbmin(r.br.x, t.tl.x + 64);
  118. zlibHasData(zis, 1);
  119. int mode = zis->readU8();
  120. bool rle = mode & 128;
  121. int palSize = mode & 127;
  122. T palette[128];
  123. if (isLowCPixel || isHighCPixel)
  124. zlibHasData(zis, 3 * palSize);
  125. else
  126. zlibHasData(zis, sizeof(T) * palSize);
  127. for (int i = 0; i < palSize; i++) {
  128. if (isLowCPixel)
  129. palette[i] = readOpaque24A(zis);
  130. else if (isHighCPixel)
  131. palette[i] = readOpaque24B(zis);
  132. else
  133. palette[i] = readPixel<T>(zis);
  134. }
  135. if (palSize == 1) {
  136. T pix = palette[0];
  137. pb->fillRect(pf, t, &pix);
  138. continue;
  139. }
  140. if (!rle) {
  141. if (palSize == 0) {
  142. // raw
  143. if (isLowCPixel || isHighCPixel)
  144. zlibHasData(zis, 3 * t.area());
  145. else
  146. zlibHasData(zis, sizeof(T) * t.area());
  147. if (isLowCPixel || isHighCPixel) {
  148. for (T* ptr = buf; ptr < buf+t.area(); ptr++) {
  149. if (isLowCPixel)
  150. *ptr = readOpaque24A(zis);
  151. else
  152. *ptr = readOpaque24B(zis);
  153. }
  154. } else {
  155. zis->readBytes((uint8_t*)buf, t.area() * sizeof(T));
  156. }
  157. } else {
  158. // packed pixels
  159. int bppp = ((palSize > 16) ? 8 :
  160. ((palSize > 4) ? 4 : ((palSize > 2) ? 2 : 1)));
  161. T* ptr = buf;
  162. for (int i = 0; i < t.height(); i++) {
  163. T* eol = ptr + t.width();
  164. uint8_t byte = 0;
  165. uint8_t nbits = 0;
  166. while (ptr < eol) {
  167. if (nbits == 0) {
  168. zlibHasData(zis, 1);
  169. byte = zis->readU8();
  170. nbits = 8;
  171. }
  172. nbits -= bppp;
  173. uint8_t index = (byte >> nbits) & ((1 << bppp) - 1) & 127;
  174. *ptr++ = palette[index];
  175. }
  176. }
  177. }
  178. } else {
  179. if (palSize == 0) {
  180. // plain RLE
  181. T* ptr = buf;
  182. T* end = ptr + t.area();
  183. while (ptr < end) {
  184. T pix;
  185. if (isLowCPixel || isHighCPixel)
  186. zlibHasData(zis, 3);
  187. else
  188. zlibHasData(zis, sizeof(T));
  189. if (isLowCPixel)
  190. pix = readOpaque24A(zis);
  191. else if (isHighCPixel)
  192. pix = readOpaque24B(zis);
  193. else
  194. pix = readPixel<T>(zis);
  195. int len = 1;
  196. int b;
  197. do {
  198. zlibHasData(zis, 1);
  199. b = zis->readU8();
  200. len += b;
  201. } while (b == 255);
  202. if (end - ptr < len) {
  203. throw Exception ("ZRLE decode error");
  204. }
  205. while (len-- > 0) *ptr++ = pix;
  206. }
  207. } else {
  208. // palette RLE
  209. T* ptr = buf;
  210. T* end = ptr + t.area();
  211. while (ptr < end) {
  212. zlibHasData(zis, 1);
  213. int index = zis->readU8();
  214. int len = 1;
  215. if (index & 128) {
  216. int b;
  217. do {
  218. zlibHasData(zis, 1);
  219. b = zis->readU8();
  220. len += b;
  221. } while (b == 255);
  222. if (end - ptr < len) {
  223. throw Exception ("ZRLE decode error");
  224. }
  225. }
  226. index &= 127;
  227. T pix = palette[index];
  228. while (len-- > 0) *ptr++ = pix;
  229. }
  230. }
  231. }
  232. pb->imageRect(pf, t, buf);
  233. }
  234. }
  235. zis->flushUnderlying();
  236. zis->setUnderlying(NULL, 0);
  237. }