blob: cb4ec8072f94ed9a608605a8086180212b68b843 [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * uuid.c -- utility routines for manipulating UUID's.
3 */
4
5#include <stdio.h>
6#include <string.h>
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00007#include "../ext2fs/ext2_types.h"
Mike Frysingerd89e6292005-04-24 05:07:59 +00008
9#include "e2p.h"
10
11struct uuid {
12 __u32 time_low;
13 __u16 time_mid;
14 __u16 time_hi_and_version;
15 __u16 clock_seq;
16 __u8 node[6];
17};
18
19/* Returns 1 if the uuid is the NULL uuid */
20int e2p_is_null_uuid(void *uu)
21{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000022 __u8 *cp;
Mike Frysingerd89e6292005-04-24 05:07:59 +000023 int i;
24
25 for (i=0, cp = uu; i < 16; i++)
26 if (*cp)
27 return 0;
28 return 1;
29}
30
31static void e2p_unpack_uuid(void *in, struct uuid *uu)
32{
33 __u8 *ptr = in;
34 __u32 tmp;
35
36 tmp = *ptr++;
37 tmp = (tmp << 8) | *ptr++;
38 tmp = (tmp << 8) | *ptr++;
39 tmp = (tmp << 8) | *ptr++;
40 uu->time_low = tmp;
41
42 tmp = *ptr++;
43 tmp = (tmp << 8) | *ptr++;
44 uu->time_mid = tmp;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000045
Mike Frysingerd89e6292005-04-24 05:07:59 +000046 tmp = *ptr++;
47 tmp = (tmp << 8) | *ptr++;
48 uu->time_hi_and_version = tmp;
49
50 tmp = *ptr++;
51 tmp = (tmp << 8) | *ptr++;
52 uu->clock_seq = tmp;
53
54 memcpy(uu->node, ptr, 6);
55}
56
57void e2p_uuid_to_str(void *uu, char *out)
58{
59 struct uuid uuid;
60
61 e2p_unpack_uuid(uu, &uuid);
62 sprintf(out,
63 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
64 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
65 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
66 uuid.node[0], uuid.node[1], uuid.node[2],
67 uuid.node[3], uuid.node[4], uuid.node[5]);
68}
69
70const char *e2p_uuid2str(void *uu)
71{
72 static char buf[80];
Mike Frysingerd89e6292005-04-24 05:07:59 +000073 if (e2p_is_null_uuid(uu))
74 return "<none>";
75 e2p_uuid_to_str(uu, buf);
76 return buf;
77}