00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <vpr/vprConfig.h>
00043
00044 #include <stdio.h>
00045 #include <string.h>
00046 #include <cctype>
00047
00048 #ifdef HAVE_SYS_TYPES_H
00049 # include <sys/types.h>
00050 #endif
00051
00052 #if defined(VPR_USE_LEACH_UUID)
00053 # include <uuid/sysdep.h>
00054 #else
00055 # define PROTOTYPES 1
00056 extern "C" {
00057 # include <global.h>
00058 # include <md5.h>
00059 }
00060 #endif
00061
00062
00063 #if defined(HAVE_UUID_H)
00064 # include <uuid.h>
00065 #elif defined(HAVE_UUID_UUID_H) || defined(VPR_USE_LEACH_UUID)
00066 # include <uuid/uuid.h>
00067 #elif defined(HAVE_SYS_UUID_H)
00068
00069
00070 # ifdef VPR_OS_IRIX
00071 extern "C" {
00072 # endif
00073 # include <sys/uuid.h>
00074 # ifdef VPR_OS_IRIX
00075 }
00076 # endif
00077 #endif
00078
00079 #include <boost/concept_check.hpp>
00080 #include <vpr/System.h>
00081 #include <vpr/IO/ObjectWriter.h>
00082 #include <vpr/IO/ObjectReader.h>
00083 #include <vpr/Util/GUID.h>
00084
00085
00086 namespace vpr
00087 {
00088
00089 vpr::GUID::StdGUID null_guid_struct = { 0x00000000, 0x0000, 0x0000, 0x00, 0x00,
00090 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00091 };
00092
00093 const vpr::GUID GUID::NullGUID(null_guid_struct);
00094
00095 vpr::GUID::GenerateTag GUID::generateTag;
00096
00097 GUID::GUID(const GenerateTag& tag)
00098 {
00099 boost::ignore_unused_variable_warning(tag);
00100 generate();
00101 }
00102
00103 std::string GUID::toString() const
00104 {
00105 std::string guid_str;
00106 char guid_c_str[37];
00107
00108 #ifdef HAVE_SNPRINTF
00109 snprintf(guid_c_str, 37,
00110 #else
00111 sprintf(guid_c_str,
00112 #endif
00113 "%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X",
00114 mGuid.moz.m0, mGuid.moz.m1, mGuid.moz.m2, mGuid.moz.m3[0],
00115 (vpr::Uint32) mGuid.moz.m3[1], (vpr::Uint32) mGuid.moz.m3[2],
00116 (vpr::Uint32) mGuid.moz.m3[3], (vpr::Uint32) mGuid.moz.m3[4],
00117 (vpr::Uint32) mGuid.moz.m3[5], (vpr::Uint32) mGuid.moz.m3[6],
00118 (vpr::Uint32) mGuid.moz.m3[7]);
00119 guid_str = guid_c_str;
00120
00121 return guid_str;
00122 }
00123
00124 bool GUID::operator== (const GUID& guid) const
00125 {
00126
00127
00128
00129
00130
00131
00132
00133
00134 return ( (mGuid.packed.l0 == guid.mGuid.packed.l0) &&
00135 (mGuid.packed.l1 == guid.mGuid.packed.l1) &&
00136 (mGuid.packed.l2 == guid.mGuid.packed.l2) &&
00137 (mGuid.packed.l3 == guid.mGuid.packed.l3) );
00138 }
00139
00140
00141
00142
00143
00144 GUID::GUID()
00145 : mGuid(GUID::NullGUID.mGuid)
00146 {
00147 ;
00148 }
00149
00150 GUID::GUID(const struct vpr::GUID::StdGUID& guid)
00151 {
00152 memcpy(&mGuid, &guid, sizeof(vpr::GUID::StdGUID));
00153 }
00154
00155 GUID::GUID(const GUID& ns_guid, const std::string& name)
00156 {
00157 generate(ns_guid,name);
00158 }
00159
00160 void GUID::generate()
00161 {
00162
00163 #if defined(VPR_USE_DCE_1_1_UUID)
00164 uint32_t status(0);
00165 uuid_create((uuid_t*) &mGuid.standard, &status);
00166
00167 #elif defined(VPR_USE_LIBUUID)
00168 uuid_t storage;
00169 uuid_generate(storage);
00170 memcpy((void*) &mGuid.standard, storage, sizeof(mGuid));
00171
00172 #else
00173 uuid_create( (uuid_t*)(&mGuid.standard));
00174 #endif
00175 }
00176
00177 void GUID::generate(const GUID& ns_guid, const std::string& name)
00178 {
00179
00180 #if defined(VPR_USE_LEACH_UUID)
00181 uuid_t temp_ns_id = *((uuid_t*)(&ns_guid.mGuid.standard));
00182
00183 uuid_create_from_name((uuid_t*) (&mGuid.standard), temp_ns_id,
00184 (void*) name.c_str(), name.length());
00185
00186
00187 #else
00188
00189 vpr::GUID net_ns_guid = ns_guid;
00190 net_ns_guid.mGuid.standard.m0 = vpr::System::Htonl(net_ns_guid.mGuid.standard.m0);
00191 net_ns_guid.mGuid.standard.m1 = vpr::System::Htons(net_ns_guid.mGuid.standard.m1);
00192 net_ns_guid.mGuid.standard.m2 = vpr::System::Htons(net_ns_guid.mGuid.standard.m2);
00193
00194
00195 MD5_CTX c;
00196 unsigned char hash[16];
00197
00198 MD5Init(&c);
00199 MD5Update(&c, (unsigned char*) &net_ns_guid.mGuid.standard,
00200 sizeof(net_ns_guid.mGuid.standard));
00201 MD5Update(&c, (unsigned char*) name.c_str(), name.length());
00202 MD5Final(hash, &c);
00203
00204
00205 memcpy((void*) &mGuid.standard, hash, sizeof(mGuid.standard));
00206
00207
00208 mGuid.standard.m0 = vpr::System::Ntohl(mGuid.standard.m0);
00209 mGuid.standard.m1 = vpr::System::Ntohs(mGuid.standard.m1);
00210 mGuid.standard.m2 = vpr::System::Ntohs(mGuid.standard.m2);
00211
00212
00213
00214 mGuid.standard.m2 &= 0x0FFF;
00215
00216 mGuid.standard.m2 |= 0x3000;
00217 mGuid.standard.m3 &= 0x3F;
00218 mGuid.standard.m3 |= 0x80;
00219 #endif
00220 }
00221
00222 vpr::ReturnStatus GUID::writeObject(vpr::ObjectWriter* writer)
00223 {
00224 writer->writeUint32(mGuid.standard.m0);
00225 writer->writeUint16(mGuid.standard.m1);
00226 writer->writeUint16(mGuid.standard.m2);
00227 writer->writeUint8(mGuid.standard.m3);
00228 writer->writeUint8(mGuid.standard.m4);
00229 writer->writeUint8(mGuid.standard.m5[0]);
00230 writer->writeUint8(mGuid.standard.m5[1]);
00231 writer->writeUint8(mGuid.standard.m5[2]);
00232 writer->writeUint8(mGuid.standard.m5[3]);
00233 writer->writeUint8(mGuid.standard.m5[4]);
00234 writer->writeUint8(mGuid.standard.m5[5]);
00235 return vpr::ReturnStatus::Succeed;
00236 }
00237
00238 vpr::ReturnStatus GUID::readObject(vpr::ObjectReader* reader)
00239 {
00240 mGuid.standard.m0 = reader->readUint32();
00241 mGuid.standard.m1 = reader->readUint16();
00242 mGuid.standard.m2 = reader->readUint16();
00243 mGuid.standard.m3 = reader->readUint8();
00244 mGuid.standard.m4 = reader->readUint8();
00245 mGuid.standard.m5[0] = reader->readUint8();
00246 mGuid.standard.m5[1] = reader->readUint8();
00247 mGuid.standard.m5[2] = reader->readUint8();
00248 mGuid.standard.m5[3] = reader->readUint8();
00249 mGuid.standard.m5[4] = reader->readUint8();
00250 mGuid.standard.m5[5] = reader->readUint8();
00251 return vpr::ReturnStatus::Succeed;
00252 }
00253
00254 void GUID::fromString(const std::string& guid_string)
00255 {
00256 vpr::Uint32 m3[8];
00257
00258
00259
00260 sscanf(guid_string.c_str(),
00261 "%08x-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x",
00262 &mGuid.moz.m0, &mGuid.moz.m1, &mGuid.moz.m2,
00263 &m3[0], &m3[1], &m3[2], &m3[3], &m3[4], &m3[5], &m3[6], &m3[7]);
00264
00265
00266 for ( int i = 0; i < 8; ++i )
00267 {
00268 mGuid.moz.m3[i] = (vpr::Uint8) m3[i];
00269 }
00270 }
00271
00272
00273 }