Whamcloud - gitweb
rewrite inherited code in obdclass/uuid.c (uuid parsing code from ext2 utils)
[fs/lustre-release.git] / lustre / obdclass / uuid.c
1 /*
2  * Public include file for the UUID library
3  *
4  * Copyright (C) 2007 Cluster File System
5  */
6
7 #define DEBUG_SUBSYSTEM S_CLASS
8
9 #ifndef __KERNEL__
10 # include <liblustre.h>
11 #else
12 # include <libcfs/kp30.h>
13 #endif
14
15 #include <obd_support.h>
16 #include <obd_class.h>
17
18
19 static inline __u32 consume(int nob, __u8 **ptr)
20 {
21         __u32 value;
22
23         LASSERT(nob <= sizeof value);
24
25         for (value = 0; nob > 0; --nob)
26                 value = (value << 8) | *((*ptr)++);
27         return value;
28 }
29
30 #define CONSUME(val, ptr) (val) = consume(sizeof(val), (ptr))
31
32 static void uuid_unpack(class_uuid_t in, __u16 *uu, int nr)
33 {
34         __u8 *ptr = in;
35
36         LASSERT(nr * sizeof *uu == sizeof(class_uuid_t));
37
38         while (nr-- > 0)
39                 CONSUME(uu[nr], &ptr);
40 }
41
42 void class_uuid_unparse(class_uuid_t uu, struct obd_uuid *out)
43 {
44         /* uu as an array of __u16's */
45         __u16 uuid[sizeof(class_uuid_t) / sizeof(__u16)];
46
47         CLASSERT(ARRAY_SIZE(uuid) == 8);
48
49         uuid_unpack(uu, uuid, ARRAY_SIZE(uuid));
50         sprintf(out->uuid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
51                 uuid[0], uuid[1], uuid[2], uuid[3],
52                 uuid[4], uuid[5], uuid[6], uuid[7]);
53 }