1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (c) 2010, Brian Aker
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* * Neither the name of the <organization> nor the
15
* names of its contributors may be used to endorse or promote products
16
* derived from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
// I looked at code which also had this Copyright header.
34
* Copyright (C) 1996, 1997 Theodore Ts'o.
37
* Redistribution and use in source and binary forms, with or without
38
* modification, are permitted provided that the following conditions
40
* 1. Redistributions of source code must retain the above copyright
41
* notice, and the entire permission notice in its entirety,
42
* including the disclaimer of warranties.
43
* 2. Redistributions in binary form must reproduce the above copyright
44
* notice, this list of conditions and the following disclaimer in the
45
* documentation and/or other materials provided with the distribution.
46
* 3. The name of the author may not be used to endorse or promote
47
* products derived from this software without specific prior
50
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
51
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
53
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
54
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
57
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
60
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
65
#ifndef DRIZZLED_FIELD_UUID_ST_H
66
#define DRIZZLED_FIELD_UUID_ST_H
79
uint16_t time_hi_and_version;
86
time_hi_and_version(0),
89
node[0]= node[1]= node[2]= node[3]= node[4]= node[5]= 0;
92
void unpack(const unsigned char *in)
94
const uint8_t *ptr= reinterpret_cast<const uint8_t *>(in);
98
tmp= (tmp << 8) | *ptr++;
99
tmp= (tmp << 8) | *ptr++;
100
tmp= (tmp << 8) | *ptr++;
104
tmp= (tmp << 8) | *ptr++;
108
tmp= (tmp << 8) | *ptr++;
109
time_hi_and_version = tmp;
112
tmp= (tmp << 8) | *ptr++;
115
memcpy(node, ptr, 6);
118
void pack(unsigned char *out)
123
out[3] = (unsigned char) tmp;
125
out[2] = (unsigned char) tmp;
127
out[1] = (unsigned char) tmp;
129
out[0] = (unsigned char) tmp;
132
out[5] = (unsigned char) tmp;
134
out[4] = (unsigned char) tmp;
136
tmp = time_hi_and_version;
137
out[7] = (unsigned char) tmp;
139
out[6] = (unsigned char) tmp;
142
out[9] = (unsigned char) tmp;
144
out[8] = (unsigned char) tmp;
146
memcpy(out+10, node, 6);
149
bool parse(const char *in)
155
for (i= 0, cp= in; i < DISPLAY_LENGTH; i++, cp++)
157
if ((i == 8) || (i == 13) || (i == 18) || (i == 23))
169
if (not isxdigit(*cp))
173
time_low= strtoul(in, NULL, 16);
174
time_mid= strtoul(in+9, NULL, 16);
175
time_hi_and_version= strtoul(in+14, NULL, 16);
176
clock_seq= strtoul(in+19, NULL, 16);
180
for (i= 0; i < 6; i++)
184
node[i]= strtoul(buf, NULL, 16);
190
void unparse(char *out)
192
snprintf(out, DISPLAY_BUFFER_LENGTH, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
206
void time(struct timeval ret_val)
211
high= time_mid | ((time_hi_and_version & 0xFFF) << 16);
212
clock_reg= time_low | ((uint64_t) high << 32);
214
clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
215
ret_val.tv_sec = clock_reg / 10000000;
216
ret_val.tv_usec = (clock_reg % 10000000) / 10;
221
return ((time_hi_and_version >> 12) & 0xF) == 1 ? true : false;
224
static const size_t LENGTH= 16;
225
static const size_t DISPLAY_LENGTH= 36;
226
static const size_t DISPLAY_BUFFER_LENGTH= DISPLAY_LENGTH+1;
229
} /* namespace field */
230
} /* namespace drizzled */
232
#endif /* DRIZZLED_FIELD_UUID_ST_H */