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