[git commit] uuidgen: move UUID formatting to libbb

Denys Vlasenko vda.linux at googlemail.com
Tue Feb 3 08:10:25 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=3e8010196325c49537fdcfddf58a1b68ae84945b
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
format_uuid_DCE_37_chars                               -      94     +94
uuidgen_main                                          71      53     -18
.rodata                                           107089  107066     -23
mkswap_main                                          278     253     -25
volume_id_set_uuid                                   258     178     -80
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/4 up/down: 94/-146)           Total: -52 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/libbb.h             |  1 +
 libbb/xfuncs_printf.c       | 13 +++++++++++++
 util-linux/fdisk_gpt.c      |  6 +++---
 util-linux/mkswap.c         | 16 +++++-----------
 util-linux/uuidgen.c        |  9 ++++-----
 util-linux/volume_id/util.c |  8 +-------
 6 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 17c9bc785..aacff42ee 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1152,6 +1152,7 @@ void FAST_FUNC xorbuf16(void* buf, const void* mask);
 
 /* Generate a UUID */
 void generate_uuid(uint8_t *buf) FAST_FUNC;
+void FAST_FUNC format_uuid_DCE_37_chars(char *dst37, const uint8_t *buf16);
 
 /* Last element is marked by mult == 0 */
 struct suffix_mult {
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 63054edc4..5d26e2bfa 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -685,6 +685,19 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
 	buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
 }
 
+void FAST_FUNC format_uuid_DCE_37_chars(char *dst37, const uint8_t *buf)
+{
+	/* 37 = 16*2 hexdigits + 4 dashes + 1 NUL */
+	sprintf(dst37,
+		"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+		buf[0], buf[1], buf[2], buf[3],
+		buf[4], buf[5],
+		buf[6], buf[7],
+		buf[8], buf[9],
+		buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]
+	);
+}
+
 #if BB_MMU
 pid_t FAST_FUNC xfork(void)
 {
diff --git a/util-linux/fdisk_gpt.c b/util-linux/fdisk_gpt.c
index 60c7c1570..ec8818ec3 100644
--- a/util-linux/fdisk_gpt.c
+++ b/util-linux/fdisk_gpt.c
@@ -59,9 +59,9 @@ gpt_print_guid(uint8_t *buf)
 {
 	printf(
 		"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
-		buf[3], buf[2], buf[1], buf[0],
-		buf[5], buf[4],
-		buf[7], buf[6],
+		buf[3], buf[2], buf[1], buf[0], /* GPT byteswaps... */
+		buf[5], buf[4], /* ...these */
+		buf[7], buf[6], /* ...fields */
 		buf[8], buf[9],
 		buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
 }
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c
index f80457a31..bca3de199 100644
--- a/util-linux/mkswap.c
+++ b/util-linux/mkswap.c
@@ -91,7 +91,7 @@ struct swap_header_v1 {
 	uint32_t version;        /* second kbyte, word 0 */
 	uint32_t last_page;      /* 1 */
 	uint32_t nr_badpages;    /* 2 */
-	char     sws_uuid[16];   /* 3,4,5,6 */
+	uint8_t  sws_uuid[16];   /* 3,4,5,6 */
 	char     sws_volume[16]; /* 7,8,9,10 */
 	uint32_t padding[117];   /* 11..127 */
 	uint32_t badpages[1];    /* 128 */
@@ -148,17 +148,11 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv)
 	hdr->last_page = (uoff_t)len / pagesize;
 
 	if (ENABLE_FEATURE_MKSWAP_UUID) {
-		char uuid_string[32];
-		generate_uuid((void*)hdr->sws_uuid);
-		bin2hex(uuid_string, hdr->sws_uuid, 16);
+		char uuid_string37[37];
 		/* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
-		printf("UUID=%.8s"  "-%.4s-%.4s-%.4s-%.12s\n",
-			uuid_string,
-			uuid_string+8,
-			uuid_string+8+4,
-			uuid_string+8+4+4,
-			uuid_string+8+4+4+4
-		);
+		generate_uuid(hdr->sws_uuid);
+		format_uuid_DCE_37_chars(uuid_string37, hdr->sws_uuid);
+		printf("UUID=%s\n", uuid_string37);
 	}
 	safe_strncpy(hdr->sws_volume, label, 16);
 
diff --git a/util-linux/uuidgen.c b/util-linux/uuidgen.c
index c2e1dd4da..96d7cd1db 100644
--- a/util-linux/uuidgen.c
+++ b/util-linux/uuidgen.c
@@ -36,22 +36,21 @@
  * Otherwise, it will choose a time-based UUID."
  */
 #include "libbb.h"
-#include "volume_id/volume_id_internal.h"
 
 /* This is a NOFORK applet. Be very careful! */
 
 int uuidgen_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int uuidgen_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
-	struct volume_id id;
+	char str37[37];
 	uint8_t uuid[16];
 
 	/* support/ignore -r (--random) */
 	getopt32(argv, "^" "r" "\0" "=0"/* no args!*/);
 
 	generate_uuid(uuid);
-	volume_id_set_uuid(&id, uuid, UUID_DCE);
-	puts(id.uuid);
+	format_uuid_DCE_37_chars(str37, uuid);
+	puts(str37);
 
-	return 0;
+	fflush_stdout_and_exit_SUCCESS();
 }
diff --git a/util-linux/volume_id/util.c b/util-linux/volume_id/util.c
index 061545fde..ddfe98d50 100644
--- a/util-linux/volume_id/util.c
+++ b/util-linux/volume_id/util.c
@@ -158,13 +158,7 @@ set:
 			buf[3], buf[2], buf[1], buf[0]);
 		break;
 	case UUID_DCE:
-		sprintf(id->uuid,
-			"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
-			buf[0], buf[1], buf[2], buf[3],
-			buf[4], buf[5],
-			buf[6], buf[7],
-			buf[8], buf[9],
-			buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
+		format_uuid_DCE_37_chars(id->uuid, buf);
 		break;
 	case UUID_DCE_STRING:
 		memcpy(id->uuid, buf, count);


More information about the busybox-cvs mailing list