1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include "rfbdecoder.h"
6 #include "d3des.h"
7 #include <gst/gst.h>
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #define RFB_GET_UINT32(ptr) GST_READ_UINT32_BE(ptr)
13 #define RFB_GET_UINT16(ptr) GST_READ_UINT16_BE(ptr)
14 #define RFB_GET_UINT8(ptr) GST_READ_UINT8(ptr)
15 
16 #define RFB_SET_UINT32(ptr, val) GST_WRITE_UINT32_BE((ptr),(val))
17 #define RFB_SET_UINT16(ptr, val) GST_WRITE_UINT16_BE((ptr),(val))
18 #define RFB_SET_UINT8(ptr, val) GST_WRITE_UINT8((ptr),(val))
19 
20 GST_DEBUG_CATEGORY_EXTERN (rfbdecoder_debug);
21 #define GST_CAT_DEFAULT rfbdecoder_debug
22 
23 
24 static gboolean rfb_decoder_state_wait_for_protocol_version (RfbDecoder *
25     decoder);
26 static gboolean rfb_decoder_state_wait_for_security (RfbDecoder * decoder);
27 static gboolean rfb_decoder_state_send_client_initialisation (RfbDecoder *
28     decoder);
29 static gboolean rfb_decoder_state_wait_for_server_initialisation (RfbDecoder *
30     decoder);
31 static gboolean rfb_decoder_state_security_result (RfbDecoder * decoder);
32 static gboolean rfb_decoder_state_normal (RfbDecoder * decoder);
33 static gboolean rfb_decoder_state_framebuffer_update (RfbDecoder * decoder);
34 static gboolean rfb_decoder_state_framebuffer_update_rectangle (RfbDecoder *
35     decoder);
36 static gboolean rfb_decoder_state_set_colour_map_entries (RfbDecoder * decoder);
37 static gboolean rfb_decoder_state_server_cut_text (RfbDecoder * decoder);
38 static gboolean rfb_decoder_raw_encoding (RfbDecoder * decoder, gint start_x,
39     gint start_y, gint rect_w, gint rect_h);
40 static gboolean rfb_decoder_copyrect_encoding (RfbDecoder * decoder,
41     gint start_x, gint start_y, gint rect_w, gint rect_h);
42 static gboolean rfb_decoder_rre_encoding (RfbDecoder * decoder, gint start_x,
43     gint start_y, gint rect_w, gint rect_h);
44 static gboolean rfb_decoder_corre_encoding (RfbDecoder * decoder, gint start_x,
45     gint start_y, gint rect_w, gint rect_h);
46 static gboolean rfb_decoder_hextile_encoding (RfbDecoder * decoder,
47     gint start_x, gint start_y, gint rect_w, gint rect_h);
48 
49 RfbDecoder *
rfb_decoder_new(void)50 rfb_decoder_new (void)
51 {
52   RfbDecoder *decoder = g_new0 (RfbDecoder, 1);
53 
54   decoder->socket_client = g_socket_client_new ();
55   decoder->connection = NULL;
56   decoder->cancellable = g_cancellable_new ();
57 
58   decoder->password = NULL;
59 
60   decoder->use_copyrect = FALSE;
61 
62   decoder->offset_x = 0;
63   decoder->offset_y = 0;
64   decoder->rect_width = 0;
65   decoder->rect_height = 0;
66   decoder->shared_flag = TRUE;
67   decoder->data = NULL;
68   decoder->data_len = 0;
69   decoder->error = NULL;
70 
71   g_mutex_init (&decoder->write_lock);
72 
73   return decoder;
74 }
75 
76 void
rfb_decoder_free(RfbDecoder * decoder)77 rfb_decoder_free (RfbDecoder * decoder)
78 {
79   g_return_if_fail (decoder != NULL);
80 
81   rfb_decoder_disconnect (decoder);
82 
83   g_clear_object (&decoder->socket_client);
84   g_clear_object (&decoder->cancellable);
85   g_mutex_clear (&decoder->write_lock);
86   g_free (decoder);
87 }
88 
89 gboolean
rfb_decoder_connect_tcp(RfbDecoder * decoder,gchar * host,guint port)90 rfb_decoder_connect_tcp (RfbDecoder * decoder, gchar * host, guint port)
91 {
92   GError *err = NULL;
93   GSocketConnection *connection;
94 
95   GST_DEBUG ("connecting to the rfb server");
96 
97   g_return_val_if_fail (decoder != NULL, FALSE);
98   g_return_val_if_fail (decoder->connection == NULL, FALSE);
99   g_return_val_if_fail (host != NULL, FALSE);
100 
101   g_cancellable_reset (decoder->cancellable);
102 
103   connection =
104       g_socket_client_connect_to_host (decoder->socket_client, host, port,
105       decoder->cancellable, &err);
106 
107   if (!connection)
108     goto connect_failed;
109 
110   decoder->connection = connection;
111 
112   return TRUE;
113 
114 connect_failed:
115   {
116     if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
117       GST_DEBUG ("Cancelled connecting");
118     } else {
119       GST_WARNING ("Failed to connect to host '%s:%d': %s", host, port,
120           err->message);
121       if (decoder->error == NULL) {
122         decoder->error = err;
123         err = NULL;
124       }
125     }
126     g_clear_error (&err);
127     return FALSE;
128   }
129 }
130 
131 void
rfb_decoder_disconnect(RfbDecoder * decoder)132 rfb_decoder_disconnect (RfbDecoder * decoder)
133 {
134   GST_DEBUG ("Disconnecting from the rfb server");
135 
136   g_return_if_fail (decoder);
137   g_return_if_fail (decoder->cancellable);
138 
139   g_cancellable_cancel (decoder->cancellable);
140 
141   /* Make sure threaded write a done first, this avoids race condition,
142    * specially when the decoder is freed */
143   g_mutex_lock (&decoder->write_lock);
144 
145   g_clear_object (&decoder->connection);
146   g_clear_error (&decoder->error);
147   g_clear_pointer (&decoder->data, g_free);
148 
149   g_mutex_unlock (&decoder->write_lock);
150 }
151 
152 /**
153  * rfb_decoder_iterate:
154  * @decoder: The rfb context
155  *
156  * Initializes the connection with the rfb server
157  *
158  * Returns: TRUE if initialization was succesfull, FALSE on fail.
159  */
160 gboolean
rfb_decoder_iterate(RfbDecoder * decoder)161 rfb_decoder_iterate (RfbDecoder * decoder)
162 {
163   gboolean ret;
164 
165   g_return_val_if_fail (decoder != NULL, FALSE);
166   g_return_val_if_fail (decoder->connection != NULL, FALSE);
167 
168   if (decoder->state == NULL) {
169     GST_DEBUG ("First iteration: set state to -> wait for protocol version");
170     decoder->state = rfb_decoder_state_wait_for_protocol_version;
171   }
172 
173   GST_DEBUG ("Executing next state in initialization");
174   ret = decoder->state (decoder);
175 
176   if (ret == FALSE) {
177     if (decoder->error == NULL)
178       GST_WARNING ("Failure, but no error stored");
179     else
180       GST_WARNING ("Failure: %s", decoder->error->message);
181   }
182 
183   return ret;
184 }
185 
186 static guint8 *
rfb_decoder_read(RfbDecoder * decoder,guint32 len)187 rfb_decoder_read (RfbDecoder * decoder, guint32 len)
188 {
189   GInputStream *in;
190   GError *err = NULL;
191   gsize count = 0;
192 
193   if (!decoder->connection)
194     return FALSE;
195 
196   g_return_val_if_fail (len > 0, NULL);
197 
198   in = g_io_stream_get_input_stream (G_IO_STREAM (decoder->connection));
199 
200   g_return_val_if_fail (in != NULL, NULL);
201 
202   if (G_UNLIKELY (len > decoder->data_len)) {
203     g_free (decoder->data);
204     decoder->data = g_malloc (len);
205     decoder->data_len = len;
206   }
207 
208   if (!g_input_stream_read_all (in, decoder->data, len, &count,
209           decoder->cancellable, &err))
210     goto recv_error;
211 
212   if (count == 0) {
213     g_set_error_literal (&err, G_IO_ERROR, G_IO_ERROR_BROKEN_PIPE,
214         "Connection was closed.");
215     goto recv_error;
216   }
217 
218   return decoder->data;
219 
220 recv_error:
221   {
222     if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
223       GST_DEBUG ("Read on socket cancelled");
224     } else {
225       GST_ERROR ("Read error on socket: %s", err->message);
226       if (decoder->error == NULL) {
227         decoder->error = err;
228         err = NULL;
229       }
230     }
231     g_clear_error (&err);
232     return NULL;
233   }
234 }
235 
236 static gboolean
rfb_decoder_send(RfbDecoder * decoder,guint8 * buffer,guint len)237 rfb_decoder_send (RfbDecoder * decoder, guint8 * buffer, guint len)
238 {
239   GOutputStream *out;
240   GError *err = NULL;
241 
242   if (!decoder->connection)
243     return FALSE;
244 
245   g_return_val_if_fail (buffer != NULL, 0);
246   g_return_val_if_fail (len > 0, 0);
247 
248   g_mutex_lock (&decoder->write_lock);
249 
250   out = g_io_stream_get_output_stream (G_IO_STREAM (decoder->connection));
251 
252   if (!g_output_stream_write_all (out, buffer, len, NULL, decoder->cancellable,
253           &err))
254     goto send_error;
255 
256   g_mutex_unlock (&decoder->write_lock);
257 
258   return TRUE;
259 
260 send_error:
261   {
262     if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
263       GST_DEBUG ("Send on socket cancelled");
264     } else {
265       GST_ERROR ("Send error on socket: %s", err->message);
266       if (decoder->error == NULL) {
267         decoder->error = err;
268         err = NULL;
269       }
270     }
271     g_clear_error (&err);
272     g_mutex_unlock (&decoder->write_lock);
273     return FALSE;
274   }
275 }
276 
277 void
rfb_decoder_send_update_request(RfbDecoder * decoder,gboolean incremental,gint x,gint y,gint width,gint height)278 rfb_decoder_send_update_request (RfbDecoder * decoder,
279     gboolean incremental, gint x, gint y, gint width, gint height)
280 {
281   guint8 data[10];
282 
283   g_return_if_fail (decoder != NULL);
284   g_return_if_fail (decoder->connection != NULL);
285 
286   data[0] = 3;
287   data[1] = incremental;
288   RFB_SET_UINT16 (data + 2, x);
289   RFB_SET_UINT16 (data + 4, y);
290   RFB_SET_UINT16 (data + 6, width);
291   RFB_SET_UINT16 (data + 8, height);
292 
293   rfb_decoder_send (decoder, data, 10);
294 
295   /* create a backup of the prev frame for copyrect encoding */
296   if (decoder->use_copyrect) {
297     memcpy (decoder->prev_frame, decoder->frame,
298         decoder->rect_width * decoder->rect_height * decoder->bpp / 8);
299   }
300 
301   decoder->state = rfb_decoder_state_normal;
302 }
303 
304 void
rfb_decoder_send_key_event(RfbDecoder * decoder,guint key,gboolean down_flag)305 rfb_decoder_send_key_event (RfbDecoder * decoder, guint key, gboolean down_flag)
306 {
307   guint8 data[8];
308 
309   g_return_if_fail (decoder != NULL);
310   g_return_if_fail (decoder->connection != NULL);
311 
312   data[0] = 4;
313   data[1] = down_flag;
314   RFB_SET_UINT16 (data + 2, 0);
315   RFB_SET_UINT32 (data + 4, key);
316 
317   rfb_decoder_send (decoder, data, 8);
318 }
319 
320 void
rfb_decoder_send_pointer_event(RfbDecoder * decoder,gint button_mask,gint x,gint y)321 rfb_decoder_send_pointer_event (RfbDecoder * decoder,
322     gint button_mask, gint x, gint y)
323 {
324   guint8 data[6];
325 
326   g_return_if_fail (decoder != NULL);
327   g_return_if_fail (decoder->connection != NULL);
328 
329   data[0] = 5;
330   data[1] = button_mask;
331   RFB_SET_UINT16 (data + 2, x);
332   RFB_SET_UINT16 (data + 4, y);
333 
334   rfb_decoder_send (decoder, data, 6);
335 }
336 
337 /**
338  * rfb_decoder_state_wait_for_protocol_version:
339  *
340  * Negotiate the rfb version used
341  */
342 static gboolean
rfb_decoder_state_wait_for_protocol_version(RfbDecoder * decoder)343 rfb_decoder_state_wait_for_protocol_version (RfbDecoder * decoder)
344 {
345   gchar version_str[] = "RFB 003.003\n";
346 
347   if (!rfb_decoder_read (decoder, 12))
348     return FALSE;
349 
350   g_return_val_if_fail (memcmp (decoder->data, "RFB 003.", 8) == 0, FALSE);
351   g_return_val_if_fail (*(decoder->data + 11) == 0x0a, FALSE);
352 
353   GST_DEBUG ("\"%.11s\"", decoder->data);
354   *(decoder->data) = 0x00;
355   *(decoder->data + 11) = 0x00;
356   decoder->protocol_major = atoi ((char *) (decoder->data + 4));
357   decoder->protocol_minor = atoi ((char *) (decoder->data + 8));
358   GST_DEBUG ("Major version : %d", decoder->protocol_major);
359   GST_DEBUG ("Minor version : %d", decoder->protocol_minor);
360 
361   if (decoder->protocol_major != 3) {
362     GST_INFO
363         ("A major protocol version of %d is not supported, falling back to 3",
364         decoder->protocol_major);
365     decoder->protocol_major = 3;
366     decoder->protocol_minor = 3;
367   }
368   switch (decoder->protocol_minor) {
369     case 3:
370     case 7:
371     case 8:
372       break;
373     default:
374       GST_INFO ("Minor version %d is not supported, using 3",
375           decoder->protocol_minor);
376       decoder->protocol_minor = 3;
377   }
378 
379   version_str[10] = '0' + decoder->protocol_minor;
380 
381   if (!rfb_decoder_send (decoder, (guint8 *) version_str, 12))
382     return FALSE;
383 
384   decoder->state = rfb_decoder_state_wait_for_security;
385   return TRUE;
386 }
387 
388 /*
389  * a string describing the reason (where a string is specified as a length
390  * followed by that many ASCII characters)
391  **/
392 static gboolean
rfb_decoder_state_reason(RfbDecoder * decoder)393 rfb_decoder_state_reason (RfbDecoder * decoder)
394 {
395   gint reason_length;
396 
397   if (!rfb_decoder_read (decoder, 4))
398     return FALSE;
399 
400   reason_length = RFB_GET_UINT32 (decoder->data);
401 
402   if (!rfb_decoder_read (decoder, reason_length))
403     return FALSE;
404 
405   GST_WARNING ("Reason by server: %s", decoder->data);
406 
407   if (decoder->error == NULL) {
408     decoder->error = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
409         "VNC server error: %s", decoder->data);
410   }
411 
412   return FALSE;
413 }
414 
415 static gboolean
rfb_decoder_state_wait_for_security(RfbDecoder * decoder)416 rfb_decoder_state_wait_for_security (RfbDecoder * decoder)
417 {
418   /*
419    * Version 3.3 The server decides the security type and sends a single word
420    *
421    * The security-type may only take the value 0, 1 or 2. A value of 0 means that the
422    * connection has failed and is followed by a string giving the reason, as described
423    * above.
424    */
425   if (IS_VERSION_3_3 (decoder)) {
426     if (!rfb_decoder_read (decoder, 4))
427       return FALSE;
428 
429     decoder->security_type = RFB_GET_UINT32 (decoder->data);
430     GST_DEBUG ("security = %d", decoder->security_type);
431 
432     g_return_val_if_fail (decoder->security_type < 3, FALSE);
433 
434     if (decoder->security_type == SECURITY_FAIL) {
435       decoder->state = rfb_decoder_state_reason;
436       return TRUE;
437     }
438   } else {
439     guint8 num_type;
440     gint i;
441     guint8 *type = NULL;
442 
443     if (!rfb_decoder_read (decoder, 1))
444       return FALSE;
445 
446     num_type = RFB_GET_UINT8 (decoder->data);
447     if (num_type == 0) {
448       decoder->state = rfb_decoder_state_reason;
449       return TRUE;
450     }
451 
452     if (!rfb_decoder_read (decoder, num_type))
453       return FALSE;
454 
455     decoder->security_type = SECURITY_FAIL;
456 
457     /* For now, simply pick the first support security method */
458     for (i = 0; i < num_type; i++) {
459       guint val = RFB_GET_UINT8 (decoder->data + i);
460 
461       GST_DEBUG ("Server supports security type %u", val);
462 
463       if (val == SECURITY_NONE || val == SECURITY_VNC) {
464         decoder->security_type = val;
465         type = decoder->data + i;
466         break;
467       }
468     }
469 
470     if (!type) {
471       GST_WARNING ("Security type negotiation failed.");
472       decoder->error = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
473           "VNC server requires unsupported security method.");
474       return FALSE;
475     }
476 
477     GST_DEBUG ("security = %d", decoder->security_type);
478     if (!rfb_decoder_send (decoder, type, 1))
479       return FALSE;
480   }
481 
482   switch (decoder->security_type) {
483     case SECURITY_NONE:
484       GST_DEBUG ("Security type is None");
485       if (IS_VERSION_3_8 (decoder)) {
486         decoder->state = rfb_decoder_state_security_result;
487       } else {
488         decoder->state = rfb_decoder_state_send_client_initialisation;
489       }
490       break;
491     case SECURITY_VNC:{
492       unsigned char key[8], *challenge;
493       DESContext des_ctx;
494       gsize password_len;
495 
496       /*
497        * VNC authentication is to be used and protocol data is to be sent
498        * unencrypted. The server sends a random 16-byte challenge
499        */
500       GST_DEBUG ("Security type is VNC Authentication");
501       /* VNC Authentication can't be used if the password is not set */
502       if (!decoder->password) {
503         GST_WARNING
504             ("VNC Authentication can't be used if the password is not set");
505         decoder->error =
506             g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
507             "VNC servers needs authentication, but no password set");
508         return FALSE;
509       }
510 
511       /* key is 8 bytes and made up of password (padded with 0s if needed) */
512       memset (key, 0, 8);
513       password_len = strlen (decoder->password);
514       memcpy (key, decoder->password, MIN (password_len, 8));
515 
516       /* read challenge */
517       challenge = rfb_decoder_read (decoder, 16);
518       if (challenge == NULL)
519         return FALSE;
520 
521       /* encrypt 16 challenge bytes in place using key */
522       memset (&des_ctx, 0, sizeof (DESContext));
523       deskey (&des_ctx, key, EN0);
524       des (&des_ctx, challenge, challenge);
525       des (&des_ctx, challenge + 8, challenge + 8);
526 
527       /* .. and send back to server */
528       if (!rfb_decoder_send (decoder, challenge, 16))
529         return FALSE;
530 
531       GST_DEBUG ("Encrypted challenge sent to server");
532 
533       decoder->state = rfb_decoder_state_security_result;
534       break;
535     }
536     default:
537       GST_WARNING ("Security type is not known");
538       return FALSE;
539       break;
540   }
541   return TRUE;
542 }
543 
544 /*
545  * The server sends a word to inform the client whether the security handshaking was
546  * successful.
547  */
548 static gboolean
rfb_decoder_state_security_result(RfbDecoder * decoder)549 rfb_decoder_state_security_result (RfbDecoder * decoder)
550 {
551   if (!rfb_decoder_read (decoder, 4))
552     return FALSE;
553 
554   if (RFB_GET_UINT32 (decoder->data) != 0) {
555     GST_WARNING ("Security handshaking failed");
556     if (IS_VERSION_3_8 (decoder)) {
557       decoder->state = rfb_decoder_state_reason;
558       return TRUE;
559     }
560     if (decoder->error == NULL) {
561       decoder->error = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
562           "authentication failed");
563     }
564     return FALSE;
565   }
566 
567   GST_DEBUG ("Security handshaking succesful");
568   decoder->state = rfb_decoder_state_send_client_initialisation;
569 
570   return TRUE;
571 }
572 
573 static guint8 *
rfb_decoder_message_set_encodings(GSList * encodings_list)574 rfb_decoder_message_set_encodings (GSList * encodings_list)
575 {
576 
577   guint8 *message = g_malloc0 (4 + 4 * g_slist_length (encodings_list));
578   guint32 *encoding_type;
579 
580   message[0] = 0x02;            /* message type */
581   RFB_SET_UINT16 (message + 2, g_slist_length (encodings_list));        /* number of encodings */
582 
583   /* write all the encoding types */
584   encoding_type = (guint32 *) (message + 4);
585 
586   while (encodings_list) {
587     RFB_SET_UINT32 (encoding_type, GPOINTER_TO_UINT (encodings_list->data));
588     encoding_type++;
589     encodings_list = encodings_list->next;
590   }
591 
592   return message;
593 }
594 
595 /*
596  * rfb_decoder_state_set_encodings:
597  * @decoder: The rfb context
598  *
599  * Sends the encoding types that the client can decode to the server
600  *
601  * Returns: TRUE if initialization was succesfull, FALSE on fail.
602  */
603 static gboolean
rfb_decoder_state_set_encodings(RfbDecoder * decoder)604 rfb_decoder_state_set_encodings (RfbDecoder * decoder)
605 {
606   GSList *encoder_list = NULL;
607   guint8 *message;
608 
609   GST_DEBUG ("entered set encodings");
610 
611   encoder_list =
612       g_slist_append (encoder_list, GUINT_TO_POINTER (ENCODING_TYPE_HEXTILE));
613   encoder_list =
614       g_slist_append (encoder_list, GUINT_TO_POINTER (ENCODING_TYPE_CORRE));
615   encoder_list =
616       g_slist_append (encoder_list, GUINT_TO_POINTER (ENCODING_TYPE_RRE));
617   if (decoder->use_copyrect) {
618     encoder_list =
619         g_slist_append (encoder_list,
620         GUINT_TO_POINTER (ENCODING_TYPE_COPYRECT));
621   }
622   encoder_list =
623       g_slist_append (encoder_list, GUINT_TO_POINTER (ENCODING_TYPE_RAW));
624 
625   message = rfb_decoder_message_set_encodings (encoder_list);
626 
627   if (!rfb_decoder_send (decoder, message,
628           4 + 4 * g_slist_length (encoder_list))) {
629     g_free (message);
630     return FALSE;
631   }
632 
633   g_free (message);
634 
635   decoder->state = rfb_decoder_state_normal;
636   decoder->inited = TRUE;
637 
638   return TRUE;
639 }
640 
641 static gboolean
rfb_decoder_state_send_client_initialisation(RfbDecoder * decoder)642 rfb_decoder_state_send_client_initialisation (RfbDecoder * decoder)
643 {
644   guint8 shared_flag;
645 
646   shared_flag = decoder->shared_flag;
647 
648   if (!rfb_decoder_send (decoder, &shared_flag, 1))
649     return FALSE;
650 
651   GST_DEBUG ("shared_flag is %d", shared_flag);
652 
653   decoder->state = rfb_decoder_state_wait_for_server_initialisation;
654   return TRUE;
655 }
656 
657 static gboolean
rfb_decoder_state_wait_for_server_initialisation(RfbDecoder * decoder)658 rfb_decoder_state_wait_for_server_initialisation (RfbDecoder * decoder)
659 {
660   guint32 name_length;
661 
662   if (!rfb_decoder_read (decoder, 24))
663     return FALSE;
664 
665   decoder->width = RFB_GET_UINT16 (decoder->data + 0);
666   decoder->height = RFB_GET_UINT16 (decoder->data + 2);
667   decoder->bpp = RFB_GET_UINT8 (decoder->data + 4);
668   decoder->depth = RFB_GET_UINT8 (decoder->data + 5);
669   decoder->big_endian = RFB_GET_UINT8 (decoder->data + 6);
670   decoder->true_colour = RFB_GET_UINT8 (decoder->data + 7);
671   decoder->red_max = RFB_GET_UINT16 (decoder->data + 8);
672   decoder->green_max = RFB_GET_UINT16 (decoder->data + 10);
673   decoder->blue_max = RFB_GET_UINT16 (decoder->data + 12);
674   decoder->red_shift = RFB_GET_UINT8 (decoder->data + 14);
675   decoder->green_shift = RFB_GET_UINT8 (decoder->data + 15);
676   decoder->blue_shift = RFB_GET_UINT8 (decoder->data + 16);
677 
678   GST_DEBUG ("Server Initialization");
679   GST_DEBUG ("width      = %d", decoder->width);
680   GST_DEBUG ("height     = %d", decoder->height);
681   GST_DEBUG ("bpp        = %d", decoder->bpp);
682   GST_DEBUG ("depth      = %d", decoder->depth);
683   GST_DEBUG ("big_endian = %d", decoder->big_endian);
684   GST_DEBUG ("true_colour= %d", decoder->true_colour);
685   GST_DEBUG ("red_max    = %d", decoder->red_max);
686   GST_DEBUG ("green_max  = %d", decoder->green_max);
687   GST_DEBUG ("blue_max   = %d", decoder->blue_max);
688   GST_DEBUG ("red_shift  = %d", decoder->red_shift);
689   GST_DEBUG ("green_shift= %d", decoder->green_shift);
690   GST_DEBUG ("blue_shift = %d", decoder->blue_shift);
691 
692   name_length = RFB_GET_UINT32 (decoder->data + 20);
693 
694   if (!rfb_decoder_read (decoder, name_length))
695     return FALSE;
696 
697   decoder->name = g_strndup ((gchar *) (decoder->data), name_length);
698   GST_DEBUG ("name       = %s", decoder->name);
699 
700   /* check if we need cropping */
701 
702   if (decoder->offset_x > 0) {
703     if (decoder->offset_x > decoder->width) {
704       GST_WARNING
705           ("Trying to crop more than the width of the server.  Setting offset-x to 0.");
706       decoder->offset_x = 0;
707     } else {
708       decoder->width -= decoder->offset_x;
709     }
710   }
711   if (decoder->offset_y > 0) {
712     if (decoder->offset_y > decoder->height) {
713       GST_WARNING
714           ("Trying to crop more than the height of the server. Setting offset-y to 0.");
715       decoder->offset_y = 0;
716     } else {
717       decoder->height -= decoder->offset_y;
718     }
719   }
720   if (decoder->rect_width > 0) {
721     if (decoder->rect_width > decoder->width) {
722       GST_WARNING
723           ("Trying to crop more than the width of the server. Setting width to %u.",
724           decoder->width);
725       decoder->rect_width = decoder->width;
726     } else {
727       decoder->width = decoder->rect_width;
728     }
729   }
730   if (decoder->rect_height > 0) {
731     if (decoder->rect_height > decoder->height) {
732       GST_WARNING
733           ("Trying to crop more than the height of the server. Setting height to %u.",
734           decoder->height);
735       decoder->rect_height = decoder->height;
736     } else {
737       decoder->height = decoder->rect_height;
738     }
739   }
740 
741   decoder->state = rfb_decoder_state_set_encodings;
742   return TRUE;
743 }
744 
745 static gboolean
rfb_decoder_state_normal(RfbDecoder * decoder)746 rfb_decoder_state_normal (RfbDecoder * decoder)
747 {
748   gint message_type;
749 
750   GST_DEBUG ("decoder_state_normal");
751 
752   if (!rfb_decoder_read (decoder, 1))
753     return FALSE;
754 
755   message_type = RFB_GET_UINT8 (decoder->data);
756 
757   switch (message_type) {
758     case MESSAGE_TYPE_FRAMEBUFFER_UPDATE:
759       GST_DEBUG ("Receiving framebuffer update");
760       decoder->state = rfb_decoder_state_framebuffer_update;
761       break;
762     case 1:
763       decoder->state = rfb_decoder_state_set_colour_map_entries;
764       break;
765     case 2:
766       /* bell, ignored */
767       decoder->state = rfb_decoder_state_normal;
768       break;
769     case 3:
770       decoder->state = rfb_decoder_state_server_cut_text;
771       break;
772     default:
773       g_critical ("unknown message type %d", message_type);
774   }
775 
776   return TRUE;
777 }
778 
779 static gboolean
rfb_decoder_state_framebuffer_update(RfbDecoder * decoder)780 rfb_decoder_state_framebuffer_update (RfbDecoder * decoder)
781 {
782 
783   if (!rfb_decoder_read (decoder, 3))
784     return FALSE;
785 
786   decoder->n_rects = RFB_GET_UINT16 (decoder->data + 1);
787   GST_DEBUG ("Number of rectangles : %d", decoder->n_rects);
788 
789   decoder->state = rfb_decoder_state_framebuffer_update_rectangle;
790 
791   return TRUE;
792 }
793 
794 static gboolean
rfb_decoder_state_framebuffer_update_rectangle(RfbDecoder * decoder)795 rfb_decoder_state_framebuffer_update_rectangle (RfbDecoder * decoder)
796 {
797   gint x, y, w, h;
798   gint encoding;
799   gboolean ret = FALSE;
800 
801   if (!rfb_decoder_read (decoder, 12))
802     return FALSE;
803 
804   x = RFB_GET_UINT16 (decoder->data + 0) - decoder->offset_x;
805   y = RFB_GET_UINT16 (decoder->data + 2) - decoder->offset_y;
806   w = RFB_GET_UINT16 (decoder->data + 4);
807   h = RFB_GET_UINT16 (decoder->data + 6);
808   encoding = RFB_GET_UINT32 (decoder->data + 8);
809 
810   GST_DEBUG ("update recieved");
811   GST_DEBUG ("x:%d y:%d", x, y);
812   GST_DEBUG ("w:%d h:%d", w, h);
813   GST_DEBUG ("encoding: %d", encoding);
814 
815   if (((w * h) + (x * y)) > (decoder->width * decoder->height)) {
816     GST_ERROR ("Desktop resize is unsupported.");
817     decoder->state = NULL;
818     return TRUE;
819   }
820 
821   switch (encoding) {
822     case ENCODING_TYPE_RAW:
823       ret = rfb_decoder_raw_encoding (decoder, x, y, w, h);
824       break;
825     case ENCODING_TYPE_COPYRECT:
826       ret = rfb_decoder_copyrect_encoding (decoder, x, y, w, h);
827       break;
828     case ENCODING_TYPE_RRE:
829       ret = rfb_decoder_rre_encoding (decoder, x, y, w, h);
830       break;
831     case ENCODING_TYPE_CORRE:
832       ret = rfb_decoder_corre_encoding (decoder, x, y, w, h);
833       break;
834     case ENCODING_TYPE_HEXTILE:
835       ret = rfb_decoder_hextile_encoding (decoder, x, y, w, h);
836       break;
837     default:
838       g_critical ("unimplemented encoding\n");
839       break;
840   }
841 
842   if (!ret)
843     return FALSE;
844 
845   decoder->n_rects--;
846   if (decoder->n_rects == 0) {
847     decoder->state = NULL;
848   } else {
849     decoder->state = rfb_decoder_state_framebuffer_update_rectangle;
850   }
851 
852   return TRUE;
853 }
854 
855 static gboolean
rfb_decoder_raw_encoding(RfbDecoder * decoder,gint start_x,gint start_y,gint rect_w,gint rect_h)856 rfb_decoder_raw_encoding (RfbDecoder * decoder, gint start_x, gint start_y,
857     gint rect_w, gint rect_h)
858 {
859   gint size;
860   guint8 *frame, *p;
861   guint32 raw_line_size;
862 
863   raw_line_size = rect_w * decoder->bytespp;
864   size = rect_h * raw_line_size;
865 
866   GST_DEBUG ("Reading %d bytes (%dx%d)", size, rect_w, rect_h);
867 
868   if (!rfb_decoder_read (decoder, size))
869     return FALSE;
870 
871   frame =
872       decoder->frame + (((start_y * decoder->rect_width) +
873           start_x) * decoder->bytespp);
874   p = decoder->data;
875 
876   while (rect_h--) {
877     memcpy (frame, p, raw_line_size);
878     p += raw_line_size;
879     frame += decoder->line_size;
880   }
881 
882   return TRUE;
883 }
884 
885 static gboolean
rfb_decoder_copyrect_encoding(RfbDecoder * decoder,gint start_x,gint start_y,gint rect_w,gint rect_h)886 rfb_decoder_copyrect_encoding (RfbDecoder * decoder, gint start_x, gint start_y,
887     gint rect_w, gint rect_h)
888 {
889   guint16 src_x, src_y;
890   gint line_width, copyrect_width;
891   guint8 *src, *dst;
892 
893   if (!rfb_decoder_read (decoder, 4))
894     return FALSE;
895 
896   /* don't forget the offset */
897   src_x = RFB_GET_UINT16 (decoder->data) - decoder->offset_x;
898   src_y = RFB_GET_UINT16 (decoder->data + 2) - decoder->offset_y;
899   GST_DEBUG ("Copyrect from %d %d", src_x, src_y);
900 
901   copyrect_width = rect_w * decoder->bytespp;
902   line_width = decoder->line_size;
903   src =
904       decoder->prev_frame + ((src_y * decoder->rect_width) +
905       src_x) * decoder->bytespp;
906   dst =
907       decoder->frame + ((start_y * decoder->rect_width) +
908       start_x) * decoder->bytespp;
909 
910   while (rect_h--) {
911     memcpy (dst, src, copyrect_width);
912     src += line_width;
913     dst += line_width;
914   }
915 
916   return TRUE;
917 }
918 
919 static void
rfb_decoder_fill_rectangle(RfbDecoder * decoder,gint x,gint y,gint w,gint h,guint32 color)920 rfb_decoder_fill_rectangle (RfbDecoder * decoder, gint x, gint y, gint w,
921     gint h, guint32 color)
922 {
923   /* fill the whole region with the same color */
924 
925   guint32 *offset;
926   gint i, j;
927 
928   for (i = 0; i < h; i++) {
929     offset =
930         (guint32 *) (decoder->frame + ((x + (y +
931                     i) * decoder->rect_width)) * decoder->bytespp);
932     for (j = 0; j < w; j++) {
933       *(offset++) = color;
934     }
935   }
936 }
937 
938 static gboolean
rfb_decoder_rre_encoding(RfbDecoder * decoder,gint start_x,gint start_y,gint rect_w,gint rect_h)939 rfb_decoder_rre_encoding (RfbDecoder * decoder, gint start_x, gint start_y,
940     gint rect_w, gint rect_h)
941 {
942   guint32 number_of_rectangles, color;
943   guint16 x, y, w, h;
944 
945   if (!rfb_decoder_read (decoder, 4 + decoder->bytespp))
946     return FALSE;
947 
948   number_of_rectangles = RFB_GET_UINT32 (decoder->data);
949   color = GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data + 4)));
950 
951   GST_DEBUG ("number of rectangles :%d", number_of_rectangles);
952 
953   /* color the background of this rectangle */
954   rfb_decoder_fill_rectangle (decoder, start_x, start_y, rect_w, rect_h, color);
955 
956   while (number_of_rectangles--) {
957 
958     if (!rfb_decoder_read (decoder, decoder->bytespp + 8))
959       return FALSE;
960 
961     color = GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data)));
962     x = RFB_GET_UINT16 (decoder->data + decoder->bytespp);
963     y = RFB_GET_UINT16 (decoder->data + decoder->bytespp + 2);
964     w = RFB_GET_UINT16 (decoder->data + decoder->bytespp + 4);
965     h = RFB_GET_UINT16 (decoder->data + decoder->bytespp + 6);
966 
967     /* draw the rectangle in the foreground */
968     rfb_decoder_fill_rectangle (decoder, start_x + x, start_y + y, w, h, color);
969   }
970 
971   return TRUE;
972 }
973 
974 static gboolean
rfb_decoder_corre_encoding(RfbDecoder * decoder,gint start_x,gint start_y,gint rect_w,gint rect_h)975 rfb_decoder_corre_encoding (RfbDecoder * decoder, gint start_x, gint start_y,
976     gint rect_w, gint rect_h)
977 {
978   guint32 number_of_rectangles, color;
979   guint8 x, y, w, h;
980 
981   if (!rfb_decoder_read (decoder, 4 + decoder->bytespp))
982     return FALSE;
983 
984   number_of_rectangles = RFB_GET_UINT32 (decoder->data);
985   color = GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data + 4)));
986 
987   GST_DEBUG ("number of rectangles :%d", number_of_rectangles);
988 
989   /* color the background of this rectangle */
990   rfb_decoder_fill_rectangle (decoder, start_x, start_y, rect_w, rect_h, color);
991 
992   while (number_of_rectangles--) {
993 
994     if (!rfb_decoder_read (decoder, decoder->bytespp + 4))
995       return FALSE;
996 
997     color = GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data)));
998     x = RFB_GET_UINT8 (decoder->data + decoder->bytespp);
999     y = RFB_GET_UINT8 (decoder->data + decoder->bytespp + 1);
1000     w = RFB_GET_UINT8 (decoder->data + decoder->bytespp + 2);
1001     h = RFB_GET_UINT8 (decoder->data + decoder->bytespp + 3);
1002 
1003     /* draw the rectangle in the foreground */
1004     rfb_decoder_fill_rectangle (decoder, start_x + x, start_y + y, w, h, color);
1005   }
1006 
1007   return TRUE;
1008 }
1009 
1010 static gboolean
rfb_decoder_hextile_encoding(RfbDecoder * decoder,gint start_x,gint start_y,gint rect_w,gint rect_h)1011 rfb_decoder_hextile_encoding (RfbDecoder * decoder, gint start_x, gint start_y,
1012     gint rect_w, gint rect_h)
1013 {
1014   gint32 x, x_count G_GNUC_UNUSED, x_end, x_max, x_max_16;
1015   gint32 y, y_count G_GNUC_UNUSED, y_end, y_max, y_max_16;
1016   guint8 subencoding, nr_subrect, xy, wh;
1017   guint32 background, foreground;
1018 
1019   foreground = background = 0;
1020   x_end = rect_w % 16;
1021   x_count = rect_w / 16 + (x_end > 0 ? 1 : 0);
1022   y_end = rect_h % 16;
1023   y_count = rect_h / 16 + (y_end > 0 ? 1 : 0);
1024   x_max = start_x + rect_w;
1025   y_max = start_y + rect_h;
1026   x_max_16 = x_max - 16;
1027   y_max_16 = y_max - 16;
1028 
1029   for (y = start_y; y < y_max; y += 16) {
1030     for (x = start_x; x < x_max; x += 16) {
1031 
1032       if (!rfb_decoder_read (decoder, 1))
1033         return FALSE;
1034 
1035       subencoding = RFB_GET_UINT8 (decoder->data);
1036 
1037       if (subencoding & SUBENCODING_RAW) {
1038         rfb_decoder_raw_encoding (decoder, x, y,
1039             (x <= x_max_16 ? 16 : x_end), (y <= y_max_16 ? 16 : y_end));
1040         continue;
1041       }
1042 
1043       if (subencoding & SUBENCODING_BACKGROUND) {
1044         if (!rfb_decoder_read (decoder, decoder->bytespp))
1045           return FALSE;
1046 
1047         background = GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data)));
1048       }
1049       rfb_decoder_fill_rectangle (decoder, x, y,
1050           (x <= x_max_16 ? 16 : x_end), (y <= y_max_16 ? 16 : y_end),
1051           background);
1052 
1053       if (subencoding & SUBENCODING_FOREGROUND) {
1054         if (!rfb_decoder_read (decoder, decoder->bytespp))
1055           return FALSE;
1056 
1057         foreground = GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data)));
1058       }
1059 
1060       if (subencoding & SUBENCODING_ANYSUBRECTS) {
1061         if (!rfb_decoder_read (decoder, 1))
1062           return FALSE;
1063 
1064         nr_subrect = RFB_GET_UINT8 (decoder->data);
1065       } else {
1066         continue;
1067       }
1068 
1069       if (subencoding & SUBENCODING_SUBRECTSCOLORED) {
1070         guint offset = 0;
1071 
1072         if (!rfb_decoder_read (decoder, nr_subrect * (2 + decoder->bytespp)))
1073           return FALSE;
1074 
1075         while (nr_subrect--) {
1076           foreground =
1077               GUINT32_SWAP_LE_BE ((RFB_GET_UINT32 (decoder->data + offset)));
1078           offset += decoder->bytespp;
1079           xy = RFB_GET_UINT8 (decoder->data + offset++);
1080           wh = RFB_GET_UINT8 (decoder->data + offset++);
1081           rfb_decoder_fill_rectangle (decoder, x + (xy >> 4), y + (xy & 0xF),
1082               1 + (wh >> 4), 1 + (wh & 0xF), foreground);
1083         }
1084       } else {
1085         guint offset = 0;
1086 
1087         if (!rfb_decoder_read (decoder, 2 * nr_subrect))
1088           return FALSE;
1089 
1090         while (nr_subrect--) {
1091           xy = RFB_GET_UINT8 (decoder->data + offset++);
1092           wh = RFB_GET_UINT8 (decoder->data + offset++);
1093           rfb_decoder_fill_rectangle (decoder, x + (xy >> 4), y + (xy & 0xF),
1094               1 + (wh >> 4), 1 + (wh & 0xF), foreground);
1095         }
1096       }
1097     }
1098   }
1099 
1100   return TRUE;
1101 }
1102 
1103 static gboolean
rfb_decoder_state_set_colour_map_entries(RfbDecoder * decoder)1104 rfb_decoder_state_set_colour_map_entries (RfbDecoder * decoder)
1105 {
1106   g_critical ("not implemented");
1107 
1108   return FALSE;
1109 }
1110 
1111 static gboolean
rfb_decoder_state_server_cut_text(RfbDecoder * decoder)1112 rfb_decoder_state_server_cut_text (RfbDecoder * decoder)
1113 {
1114   gint cut_text_length;
1115 
1116   /* 3 bytes padding, 4 bytes cut_text_length */
1117   if (!rfb_decoder_read (decoder, 7))
1118     return FALSE;
1119 
1120   cut_text_length = RFB_GET_UINT32 (decoder->data + 3);
1121 
1122   if (!rfb_decoder_read (decoder, cut_text_length))
1123     return FALSE;
1124 
1125   GST_DEBUG ("rfb_decoder_state_server_cut_text: throw away '%s'",
1126       decoder->data);
1127 
1128   decoder->state = rfb_decoder_state_normal;
1129 
1130   return TRUE;
1131 }
1132