Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sbin/gpt - make sure that the utf16 string is padded with 0'...
details: https://anonhg.NetBSD.org/src/rev/324d2d8b0a92
branches: trunk
changeset: 356164:324d2d8b0a92
user: christos <christos%NetBSD.org@localhost>
date: Wed Sep 06 18:17:18 2017 +0000
description:
- 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.
diffstat:
sbin/gpt/backup.c | 5 +++--
sbin/gpt/biosboot.c | 7 ++++---
sbin/gpt/gpt.c | 28 +++++++++++++++++++++-------
sbin/gpt/gpt.h | 2 +-
sbin/gpt/show.c | 12 +++++++-----
5 files changed, 36 insertions(+), 18 deletions(-)
diffs (176 lines):
diff -r c3ba9c287636 -r 324d2d8b0a92 sbin/gpt/backup.c
--- a/sbin/gpt/backup.c Wed Sep 06 18:08:35 2017 +0000
+++ b/sbin/gpt/backup.c Wed Sep 06 18:17:18 2017 +0000
@@ -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 @@
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);
diff -r c3ba9c287636 -r 324d2d8b0a92 sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c Wed Sep 06 18:08:35 2017 +0000
+++ b/sbin/gpt/biosboot.c Wed Sep 06 18:17:18 2017 +0000
@@ -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 @@
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;
}
diff -r c3ba9c287636 -r 324d2d8b0a92 sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c Wed Sep 06 18:08:35 2017 +0000
+++ b/sbin/gpt/gpt.c Wed Sep 06 18:17:18 2017 +0000
@@ -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 @@
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 @@
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 @@
}
}
} while (c != 0);
+
+ while (s16idx < s16len)
+ s16[s16idx++] = 0;
}
void *
@@ -1027,7 +1040,8 @@
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;
}
diff -r c3ba9c287636 -r 324d2d8b0a92 sbin/gpt/gpt.h
--- a/sbin/gpt/gpt.h Wed Sep 06 18:08:35 2017 +0000
+++ b/sbin/gpt/gpt.h Wed Sep 06 18:17:18 2017 +0000
@@ -99,7 +99,7 @@
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:"
diff -r c3ba9c287636 -r 324d2d8b0a92 sbin/gpt/show.c
--- a/sbin/gpt/show.c Wed Sep 06 18:08:35 2017 +0000
+++ b/sbin/gpt/show.c Wed Sep 06 18:17:18 2017 +0000
@@ -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 @@
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 @@
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 @@
#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: ");
Home |
Main Index |
Thread Index |
Old Index