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.

CMsgReader.cxx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2017 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. #include <assert.h>
  20. #include <stdio.h>
  21. #include <rfb/msgTypes.h>
  22. #include <rdr/InStream.h>
  23. #include <rfb/Exception.h>
  24. #include <rfb/LogWriter.h>
  25. #include <rfb/util.h>
  26. #include <rfb/CMsgHandler.h>
  27. #include <rfb/CMsgReader.h>
  28. static rfb::LogWriter vlog("CMsgReader");
  29. using namespace rfb;
  30. CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
  31. : imageBufIdealSize(0), handler(handler_), is(is_),
  32. nUpdateRectsLeft(0)
  33. {
  34. }
  35. CMsgReader::~CMsgReader()
  36. {
  37. }
  38. void CMsgReader::readServerInit()
  39. {
  40. int width = is->readU16();
  41. int height = is->readU16();
  42. PixelFormat pf;
  43. pf.read(is);
  44. CharArray name(is->readString());
  45. handler->serverInit(width, height, pf, name.buf);
  46. }
  47. void CMsgReader::readMsg()
  48. {
  49. if (nUpdateRectsLeft == 0) {
  50. int type = is->readU8();
  51. switch (type) {
  52. case msgTypeSetColourMapEntries:
  53. readSetColourMapEntries();
  54. break;
  55. case msgTypeBell:
  56. readBell();
  57. break;
  58. case msgTypeServerCutText:
  59. readServerCutText();
  60. break;
  61. case msgTypeFramebufferUpdate:
  62. readFramebufferUpdate();
  63. break;
  64. case msgTypeServerFence:
  65. readFence();
  66. break;
  67. case msgTypeEndOfContinuousUpdates:
  68. readEndOfContinuousUpdates();
  69. break;
  70. default:
  71. vlog.error("unknown message type %d", type);
  72. throw Exception("unknown message type");
  73. }
  74. } else {
  75. int x = is->readU16();
  76. int y = is->readU16();
  77. int w = is->readU16();
  78. int h = is->readU16();
  79. int encoding = is->readS32();
  80. switch (encoding) {
  81. case pseudoEncodingLastRect:
  82. nUpdateRectsLeft = 1; // this rectangle is the last one
  83. break;
  84. case pseudoEncodingXCursor:
  85. readSetXCursor(w, h, Point(x,y));
  86. break;
  87. case pseudoEncodingCursor:
  88. readSetCursor(w, h, Point(x,y));
  89. break;
  90. case pseudoEncodingCursorWithAlpha:
  91. readSetCursorWithAlpha(w, h, Point(x,y));
  92. break;
  93. case pseudoEncodingVMwareCursor:
  94. readSetVMwareCursor(w, h, Point(x,y));
  95. break;
  96. case pseudoEncodingDesktopName:
  97. readSetDesktopName(x, y, w, h);
  98. break;
  99. case pseudoEncodingDesktopSize:
  100. handler->setDesktopSize(w, h);
  101. break;
  102. case pseudoEncodingExtendedDesktopSize:
  103. readExtendedDesktopSize(x, y, w, h);
  104. break;
  105. case pseudoEncodingLEDState:
  106. readLEDState();
  107. break;
  108. case pseudoEncodingVMwareLEDState:
  109. readVMwareLEDState();
  110. break;
  111. case pseudoEncodingQEMUKeyEvent:
  112. handler->supportsQEMUKeyEvent();
  113. break;
  114. default:
  115. readRect(Rect(x, y, x+w, y+h), encoding);
  116. break;
  117. };
  118. nUpdateRectsLeft--;
  119. if (nUpdateRectsLeft == 0)
  120. handler->framebufferUpdateEnd();
  121. }
  122. }
  123. void CMsgReader::readSetColourMapEntries()
  124. {
  125. is->skip(1);
  126. int firstColour = is->readU16();
  127. int nColours = is->readU16();
  128. rdr::U16Array rgbs(nColours * 3);
  129. for (int i = 0; i < nColours * 3; i++)
  130. rgbs.buf[i] = is->readU16();
  131. handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
  132. }
  133. void CMsgReader::readBell()
  134. {
  135. handler->bell();
  136. }
  137. void CMsgReader::readServerCutText()
  138. {
  139. is->skip(3);
  140. rdr::U32 len = is->readU32();
  141. if (len > 256*1024) {
  142. is->skip(len);
  143. vlog.error("cut text too long (%d bytes) - ignoring",len);
  144. return;
  145. }
  146. CharArray ca(len);
  147. is->readBytes(ca.buf, len);
  148. CharArray filtered(convertLF(ca.buf, len));
  149. handler->serverCutText(filtered.buf);
  150. }
  151. void CMsgReader::readFence()
  152. {
  153. rdr::U32 flags;
  154. rdr::U8 len;
  155. char data[64];
  156. is->skip(3);
  157. flags = is->readU32();
  158. len = is->readU8();
  159. if (len > sizeof(data)) {
  160. vlog.error("Ignoring fence with too large payload");
  161. is->skip(len);
  162. return;
  163. }
  164. is->readBytes(data, len);
  165. handler->fence(flags, len, data);
  166. }
  167. void CMsgReader::readEndOfContinuousUpdates()
  168. {
  169. handler->endOfContinuousUpdates();
  170. }
  171. void CMsgReader::readFramebufferUpdate()
  172. {
  173. is->skip(1);
  174. nUpdateRectsLeft = is->readU16();
  175. handler->framebufferUpdateStart();
  176. }
  177. void CMsgReader::readRect(const Rect& r, int encoding)
  178. {
  179. if ((r.br.x > handler->server.width()) ||
  180. (r.br.y > handler->server.height())) {
  181. vlog.error("Rect too big: %dx%d at %d,%d exceeds %dx%d",
  182. r.width(), r.height(), r.tl.x, r.tl.y,
  183. handler->server.width(), handler->server.height());
  184. throw Exception("Rect too big");
  185. }
  186. if (r.is_empty())
  187. vlog.error("zero size rect");
  188. handler->dataRect(r, encoding);
  189. }
  190. void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
  191. {
  192. if (width > maxCursorSize || height > maxCursorSize)
  193. throw Exception("Too big cursor");
  194. rdr::U8Array rgba(width*height*4);
  195. if (width * height > 0) {
  196. rdr::U8 pr, pg, pb;
  197. rdr::U8 sr, sg, sb;
  198. int data_len = ((width+7)/8) * height;
  199. int mask_len = ((width+7)/8) * height;
  200. rdr::U8Array data(data_len);
  201. rdr::U8Array mask(mask_len);
  202. int x, y;
  203. rdr::U8* out;
  204. pr = is->readU8();
  205. pg = is->readU8();
  206. pb = is->readU8();
  207. sr = is->readU8();
  208. sg = is->readU8();
  209. sb = is->readU8();
  210. is->readBytes(data.buf, data_len);
  211. is->readBytes(mask.buf, mask_len);
  212. int maskBytesPerRow = (width+7)/8;
  213. out = rgba.buf;
  214. for (y = 0;y < height;y++) {
  215. for (x = 0;x < width;x++) {
  216. int byte = y * maskBytesPerRow + x / 8;
  217. int bit = 7 - x % 8;
  218. if (data.buf[byte] & (1 << bit)) {
  219. out[0] = pr;
  220. out[1] = pg;
  221. out[2] = pb;
  222. } else {
  223. out[0] = sr;
  224. out[1] = sg;
  225. out[2] = sb;
  226. }
  227. if (mask.buf[byte] & (1 << bit))
  228. out[3] = 255;
  229. else
  230. out[3] = 0;
  231. out += 4;
  232. }
  233. }
  234. }
  235. handler->setCursor(width, height, hotspot, rgba.buf);
  236. }
  237. void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
  238. {
  239. if (width > maxCursorSize || height > maxCursorSize)
  240. throw Exception("Too big cursor");
  241. int data_len = width * height * (handler->server.pf().bpp/8);
  242. int mask_len = ((width+7)/8) * height;
  243. rdr::U8Array data(data_len);
  244. rdr::U8Array mask(mask_len);
  245. int x, y;
  246. rdr::U8Array rgba(width*height*4);
  247. rdr::U8* in;
  248. rdr::U8* out;
  249. is->readBytes(data.buf, data_len);
  250. is->readBytes(mask.buf, mask_len);
  251. int maskBytesPerRow = (width+7)/8;
  252. in = data.buf;
  253. out = rgba.buf;
  254. for (y = 0;y < height;y++) {
  255. for (x = 0;x < width;x++) {
  256. int byte = y * maskBytesPerRow + x / 8;
  257. int bit = 7 - x % 8;
  258. handler->server.pf().rgbFromBuffer(out, in, 1);
  259. if (mask.buf[byte] & (1 << bit))
  260. out[3] = 255;
  261. else
  262. out[3] = 0;
  263. in += handler->server.pf().bpp/8;
  264. out += 4;
  265. }
  266. }
  267. handler->setCursor(width, height, hotspot, rgba.buf);
  268. }
  269. void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
  270. {
  271. if (width > maxCursorSize || height > maxCursorSize)
  272. throw Exception("Too big cursor");
  273. int encoding;
  274. const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
  275. ManagedPixelBuffer pb(rgbaPF, width, height);
  276. PixelFormat origPF;
  277. rdr::U8* buf;
  278. int stride;
  279. encoding = is->readS32();
  280. origPF = handler->server.pf();
  281. handler->server.setPF(rgbaPF);
  282. handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
  283. handler->server.setPF(origPF);
  284. // On-wire data has pre-multiplied alpha, but we store it
  285. // non-pre-multiplied
  286. buf = pb.getBufferRW(pb.getRect(), &stride);
  287. assert(stride == width);
  288. for (int i = 0;i < pb.area();i++) {
  289. rdr::U8 alpha;
  290. alpha = buf[3];
  291. if (alpha == 0)
  292. alpha = 1; // Avoid division by zero
  293. buf[0] = (unsigned)buf[0] * 255/alpha;
  294. buf[1] = (unsigned)buf[1] * 255/alpha;
  295. buf[2] = (unsigned)buf[2] * 255/alpha;
  296. buf += 4;
  297. }
  298. pb.commitBufferRW(pb.getRect());
  299. handler->setCursor(width, height, hotspot,
  300. pb.getBuffer(pb.getRect(), &stride));
  301. }
  302. void CMsgReader::readSetVMwareCursor(int width, int height, const Point& hotspot)
  303. {
  304. if (width > maxCursorSize || height > maxCursorSize)
  305. throw Exception("Too big cursor");
  306. rdr::U8 type;
  307. type = is->readU8();
  308. is->skip(1);
  309. if (type == 0) {
  310. int len = width * height * (handler->server.pf().bpp/8);
  311. rdr::U8Array andMask(len);
  312. rdr::U8Array xorMask(len);
  313. rdr::U8Array data(width*height*4);
  314. rdr::U8* andIn;
  315. rdr::U8* xorIn;
  316. rdr::U8* out;
  317. int Bpp;
  318. is->readBytes(andMask.buf, len);
  319. is->readBytes(xorMask.buf, len);
  320. andIn = andMask.buf;
  321. xorIn = xorMask.buf;
  322. out = data.buf;
  323. Bpp = handler->server.pf().bpp/8;
  324. for (int y = 0;y < height;y++) {
  325. for (int x = 0;x < width;x++) {
  326. Pixel andPixel, xorPixel;
  327. andPixel = handler->server.pf().pixelFromBuffer(andIn);
  328. xorPixel = handler->server.pf().pixelFromBuffer(xorIn);
  329. andIn += Bpp;
  330. xorIn += Bpp;
  331. if (andPixel == 0) {
  332. rdr::U8 r, g, b;
  333. // Opaque pixel
  334. handler->server.pf().rgbFromPixel(xorPixel, &r, &g, &b);
  335. *out++ = r;
  336. *out++ = g;
  337. *out++ = b;
  338. *out++ = 0xff;
  339. } else if (xorPixel == 0) {
  340. // Fully transparent pixel
  341. *out++ = 0;
  342. *out++ = 0;
  343. *out++ = 0;
  344. *out++ = 0;
  345. } else if (andPixel == xorPixel) {
  346. // Inverted pixel
  347. // We don't really support this, so just turn the pixel black
  348. // FIXME: Do an outline like WinVNC does?
  349. *out++ = 0;
  350. *out++ = 0;
  351. *out++ = 0;
  352. *out++ = 0xff;
  353. } else {
  354. // Partially transparent/inverted pixel
  355. // We _really_ can't handle this, just make it black
  356. *out++ = 0;
  357. *out++ = 0;
  358. *out++ = 0;
  359. *out++ = 0xff;
  360. }
  361. }
  362. }
  363. handler->setCursor(width, height, hotspot, data.buf);
  364. } else if (type == 1) {
  365. rdr::U8Array data(width*height*4);
  366. // FIXME: Is alpha premultiplied?
  367. is->readBytes(data.buf, width*height*4);
  368. handler->setCursor(width, height, hotspot, data.buf);
  369. } else {
  370. throw Exception("Unknown cursor type");
  371. }
  372. }
  373. void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
  374. {
  375. char* name = is->readString();
  376. if (x || y || w || h) {
  377. vlog.error("Ignoring DesktopName rect with non-zero position/size");
  378. } else {
  379. handler->setName(name);
  380. }
  381. delete [] name;
  382. }
  383. void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
  384. {
  385. unsigned int screens, i;
  386. rdr::U32 id, flags;
  387. int sx, sy, sw, sh;
  388. ScreenSet layout;
  389. screens = is->readU8();
  390. is->skip(3);
  391. for (i = 0;i < screens;i++) {
  392. id = is->readU32();
  393. sx = is->readU16();
  394. sy = is->readU16();
  395. sw = is->readU16();
  396. sh = is->readU16();
  397. flags = is->readU32();
  398. layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
  399. }
  400. handler->setExtendedDesktopSize(x, y, w, h, layout);
  401. }
  402. void CMsgReader::readLEDState()
  403. {
  404. rdr::U8 state;
  405. state = is->readU8();
  406. handler->setLEDState(state);
  407. }
  408. void CMsgReader::readVMwareLEDState()
  409. {
  410. rdr::U32 state;
  411. state = is->readU32();
  412. // As luck has it, this extension uses the same bit definitions,
  413. // so no conversion required
  414. handler->setLEDState(state);
  415. }