blob: 310b01de611b6fe3d8150a941d10d46724a7b407 [file] [log] [blame]
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001/*
2 * uuid.c -- utility routines for manipulating UUID's.
Theodore Ts'o543547a2010-05-17 21:31:56 -04003 *
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%
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00008 */
9
10#include <stdio.h>
Theodore Ts'o21c84b71997-04-29 16:15:03 +000011#include <string.h>
Theodore Ts'o797f5ef2001-06-01 23:49:46 +000012#include <ext2fs/ext2_types.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000013
Theodore Ts'o21c84b71997-04-29 16:15:03 +000014#include "e2p.h"
15
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000016struct 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++)
Theodore Ts'ob116e782006-04-22 04:32:36 -040031 if (*cp++)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000032 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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040050
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000051 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}
Theodore Ts'of61fc0b2002-08-24 00:04:03 -040074
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