1 /******************************************************************************
2  *
3  *  Copyright (C) 2017 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "uuid.h"
20 
21 #include <string.h>
22 
23 #include <algorithm>
24 #include <cstring>
25 #include <iomanip>
26 #include <ios>
27 #include <sstream>
28 
29 namespace bluetooth {
30 
31 static_assert(sizeof(Uuid) == 16, "Uuid must be 16 bytes long!");
32 
33 using UUID128Bit = Uuid::UUID128Bit;
34 
35 const Uuid Uuid::kEmpty = Uuid::From128BitBE(UUID128Bit{{0x00}});
36 
37 namespace {
38 constexpr Uuid kBase =
39         Uuid::From128BitBE(UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
40                                        0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}});
41 }  // namespace
42 
GetShortestRepresentationSize() const43 size_t Uuid::GetShortestRepresentationSize() const {
44   if (memcmp(uu.data() + kNumBytes32, kBase.uu.data() + kNumBytes32, kNumBytes128 - kNumBytes32) !=
45       0) {
46     return kNumBytes128;
47   }
48 
49   if (uu[0] == 0 && uu[1] == 0) {
50     return kNumBytes16;
51   }
52 
53   return kNumBytes32;
54 }
55 
Is16Bit() const56 bool Uuid::Is16Bit() const { return GetShortestRepresentationSize() == kNumBytes16; }
57 
As16Bit() const58 uint16_t Uuid::As16Bit() const { return (((uint16_t)uu[2]) << 8) + uu[3]; }
59 
As32Bit() const60 uint32_t Uuid::As32Bit() const {
61   return (((uint32_t)uu[0]) << 24) + (((uint32_t)uu[1]) << 16) + (((uint32_t)uu[2]) << 8) + uu[3];
62 }
63 
FromString(const std::string & uuid,bool * is_valid)64 Uuid Uuid::FromString(const std::string& uuid, bool* is_valid) {
65   if (is_valid) {
66     *is_valid = false;
67   }
68   Uuid ret = kBase;
69 
70   if (uuid.empty()) {
71     return ret;
72   }
73 
74   uint8_t* p = ret.uu.data();
75   if (uuid.size() == kString128BitLen) {
76     if (uuid[8] != '-' || uuid[13] != '-' || uuid[18] != '-' || uuid[23] != '-') {
77       return ret;
78     }
79 
80     int c;
81     int rc = sscanf(uuid.c_str(),
82                     "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
83                     "-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%n",
84                     &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8], &p[9], &p[10],
85                     &p[11], &p[12], &p[13], &p[14], &p[15], &c);
86     if (rc != 16) {
87       return ret;
88     }
89     if (c != kString128BitLen) {
90       return ret;
91     }
92 
93     if (is_valid) {
94       *is_valid = true;
95     }
96   } else if (uuid.size() == 8) {
97     int c;
98     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%02hhx%02hhx%n", &p[0], &p[1], &p[2], &p[3], &c);
99     if (rc != 4) {
100       return ret;
101     }
102     if (c != 8) {
103       return ret;
104     }
105 
106     if (is_valid) {
107       *is_valid = true;
108     }
109   } else if (uuid.size() == 4) {
110     int c;
111     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%n", &p[2], &p[3], &c);
112     if (rc != 2) {
113       return ret;
114     }
115     if (c != 4) {
116       return ret;
117     }
118 
119     if (is_valid) {
120       *is_valid = true;
121     }
122   }
123 
124   return ret;
125 }
126 
From16Bit(uint16_t uuid16)127 Uuid Uuid::From16Bit(uint16_t uuid16) {
128   Uuid u = kBase;
129 
130   u.uu[2] = (uint8_t)((0xFF00 & uuid16) >> 8);
131   u.uu[3] = (uint8_t)(0x00FF & uuid16);
132   return u;
133 }
134 
From32Bit(uint32_t uuid32)135 Uuid Uuid::From32Bit(uint32_t uuid32) {
136   Uuid u = kBase;
137 
138   u.uu[0] = (uint8_t)((0xFF000000 & uuid32) >> 24);
139   u.uu[1] = (uint8_t)((0x00FF0000 & uuid32) >> 16);
140   u.uu[2] = (uint8_t)((0x0000FF00 & uuid32) >> 8);
141   u.uu[3] = (uint8_t)(0x000000FF & uuid32);
142   return u;
143 }
144 
From128BitBE(const uint8_t * uuid)145 Uuid Uuid::From128BitBE(const uint8_t* uuid) {
146   UUID128Bit tmp;
147   memcpy(tmp.data(), uuid, kNumBytes128);
148   return From128BitBE(tmp);
149 }
150 
From128BitLE(const UUID128Bit & uuid)151 Uuid Uuid::From128BitLE(const UUID128Bit& uuid) {
152   Uuid u;
153   std::reverse_copy(uuid.data(), uuid.data() + kNumBytes128, u.uu.begin());
154   return u;
155 }
156 
From128BitLE(const uint8_t * uuid)157 Uuid Uuid::From128BitLE(const uint8_t* uuid) {
158   UUID128Bit tmp;
159   memcpy(tmp.data(), uuid, kNumBytes128);
160   return From128BitLE(tmp);
161 }
162 
To128BitLE() const163 const UUID128Bit Uuid::To128BitLE() const {
164   UUID128Bit le;
165   std::reverse_copy(uu.data(), uu.data() + kNumBytes128, le.begin());
166   return le;
167 }
168 
To128BitBE() const169 const UUID128Bit& Uuid::To128BitBE() const { return uu; }
170 
IsEmpty() const171 bool Uuid::IsEmpty() const { return *this == kEmpty; }
172 
IsBase() const173 bool Uuid::IsBase() const { return *this == kBase; }
174 
UpdateUuid(const Uuid & uuid)175 void Uuid::UpdateUuid(const Uuid& uuid) { uu = uuid.uu; }
176 
operator <(const Uuid & rhs) const177 bool Uuid::operator<(const Uuid& rhs) const {
178   return std::lexicographical_compare(uu.begin(), uu.end(), rhs.uu.begin(), rhs.uu.end());
179 }
180 
operator ==(const Uuid & rhs) const181 bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; }
182 
operator !=(const Uuid & rhs) const183 bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; }
184 
ToString() const185 std::string Uuid::ToString() const {
186   std::stringstream uuid;
187   uuid << std::hex << std::setfill('0');
188   for (size_t i = 0; i < 16; i++) {
189     uuid << std::setw(2) << +uu[i];
190     if (i == 3 || i == 5 || i == 7 || i == 9) {
191       uuid << "-";
192     }
193   }
194   return uuid.str();
195 }
196 }  // namespace bluetooth
197