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