~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/lib/util_uuid.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 *  Unix SMB/CIFS implementation.
3
 
 *  UUID server routines
4
 
 *  Copyright (C) Theodore Ts'o               1996, 1997,
5
 
 *  Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002, 2003
6
 
 *
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.
11
 
 *  
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.
16
 
 *  
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.
20
 
 */
21
 
 
22
 
#include "includes.h"
23
 
 
24
 
/*
25
 
 * Offset between 15-Oct-1582 and 1-Jan-70
26
 
 */
27
 
#define TIME_OFFSET_HIGH 0x01B21DD2
28
 
#define TIME_OFFSET_LOW  0x13814000
29
 
 
30
 
void smb_uuid_pack(const struct uuid uu, UUID_FLAT *ptr)
31
 
{
32
 
        SIVAL(ptr->info, 0, uu.time_low);
33
 
        SSVAL(ptr->info, 4, uu.time_mid);
34
 
        SSVAL(ptr->info, 6, uu.time_hi_and_version);
35
 
        memcpy(ptr->info+8, uu.clock_seq, 2);
36
 
        memcpy(ptr->info+10, uu.node, 6);
37
 
}
38
 
 
39
 
void smb_uuid_unpack(const UUID_FLAT in, struct uuid *uu)
40
 
{
41
 
        uu->time_low = IVAL(in.info, 0);
42
 
        uu->time_mid = SVAL(in.info, 4);
43
 
        uu->time_hi_and_version = SVAL(in.info, 6);
44
 
        memcpy(uu->clock_seq, in.info+8, 2);
45
 
        memcpy(uu->node, in.info+10, 6);
46
 
}
47
 
 
48
 
struct uuid smb_uuid_unpack_static(const UUID_FLAT in)
49
 
{
50
 
        static struct uuid uu;
51
 
 
52
 
        smb_uuid_unpack(in, &uu);
53
 
        return uu;
54
 
}
55
 
 
56
 
void smb_uuid_generate_random(struct uuid *uu)
57
 
{
58
 
        UUID_FLAT tmp;
59
 
 
60
 
        generate_random_buffer(tmp.info, sizeof(tmp.info));
61
 
        smb_uuid_unpack(tmp, uu);
62
 
 
63
 
        uu->clock_seq[0] = (uu->clock_seq[0] & 0x3F) | 0x80;
64
 
        uu->time_hi_and_version = (uu->time_hi_and_version & 0x0FFF) | 0x4000;
65
 
}
66
 
 
67
 
char *smb_uuid_to_string(const struct uuid uu)
68
 
{
69
 
        char *out;
70
 
 
71
 
        asprintf(&out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
72
 
                 uu.time_low, uu.time_mid, uu.time_hi_and_version,
73
 
                 uu.clock_seq[0], uu.clock_seq[1],
74
 
                 uu.node[0], uu.node[1], uu.node[2], 
75
 
                 uu.node[3], uu.node[4], uu.node[5]);
76
 
 
77
 
        return out;
78
 
}
79
 
 
80
 
const char *smb_uuid_string_static(const struct uuid uu)
81
 
{
82
 
        static char out[37];
83
 
 
84
 
        slprintf(out, sizeof(out), 
85
 
                 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
86
 
                 uu.time_low, uu.time_mid, uu.time_hi_and_version,
87
 
                 uu.clock_seq[0], uu.clock_seq[1],
88
 
                 uu.node[0], uu.node[1], uu.node[2], 
89
 
                 uu.node[3], uu.node[4], uu.node[5]);
90
 
        return out;
91
 
}
92
 
 
93
 
BOOL smb_string_to_uuid(const char *in, struct uuid* uu)
94
 
{
95
 
        BOOL ret = False;
96
 
        const char *ptr = in;
97
 
        char *end = (char *)in;
98
 
        int i;
99
 
        unsigned v1, v2;
100
 
 
101
 
        if (!in || !uu) goto out;
102
 
 
103
 
        uu->time_low = strtoul(ptr, &end, 16);
104
 
        if ((end - ptr) != 8 || *end != '-') goto out;
105
 
        ptr = (end + 1);
106
 
 
107
 
        uu->time_mid = strtoul(ptr, &end, 16);
108
 
        if ((end - ptr) != 4 || *end != '-') goto out;
109
 
        ptr = (end + 1);
110
 
 
111
 
        uu->time_hi_and_version = strtoul(ptr, &end, 16);
112
 
        if ((end - ptr) != 4 || *end != '-') goto out;
113
 
        ptr = (end + 1);
114
 
 
115
 
        if (sscanf(ptr, "%02x%02x", &v1, &v2) != 2) {
116
 
                goto out;
117
 
        }
118
 
        uu->clock_seq[0] = v1;
119
 
        uu->clock_seq[1] = v2;
120
 
        ptr += 4;
121
 
 
122
 
        if (*ptr != '-') goto out;
123
 
        ptr++;
124
 
 
125
 
        for (i = 0; i < 6; i++) {
126
 
                if (sscanf(ptr, "%02x", &v1) != 1) {
127
 
                        goto out;
128
 
                }
129
 
                uu->node[i] = v1;
130
 
                ptr += 2;
131
 
        }
132
 
 
133
 
        ret = True;
134
 
out:
135
 
        return ret;
136
 
}