Code to generate uuids for ADS setups. Uses our random generator but
[Samba/gbeck.git] / source / lib / util_uuid.c
blobb70db8de5b03ffcfa95754ccd3a02313d5d0a1c1
1 /*
2 * Unix SMB/CIFS implementation.
3 * UUID server routines
4 * Copyright (C) Theodore Ts'o 1996, 1997,
5 * Copyright (C) Jim McDonough 2002.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
25 * Offset between 15-Oct-1582 and 1-Jan-70
27 #define TIME_OFFSET_HIGH 0x01B21DD2
28 #define TIME_OFFSET_LOW 0x13814000
30 struct uuid {
31 uint32 time_low;
32 uint16 time_mid;
33 uint16 time_hi_and_version;
34 uint16 clock_seq;
35 uint8 node[6];
39 static void uuid_pack(const struct uuid *uu, GUID *ptr)
41 uint32 tmp;
42 uint8 *out = ptr->info;
44 tmp = uu->time_low;
45 out[3] = (uint8) tmp;
46 tmp >>= 8;
47 out[2] = (uint8) tmp;
48 tmp >>= 8;
49 out[1] = (uint8) tmp;
50 tmp >>= 8;
51 out[0] = (uint8) tmp;
53 tmp = uu->time_mid;
54 out[5] = (uint8) tmp;
55 tmp >>= 8;
56 out[4] = (uint8) tmp;
58 tmp = uu->time_hi_and_version;
59 out[7] = (uint8) tmp;
60 tmp >>= 8;
61 out[6] = (uint8) tmp;
63 tmp = uu->clock_seq;
64 out[9] = (uint8) tmp;
65 tmp >>= 8;
66 out[8] = (uint8) tmp;
68 memcpy(out+10, uu->node, 6);
71 static void uuid_unpack(const GUID in, struct uuid *uu)
73 const uint8 *ptr = in.info;
74 uint32 tmp;
76 tmp = *ptr++;
77 tmp = (tmp << 8) | *ptr++;
78 tmp = (tmp << 8) | *ptr++;
79 tmp = (tmp << 8) | *ptr++;
80 uu->time_low = tmp;
82 tmp = *ptr++;
83 tmp = (tmp << 8) | *ptr++;
84 uu->time_mid = tmp;
86 tmp = *ptr++;
87 tmp = (tmp << 8) | *ptr++;
88 uu->time_hi_and_version = tmp;
90 tmp = *ptr++;
91 tmp = (tmp << 8) | *ptr++;
92 uu->clock_seq = tmp;
94 memcpy(uu->node, ptr, 6);
97 void uuid_generate_random(GUID *out)
99 GUID tmp;
100 struct uuid uu;
102 generate_random_buffer(tmp.info, sizeof(tmp.info), True);
103 uuid_unpack(tmp, &uu);
105 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
106 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
107 uuid_pack(&uu, out);