00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "includes.h"
00023
00024
00025
00026
00027 #define TIME_OFFSET_HIGH 0x01B21DD2
00028 #define TIME_OFFSET_LOW 0x13814000
00029
00030 void smb_uuid_pack(const struct GUID uu, UUID_FLAT *ptr)
00031 {
00032 SIVAL(ptr->info, 0, uu.time_low);
00033 SSVAL(ptr->info, 4, uu.time_mid);
00034 SSVAL(ptr->info, 6, uu.time_hi_and_version);
00035 memcpy(ptr->info+8, uu.clock_seq, 2);
00036 memcpy(ptr->info+10, uu.node, 6);
00037 }
00038
00039 void smb_uuid_unpack(const UUID_FLAT in, struct GUID *uu)
00040 {
00041 uu->time_low = IVAL(in.info, 0);
00042 uu->time_mid = SVAL(in.info, 4);
00043 uu->time_hi_and_version = SVAL(in.info, 6);
00044 memcpy(uu->clock_seq, in.info+8, 2);
00045 memcpy(uu->node, in.info+10, 6);
00046 }
00047
00048 struct GUID smb_uuid_unpack_static(const UUID_FLAT in)
00049 {
00050 static struct GUID uu;
00051
00052 smb_uuid_unpack(in, &uu);
00053 return uu;
00054 }
00055
00056 void smb_uuid_generate_random(struct GUID *uu)
00057 {
00058 UUID_FLAT tmp;
00059
00060 generate_random_buffer(tmp.info, sizeof(tmp.info));
00061 smb_uuid_unpack(tmp, uu);
00062
00063 uu->clock_seq[0] = (uu->clock_seq[0] & 0x3F) | 0x80;
00064 uu->time_hi_and_version = (uu->time_hi_and_version & 0x0FFF) | 0x4000;
00065 }
00066
00067 char *smb_uuid_to_string(const struct GUID uu)
00068 {
00069 char *out;
00070
00071 asprintf(&out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00072 uu.time_low, uu.time_mid, uu.time_hi_and_version,
00073 uu.clock_seq[0], uu.clock_seq[1],
00074 uu.node[0], uu.node[1], uu.node[2],
00075 uu.node[3], uu.node[4], uu.node[5]);
00076
00077 return out;
00078 }
00079
00080 const char *smb_uuid_string_static(const struct GUID uu)
00081 {
00082 static char out[37];
00083
00084 slprintf(out, sizeof(out),
00085 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00086 uu.time_low, uu.time_mid, uu.time_hi_and_version,
00087 uu.clock_seq[0], uu.clock_seq[1],
00088 uu.node[0], uu.node[1], uu.node[2],
00089 uu.node[3], uu.node[4], uu.node[5]);
00090 return out;
00091 }
00092
00093 BOOL smb_string_to_uuid(const char *in, struct GUID* uu)
00094 {
00095 BOOL ret = False;
00096 const char *ptr = in;
00097 char *end = (char *)in;
00098 int i;
00099 unsigned v1, v2;
00100
00101 if (!in || !uu) goto out;
00102
00103 uu->time_low = strtoul(ptr, &end, 16);
00104 if ((end - ptr) != 8 || *end != '-') goto out;
00105 ptr = (end + 1);
00106
00107 uu->time_mid = strtoul(ptr, &end, 16);
00108 if ((end - ptr) != 4 || *end != '-') goto out;
00109 ptr = (end + 1);
00110
00111 uu->time_hi_and_version = strtoul(ptr, &end, 16);
00112 if ((end - ptr) != 4 || *end != '-') goto out;
00113 ptr = (end + 1);
00114
00115 if (sscanf(ptr, "%02x%02x", &v1, &v2) != 2) {
00116 goto out;
00117 }
00118 uu->clock_seq[0] = v1;
00119 uu->clock_seq[1] = v2;
00120 ptr += 4;
00121
00122 if (*ptr != '-') goto out;
00123 ptr++;
00124
00125 for (i = 0; i < 6; i++) {
00126 if (sscanf(ptr, "%02x", &v1) != 1) {
00127 goto out;
00128 }
00129 uu->node[i] = v1;
00130 ptr += 2;
00131 }
00132
00133 ret = True;
00134 out:
00135 return ret;
00136 }