[parted-devel] [PATCH] GPT strings are UCS-2LE not UTF-16

Brian C. Lane bcl at redhat.com
Fri May 16 23:45:17 UTC 2014


There was a problem using UTF-16, it was writing the Byte Order Mark
before the string which caused problems with older versions of parted
reading the partition name. The test was skipping these 2 bytes when
checking the written string.

* libparted/labels/gpt.c: Switch to UCS-2LE for GPT Partition names
* tests/t0251-gpt-unicode.sh: Update the test
---
 libparted/labels/gpt.c     | 22 ++++++++++------------
 tests/t0251-gpt-unicode.sh |  2 +-
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 31f6d17..e1113ac 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -819,8 +819,7 @@ _parse_part_entry (PedDisk *disk, GuidPartitionEntry_t *pte)
   gpt_part_data->type = pte->PartitionTypeGuid;
   gpt_part_data->uuid = pte->UniquePartitionGuid;
   for (i = 0; i < 36; i++)
-    gpt_part_data->name[i] =
-      (efi_char16_t) PED_LE16_TO_CPU ((uint16_t) pte->PartitionName[i]);
+    gpt_part_data->name[i] = (efi_char16_t) pte->PartitionName[i];
   gpt_part_data->name[i] = 0;
   gpt_part_data->translated_name = 0;
 
@@ -1237,8 +1236,7 @@ _partition_generate_part_entry (PedPartition *part, GuidPartitionEntry_t *pte)
     pte->Attributes.LegacyBIOSBootable = 1;
 
   for (i = 0; i < 36; i++)
-    pte->PartitionName[i]
-      = (efi_char16_t) PED_CPU_TO_LE16 ((uint16_t) gpt_part_data->name[i]);
+    pte->PartitionName[i] = gpt_part_data->name[i];
 }
 
 static int
@@ -1891,7 +1889,7 @@ gpt_partition_set_name (PedPartition *part, const char *name)
 
   free(gpt_part_data->translated_name);
   gpt_part_data->translated_name = xstrdup(name);
-  iconv_t conv = iconv_open ("UTF-16", nl_langinfo (CODESET));
+  iconv_t conv = iconv_open ("UCS-2LE", nl_langinfo (CODESET));
   if (conv == (iconv_t)-1)
     goto err;
   char *inbuff = gpt_part_data->translated_name;
@@ -1904,8 +1902,8 @@ gpt_partition_set_name (PedPartition *part, const char *name)
   return;
  err:
   ped_exception_throw (PED_EXCEPTION_WARNING,
-		       PED_EXCEPTION_IGNORE,
-		       _("Can not translate partition name"));
+                       PED_EXCEPTION_IGNORE,
+                       _("Can not translate partition name"));
   iconv_close (conv);
 }
 
@@ -1916,23 +1914,23 @@ gpt_partition_get_name (const PedPartition *part)
   if (gpt_part_data->translated_name == NULL)
     {
       char buffer[200];
-      iconv_t conv = iconv_open (nl_langinfo (CODESET), "UTF-16");
+      iconv_t conv = iconv_open (nl_langinfo (CODESET), "UCS-2LE");
       if (conv == (iconv_t)-1)
-	goto err;
+        goto err;
       char *inbuff = (char *)&gpt_part_data->name;
       char *outbuff = buffer;
       size_t inbuffsize = 72;
       size_t outbuffsize = sizeof(buffer);
       if (iconv (conv, &inbuff, &inbuffsize, &outbuff, &outbuffsize) == -1)
-	goto err;
+        goto err;
       iconv_close (conv);
       *outbuff = 0;
       gpt_part_data->translated_name = xstrdup (buffer);
       return gpt_part_data->translated_name;
     err:
       ped_exception_throw (PED_EXCEPTION_WARNING,
-			   PED_EXCEPTION_IGNORE,
-			   _("Can not translate partition name"));
+                           PED_EXCEPTION_IGNORE,
+                           _("Can not translate partition name"));
       iconv_close (conv);
       return "";
     }
diff --git a/tests/t0251-gpt-unicode.sh b/tests/t0251-gpt-unicode.sh
index 36a4c26..fbffbcb 100755
--- a/tests/t0251-gpt-unicode.sh
+++ b/tests/t0251-gpt-unicode.sh
@@ -31,7 +31,7 @@ parted -s $dev mklabel gpt mkpart primary ext2 1MiB 2MiB name 1 $part_name > emp
 compare /dev/null empty || fail=1
 
 # check for expected output
-dd if=$dev bs=1 skip=$(($sector_size_+$sector_size_+58)) count=10 2>/dev/null | od -An -tx1 > out || fail=1
+dd if=$dev bs=1 skip=$(($sector_size_+$sector_size_+56)) count=10 2>/dev/null | od -An -tx1 > out || fail=1
 echo ' 66 00 6f 00 6f 00 24 1d 00 00' >> exp
 compare exp out || fail=1
 
-- 
1.9.0




More information about the parted-devel mailing list