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 = Uuid::From128BitBE(
39     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,
45              kNumBytes128 - kNumBytes32) != 0) {
46     return kNumBytes128;
47   }
48 
49   if (uu[0] == 0 && uu[1] == 0) return kNumBytes16;
50 
51   return kNumBytes32;
52 }
53 
Is16Bit() const54 bool Uuid::Is16Bit() const {
55   return GetShortestRepresentationSize() == kNumBytes16;
56 }
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) +
62          (((uint32_t)uu[2]) << 8) + uu[3];
63 }
64 
FromString(const std::string & uuid,bool * is_valid)65 Uuid Uuid::FromString(const std::string& uuid, bool* is_valid) {
66   if (is_valid) *is_valid = false;
67   Uuid ret = kBase;
68 
69   if (uuid.empty()) return ret;
70 
71   uint8_t* p = ret.uu.data();
72   if (uuid.size() == kString128BitLen) {
73     if (uuid[8] != '-' || uuid[13] != '-' || uuid[18] != '-' ||
74         uuid[23] != '-') {
75       return ret;
76     }
77 
78     int c;
79     int rc =
80         sscanf(uuid.c_str(),
81                "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
82                "-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%n",
83                &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8],
84                &p[9], &p[10], &p[11], &p[12], &p[13], &p[14], &p[15], &c);
85     if (rc != 16) return ret;
86     if (c != kString128BitLen) return ret;
87 
88     if (is_valid) *is_valid = true;
89   } else if (uuid.size() == 8) {
90     int c;
91     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%02hhx%02hhx%n", &p[0], &p[1],
92                     &p[2], &p[3], &c);
93     if (rc != 4) return ret;
94     if (c != 8) return ret;
95 
96     if (is_valid) *is_valid = true;
97   } else if (uuid.size() == 4) {
98     int c;
99     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%n", &p[2], &p[3], &c);
100     if (rc != 2) return ret;
101     if (c != 4) return ret;
102 
103     if (is_valid) *is_valid = true;
104   }
105 
106   return ret;
107 }
108 
From16Bit(uint16_t uuid16)109 Uuid Uuid::From16Bit(uint16_t uuid16) {
110   Uuid u = kBase;
111 
112   u.uu[2] = (uint8_t)((0xFF00 & uuid16) >> 8);
113   u.uu[3] = (uint8_t)(0x00FF & uuid16);
114   return u;
115 }
116 
From32Bit(uint32_t uuid32)117 Uuid Uuid::From32Bit(uint32_t uuid32) {
118   Uuid u = kBase;
119 
120   u.uu[0] = (uint8_t)((0xFF000000 & uuid32) >> 24);
121   u.uu[1] = (uint8_t)((0x00FF0000 & uuid32) >> 16);
122   u.uu[2] = (uint8_t)((0x0000FF00 & uuid32) >> 8);
123   u.uu[3] = (uint8_t)(0x000000FF & uuid32);
124   return u;
125 }
126 
From128BitBE(const uint8_t * uuid)127 Uuid Uuid::From128BitBE(const uint8_t* uuid) {
128   UUID128Bit tmp;
129   memcpy(tmp.data(), uuid, kNumBytes128);
130   return From128BitBE(tmp);
131 }
132 
From128BitLE(const UUID128Bit & uuid)133 Uuid Uuid::From128BitLE(const UUID128Bit& uuid) {
134   Uuid u;
135   std::reverse_copy(uuid.data(), uuid.data() + kNumBytes128, u.uu.begin());
136   return u;
137 }
138 
From128BitLE(const uint8_t * uuid)139 Uuid Uuid::From128BitLE(const uint8_t* uuid) {
140   UUID128Bit tmp;
141   memcpy(tmp.data(), uuid, kNumBytes128);
142   return From128BitLE(tmp);
143 }
144 
To128BitLE() const145 const UUID128Bit Uuid::To128BitLE() const {
146   UUID128Bit le;
147   std::reverse_copy(uu.data(), uu.data() + kNumBytes128, le.begin());
148   return le;
149 }
150 
To128BitBE() const151 const UUID128Bit& Uuid::To128BitBE() const { return uu; }
152 
IsEmpty() const153 bool Uuid::IsEmpty() const { return *this == kEmpty; }
154 
IsBase() const155 bool Uuid::IsBase() const { return *this == kBase; }
156 
UpdateUuid(const Uuid & uuid)157 void Uuid::UpdateUuid(const Uuid& uuid) {
158   uu = uuid.uu;
159 }
160 
operator <(const Uuid & rhs) const161 bool Uuid::operator<(const Uuid& rhs) const {
162   return std::lexicographical_compare(uu.begin(), uu.end(), rhs.uu.begin(),
163                                       rhs.uu.end());
164 }
165 
operator ==(const Uuid & rhs) const166 bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; }
167 
operator !=(const Uuid & rhs) const168 bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; }
169 
ToString() const170 std::string Uuid::ToString() const {
171   std::stringstream uuid;
172   uuid << std::hex << std::setfill('0');
173   for (size_t i = 0; i < 16; i++) {
174     uuid << std::setw(2) << +uu[i];
175     if (i == 3 || i == 5 || i == 7 || i == 9) {
176       uuid << "-";
177     }
178   }
179   return uuid.str();
180 }
181 }  // namespace bluetooth
182