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.

SMsgReader.cxx 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2019 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 <stdio.h>
  23. #include <vector>
  24. #include <rdr/InStream.h>
  25. #include <rdr/ZlibInStream.h>
  26. #include <rfb/msgTypes.h>
  27. #include <rfb/qemuTypes.h>
  28. #include <rfb/clipboardTypes.h>
  29. #include <rfb/Exception.h>
  30. #include <rfb/util.h>
  31. #include <rfb/SMsgHandler.h>
  32. #include <rfb/SMsgReader.h>
  33. #include <rfb/Configuration.h>
  34. #include <rfb/LogWriter.h>
  35. using namespace rfb;
  36. static LogWriter vlog("SMsgReader");
  37. static IntParameter maxCutText("MaxCutText", "Maximum permitted length of an incoming clipboard update", 256*1024);
  38. SMsgReader::SMsgReader(SMsgHandler* handler_, rdr::InStream* is_)
  39. : handler(handler_), is(is_), state(MSGSTATE_IDLE)
  40. {
  41. }
  42. SMsgReader::~SMsgReader()
  43. {
  44. }
  45. bool SMsgReader::readClientInit()
  46. {
  47. if (!is->hasData(1))
  48. return false;
  49. bool shared = is->readU8();
  50. handler->clientInit(shared);
  51. return true;
  52. }
  53. bool SMsgReader::readMsg()
  54. {
  55. bool ret;
  56. if (state == MSGSTATE_IDLE) {
  57. if (!is->hasData(1))
  58. return false;
  59. currentMsgType = is->readU8();
  60. state = MSGSTATE_MESSAGE;
  61. }
  62. switch (currentMsgType) {
  63. case msgTypeSetPixelFormat:
  64. ret = readSetPixelFormat();
  65. break;
  66. case msgTypeSetEncodings:
  67. ret = readSetEncodings();
  68. break;
  69. case msgTypeSetDesktopSize:
  70. ret = readSetDesktopSize();
  71. break;
  72. case msgTypeFramebufferUpdateRequest:
  73. ret = readFramebufferUpdateRequest();
  74. break;
  75. case msgTypeEnableContinuousUpdates:
  76. ret = readEnableContinuousUpdates();
  77. break;
  78. case msgTypeClientFence:
  79. ret = readFence();
  80. break;
  81. case msgTypeKeyEvent:
  82. ret = readKeyEvent();
  83. break;
  84. case msgTypePointerEvent:
  85. ret = readPointerEvent();
  86. break;
  87. case msgTypeClientCutText:
  88. ret = readClientCutText();
  89. break;
  90. case msgTypeQEMUClientMessage:
  91. ret = readQEMUMessage();
  92. break;
  93. default:
  94. vlog.error("unknown message type %d", currentMsgType);
  95. throw Exception("unknown message type");
  96. }
  97. if (ret)
  98. state = MSGSTATE_IDLE;
  99. return ret;
  100. }
  101. bool SMsgReader::readSetPixelFormat()
  102. {
  103. if (!is->hasData(3 + 16))
  104. return false;
  105. is->skip(3);
  106. PixelFormat pf;
  107. pf.read(is);
  108. handler->setPixelFormat(pf);
  109. return true;
  110. }
  111. bool SMsgReader::readSetEncodings()
  112. {
  113. if (!is->hasData(1 + 2))
  114. return false;
  115. is->setRestorePoint();
  116. is->skip(1);
  117. int nEncodings = is->readU16();
  118. if (!is->hasDataOrRestore(nEncodings * 4))
  119. return false;
  120. is->clearRestorePoint();
  121. std::vector<int32_t> encodings(nEncodings);
  122. for (size_t i = 0; i < encodings.size(); i++)
  123. encodings[i] = is->readU32();
  124. handler->setEncodings(nEncodings, encodings.data());
  125. return true;
  126. }
  127. bool SMsgReader::readSetDesktopSize()
  128. {
  129. int width, height;
  130. int screens, i;
  131. uint32_t id, flags;
  132. int sx, sy, sw, sh;
  133. ScreenSet layout;
  134. if (!is->hasData(1 + 2 + 2 + 1 + 1))
  135. return true;
  136. is->setRestorePoint();
  137. is->skip(1);
  138. width = is->readU16();
  139. height = is->readU16();
  140. screens = is->readU8();
  141. is->skip(1);
  142. if (!is->hasDataOrRestore(screens * (4 + 2 + 2 + 2 + 2 + 4)))
  143. return false;
  144. is->clearRestorePoint();
  145. for (i = 0;i < screens;i++) {
  146. id = is->readU32();
  147. sx = is->readU16();
  148. sy = is->readU16();
  149. sw = is->readU16();
  150. sh = is->readU16();
  151. flags = is->readU32();
  152. layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
  153. }
  154. handler->setDesktopSize(width, height, layout);
  155. return true;
  156. }
  157. bool SMsgReader::readFramebufferUpdateRequest()
  158. {
  159. if (!is->hasData(1 + 2 + 2 + 2 + 2))
  160. return false;
  161. bool inc = is->readU8();
  162. int x = is->readU16();
  163. int y = is->readU16();
  164. int w = is->readU16();
  165. int h = is->readU16();
  166. handler->framebufferUpdateRequest(Rect(x, y, x+w, y+h), inc);
  167. return true;
  168. }
  169. bool SMsgReader::readEnableContinuousUpdates()
  170. {
  171. bool enable;
  172. int x, y, w, h;
  173. if (!is->hasData(1 + 2 + 2 + 2 + 2))
  174. return false;
  175. enable = is->readU8();
  176. x = is->readU16();
  177. y = is->readU16();
  178. w = is->readU16();
  179. h = is->readU16();
  180. handler->enableContinuousUpdates(enable, x, y, w, h);
  181. return true;
  182. }
  183. bool SMsgReader::readFence()
  184. {
  185. uint32_t flags;
  186. uint8_t len;
  187. char data[64];
  188. if (!is->hasData(3 + 4 + 1))
  189. return false;
  190. is->setRestorePoint();
  191. is->skip(3);
  192. flags = is->readU32();
  193. len = is->readU8();
  194. if (!is->hasDataOrRestore(len))
  195. return false;
  196. is->clearRestorePoint();
  197. if (len > sizeof(data)) {
  198. vlog.error("Ignoring fence with too large payload");
  199. is->skip(len);
  200. return true;
  201. }
  202. is->readBytes(data, len);
  203. handler->fence(flags, len, data);
  204. return true;
  205. }
  206. bool SMsgReader::readKeyEvent()
  207. {
  208. if (!is->hasData(1 + 2 + 4))
  209. return false;
  210. bool down = is->readU8();
  211. is->skip(2);
  212. uint32_t key = is->readU32();
  213. handler->keyEvent(key, 0, down);
  214. return true;
  215. }
  216. bool SMsgReader::readPointerEvent()
  217. {
  218. if (!is->hasData(1 + 2 + 2))
  219. return false;
  220. int mask = is->readU8();
  221. int x = is->readU16();
  222. int y = is->readU16();
  223. handler->pointerEvent(Point(x, y), mask);
  224. return true;
  225. }
  226. bool SMsgReader::readClientCutText()
  227. {
  228. if (!is->hasData(3 + 4))
  229. return false;
  230. is->setRestorePoint();
  231. is->skip(3);
  232. uint32_t len = is->readU32();
  233. if (len & 0x80000000) {
  234. int32_t slen = len;
  235. slen = -slen;
  236. if (readExtendedClipboard(slen)) {
  237. is->clearRestorePoint();
  238. return true;
  239. } else {
  240. is->gotoRestorePoint();
  241. return false;
  242. }
  243. }
  244. if (!is->hasDataOrRestore(len))
  245. return false;
  246. is->clearRestorePoint();
  247. if (len > (size_t)maxCutText) {
  248. is->skip(len);
  249. vlog.error("Cut text too long (%d bytes) - ignoring", len);
  250. return true;
  251. }
  252. CharArray ca(len);
  253. is->readBytes(ca.buf, len);
  254. std::string filtered(convertLF(ca.buf, len));
  255. handler->clientCutText(filtered.c_str());
  256. return true;
  257. }
  258. bool SMsgReader::readExtendedClipboard(int32_t len)
  259. {
  260. uint32_t flags;
  261. uint32_t action;
  262. if (!is->hasData(len))
  263. return false;
  264. if (len < 4)
  265. throw Exception("Invalid extended clipboard message");
  266. if (len > maxCutText) {
  267. vlog.error("Extended clipboard message too long (%d bytes) - ignoring", len);
  268. is->skip(len);
  269. return true;
  270. }
  271. flags = is->readU32();
  272. action = flags & clipboardActionMask;
  273. if (action & clipboardCaps) {
  274. int i;
  275. size_t num;
  276. uint32_t lengths[16];
  277. num = 0;
  278. for (i = 0;i < 16;i++) {
  279. if (flags & (1 << i))
  280. num++;
  281. }
  282. if (len < (int32_t)(4 + 4*num))
  283. throw Exception("Invalid extended clipboard message");
  284. num = 0;
  285. for (i = 0;i < 16;i++) {
  286. if (flags & (1 << i))
  287. lengths[num++] = is->readU32();
  288. }
  289. handler->handleClipboardCaps(flags, lengths);
  290. } else if (action == clipboardProvide) {
  291. rdr::ZlibInStream zis;
  292. int i;
  293. size_t num;
  294. size_t lengths[16];
  295. uint8_t* buffers[16];
  296. zis.setUnderlying(is, len - 4);
  297. num = 0;
  298. for (i = 0;i < 16;i++) {
  299. if (!(flags & 1 << i))
  300. continue;
  301. if (!zis.hasData(4))
  302. throw Exception("Extended clipboard decode error");
  303. lengths[num] = zis.readU32();
  304. if (lengths[num] > (size_t)maxCutText) {
  305. vlog.error("Extended clipboard data too long (%d bytes) - ignoring",
  306. (unsigned)lengths[num]);
  307. // Slowly (safely) drain away the data
  308. while (lengths[num] > 0) {
  309. size_t chunk;
  310. if (!zis.hasData(1))
  311. throw Exception("Extended clipboard decode error");
  312. chunk = zis.avail();
  313. if (chunk > lengths[num])
  314. chunk = lengths[num];
  315. zis.skip(chunk);
  316. lengths[num] -= chunk;
  317. }
  318. flags &= ~(1 << i);
  319. continue;
  320. }
  321. if (!zis.hasData(lengths[num]))
  322. throw Exception("Extended clipboard decode error");
  323. buffers[num] = new uint8_t[lengths[num]];
  324. zis.readBytes(buffers[num], lengths[num]);
  325. num++;
  326. }
  327. zis.flushUnderlying();
  328. zis.setUnderlying(NULL, 0);
  329. handler->handleClipboardProvide(flags, lengths, buffers);
  330. num = 0;
  331. for (i = 0;i < 16;i++) {
  332. if (!(flags & 1 << i))
  333. continue;
  334. delete [] buffers[num++];
  335. }
  336. } else {
  337. switch (action) {
  338. case clipboardRequest:
  339. handler->handleClipboardRequest(flags);
  340. break;
  341. case clipboardPeek:
  342. handler->handleClipboardPeek();
  343. break;
  344. case clipboardNotify:
  345. handler->handleClipboardNotify(flags);
  346. break;
  347. default:
  348. throw Exception("Invalid extended clipboard action");
  349. }
  350. }
  351. return true;
  352. }
  353. bool SMsgReader::readQEMUMessage()
  354. {
  355. int subType;
  356. bool ret;
  357. if (!is->hasData(1))
  358. return false;
  359. is->setRestorePoint();
  360. subType = is->readU8();
  361. switch (subType) {
  362. case qemuExtendedKeyEvent:
  363. ret = readQEMUKeyEvent();
  364. break;
  365. default:
  366. throw Exception("unknown QEMU submessage type %d", subType);
  367. }
  368. if (!ret) {
  369. is->gotoRestorePoint();
  370. return false;
  371. } else {
  372. is->clearRestorePoint();
  373. return true;
  374. }
  375. }
  376. bool SMsgReader::readQEMUKeyEvent()
  377. {
  378. if (!is->hasData(2 + 4 + 4))
  379. return false;
  380. bool down = is->readU16();
  381. uint32_t keysym = is->readU32();
  382. uint32_t keycode = is->readU32();
  383. if (!keycode) {
  384. vlog.error("Key event without keycode - ignoring");
  385. return true;
  386. }
  387. handler->keyEvent(keysym, keycode, down);
  388. return true;
  389. }