* 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

@ -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)