From 851ffadac2c2817fa51410edaa800225bf04dd8c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 7 Apr 2012 20:11:04 +0200 Subject: [PATCH] * grub-core/normal/charset.c (grub_ucs4_to_utf8): Return number of written bytes. (grub_get_num_of_utf8_bytes): New function. (grub_ucs4_to_utf8_alloc): Use grub_get_num_of_utf8_bytes. * grub-core/normal/menu_entry.c (run): Convert entry to UTF-8 before executing it. * include/grub/charset.h (grub_get_num_of_utf8_bytes): New proto. (grub_ucs4_to_utf8): Change return type. --- ChangeLog | 13 +++++++++++++ grub-core/normal/charset.c | 22 ++++++++++++++++------ grub-core/normal/menu_entry.c | 13 +++++++------ include/grub/charset.h | 9 ++++++++- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 282f1d4a7..76ddaf6d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,20 @@ +2012-04-07 Vladimir Serbinenko + + * grub-core/normal/charset.c (grub_ucs4_to_utf8): Return number of + written bytes. + (grub_get_num_of_utf8_bytes): New function. + (grub_ucs4_to_utf8_alloc): Use grub_get_num_of_utf8_bytes. + * grub-core/normal/menu_entry.c (run): Convert entry to UTF-8 before + executing it. + * include/grub/charset.h (grub_get_num_of_utf8_bytes): New proto. + (grub_ucs4_to_utf8): Change return type. + 2012-04-07 Vladimir Serbinenko * grub-core/commands/usbtest.c (usb_print_str): Silence spurious warning. + * grub-core/fs/bfs.c (hop_level): Likewise. + * grub-core/net/bootp.c (grub_cmd_bootp): Likewise. 2012-04-07 Vladimir Serbinenko diff --git a/grub-core/normal/charset.c b/grub-core/normal/charset.c index 3a2f1820a..25593ce81 100644 --- a/grub-core/normal/charset.c +++ b/grub-core/normal/charset.c @@ -101,12 +101,13 @@ grub_encode_utf8_character (grub_uint8_t *dest, grub_uint8_t *destend, } /* Convert UCS-4 to UTF-8. */ -void +grub_size_t grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size, grub_uint8_t *dest, grub_size_t destsize) { /* Keep last char for \0. */ grub_uint8_t *destend = dest + destsize - 1; + grub_uint8_t *dest0 = dest; while (size-- && dest < destend) { @@ -123,16 +124,17 @@ grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size, dest += s; } *dest = 0; + return dest - dest0; } -/* Convert UCS-4 to UTF-8. */ -char * -grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size) +/* Returns the number of bytes the string src would occupy is converted + to UTF-8, excluding trailing \0. */ +grub_size_t +grub_get_num_of_utf8_bytes (const grub_uint32_t *src, grub_size_t size) { grub_size_t remaining; const grub_uint32_t *ptr; grub_size_t cnt = 0; - grub_uint8_t *ret; remaining = size; ptr = src; @@ -153,7 +155,15 @@ grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size) else cnt += 4; } - cnt++; + return cnt; +} + +/* Convert UCS-4 to UTF-8. */ +char * +grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size) +{ + grub_uint8_t *ret; + grub_size_t cnt = grub_get_num_of_utf8_bytes (src, size) + 1; ret = grub_malloc (cnt); if (!ret) diff --git a/grub-core/normal/menu_entry.c b/grub-core/normal/menu_entry.c index 962d077ec..7fc890daa 100644 --- a/grub-core/normal/menu_entry.c +++ b/grub-core/normal/menu_entry.c @@ -1205,21 +1205,22 @@ run (struct screen *screen) char * editor_getsource (void) { int i; - int size = 0; + grub_size_t size = 0, tot_size = 0; char *source; for (i = 0; i < screen->num_lines; i++) - size += screen->lines[i].len + 1; + tot_size += grub_get_num_of_utf8_bytes (screen->lines[i].buf, + screen->lines[i].len) + 1; - source = grub_malloc (size + 1); + source = grub_malloc (tot_size + 1); if (! source) return NULL; - size = 0; for (i = 0; i < screen->num_lines; i++) { - grub_memcpy (source + size, screen->lines[i].buf, screen->lines[i].len); - size += screen->lines[i].len; + size += grub_ucs4_to_utf8 (screen->lines[i].buf, screen->lines[i].len, + (grub_uint8_t *) source + size, + tot_size - size); source[size++] = '\n'; } source[size] = '\0'; diff --git a/include/grub/charset.h b/include/grub/charset.h index 92927176c..8cb228703 100644 --- a/include/grub/charset.h +++ b/include/grub/charset.h @@ -282,7 +282,14 @@ grub_ssize_t grub_utf8_to_ucs4_alloc (const char *msg, grub_uint32_t **unicode_msg, grub_uint32_t **last_position); -void +/* Returns the number of bytes the string src would occupy is converted + to UTF-8, excluding \0. */ +grub_size_t +grub_get_num_of_utf8_bytes (const grub_uint32_t *src, grub_size_t size); + +/* Converts UCS-4 to UTF-8. Returns the number of bytes effectively written + excluding the trailing \0. */ +grub_size_t grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size, grub_uint8_t *dest, grub_size_t destsize); grub_size_t grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize,