1 /*
2  * uuid.c -- utility routines for manipulating UUID's.
3  *
4  * %Begin-Header%
5  * This file may be redistributed under the terms of the GNU Library
6  * General Public License, version 2.
7  * %End-Header%
8  */
9 
10 #include "config.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <ext2fs/ext2_types.h>
14 
15 #include "e2p.h"
16 
17 struct uuid {
18 	__u32	time_low;
19 	__u16	time_mid;
20 	__u16	time_hi_and_version;
21 	__u16	clock_seq;
22 	__u8	node[6];
23 };
24 
25 /* Returns 1 if the uuid is the NULL uuid */
e2p_is_null_uuid(void * uu)26 int e2p_is_null_uuid(void *uu)
27 {
28 	__u8 	*cp;
29 	int	i;
30 
31 	for (i=0, cp = uu; i < 16; i++)
32 		if (*cp++)
33 			return 0;
34 	return 1;
35 }
36 
e2p_unpack_uuid(void * in,struct uuid * uu)37 static void e2p_unpack_uuid(void *in, struct uuid *uu)
38 {
39 	__u8	*ptr = in;
40 	__u32	tmp;
41 
42 	tmp = *ptr++;
43 	tmp = (tmp << 8) | *ptr++;
44 	tmp = (tmp << 8) | *ptr++;
45 	tmp = (tmp << 8) | *ptr++;
46 	uu->time_low = tmp;
47 
48 	tmp = *ptr++;
49 	tmp = (tmp << 8) | *ptr++;
50 	uu->time_mid = tmp;
51 
52 	tmp = *ptr++;
53 	tmp = (tmp << 8) | *ptr++;
54 	uu->time_hi_and_version = tmp;
55 
56 	tmp = *ptr++;
57 	tmp = (tmp << 8) | *ptr++;
58 	uu->clock_seq = tmp;
59 
60 	memcpy(uu->node, ptr, 6);
61 }
62 
e2p_uuid_to_str(void * uu,char * out)63 void e2p_uuid_to_str(void *uu, char *out)
64 {
65 	struct uuid uuid;
66 
67 	e2p_unpack_uuid(uu, &uuid);
68 	sprintf(out,
69 		"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
70 		uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
71 		uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
72 		uuid.node[0], uuid.node[1], uuid.node[2],
73 		uuid.node[3], uuid.node[4], uuid.node[5]);
74 }
75 
e2p_uuid2str(void * uu)76 const char *e2p_uuid2str(void *uu)
77 {
78 	static char buf[80];
79 
80 	if (e2p_is_null_uuid(uu))
81 		return "<none>";
82 	e2p_uuid_to_str(uu, buf);
83 	return buf;
84 }
85 
86