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>
+
+ * 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>
* 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 <phcoder@gmail.com>
}
/* 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)
{
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;
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)
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';
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,