blob: 1e73ab77d854d5d49e3cf069f60df52c726b3b2c [file] [log] [blame]
srs569420e2a972010-02-04 00:55:04 -05001//
2// C++ Implementation: GUIDData
3//
4// Description: GUIDData class header
5// Implements the GUIDData data structure and support methods
6//
7//
srs5694699941e2011-03-21 21:33:57 -04008// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2010-2011
srs569420e2a972010-02-04 00:55:04 -05009//
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>
srs56945a081752010-09-24 20:39:41 -040018#include <time.h>
srs569464cbd172011-03-01 22:03:54 -050019#include <string.h>
srs569420e2a972010-02-04 00:55:04 -050020#include <string>
21#include <iostream>
22#include "guid.h"
23#include "support.h"
24
25using namespace std;
26
Roderick W. Smith84aaff62014-02-17 16:17:11 -050027bool GUIDData::firstInstance = 1;
28
srs569420e2a972010-02-04 00:55:04 -050029GUIDData::GUIDData(void) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -050030 if (firstInstance) {
31 srand((unsigned int) time(0));
32 firstInstance = 0;
33 } // if
srs569420e2a972010-02-04 00:55:04 -050034 Zero();
35} // constructor
36
37GUIDData::GUIDData(const GUIDData & orig) {
srs569464cbd172011-03-01 22:03:54 -050038 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -050039} // copy constructor
40
srs56945a608532011-03-17 13:53:01 -040041GUIDData::GUIDData(const string & orig) {
42 operator=(orig);
43} // copy (from string) constructor
44
srs569420e2a972010-02-04 00:55:04 -050045GUIDData::GUIDData(const char * orig) {
46 operator=(orig);
47} // copy (from char*) constructor
48
49GUIDData::~GUIDData(void) {
50} // destructor
51
52GUIDData & GUIDData::operator=(const GUIDData & orig) {
srs569464cbd172011-03-01 22:03:54 -050053 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -050054 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!
srs56948f1b2d62010-05-23 13:07:19 -040064// One special case: If the first character is 'r' or 'R', a random
65// GUID is assigned.
srs569420e2a972010-02-04 00:55:04 -050066GUIDData & 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};
srs569455d92612010-03-07 22:16:07 -050072 size_t *segStart = longSegs; // Assume there are separators between segments
srs569420e2a972010-02-04 00:55:04 -050073
srs56948f1b2d62010-05-23 13:07:19 -040074 // 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();
srs569420e2a972010-02-04 00:55:04 -050080
srs56948f1b2d62010-05-23 13:07:19 -040081 // Delete stray spaces....
82 copy = DeleteSpaces(orig);
srs569420e2a972010-02-04 00:55:04 -050083
srs56948f1b2d62010-05-23 13:07:19 -040084 // If length is too short, assume there are no separators between segments
85 len = copy.length();
86 if (len < 36) {
87 segStart = shortSegs;
88 };
srs569420e2a972010-02-04 00:55:04 -050089
srs56948f1b2d62010-05-23 13:07:19 -040090 // 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]) {
srs5694058d4a52010-10-12 12:42:47 -040099 uuidData[5] = StrToHex(copy, (unsigned int) segStart[1]);
100 uuidData[4] = StrToHex(copy, (unsigned int) segStart[1] + 2);
srs56948f1b2d62010-05-23 13:07:19 -0400101 } // if
102 if (len >= segStart[3]) {
srs5694058d4a52010-10-12 12:42:47 -0400103 uuidData[7] = StrToHex(copy, (unsigned int) segStart[2]);
104 uuidData[6] = StrToHex(copy, (unsigned int) segStart[2] + 2);
srs56948f1b2d62010-05-23 13:07:19 -0400105 } // if
106 if (len >= segStart[4]) {
srs5694058d4a52010-10-12 12:42:47 -0400107 uuidData[8] = StrToHex(copy, (unsigned int) segStart[3]);
108 uuidData[9] = StrToHex(copy, (unsigned int) segStart[3] + 2);
srs56948f1b2d62010-05-23 13:07:19 -0400109 } // if
110 if (len >= segStart[5]) {
srs5694058d4a52010-10-12 12:42:47 -0400111 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);
srs56948f1b2d62010-05-23 13:07:19 -0400117 } // if
118 } // if/else randomize/set value
srs569420e2a972010-02-04 00:55:04 -0500119
120 return *this;
121} // GUIDData::operator=(const string & orig)
122
123// Assignment from C-style string; rely on C++ casting....
124GUIDData & GUIDData::operator=(const char * orig) {
125 return operator=((string) orig);
126} // GUIDData::operator=(const char * orig)
127
srs569420e2a972010-02-04 00:55:04 -0500128// Erase the contents of the GUID
129void GUIDData::Zero(void) {
srs569464cbd172011-03-01 22:03:54 -0500130 memset(uuidData, 0, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -0500131} // 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.
srs569400b6d7a2011-06-26 22:40:06 -0400136// 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).
srs569420e2a972010-02-04 00:55:04 -0500141void GUIDData::Randomize(void) {
srs569400b6d7a2011-06-26 22:40:06 -0400142 int i, uuidGenerated = 0;
143
srs569420e2a972010-02-04 00:55:04 -0500144#ifdef _UUID_UUID_H
145 uuid_generate(uuidData);
146 ReverseBytes(&uuidData[0], 4);
147 ReverseBytes(&uuidData[4], 2);
148 ReverseBytes(&uuidData[6], 2);
srs569400b6d7a2011-06-26 22:40:06 -0400149 uuidGenerated = 1;
srs569420e2a972010-02-04 00:55:04 -0500150#endif
srs569400b6d7a2011-06-26 22:40:06 -0400151#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
srs569420e2a972010-02-04 00:55:04 -0500165} // GUIDData::Randomize
166
167// Equality operator; returns 1 if the GUIDs are equal, 0 if they're unequal
srs56945a081752010-09-24 20:39:41 -0400168int GUIDData::operator==(const GUIDData & orig) const {
srs569464cbd172011-03-01 22:03:54 -0500169 return !memcmp(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -0500170} // GUIDData::operator==
171
172// Inequality operator; returns 1 if the GUIDs are unequal, 0 if they're equal
srs56945a081752010-09-24 20:39:41 -0400173int GUIDData::operator!=(const GUIDData & orig) const {
srs569420e2a972010-02-04 00:55:04 -0500174 return !operator==(orig);
175} // GUIDData::operator!=
176
177// Return the GUID as a string, suitable for display to the user.
srs56945a081752010-09-24 20:39:41 -0400178string GUIDData::AsString(void) const {
srs569420e2a972010-02-04 00:55:04 -0500179 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.
srs569455d92612010-03-07 22:16:07 -0500192string GUIDData::DeleteSpaces(string s) {
srs569420e2a972010-02-04 00:55:04 -0500193 size_t position;
194
srs569455d92612010-03-07 22:16:07 -0500195 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;
srs569420e2a972010-02-04 00:55:04 -0500203} // GUIDData::DeleteSpaces()
srs56945a081752010-09-24 20:39:41 -0400204
205/*******************************
206 * *
207 * Non-class support functions *
208 * *
209 *******************************/
210
211// Display a GUID as a string....
212ostream & operator<<(ostream & os, const GUIDData & data) {
srs56940873e9d2010-10-07 13:00:45 -0400213// string asString;
srs56945a081752010-09-24 20:39:41 -0400214
215 os << data.AsString();
216 return os;
srs569400b6d7a2011-06-26 22:40:06 -0400217} // GUIDData::operator<<()