From 5e4c7de5c73099df8842f9b93c57fe7d2dfaa7a8 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 27 Jul 2015 18:19:50 -0700 Subject: [PATCH] GUAC-298: Clarify function documentation and naming. Do not simply silently default to ISO8859-1 if encoding is invalid. --- src/protocols/vnc/client.c | 59 +++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/protocols/vnc/client.c b/src/protocols/vnc/client.c index 990c09f0..8d4ad8a7 100644 --- a/src/protocols/vnc/client.c +++ b/src/protocols/vnc/client.c @@ -222,42 +222,62 @@ static rfbClient* __guac_vnc_get_client(guac_client* client) { } /** - * Configure the VNC clipboard reader/writer based on configured encoding. - * Defaults to standard ISO8859-1 encoding if an invalid format is encountered. + * Sets the encoding of clipboard data exchanged with the VNC server to the + * encoding having the given name. If the name is left blank, or is invalid, + * the standard ISO8859-1 encoding will be used. * - * @param guac_client_data - * Structure containing VNC client configuration. + * @param client + * The client to set the clipboard encoding of. * * @param name - * Encoding name. + * The name of the encoding to use for all clipboard data. Valid values + * are: "ISO8859-1", "UTF-8", "UTF-16", "CP1252", or "". * * @return - * Returns 0 if standard ISO8859-1 encoding is used, 1 otherwise. + * Zero if the chosen encoding is standard for VNC, or non-zero if the VNC + * standard is being violated. */ -int __guac_client_configure_clipboard_encoding(vnc_guac_client_data* guac_client_data, const char* name) { +static int guac_vnc_set_clipboard_encoding(guac_client* client, + const char* name) { - /* Configure clipboard reader/writer based on encoding name */ + vnc_guac_client_data* guac_client_data = + (vnc_guac_client_data*) client->data; + + /* Use ISO8859-1 if explicitly selected or blank */ + if (name[0] == '\0' || strcmp(name, "ISO8859-1") == 0) { + guac_client_data->clipboard_reader = GUAC_READ_ISO8859_1; + guac_client_data->clipboard_writer = GUAC_WRITE_ISO8859_1; + return 0; + } + + /* UTF-8 */ if (strcmp(name, "UTF-8") == 0) { guac_client_data->clipboard_reader = GUAC_READ_UTF8; guac_client_data->clipboard_writer = GUAC_WRITE_UTF8; return 1; } - else if (strcmp(name, "UTF-16") == 0) { + + /* UTF-16 */ + if (strcmp(name, "UTF-16") == 0) { guac_client_data->clipboard_reader = GUAC_READ_UTF16; guac_client_data->clipboard_writer = GUAC_WRITE_UTF16; return 1; } - else if (strcmp(name, "CP-1252") == 0) { + + /* CP1252 */ + if (strcmp(name, "CP1252") == 0) { guac_client_data->clipboard_reader = GUAC_READ_CP1252; guac_client_data->clipboard_writer = GUAC_WRITE_CP1252; return 1; } - else { - /* Default to ISO8859-1 */ - guac_client_data->clipboard_reader = GUAC_READ_ISO8859_1; - guac_client_data->clipboard_writer = GUAC_WRITE_ISO8859_1; - return 0; - } + + /* If encoding unrecognized, warn and default to ISO8859-1 */ + guac_client_log(client, GUAC_LOG_WARNING, + "Encoding '%s' is invalid. Defaulting to ISO8859-1.", name); + + guac_client_data->clipboard_reader = GUAC_READ_ISO8859_1; + guac_client_data->clipboard_writer = GUAC_WRITE_ISO8859_1; + return 0; } @@ -336,11 +356,10 @@ int guac_client_init(guac_client* client, int argc, char** argv) { guac_client_data->clipboard = guac_common_clipboard_alloc(GUAC_VNC_CLIPBOARD_MAX_LENGTH); /* Configure clipboard encoding */ - if (__guac_client_configure_clipboard_encoding( - guac_client_data, argv[IDX_CLIPBOARD_ENCODING])) { + if (guac_vnc_set_clipboard_encoding(client, argv[IDX_CLIPBOARD_ENCODING])) guac_client_log(client, GUAC_LOG_INFO, - "Using non-standard VNC clipboard encoding: '%s'.", argv[IDX_CLIPBOARD_ENCODING]); - } + "Using non-standard VNC clipboard encoding: '%s'.", + argv[IDX_CLIPBOARD_ENCODING]); /* Ensure connection is kept alive during lengthy connects */ guac_socket_require_keep_alive(client->socket);