]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: use ul_encode_to_utf8()
authorKarel Zak <kzak@redhat.com>
Tue, 25 Feb 2020 11:21:32 +0000 (12:21 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 25 Feb 2020 11:21:32 +0000 (12:21 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/gpt.c

index 11bf9fefb76f3ff891871d140cdf04a4847ad5af..e46b0fba014ecdfcac8cd81d81da16637c01dfaa 100644 (file)
@@ -27,6 +27,7 @@
 #include "strutils.h"
 #include "all-io.h"
 #include "pt-mbr.h"
+#include "encode.h"
 
 /**
  * SECTION: gpt
@@ -1637,45 +1638,20 @@ failed:
        return 0;
 }
 
-/*
- * Stolen from libblkid - can be removed once partition semantics
- * are added to the fdisk API.
- */
 static char *encode_to_utf8(unsigned char *src, size_t count)
 {
-       uint16_t c;
-       char *dest;
-       size_t i, j;
-       size_t len = count * 3 / 2;
+       unsigned char *dest;
+       size_t len = (count * 3 / 2) + 1;
 
-       dest = calloc(1, len + 1);
+       dest = calloc(1, len);
        if (!dest)
                return NULL;
 
-       for (j = i = 0; i + 2 <= count; i += 2) {
-               /* always little endian */
-               c = (src[i+1] << 8) | src[i];
-               if (c == 0) {
-                       break;
-               } else if (c < 0x80) {
-                       if (j+1 > len)
-                               break;
-                       dest[j++] = (uint8_t) c;
-               } else if (c < 0x800) {
-                       if (j+2 > len)
-                               break;
-                       dest[j++] = (uint8_t) (0xc0 | (c >> 6));
-                       dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
-               } else {
-                       if (j+3 > len)
-                               break;
-                       dest[j++] = (uint8_t) (0xe0 | (c >> 12));
-                       dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
-                       dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
-               }
+       if (ul_encode_to_utf8(UL_ENCODE_UTF16LE, dest, len, src, count) == 0) {
+               free(dest);
+               return NULL;
        }
-
-       return dest;
+       return (char *) dest;
 }
 
 static int gpt_entry_attrs_to_string(struct gpt_entry *e, char **res)