From 3c70e87aef691433f3ac7556dce47f264811468d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 8 Apr 2014 17:08:29 -0700 Subject: [PATCH] GUAC-608: Add inbound clipboard. Remove use of iconv(). --- src/protocols/vnc/Makefile.am | 2 - src/protocols/vnc/client.c | 2 + src/protocols/vnc/clipboard.c | 32 +++++++++- src/protocols/vnc/convert.c | 103 ------------------------------ src/protocols/vnc/convert.h | 41 ------------ src/protocols/vnc/guac_handlers.c | 27 +++----- src/protocols/vnc/guac_handlers.h | 2 + 7 files changed, 41 insertions(+), 168 deletions(-) delete mode 100644 src/protocols/vnc/convert.c delete mode 100644 src/protocols/vnc/convert.h diff --git a/src/protocols/vnc/Makefile.am b/src/protocols/vnc/Makefile.am index 44743f81..17222a70 100644 --- a/src/protocols/vnc/Makefile.am +++ b/src/protocols/vnc/Makefile.am @@ -32,14 +32,12 @@ lib_LTLIBRARIES = libguac-client-vnc.la libguac_client_vnc_la_SOURCES = \ client.c \ clipboard.c \ - convert.c \ guac_handlers.c \ vnc_handlers.c noinst_HEADERS = \ client.h \ clipboard.h \ - convert.h \ guac_handlers.h \ vnc_handlers.h diff --git a/src/protocols/vnc/client.c b/src/protocols/vnc/client.c index 35d080b8..1fe61a1c 100644 --- a/src/protocols/vnc/client.c +++ b/src/protocols/vnc/client.c @@ -351,6 +351,8 @@ int guac_client_init(guac_client* client, int argc, char** argv) { client->mouse_handler = vnc_guac_client_mouse_handler; client->key_handler = vnc_guac_client_key_handler; client->clipboard_handler = vnc_guac_client_clipboard_handler; + client->blob_handler = vnc_guac_client_blob_handler; + client->end_handler = vnc_guac_client_end_handler; /* If not read-only but cursor is remote, set a dot cursor */ if (guac_client_data->remote_cursor) diff --git a/src/protocols/vnc/clipboard.c b/src/protocols/vnc/clipboard.c index 6a195749..02abf202 100644 --- a/src/protocols/vnc/clipboard.c +++ b/src/protocols/vnc/clipboard.c @@ -21,22 +21,48 @@ */ #include "config.h" +#include "client.h" #include "clipboard.h" +#include "guac_clipboard.h" +#include "guac_iconv.h" int guac_vnc_clipboard_handler(guac_client* client, guac_stream* stream, char* mimetype) { - /* STUB */ + + /* Clear clipboard and prepare for new data */ + vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data; + guac_common_clipboard_reset(client_data->clipboard, mimetype); + return 0; } int guac_vnc_clipboard_blob_handler(guac_client* client, guac_stream* stream, void* data, int length) { - /* STUB */ + + /* Append new data */ + vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data; + guac_common_clipboard_append(client_data->clipboard, (char*) data, length); + return 0; } int guac_vnc_clipboard_end_handler(guac_client* client, guac_stream* stream) { - /* STUB */ + + vnc_guac_client_data* client_data = (vnc_guac_client_data*) client->data; + rfbClient* rfb_client = client_data->rfb_client; + + char output_data[GUAC_VNC_CLIPBOARD_MAX_LENGTH]; + + const char* input = client_data->clipboard->buffer; + char* output = output_data; + + /* Convert clipboard to ISO 8859-1 */ + guac_iconv(GUAC_READ_UTF8, &input, client_data->clipboard->length, + GUAC_WRITE_ISO8859_1, &output, sizeof(output_data)); + + /* Send via VNC */ + SendClientCutText(rfb_client, output_data, output - output_data); + return 0; } diff --git a/src/protocols/vnc/convert.c b/src/protocols/vnc/convert.c deleted file mode 100644 index 62cdc0c9..00000000 --- a/src/protocols/vnc/convert.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2013 Glyptodon LLC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "config.h" - -#include -#include -#include -#include - -char* convert(const char* from_charset, const char* to_charset, const char* input) { - size_t input_remaining; - size_t output_remaining; - size_t bytes_converted = 0; - char* output; - char* output_buffer; - char* new_buffer; - char* input_buffer; - size_t output_length; - iconv_t cd; - - cd = iconv_open(to_charset, from_charset); - - if(cd == (iconv_t) -1) - /* Cannot convert due to invalid character set */ - return NULL; - - input_remaining = strlen(input); - input_buffer = (char*) input; - - /* Start the output buffer the same size as the input buffer */ - output_length = input_remaining; - - /* Leave some space at the end for NULL terminator */ - if (!(output = (char*) malloc(output_length + 4))) { - /* Cannot convert due to memory allocation error */ - iconv_close(cd); - return NULL; - } - - do { - output_buffer = output + bytes_converted; - output_remaining = output_length - bytes_converted; - - bytes_converted = iconv(cd, &input_buffer, - &input_remaining, &output_buffer, &output_remaining); - - if(bytes_converted == -1) { - if(errno == E2BIG) { - /* The output buffer is too small, so allocate more space */ - bytes_converted = output_buffer - output; - output_length += input_remaining * 2 + 8; - - if (!(new_buffer = (char*) realloc(output, output_length + 4))) { - /* Cannot convert due to memory allocation error */ - iconv_close(cd); - free(output); - return NULL; - } - - output = new_buffer; - output_buffer = output + bytes_converted; - } - else if (errno == EILSEQ) { - /* Invalid sequence detected, return what's been converted so far */ - break; - } - else if (errno == EINVAL) { - /* Incomplete sequence detected, can be ignored */ - break; - } - } - } while (input_remaining); - - /* Flush the iconv conversion */ - iconv(cd, NULL, NULL, &output_buffer, &output_remaining); - iconv_close(cd); - - /* Add the NULL terminator */ - memset(output_buffer, 0, 4); - - return output; -} - diff --git a/src/protocols/vnc/convert.h b/src/protocols/vnc/convert.h deleted file mode 100644 index 450e12ff..00000000 --- a/src/protocols/vnc/convert.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2013 Glyptodon LLC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - - -#ifndef __GUAC_VNC_VNC_CONVERT_H -#define __GUAC_VNC_VNC_CONVERT_H - -#include "config.h" - -#include - -/** - * Converts a string from one charset to another. Returns a newly allocated string. - * @param from_charset The string representing the character set to convert from. - * @param to_charset The string representing the character set to convert to. - * @return A newly allocated string that is the result of the conversion, or NULL - * if an error has occured. - */ -char* convert (const char* from_charset, const char* to_charset, const char* input); - -#endif - diff --git a/src/protocols/vnc/guac_handlers.c b/src/protocols/vnc/guac_handlers.c index e9ab0140..0f639d96 100644 --- a/src/protocols/vnc/guac_handlers.c +++ b/src/protocols/vnc/guac_handlers.c @@ -23,7 +23,7 @@ #include "config.h" #include "client.h" -#include "convert.h" +#include "clipboard.h" #include "guac_clipboard.h" #include @@ -98,26 +98,15 @@ int vnc_guac_client_key_handler(guac_client* client, int keysym, int pressed) { } int vnc_guac_client_clipboard_handler(guac_client* client, guac_stream* stream, char* mimetype) { -#if 0 - rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client; + return guac_vnc_clipboard_handler(client, stream, mimetype); +} - /* Convert UTF-8 character data to ISO_8859-1 */ - # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 || _LIBICONV_VERSION >= 0x0105 - char* iso_8559_1_data = convert("UTF-8", "ISO_8859-1//TRANSLIT", data); - #else - char* iso_8559_1_data = convert("UTF-8", "ISO_8859-1", data); - #endif +int vnc_guac_client_blob_handler(guac_client* client, guac_stream* stream, void* data, int length) { + return guac_vnc_clipboard_blob_handler(client, stream, data, length); +} - /* If the conversion was successful, send the converted character data. */ - if(iso_8559_1_data) { - SendClientCutText(rfb_client, iso_8559_1_data, strlen(iso_8559_1_data)); - free(iso_8559_1_data); - /* Otherwise, just send an empty string. */ - } - else - SendClientCutText(rfb_client, "", 0); -#endif - return 0; +int vnc_guac_client_end_handler(guac_client* client, guac_stream* stream) { + return guac_vnc_clipboard_end_handler(client, stream); } int vnc_guac_client_free_handler(guac_client* client) { diff --git a/src/protocols/vnc/guac_handlers.h b/src/protocols/vnc/guac_handlers.h index 753cce0b..ee99cdc8 100644 --- a/src/protocols/vnc/guac_handlers.h +++ b/src/protocols/vnc/guac_handlers.h @@ -32,6 +32,8 @@ int vnc_guac_client_handle_messages(guac_client* client); int vnc_guac_client_mouse_handler(guac_client* client, int x, int y, int mask); int vnc_guac_client_key_handler(guac_client* client, int keysym, int pressed); int vnc_guac_client_clipboard_handler(guac_client* client, guac_stream* stream, char* mimetype); +int vnc_guac_client_blob_handler(guac_client* client, guac_stream* stream, void* data, int length); +int vnc_guac_client_end_handler(guac_client* client, guac_stream* stream); int vnc_guac_client_free_handler(guac_client* client); #endif