1 /*
2  Copyright (c) 2018, MIPI Alliance, Inc.
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8 
9  * Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in
14    the documentation and/or other materials provided with the
15    distribution.
16 
17  * Neither the name of the copyright holder nor the names of its
18    contributors may be used to endorse or promote products derived
19    from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /*
35  * Contributors:
36  * Norbert Schulz (Intel Corporation) - Initial API and implementation
37  */
38 
39 #ifndef MIPI_SYST_GUID_H_included
40 #define MIPI_SYST_GUID_H_included
41 
42 #include <string>
43 #include <stdexcept>
44 #include "mipi_syst_printer.h"
45 
46 
47 MIPI_SYST_NAMESPACE_BEGIN
48 
49 /// Container for 128 bit wide GUID (see https://www.ietf.org/rfc/rfc4122.txt)
50 //
51 class guid {
52 public:
53 	union {
54 		uint8_t b[16];
55 		uint64_t ll[2];
56 	} u;
57 
guid()58 	guid()
59 	{
60 		u.ll[0] = u.ll[1] = 0;
61 	}
62 
guid(const uint8_t * p)63 	guid(const uint8_t * p)
64 	{
65 		u.ll[0] = *(uint64_t*)p;
66 		u.ll[1] = *(uint64_t*)(p+sizeof(uint64_t));
67 	}
68 
guid(const std::string & f)69 	guid(const std::string& f)
70 	{
71 		if (!parse(f)) {
72 			throw std::invalid_argument(
73 				"invalid guid format " +
74 				f +
75 				" expected {xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} notation."
76 			);
77 		}
78 	}
79 
80 	/** Helper to parse a GUID "{...}" string into the binrary representation.
81 	* @param str GUID ascii string
82 	* @param guid resulting guid
83 	* @return true if guid could be parserd
84 	*/
85 	bool parse(const std::string& fmt);
86 
87 	bool operator<(const guid& other) const
88 	{
89 		if (u.ll[0] == other.u.ll[0]) {
90 			return u.ll[1] < other.u.ll[1];
91 		} else {
92 			return u.ll[0] < other.u.ll[0];
93 		}
94 	}
95 
96 	guid operator&(const guid& mask) const
97 	{
98 		guid result(*this);
99 
100 		result.u.ll[0] &= mask.u.ll[0];
101 		result.u.ll[1] &= mask.u.ll[1];
102 
103 		return result;
104 	}
105 
106 	bool operator==(const guid& other) const
107 	{
108 		return u.ll[0] == other.u.ll[0] && u.ll[1] == other.u.ll[1];
109 	}
110 };
111 
112 std::ostream& operator<<(std::ostream& os, const guid& guid);
113 
114 MIPI_SYST_NAMESPACE_END
115 
116 #endif // MIPI_SYST_COLLATERL_H_included
117