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