blob: 6abc16ed36e5bffa17d6d65f61d0894f8a36aac0 [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
27GUIDData::GUIDData(void) {
srs5694ab4b0432010-09-25 20:39:52 -040028 srand((unsigned int) time(0));
srs569420e2a972010-02-04 00:55:04 -050029 Zero();
30} // constructor
31
32GUIDData::GUIDData(const GUIDData & orig) {
srs569464cbd172011-03-01 22:03:54 -050033 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -050034} // copy constructor
35
srs56945a608532011-03-17 13:53:01 -040036GUIDData::GUIDData(const string & orig) {
37 operator=(orig);
38} // copy (from string) constructor
39
srs569420e2a972010-02-04 00:55:04 -050040GUIDData::GUIDData(const char * orig) {
41 operator=(orig);
42} // copy (from char*) constructor
43
44GUIDData::~GUIDData(void) {
45} // destructor
46
47GUIDData & GUIDData::operator=(const GUIDData & orig) {
srs569464cbd172011-03-01 22:03:54 -050048 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -050049 return *this;
50} // GUIDData::operator=(const GUIDData & orig)
51
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!
srs56948f1b2d62010-05-23 13:07:19 -040059// One special case: If the first character is 'r' or 'R', a random
60// GUID is assigned.
srs569420e2a972010-02-04 00:55:04 -050061GUIDData & GUIDData::operator=(const string & orig) {
62 string copy, fragment;
63 size_t len;
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};
srs569455d92612010-03-07 22:16:07 -050067 size_t *segStart = longSegs; // Assume there are separators between segments
srs569420e2a972010-02-04 00:55:04 -050068
srs56948f1b2d62010-05-23 13:07:19 -040069 // 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')) {
72 Randomize();
73 } else {
74 Zero();
srs569420e2a972010-02-04 00:55:04 -050075
srs56948f1b2d62010-05-23 13:07:19 -040076 // Delete stray spaces....
77 copy = DeleteSpaces(orig);
srs569420e2a972010-02-04 00:55:04 -050078
srs56948f1b2d62010-05-23 13:07:19 -040079 // If length is too short, assume there are no separators between segments
80 len = copy.length();
81 if (len < 36) {
82 segStart = shortSegs;
83 };
srs569420e2a972010-02-04 00:55:04 -050084
srs56948f1b2d62010-05-23 13:07:19 -040085 // Extract data fragments at fixed locations and convert to
86 // integral types....
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);
92 } // if
93 if (len >= segStart[2]) {
srs5694058d4a52010-10-12 12:42:47 -040094 uuidData[5] = StrToHex(copy, (unsigned int) segStart[1]);
95 uuidData[4] = StrToHex(copy, (unsigned int) segStart[1] + 2);
srs56948f1b2d62010-05-23 13:07:19 -040096 } // if
97 if (len >= segStart[3]) {
srs5694058d4a52010-10-12 12:42:47 -040098 uuidData[7] = StrToHex(copy, (unsigned int) segStart[2]);
99 uuidData[6] = StrToHex(copy, (unsigned int) segStart[2] + 2);
srs56948f1b2d62010-05-23 13:07:19 -0400100 } // if
101 if (len >= segStart[4]) {
srs5694058d4a52010-10-12 12:42:47 -0400102 uuidData[8] = StrToHex(copy, (unsigned int) segStart[3]);
103 uuidData[9] = StrToHex(copy, (unsigned int) segStart[3] + 2);
srs56948f1b2d62010-05-23 13:07:19 -0400104 } // if
105 if (len >= segStart[5]) {
srs5694058d4a52010-10-12 12:42:47 -0400106 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);
srs56948f1b2d62010-05-23 13:07:19 -0400112 } // if
113 } // if/else randomize/set value
srs569420e2a972010-02-04 00:55:04 -0500114
115 return *this;
116} // GUIDData::operator=(const string & orig)
117
118// Assignment from C-style string; rely on C++ casting....
119GUIDData & GUIDData::operator=(const char * orig) {
120 return operator=((string) orig);
121} // GUIDData::operator=(const char * orig)
122
srs569420e2a972010-02-04 00:55:04 -0500123// Erase the contents of the GUID
124void GUIDData::Zero(void) {
srs569464cbd172011-03-01 22:03:54 -0500125 memset(uuidData, 0, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -0500126} // GUIDData::Zero()
127
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.
srs569400b6d7a2011-06-26 22:40:06 -0400131// 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
135// exception).
srs569420e2a972010-02-04 00:55:04 -0500136void GUIDData::Randomize(void) {
srs569400b6d7a2011-06-26 22:40:06 -0400137 int i, uuidGenerated = 0;
138
srs569420e2a972010-02-04 00:55:04 -0500139#ifdef _UUID_UUID_H
140 uuid_generate(uuidData);
141 ReverseBytes(&uuidData[0], 4);
142 ReverseBytes(&uuidData[4], 2);
143 ReverseBytes(&uuidData[6], 2);
srs569400b6d7a2011-06-26 22:40:06 -0400144 uuidGenerated = 1;
srs569420e2a972010-02-04 00:55:04 -0500145#endif
srs569400b6d7a2011-06-26 22:40:06 -0400146#if defined (_RPC_H) || defined (__RPC_H__)
147 UUID MsUuid;
148 if (UuidCreate(&MsUuid) == RPC_S_OK) {
149 memcpy(uuidData, &MsUuid, 16);
150 uuidGenerated = 1;
151 } // if
152#endif
153
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)));
159 } // if
srs569420e2a972010-02-04 00:55:04 -0500160} // GUIDData::Randomize
161
162// Equality operator; returns 1 if the GUIDs are equal, 0 if they're unequal
srs56945a081752010-09-24 20:39:41 -0400163int GUIDData::operator==(const GUIDData & orig) const {
srs569464cbd172011-03-01 22:03:54 -0500164 return !memcmp(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -0500165} // GUIDData::operator==
166
167// Inequality operator; returns 1 if the GUIDs are unequal, 0 if they're equal
srs56945a081752010-09-24 20:39:41 -0400168int GUIDData::operator!=(const GUIDData & orig) const {
srs569420e2a972010-02-04 00:55:04 -0500169 return !operator==(orig);
170} // GUIDData::operator!=
171
172// Return the GUID as a string, suitable for display to the user.
srs56945a081752010-09-24 20:39:41 -0400173string GUIDData::AsString(void) const {
srs569420e2a972010-02-04 00:55:04 -0500174 char theString[40];
175
176 sprintf(theString,
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],
181 uuidData[15]);
182 return theString;
183} // GUIDData::AsString(void)
184
185// Delete spaces or braces (which often enclose GUIDs) from the orig string,
186// returning modified string.
srs569455d92612010-03-07 22:16:07 -0500187string GUIDData::DeleteSpaces(string s) {
srs569420e2a972010-02-04 00:55:04 -0500188 size_t position;
189
srs569455d92612010-03-07 22:16:07 -0500190 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);
194 } // if
195 } // for
196 } // if
197 return s;
srs569420e2a972010-02-04 00:55:04 -0500198} // GUIDData::DeleteSpaces()
srs56945a081752010-09-24 20:39:41 -0400199
200/*******************************
201 * *
202 * Non-class support functions *
203 * *
204 *******************************/
205
206// Display a GUID as a string....
207ostream & operator<<(ostream & os, const GUIDData & data) {
srs56940873e9d2010-10-07 13:00:45 -0400208// string asString;
srs56945a081752010-09-24 20:39:41 -0400209
210 os << data.AsString();
211 return os;
srs569400b6d7a2011-06-26 22:40:06 -0400212} // GUIDData::operator<<()