]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
* grub-core/normal/charset.c (grub_ucs4_to_utf8): Return number of
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 7 Apr 2012 18:11:04 +0000 (20:11 +0200)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 7 Apr 2012 18:11:04 +0000 (20:11 +0200)
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
grub-core/normal/charset.c
grub-core/normal/menu_entry.c
include/grub/charset.h

index 282f1d4a785a4ae41b59c22103e6bc477551ad94..76ddaf6d77b2db902caedaa9d96ca35b6b93bbe4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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>
 
        * 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>
 
index 3a2f1820ad988a67dedb7bfecb3d15f9cad2a363..25593ce81fdedc6217034094d4b32875ead19530 100644 (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)
index 962d077ec2ae22edf79a05920dee1b0321bdcbfc..7fc890daad427a0244714222a704ac5726dab5ab 100644 (file)
@@ -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';
index 92927176cea299fe06f6c6643f46aa440a5d62bc..8cb228703ea025e284245220b8c78212b3f2a68b 100644 (file)
@@ -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,