2 // C++ Implementation: GUIDData
4 // Description: GUIDData class header
5 // Implements the GUIDData data structure and support methods
8 // Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2010-2011
10 // Copyright: See COPYING file that comes with this distribution
14 #define __STDC_LIMIT_MACROS
15 #define __STDC_CONSTANT_MACROS
27 GUIDData::GUIDData(void) {
28 srand((unsigned int) time(0));
32 GUIDData::GUIDData(const GUIDData & orig) {
33 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
36 GUIDData::GUIDData(const string & orig) {
38 } // copy (from string) constructor
40 GUIDData::GUIDData(const char * orig) {
42 } // copy (from char*) constructor
44 GUIDData::~GUIDData(void) {
47 GUIDData & GUIDData::operator=(const GUIDData & orig) {
48 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
50 } // GUIDData::operator=(const GUIDData & orig)
52 // Assign the GUID from a string input value. A GUID is normally formatted
53 // with four dashes as element separators, for a total length of 36
54 // characters. If the input string is this long or longer, this function
55 // assumes standard separator positioning; if the input string is less
56 // than 36 characters long, this function assumes the input GUID has
57 // been compressed by removal of separators. In either event, there's
58 // little in the way of sanity checking, so garbage in = garbage out!
59 // One special case: If the first character is 'r' or 'R', a random
61 GUIDData & GUIDData::operator=(const string & orig) {
62 string copy, fragment;
64 // Break points for segments, either with or without characters separating the segments....
65 size_t longSegs[6] = {0, 9, 14, 19, 24, 36};
66 size_t shortSegs[6] = {0, 8, 12, 16, 20, 32};
67 size_t *segStart = longSegs; // Assume there are separators between segments
69 // If first character is an 'R' or 'r', set a random GUID; otherwise,
70 // try to parse it as a real GUID
71 if ((orig[0] == 'R') || (orig[0] == 'r')) {
76 // Delete stray spaces....
77 copy = DeleteSpaces(orig);
79 // If length is too short, assume there are no separators between segments
85 // Extract data fragments at fixed locations and convert to
87 if (len >= segStart[1]) {
88 uuidData[3] = StrToHex(copy, 0);
89 uuidData[2] = StrToHex(copy, 2);
90 uuidData[1] = StrToHex(copy, 4);
91 uuidData[0] = StrToHex(copy, 6);
93 if (len >= segStart[2]) {
94 uuidData[5] = StrToHex(copy, (unsigned int) segStart[1]);
95 uuidData[4] = StrToHex(copy, (unsigned int) segStart[1] + 2);
97 if (len >= segStart[3]) {
98 uuidData[7] = StrToHex(copy, (unsigned int) segStart[2]);
99 uuidData[6] = StrToHex(copy, (unsigned int) segStart[2] + 2);
101 if (len >= segStart[4]) {
102 uuidData[8] = StrToHex(copy, (unsigned int) segStart[3]);
103 uuidData[9] = StrToHex(copy, (unsigned int) segStart[3] + 2);
105 if (len >= segStart[5]) {
106 uuidData[10] = StrToHex(copy, (unsigned int) segStart[4]);
107 uuidData[11] = StrToHex(copy, (unsigned int) segStart[4] + 2);
108 uuidData[12] = StrToHex(copy, (unsigned int) segStart[4] + 4);
109 uuidData[13] = StrToHex(copy, (unsigned int) segStart[4] + 6);
110 uuidData[14] = StrToHex(copy, (unsigned int) segStart[4] + 8);
111 uuidData[15] = StrToHex(copy, (unsigned int) segStart[4] + 10);
113 } // if/else randomize/set value
116 } // GUIDData::operator=(const string & orig)
118 // Assignment from C-style string; rely on C++ casting....
119 GUIDData & GUIDData::operator=(const char * orig) {
120 return operator=((string) orig);
121 } // GUIDData::operator=(const char * orig)
123 // Erase the contents of the GUID
124 void GUIDData::Zero(void) {
125 memset(uuidData, 0, sizeof(uuidData));
126 } // GUIDData::Zero()
128 // Set a completely random GUID value....
129 // The uuid_generate() function returns a value that needs to have its
130 // first three fields byte-reversed to conform to Intel's GUID layout.
131 // The Windows UuidCreate() function doesn't need this adjustment. If
132 // neither function is defined, or if UuidCreate() fails, set a completely
133 // random GUID -- not completely kosher, but it works on most platforms
134 // (immediately after creating the UUID on Windows 7 being an important
136 void GUIDData::Randomize(void) {
137 int i, uuidGenerated = 0;
140 uuid_generate(uuidData);
141 ReverseBytes(&uuidData[0], 4);
142 ReverseBytes(&uuidData[4], 2);
143 ReverseBytes(&uuidData[6], 2);
146 #if defined (_RPC_H) || defined (__RPC_H__)
148 if (UuidCreate(&MsUuid) == RPC_S_OK) {
149 memcpy(uuidData, &MsUuid, 16);
154 if (!uuidGenerated) {
155 cerr << "Warning! Unable to generate a proper UUID! Creating an improper one as a last\n"
156 << "resort! Windows 7 may crash if you save this partition table!\a\n";
157 for (i = 0; i < 16; i++)
158 uuidData[i] = (unsigned char) (256.0 * (rand() / (RAND_MAX + 1.0)));
160 } // GUIDData::Randomize
162 // Equality operator; returns 1 if the GUIDs are equal, 0 if they're unequal
163 int GUIDData::operator==(const GUIDData & orig) const {
164 return !memcmp(uuidData, orig.uuidData, sizeof(uuidData));
165 } // GUIDData::operator==
167 // Inequality operator; returns 1 if the GUIDs are unequal, 0 if they're equal
168 int GUIDData::operator!=(const GUIDData & orig) const {
169 return !operator==(orig);
170 } // GUIDData::operator!=
172 // Return the GUID as a string, suitable for display to the user.
173 string GUIDData::AsString(void) const {
177 "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
178 uuidData[3], uuidData[2], uuidData[1], uuidData[0], uuidData[5],
179 uuidData[4], uuidData[7], uuidData[6], uuidData[8], uuidData[9],
180 uuidData[10], uuidData[11], uuidData[12], uuidData[13], uuidData[14],
183 } // GUIDData::AsString(void)
185 // Delete spaces or braces (which often enclose GUIDs) from the orig string,
186 // returning modified string.
187 string GUIDData::DeleteSpaces(string s) {
190 if (s.length() > 0) {
191 for (position = s.length(); position > 0; position--) {
192 if ((s[position - 1] == ' ') || (s[position - 1] == '{') || (s[position - 1] == '}')) {
193 s.erase(position - 1, 1);
198 } // GUIDData::DeleteSpaces()
200 /*******************************
202 * Non-class support functions *
204 *******************************/
206 // Display a GUID as a string....
207 ostream & operator<<(ostream & os, const GUIDData & data) {
210 os << data.AsString();
212 } // GUIDData::operator<<()