srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 1 | // |
| 2 | // C++ Implementation: GUIDData |
| 3 | // |
| 4 | // Description: GUIDData class header |
| 5 | // Implements the GUIDData data structure and support methods |
| 6 | // |
| 7 | // |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 8 | // Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2010-2011 |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 9 | // |
| 10 | // Copyright: See COPYING file that comes with this distribution |
| 11 | // |
| 12 | // |
| 13 | |
| 14 | #define __STDC_LIMIT_MACROS |
Aurimas Liutikas | fcad060 | 2016-05-10 19:16:10 -0700 | [diff] [blame] | 15 | #ifndef __STDC_CONSTANT_MACROS |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 16 | #define __STDC_CONSTANT_MACROS |
Aurimas Liutikas | fcad060 | 2016-05-10 19:16:10 -0700 | [diff] [blame] | 17 | #endif |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 20 | #include <time.h> |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 21 | #include <string.h> |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <iostream> |
| 24 | #include "guid.h" |
| 25 | #include "support.h" |
| 26 | |
| 27 | using namespace std; |
| 28 | |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 29 | bool GUIDData::firstInstance = 1; |
| 30 | |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 31 | GUIDData::GUIDData(void) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 32 | if (firstInstance) { |
| 33 | srand((unsigned int) time(0)); |
| 34 | firstInstance = 0; |
| 35 | } // if |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 36 | Zero(); |
| 37 | } // constructor |
| 38 | |
| 39 | GUIDData::GUIDData(const GUIDData & orig) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 40 | memcpy(uuidData, orig.uuidData, sizeof(uuidData)); |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 41 | } // copy constructor |
| 42 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 43 | GUIDData::GUIDData(const string & orig) { |
| 44 | operator=(orig); |
| 45 | } // copy (from string) constructor |
| 46 | |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 47 | GUIDData::GUIDData(const char * orig) { |
| 48 | operator=(orig); |
| 49 | } // copy (from char*) constructor |
| 50 | |
| 51 | GUIDData::~GUIDData(void) { |
| 52 | } // destructor |
| 53 | |
| 54 | GUIDData & GUIDData::operator=(const GUIDData & orig) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 55 | memcpy(uuidData, orig.uuidData, sizeof(uuidData)); |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 56 | return *this; |
| 57 | } // GUIDData::operator=(const GUIDData & orig) |
| 58 | |
| 59 | // Assign the GUID from a string input value. A GUID is normally formatted |
| 60 | // with four dashes as element separators, for a total length of 36 |
| 61 | // characters. If the input string is this long or longer, this function |
| 62 | // assumes standard separator positioning; if the input string is less |
| 63 | // than 36 characters long, this function assumes the input GUID has |
| 64 | // been compressed by removal of separators. In either event, there's |
| 65 | // little in the way of sanity checking, so garbage in = garbage out! |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 66 | // One special case: If the first character is 'r' or 'R', a random |
| 67 | // GUID is assigned. |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 68 | GUIDData & GUIDData::operator=(const string & orig) { |
| 69 | string copy, fragment; |
| 70 | size_t len; |
| 71 | // Break points for segments, either with or without characters separating the segments.... |
| 72 | size_t longSegs[6] = {0, 9, 14, 19, 24, 36}; |
| 73 | size_t shortSegs[6] = {0, 8, 12, 16, 20, 32}; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 74 | size_t *segStart = longSegs; // Assume there are separators between segments |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 75 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 76 | // If first character is an 'R' or 'r', set a random GUID; otherwise, |
| 77 | // try to parse it as a real GUID |
| 78 | if ((orig[0] == 'R') || (orig[0] == 'r')) { |
| 79 | Randomize(); |
| 80 | } else { |
| 81 | Zero(); |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 82 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 83 | // Delete stray spaces.... |
| 84 | copy = DeleteSpaces(orig); |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 85 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 86 | // If length is too short, assume there are no separators between segments |
| 87 | len = copy.length(); |
| 88 | if (len < 36) { |
| 89 | segStart = shortSegs; |
| 90 | }; |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 91 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 92 | // Extract data fragments at fixed locations and convert to |
| 93 | // integral types.... |
| 94 | if (len >= segStart[1]) { |
| 95 | uuidData[3] = StrToHex(copy, 0); |
| 96 | uuidData[2] = StrToHex(copy, 2); |
| 97 | uuidData[1] = StrToHex(copy, 4); |
| 98 | uuidData[0] = StrToHex(copy, 6); |
| 99 | } // if |
| 100 | if (len >= segStart[2]) { |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 101 | uuidData[5] = StrToHex(copy, (unsigned int) segStart[1]); |
| 102 | uuidData[4] = StrToHex(copy, (unsigned int) segStart[1] + 2); |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 103 | } // if |
| 104 | if (len >= segStart[3]) { |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 105 | uuidData[7] = StrToHex(copy, (unsigned int) segStart[2]); |
| 106 | uuidData[6] = StrToHex(copy, (unsigned int) segStart[2] + 2); |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 107 | } // if |
| 108 | if (len >= segStart[4]) { |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 109 | uuidData[8] = StrToHex(copy, (unsigned int) segStart[3]); |
| 110 | uuidData[9] = StrToHex(copy, (unsigned int) segStart[3] + 2); |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 111 | } // if |
| 112 | if (len >= segStart[5]) { |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 113 | uuidData[10] = StrToHex(copy, (unsigned int) segStart[4]); |
| 114 | uuidData[11] = StrToHex(copy, (unsigned int) segStart[4] + 2); |
| 115 | uuidData[12] = StrToHex(copy, (unsigned int) segStart[4] + 4); |
| 116 | uuidData[13] = StrToHex(copy, (unsigned int) segStart[4] + 6); |
| 117 | uuidData[14] = StrToHex(copy, (unsigned int) segStart[4] + 8); |
| 118 | uuidData[15] = StrToHex(copy, (unsigned int) segStart[4] + 10); |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 119 | } // if |
| 120 | } // if/else randomize/set value |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 121 | |
| 122 | return *this; |
| 123 | } // GUIDData::operator=(const string & orig) |
| 124 | |
| 125 | // Assignment from C-style string; rely on C++ casting.... |
| 126 | GUIDData & GUIDData::operator=(const char * orig) { |
| 127 | return operator=((string) orig); |
| 128 | } // GUIDData::operator=(const char * orig) |
| 129 | |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 130 | // Erase the contents of the GUID |
| 131 | void GUIDData::Zero(void) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 132 | memset(uuidData, 0, sizeof(uuidData)); |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 133 | } // GUIDData::Zero() |
| 134 | |
| 135 | // Set a completely random GUID value.... |
| 136 | // The uuid_generate() function returns a value that needs to have its |
| 137 | // first three fields byte-reversed to conform to Intel's GUID layout. |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 138 | // The Windows UuidCreate() function doesn't need this adjustment. If |
| 139 | // neither function is defined, or if UuidCreate() fails, set a completely |
| 140 | // random GUID -- not completely kosher, but it works on most platforms |
| 141 | // (immediately after creating the UUID on Windows 7 being an important |
| 142 | // exception). |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 143 | void GUIDData::Randomize(void) { |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 144 | int i, uuidGenerated = 0; |
| 145 | |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 146 | #ifdef _UUID_UUID_H |
| 147 | uuid_generate(uuidData); |
| 148 | ReverseBytes(&uuidData[0], 4); |
| 149 | ReverseBytes(&uuidData[4], 2); |
| 150 | ReverseBytes(&uuidData[6], 2); |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 151 | uuidGenerated = 1; |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 152 | #endif |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 153 | #if defined (_RPC_H) || defined (__RPC_H__) |
| 154 | UUID MsUuid; |
| 155 | if (UuidCreate(&MsUuid) == RPC_S_OK) { |
| 156 | memcpy(uuidData, &MsUuid, 16); |
| 157 | uuidGenerated = 1; |
| 158 | } // if |
| 159 | #endif |
| 160 | |
| 161 | if (!uuidGenerated) { |
| 162 | cerr << "Warning! Unable to generate a proper UUID! Creating an improper one as a last\n" |
| 163 | << "resort! Windows 7 may crash if you save this partition table!\a\n"; |
| 164 | for (i = 0; i < 16; i++) |
| 165 | uuidData[i] = (unsigned char) (256.0 * (rand() / (RAND_MAX + 1.0))); |
| 166 | } // if |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 167 | } // GUIDData::Randomize |
| 168 | |
| 169 | // Equality operator; returns 1 if the GUIDs are equal, 0 if they're unequal |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 170 | int GUIDData::operator==(const GUIDData & orig) const { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 171 | return !memcmp(uuidData, orig.uuidData, sizeof(uuidData)); |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 172 | } // GUIDData::operator== |
| 173 | |
| 174 | // Inequality operator; returns 1 if the GUIDs are unequal, 0 if they're equal |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 175 | int GUIDData::operator!=(const GUIDData & orig) const { |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 176 | return !operator==(orig); |
| 177 | } // GUIDData::operator!= |
| 178 | |
| 179 | // Return the GUID as a string, suitable for display to the user. |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 180 | string GUIDData::AsString(void) const { |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 181 | char theString[40]; |
| 182 | |
| 183 | sprintf(theString, |
| 184 | "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", |
| 185 | uuidData[3], uuidData[2], uuidData[1], uuidData[0], uuidData[5], |
| 186 | uuidData[4], uuidData[7], uuidData[6], uuidData[8], uuidData[9], |
| 187 | uuidData[10], uuidData[11], uuidData[12], uuidData[13], uuidData[14], |
| 188 | uuidData[15]); |
| 189 | return theString; |
| 190 | } // GUIDData::AsString(void) |
| 191 | |
| 192 | // Delete spaces or braces (which often enclose GUIDs) from the orig string, |
| 193 | // returning modified string. |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 194 | string GUIDData::DeleteSpaces(string s) { |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 195 | size_t position; |
| 196 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 197 | if (s.length() > 0) { |
| 198 | for (position = s.length(); position > 0; position--) { |
| 199 | if ((s[position - 1] == ' ') || (s[position - 1] == '{') || (s[position - 1] == '}')) { |
| 200 | s.erase(position - 1, 1); |
| 201 | } // if |
| 202 | } // for |
| 203 | } // if |
| 204 | return s; |
srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 205 | } // GUIDData::DeleteSpaces() |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 206 | |
| 207 | /******************************* |
| 208 | * * |
| 209 | * Non-class support functions * |
| 210 | * * |
| 211 | *******************************/ |
| 212 | |
| 213 | // Display a GUID as a string.... |
| 214 | ostream & operator<<(ostream & os, const GUIDData & data) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 215 | // string asString; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 216 | |
| 217 | os << data.AsString(); |
| 218 | return os; |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 219 | } // GUIDData::operator<<() |