Skip to content

Commit 39d2854

Browse files
committed
GUACAMOLE-2153: Fix GUAC_*_UTF16 on big-endian machine
Since UTF-16 works with 16-bit values it has a byte order and that byte order is not well defined. There is a convention to use U+FEFF as first character which allows determining the endianess but it's commonly not present and doesn't seem to fit into the current code. The most common thing is to assume little-endian which is what I did here and is already implied by the test suite. If supporting both big- and little-endian UTF-16 is wanted one could create _UTF16LE and _UTF16BE variations of the functions.
1 parent 74e6165 commit 39d2854

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/common/iconv.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <guacamole/unicode.h>
2424
#include <stdint.h>
25+
#include <endian.h>
2526

2627
/**
2728
* Lookup table for Unicode code points, indexed by CP-1252 codepoint.
@@ -109,7 +110,7 @@ int GUAC_READ_UTF16(const char** input, int remaining) {
109110
return 0;
110111

111112
/* Read two bytes as integer */
112-
value = *((uint16_t*) *input);
113+
value = le16toh(*((uint16_t*) *input));
113114
*input += 2;
114115

115116
return value;
@@ -213,7 +214,7 @@ void GUAC_WRITE_UTF16(char** output, int remaining, int value) {
213214
return;
214215

215216
/* Write two bytes as integer */
216-
*((uint16_t*) *output) = value;
217+
*((uint16_t*) *output) = htole16(value);
217218
*output += 2;
218219

219220
}

0 commit comments

Comments
 (0)