diff --git a/protocols/vnc/Makefile.am b/protocols/vnc/Makefile.am index f811c835..9180cc13 100644 --- a/protocols/vnc/Makefile.am +++ b/protocols/vnc/Makefile.am @@ -41,8 +41,8 @@ AM_CFLAGS = -Werror -Wall -pedantic -Iinclude lib_LTLIBRARIES = libguac-client-vnc.la -libguac_client_vnc_la_SOURCES = src/client.c src/vnc_handlers.c src/guac_handlers.c -noinst_HEADERS = include/client.h include/vnc_handlers.h include/guac_handlers.h +libguac_client_vnc_la_SOURCES = src/client.c src/vnc_handlers.c src/guac_handlers.c src/convert.c +noinst_HEADERS = include/client.h include/vnc_handlers.h include/guac_handlers.h include/convert.h libguac_client_vnc_la_LDFLAGS = -version-info 0:0:0 diff --git a/protocols/vnc/configure.in b/protocols/vnc/configure.in index 77b12b3b..c40d5ca4 100644 --- a/protocols/vnc/configure.in +++ b/protocols/vnc/configure.in @@ -46,6 +46,7 @@ AC_PROG_LIBTOOL AC_CHECK_LIB([guac], [guac_client_plugin_open],, AC_MSG_ERROR("libguac must be installed first")) AC_CHECK_LIB([cairo], [cairo_create],, AC_MSG_ERROR("cairo is required for drawing instructions")) AC_CHECK_LIB([vncclient], [rfbInitClient],, AC_MSG_ERROR("libvncclient is required")) +#AC_CHECK_LIB([iconv], [iconv],, AC_MSG_ERROR("libiconv is required")) # Checks for header files. AC_CHECK_HEADERS([stdlib.h string.h syslog.h guacamole/client.h guacamole/socket.h guacamole/protocol.h]) diff --git a/protocols/vnc/include/convert.h b/protocols/vnc/include/convert.h new file mode 100644 index 00000000..5e5b7a71 --- /dev/null +++ b/protocols/vnc/include/convert.h @@ -0,0 +1,46 @@ + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is libguac-client-vnc. + * + * The Initial Developer of the Original Code is + * James Muehlner. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef __GUAC_VNC_VNC_CONVERT_H +#define __GUAC_VNC_VNC_CONVERT_H + +#include + +char * convert (const char *from_charset, const char *to_charset, const char *input); + +#endif + diff --git a/protocols/vnc/src/convert.c b/protocols/vnc/src/convert.c new file mode 100644 index 00000000..8220cc3b --- /dev/null +++ b/protocols/vnc/src/convert.c @@ -0,0 +1,118 @@ + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is libguac-client-vnc. + * + * The Initial Developer of the Original Code is + * James Muehlner. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include +#include +#include +#include + +char* convert(const char* from_charset, const char* to_charset, 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 = 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) { + /* Cannot convert because an invalid sequence was discovered */ + iconv_close(cd); + free(output); + return NULL; + } + 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/protocols/vnc/src/guac_handlers.c b/protocols/vnc/src/guac_handlers.c index d0b314a7..c456baae 100644 --- a/protocols/vnc/src/guac_handlers.c +++ b/protocols/vnc/src/guac_handlers.c @@ -38,12 +38,14 @@ #include #include #include +#include #include #include #include "client.h" +#include "convert.h" int vnc_guac_client_handle_messages(guac_client* client) { @@ -92,7 +94,12 @@ int vnc_guac_client_clipboard_handler(guac_client* client, char* data) { rfbClient* rfb_client = ((vnc_guac_client_data*) client->data)->rfb_client; - SendClientCutText(rfb_client, data, strlen(data)); + /* Convert UTF-8 character data to ISO_8859-1 */ + char* iso_8559_1_data = convert("UTF-8", "ISO_8859-1", data); + + SendClientCutText(rfb_client, iso_8559_1_data, strlen(iso_8559_1_data)); + + free(iso_8559_1_data); return 0; } diff --git a/protocols/vnc/src/vnc_handlers.c b/protocols/vnc/src/vnc_handlers.c index 11448d41..6b593bd1 100644 --- a/protocols/vnc/src/vnc_handlers.c +++ b/protocols/vnc/src/vnc_handlers.c @@ -38,6 +38,7 @@ #include #include #include +#include #include @@ -48,6 +49,7 @@ #include #include "client.h" +#include "convert.h" void guac_vnc_cursor(rfbClient* client, int x, int y, int w, int h, int bpp) { @@ -307,7 +309,12 @@ void guac_vnc_cut_text(rfbClient* client, const char* text, int textlen) { guac_client* gc = rfbClientGetClientData(client, __GUAC_CLIENT); guac_socket* socket = gc->socket; - guac_protocol_send_clipboard(socket, text); + /* Convert ASCII character data to UTF-8 */ + char* utf8_text = convert("ISO_8859-1", "UTF-8", text); + + guac_protocol_send_clipboard(socket, utf8_text); + + free(utf8_text); }