Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

GUID.cpp

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile: GUID.cpp,v $
00010  * Date modified: $Date: 2003/11/12 20:26:07 $
00011  * Version:       $Revision: 1.30 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
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 // Too bad there isn't a standard location for uuid.h ...
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 // XXX: On IRIX, sys/uuid.h is not a C++-safe header.  Blah...
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>  // for ignore_unused_variable_warning
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    bool ret_val0 = (mGuid.packed.l0 == guid.mGuid.packed.l0);
00128    bool ret_val1 = (mGuid.packed.l1 == guid.mGuid.packed.l1);
00129    bool ret_val2 = (mGuid.packed.l2 == guid.mGuid.packed.l2);
00130    bool ret_val3 = (mGuid.packed.l3 == guid.mGuid.packed.l3);
00131    return (ret_val0 && ret_val1 && ret_val2 && ret_val3);
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 // Private methods.
00142 // ============================================================================
00143 
00144 GUID::GUID()
00145    : mGuid(GUID::NullGUID.mGuid)     // Assign a null guid
00146 {
00147    ; /* Do nothing */
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 // DCE 1.1 UUID.
00163 #if defined(VPR_USE_DCE_1_1_UUID)
00164    uint32_t status(0);
00165    uuid_create((uuid_t*) &mGuid.standard, &status);
00166 // Linux e2fsprogs libuuid.
00167 #elif defined(VPR_USE_LIBUUID)
00168    uuid_t storage;
00169    uuid_generate(storage);
00170    memcpy((void*) &mGuid.standard, storage, sizeof(mGuid));
00171 // Leach UUID (see juggler/external/leach-uuid).
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 // Leach UUID (see juggler/external/leach-uuid).
00180 #if defined(VPR_USE_LEACH_UUID)
00181    uuid_t temp_ns_id = *((uuid_t*)(&ns_guid.mGuid.standard));    // nasty, but works
00182 
00183    uuid_create_from_name((uuid_t*) (&mGuid.standard), temp_ns_id,
00184                          (void*) name.c_str(), name.length());
00185 
00186 // Implementation of the algorithm for creating a name-based UUID.
00187 #else
00188    // Convert to network byte order.
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    // The following uses the MD5 implementation in juggler/external/md5-c.
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    // At this point, the hash is in network byte order.
00205    memcpy((void*) &mGuid.standard, hash, sizeof(mGuid.standard));
00206 
00207    // Convert to host byte order.
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    // Put in the variant and version bits.  This is based on the Leach UUID
00213    // implementation (the function format_uuid_v3).
00214    mGuid.standard.m2 &= 0x0FFF;
00215 //   mGuid.standard.m2 |= (3 << 12);
00216    mGuid.standard.m2 |= 0x3000; // Version 3 UUID
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    // The array of vpr::Uint32's is used to avoid alignment problems on
00259    // architectures such as MIPS.
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    // Fill the mGuid struct with the values read into m3[] above.
00266    for ( int i = 0; i < 8; ++i )
00267    {
00268       mGuid.moz.m3[i] = (vpr::Uint8) m3[i];
00269    }
00270 }
00271 
00272 
00273 } // End of vpr namespace

Generated on Sun May 2 14:43:01 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002