Module Name: src
Committed By: christos
Date: Wed Sep 6 18:17:18 UTC 2017
Modified Files:
src/sbin/gpt: backup.c biosboot.c gpt.c gpt.h show.c
Log Message:
- make sure that the utf16 string is padded with 0's where needed.
- since the utf16 string is not 0 terminated, pass the size of the string.
To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sbin/gpt/backup.c
cvs rdiff -u -r1.28 -r1.29 src/sbin/gpt/biosboot.c
cvs rdiff -u -r1.71 -r1.72 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.35 -r1.36 src/sbin/gpt/gpt.h
cvs rdiff -u -r1.39 -r1.40 src/sbin/gpt/show.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sbin/gpt/backup.c
diff -u src/sbin/gpt/backup.c:1.16 src/sbin/gpt/backup.c:1.17
--- src/sbin/gpt/backup.c:1.16 Thu Dec 3 16:40:32 2015
+++ src/sbin/gpt/backup.c Wed Sep 6 14:17:18 2017
@@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
#endif
#ifdef __RCSID
-__RCSID("$NetBSD: backup.c,v 1.16 2015/12/03 21:40:32 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.17 2017/09/06 18:17:18 christos Exp $");
#endif
#include <sys/bootblock.h>
@@ -231,7 +231,8 @@ store_tbl(gpt_t gpt, const map_t m, prop
PROP_ERR(propnum);
rc = prop_dictionary_set(gpt_dict, "attributes", propnum);
PROP_ERR(rc);
- utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+ utf16_to_utf8(ent->ent_name, sizeof(ent->ent_name), utfbuf,
+ sizeof(utfbuf));
if (utfbuf[0] != '\0') {
propstr = prop_string_create_cstring((char *)utfbuf);
PROP_ERR(propstr);
Index: src/sbin/gpt/biosboot.c
diff -u src/sbin/gpt/biosboot.c:1.28 src/sbin/gpt/biosboot.c:1.29
--- src/sbin/gpt/biosboot.c:1.28 Mon Jul 3 02:44:58 2017
+++ src/sbin/gpt/biosboot.c Wed Sep 6 14:17:18 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: biosboot.c,v 1.28 2017/07/03 06:44:58 mrg Exp $ */
+/* $NetBSD: biosboot.c,v 1.29 2017/09/06 18:17:18 christos Exp $ */
/*
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.28 2017/07/03 06:44:58 mrg Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.29 2017/09/06 18:17:18 christos Exp $");
#endif
#include <sys/stat.h>
@@ -219,7 +219,8 @@ biosboot(gpt_t gpt, daddr_t start, uint6
break;
if (label != NULL) {
- utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+ utf16_to_utf8(ent->ent_name, sizeof(ent->ent_name),
+ utfbuf, sizeof(utfbuf));
if (strcmp((char *)label, (char *)utfbuf) == 0)
break;
}
Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.71 src/sbin/gpt/gpt.c:1.72
--- src/sbin/gpt/gpt.c:1.71 Tue Sep 5 14:30:46 2017
+++ src/sbin/gpt/gpt.c Wed Sep 6 14:17:18 2017
@@ -35,7 +35,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
#endif
#ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.71 2017/09/05 18:30:46 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.72 2017/09/06 18:17:18 christos Exp $");
#endif
#include <sys/param.h>
@@ -121,16 +121,22 @@ crc32(const void *buf, size_t size)
return crc ^ ~0U;
}
+/*
+ * Produce a NUL-terminated utf-8 string from the non-NUL-terminated
+ * utf16 string.
+ */
void
-utf16_to_utf8(const uint16_t *s16, uint8_t *s8, size_t s8len)
+utf16_to_utf8(const uint16_t *s16, size_t s16len, uint8_t *s8, size_t s8len)
{
- size_t s8idx, s16idx, s16len;
+ size_t s8idx, s16idx;
uint32_t utfchar;
unsigned int c;
- s16len = 0;
- while (s16[s16len++] != 0)
- continue;
+ for (s16idx = 0; s16idx < s16len; s16idx++)
+ if (s16[s16idx] == 0)
+ break;
+
+ s16len = s16idx;
s8idx = s16idx = 0;
while (s16idx < s16len) {
utfchar = le16toh(s16[s16idx++]);
@@ -168,6 +174,10 @@ utf16_to_utf8(const uint16_t *s16, uint8
s8[s8idx] = 0;
}
+/*
+ * Produce a non-NUL-terminated utf-16 string from the NUL-terminated
+ * utf8 string.
+ */
void
utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
{
@@ -228,6 +238,9 @@ utf8_to_utf16(const uint8_t *s8, uint16_
}
}
} while (c != 0);
+
+ while (s16idx < s16len)
+ s16[s16idx++] = 0;
}
void *
@@ -1027,7 +1040,8 @@ gpt_change_ent(gpt_t gpt, const struct g
ent = gpt_ent_primary(gpt, i);
if (find->label != NULL) {
- utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+ utf16_to_utf8(ent->ent_name, sizeof(ent->ent_name),
+ utfbuf, sizeof(utfbuf));
if (strcmp((char *)find->label, (char *)utfbuf) == 0)
continue;
}
Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.35 src/sbin/gpt/gpt.h:1.36
--- src/sbin/gpt/gpt.h:1.35 Thu Feb 16 17:40:19 2017
+++ src/sbin/gpt/gpt.h Wed Sep 6 14:17:18 2017
@@ -99,7 +99,7 @@ struct gpt_ent *gpt_ent_primary(gpt_t, u
struct gpt_ent *gpt_ent_backup(gpt_t, unsigned int);
int gpt_usage(const char *, const struct gpt_cmd *);
-void utf16_to_utf8(const uint16_t *, uint8_t *, size_t);
+void utf16_to_utf8(const uint16_t *, size_t, uint8_t *, size_t);
void utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
#define GPT_FIND "ab:i:L:s:t:"
Index: src/sbin/gpt/show.c
diff -u src/sbin/gpt/show.c:1.39 src/sbin/gpt/show.c:1.40
--- src/sbin/gpt/show.c:1.39 Tue Oct 4 23:06:24 2016
+++ src/sbin/gpt/show.c Wed Sep 6 14:17:18 2017
@@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
#endif
#ifdef __RCSID
-__RCSID("$NetBSD: show.c,v 1.39 2016/10/05 03:06:24 kre Exp $");
+__RCSID("$NetBSD: show.c,v 1.40 2017/09/06 18:17:18 christos Exp $");
#endif
#include <sys/bootblock.h>
@@ -129,8 +129,8 @@ print_part_type(int map_type, int flags,
printf("GPT part ");
ent = map_data;
if (flags & SHOW_LABEL) {
- utf16_to_utf8(ent->ent_name, utfbuf,
- sizeof(utfbuf));
+ utf16_to_utf8(ent->ent_name, sizeof(ent->ent_name),
+ utfbuf, sizeof(utfbuf));
b = (char *)utfbuf;
} else if (flags & SHOW_GUID) {
gpt_uuid_snprintf( buf, sizeof(buf), "%d",
@@ -215,7 +215,8 @@ show_one(gpt_t gpt, unsigned int entry)
gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
printf("GUID: %s\n", s2);
- utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+ utf16_to_utf8(ent->ent_name, sizeof(ent->ent_name), utfbuf,
+ sizeof(utfbuf));
printf("Label: %s\n", (char *)utfbuf);
printf("Attributes: ");
@@ -284,7 +285,8 @@ show_all(gpt_t gpt)
#endif
putchar('\n');
- utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
+ utf16_to_utf8(ent->ent_name, sizeof(ent->ent_name),
+ utfbuf, sizeof(utfbuf));
printf(PFX "Label: %s\n", (char *)utfbuf);
printf(PFX "Attributes: ");