From b4fb7b0c74bcef095f945c9520aebc111a3d64cc Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 17 Nov 2011 11:11:40 +0400 Subject: [PATCH] Ticket #1730 (troubles in mcviewer with utf8) Fixed troubles in mcviewer with drawing utf8 chars. It happens in the middle of an utf8 char (on every 4096 bytes), leading to a valid char treated as unprintable. Signed-off-by: Ilia Maslakov --- src/viewer/datasource.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/viewer/datasource.c b/src/viewer/datasource.c index 3641da456..a43b59dbc 100644 --- a/src/viewer/datasource.c +++ b/src/viewer/datasource.c @@ -163,6 +163,7 @@ mcview_get_utf (mcview_t * view, off_t byte_index, int *char_width, gboolean * r int res = -1; gunichar ch; gchar *next_ch = NULL; + gchar utf8buf[UTF8_CHAR_LEN + 1]; *char_width = 0; *result = FALSE; @@ -188,6 +189,25 @@ mcview_get_utf (mcview_t * view, off_t byte_index, int *char_width, gboolean * r res = g_utf8_get_char_validated (str, -1); + if (res < 0) + { + /* Retry with explicit bytes to make sure it's not a buffer boundary */ + int i; + for (i = 0; i < UTF8_CHAR_LEN; i++) + { + if (mcview_get_byte (view, byte_index + i, &res)) + utf8buf[i] = res; + else + { + utf8buf[i] = '\0'; + break; + } + } + utf8buf[UTF8_CHAR_LEN] = '\0'; + str = utf8buf; + res = g_utf8_get_char_validated (str, -1); + } + if (res < 0) { ch = *str;