From a66d82b084937fce15804f42f3d8167e536cfcb6 Mon Sep 17 00:00:00 2001 From: krw Date: Wed, 20 Apr 2022 15:49:56 +0000 Subject: [PATCH] Simpify code manipulating GPT partition names by coalescing logic into GPT_get_name(), string_to_name() and name_to_string() functions. Remove unnecessarily abstract functions ask_string(), utf16le_to_string() and string_to_utf16le(). No intentional functional change. --- sbin/fdisk/cmd.c | 40 +++------------------------------ sbin/fdisk/gpt.c | 57 +++++++++++++++++++++++++++++++++++++++++++---- sbin/fdisk/misc.c | 36 +----------------------------- sbin/fdisk/misc.h | 4 +--- 4 files changed, 58 insertions(+), 79 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index e56b41fb904..432308006e1 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.156 2022/04/20 00:47:32 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.157 2022/04/20 15:49:56 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -44,7 +44,6 @@ int parsepn(const char *); int ask_num(const char *, int, int, int); int ask_pid(const int); struct uuid *ask_uuid(const struct uuid *); -char *ask_string(const char *, const char *); extern const unsigned char manpage[]; extern const int manpage_sz; @@ -119,9 +118,6 @@ int gedit(const int pn) { struct uuid oldtype; - char *name; - uint16_t *utf; - int i; oldtype = gp[pn].gp_type; @@ -137,26 +133,11 @@ gedit(const int pn) } if (GPT_get_lba_start(pn) == -1 || - GPT_get_lba_end(pn) == -1) { + GPT_get_lba_end(pn) == -1 || + GPT_get_name(pn) == -1) { return -1; } - name = ask_string("Partition name", utf16le_to_string(gp[pn].gp_name)); - if (strlen(name) >= GPTPARTNAMESIZE) { - printf("partition name must be < %d characters\n", - GPTPARTNAMESIZE); - return -1; - } - /* - * N.B.: simple memcpy() could copy trash from static buf! This - * would create false positives for the partition having changed. - */ - utf = string_to_utf16le(name); - for (i = 0; i < GPTPARTNAMESIZE; i++) { - gp[pn].gp_name[i] = utf[i]; - if (utf[i] == 0) - break; - } return 0; } @@ -660,18 +641,3 @@ ask_uuid(const struct uuid *olduuid) free(dflt); return &uuid; } - -char * -ask_string(const char *prompt, const char *oval) -{ - static char buf[UUID_STR_LEN + 1]; - - buf[0] = '\0'; - printf("%s: [%s] ", prompt, oval ? oval : ""); - string_from_line(buf, sizeof(buf), UNTRIMMED); - - if (buf[0] == '\0' && oval) - strlcpy(buf, oval, sizeof(buf)); - - return buf; -} diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c index 1f4e6ea6c64..5c4fbf9c12a 100644 --- a/sbin/fdisk/gpt.c +++ b/sbin/fdisk/gpt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.c,v 1.70 2022/04/20 00:47:32 krw Exp $ */ +/* $OpenBSD: gpt.c,v 1.71 2022/04/20 15:49:56 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -57,6 +57,33 @@ int init_gp(const int); uint32_t crc32(const u_char *, const uint32_t); int protective_mbr(const struct mbr *); int gpt_chk_mbr(struct dos_partition *, uint64_t); +void string_to_name(const unsigned int, const char *); +const char *name_to_string(const unsigned int); + +void +string_to_name(const unsigned int pn, const char *ch) +{ + unsigned int i; + + memset(gp[pn].gp_name, 0, sizeof(gp[pn].gp_name)); + + for (i = 0; i < sizeof(gp[pn].gp_name) && ch[i] != '\0'; i++) + gp[pn].gp_name[i] = htole16((unsigned int)ch[i]); +} + +const char * +name_to_string(const unsigned int pn) +{ + static char name[GPTPARTNAMESIZE + 1]; + unsigned int i; + + memset(name, 0, sizeof(name)); + + for (i = 0; i < sizeof(name) && gp[pn].gp_name[i] != 0; i++) + name[i] = letoh16(gp[pn].gp_name[i]) & 0x7F; + + return name; +} /* * Return the index into dp[] of the EFI GPT (0xEE) partition, or -1 if no such @@ -417,7 +444,7 @@ GPT_print_part(const unsigned int pn, const char *units, const int verbosity) printf(" "); else printf(" %-36s ", guidstr); - printf("%-36s\n", utf16le_to_string(gp[pn].gp_name)); + printf("%-36s\n", name_to_string(pn)); free(guidstr); } } @@ -478,8 +505,7 @@ add_partition(const uint8_t *beuuid, const char *name, uint64_t sectors) gp[pn].gp_type = gp_type; gp[pn].gp_lba_start = htole64(start); gp[pn].gp_lba_end = htole64(end); - memcpy(gp[pn].gp_name, string_to_utf16le(name), - sizeof(gp[pn].gp_name)); + string_to_name(pn, name); uuid_create(&uuid, &status); if (status != uuid_s_ok) @@ -858,6 +884,29 @@ GPT_get_lba_end(const unsigned int pn) return 0; } +int +GPT_get_name(const unsigned int pn) +{ + char name[GPTPARTNAMESIZE + 1]; + + printf("Partition name: [%s] ", name_to_string(pn)); + string_from_line(name, sizeof(name), UNTRIMMED); + + switch (strlen(name)) { + case 0: + break; + case GPTPARTNAMESIZE: + printf("partition name must be < %d characters\n", + GPTPARTNAMESIZE); + return -1; + default: + string_to_name(pn, name); + break; + } + + return 0; +} + /* * Adapted from Hacker's Delight crc32b(). * diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index a1c8c59802e..34019bfe9c8 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.86 2021/09/13 15:07:51 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.87 2022/04/20 15:49:56 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -234,40 +234,6 @@ getuint64(const char *prompt, uint64_t oval, const uint64_t minval, return (uint64_t)d; } -char * -utf16le_to_string(const uint16_t *utf) -{ - static char name[GPTPARTNAMESIZE]; - int i; - - for (i = 0; i < GPTPARTNAMESIZE; i++) { - name[i] = letoh16(utf[i]) & 0x7F; - if (name[i] == '\0') - break; - } - if (i == GPTPARTNAMESIZE) - name[i - 1] = '\0'; - - return name; -} - -uint16_t * -string_to_utf16le(const char *ch) -{ - static uint16_t utf[GPTPARTNAMESIZE]; - int i; - - for (i = 0; i < GPTPARTNAMESIZE; i++) { - utf[i] = htole16((unsigned int)ch[i]); - if (utf[i] == 0) - break; - } - if (i == GPTPARTNAMESIZE) - utf[i - 1] = 0; - - return utf; -} - int hex_octet(char *buf) { diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index d4c666eaa2a..c4c3f1c2679 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.47 2021/10/25 13:51:25 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.48 2022/04/20 15:49:56 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -36,5 +36,3 @@ void string_from_line(char *, const size_t, const int); int ask_yn(const char *); int hex_octet(char *); uint64_t getuint64(const char *, uint64_t, const uint64_t, const uint64_t); -char *utf16le_to_string(const uint16_t *); -uint16_t *string_to_utf16le(const char *); -- 2.20.1