* 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.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2012-04-07 20:11:04 +02:00
parent 491c57a815
commit 851ffadac2
4 changed files with 44 additions and 13 deletions

View file

@ -1,7 +1,20 @@
2012-04-07 Vladimir Serbinenko <phcoder@gmail.com>
* 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 <phcoder@gmail.com> 2012-04-07 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/commands/usbtest.c (usb_print_str): Silence spurious * grub-core/commands/usbtest.c (usb_print_str): Silence spurious
warning. warning.
* grub-core/fs/bfs.c (hop_level): Likewise.
* grub-core/net/bootp.c (grub_cmd_bootp): Likewise.
2012-04-07 Vladimir Serbinenko <phcoder@gmail.com> 2012-04-07 Vladimir Serbinenko <phcoder@gmail.com>

View file

@ -101,12 +101,13 @@ grub_encode_utf8_character (grub_uint8_t *dest, grub_uint8_t *destend,
} }
/* Convert UCS-4 to UTF-8. */ /* Convert UCS-4 to UTF-8. */
void grub_size_t
grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size, grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size,
grub_uint8_t *dest, grub_size_t destsize) grub_uint8_t *dest, grub_size_t destsize)
{ {
/* Keep last char for \0. */ /* Keep last char for \0. */
grub_uint8_t *destend = dest + destsize - 1; grub_uint8_t *destend = dest + destsize - 1;
grub_uint8_t *dest0 = dest;
while (size-- && dest < destend) while (size-- && dest < destend)
{ {
@ -123,16 +124,17 @@ grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size,
dest += s; dest += s;
} }
*dest = 0; *dest = 0;
return dest - dest0;
} }
/* Convert UCS-4 to UTF-8. */ /* Returns the number of bytes the string src would occupy is converted
char * to UTF-8, excluding trailing \0. */
grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size) grub_size_t
grub_get_num_of_utf8_bytes (const grub_uint32_t *src, grub_size_t size)
{ {
grub_size_t remaining; grub_size_t remaining;
const grub_uint32_t *ptr; const grub_uint32_t *ptr;
grub_size_t cnt = 0; grub_size_t cnt = 0;
grub_uint8_t *ret;
remaining = size; remaining = size;
ptr = src; ptr = src;
@ -153,7 +155,15 @@ grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size)
else else
cnt += 4; 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); ret = grub_malloc (cnt);
if (!ret) if (!ret)

View file

@ -1205,21 +1205,22 @@ run (struct screen *screen)
char * editor_getsource (void) char * editor_getsource (void)
{ {
int i; int i;
int size = 0; grub_size_t size = 0, tot_size = 0;
char *source; char *source;
for (i = 0; i < screen->num_lines; i++) 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) if (! source)
return NULL; return NULL;
size = 0;
for (i = 0; i < screen->num_lines; i++) for (i = 0; i < screen->num_lines; i++)
{ {
grub_memcpy (source + size, screen->lines[i].buf, screen->lines[i].len); size += grub_ucs4_to_utf8 (screen->lines[i].buf, screen->lines[i].len,
size += screen->lines[i].len; (grub_uint8_t *) source + size,
tot_size - size);
source[size++] = '\n'; source[size++] = '\n';
} }
source[size] = '\0'; source[size] = '\0';

View file

@ -282,7 +282,14 @@ grub_ssize_t grub_utf8_to_ucs4_alloc (const char *msg,
grub_uint32_t **unicode_msg, grub_uint32_t **unicode_msg,
grub_uint32_t **last_position); 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_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size,
grub_uint8_t *dest, grub_size_t destsize); grub_uint8_t *dest, grub_size_t destsize);
grub_size_t grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize, grub_size_t grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize,