diff options
author | bryeung@chromium.org <bryeung@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-28 23:39:30 +0000 |
---|---|---|
committer | bryeung@chromium.org <bryeung@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-28 23:39:30 +0000 |
commit | 88e9362c9afd57d2f1b50e9bf5c212c855fd11ab (patch) | |
tree | bb7609bd630b259d2035e1da8e22aa6ed3da8a75 /chrome/browser/chromeos/bluetooth/bluetooth_utils.cc | |
parent | fa76d13877821cbc6fb2714915ab7f5fa8fc00b8 (diff) | |
download | chromium_src-88e9362c9afd57d2f1b50e9bf5c212c855fd11ab.zip chromium_src-88e9362c9afd57d2f1b50e9bf5c212c855fd11ab.tar.gz chromium_src-88e9362c9afd57d2f1b50e9bf5c212c855fd11ab.tar.bz2 |
Parse short UUIDs, and add validation.
BUG=134546
TEST=updated unit test
Review URL: https://chromiumcodereview.appspot.com/10698013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@144837 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/bluetooth/bluetooth_utils.cc')
-rw-r--r-- | chrome/browser/chromeos/bluetooth/bluetooth_utils.cc | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/chrome/browser/chromeos/bluetooth/bluetooth_utils.cc b/chrome/browser/chromeos/bluetooth/bluetooth_utils.cc index 608845f..02643c6 100644 --- a/chrome/browser/chromeos/bluetooth/bluetooth_utils.cc +++ b/chrome/browser/chromeos/bluetooth/bluetooth_utils.cc @@ -10,6 +10,13 @@ #include "base/logging.h" #include "base/string_number_conversions.h" +#include "base/string_util.h" + +namespace { +static const char* kCommonUuidPostfix = "-0000-1000-8000-00805f9b34fb"; +static const char* kCommonUuidPrefix = "0000"; +static const int kUuidSize = 36; +} // namespace namespace chromeos { namespace bluetooth_utils { @@ -41,5 +48,41 @@ bool str2ba(const std::string& in_address, bdaddr_t* out_address) { return false; } +std::string CanonicalUuid(std::string uuid) { + if (uuid.empty()) + return ""; + + if (uuid.size() < 11 && uuid.find("0x") == 0) + uuid = uuid.substr(2); + + if (!(uuid.size() == 4 || uuid.size() == 8 || uuid.size() == 36)) + return ""; + + if (uuid.size() == 4 || uuid.size() == 8) { + for (size_t i = 0; i < uuid.size(); ++i) { + if (!IsHexDigit(uuid[i])) + return ""; + } + + if (uuid.size() == 4) + return kCommonUuidPrefix + uuid + kCommonUuidPostfix; + + return uuid + kCommonUuidPostfix; + } + + std::string uuid_result(uuid); + for (int i = 0; i < kUuidSize; ++i) { + if (i == 8 || i == 13 || i == 18 || i == 23) { + if (uuid[i] != '-') + return ""; + } else { + if (!IsHexDigit(uuid[i])) + return ""; + uuid_result[i] = tolower(uuid[i]); + } + } + return uuid_result; +} + } // namespace bluetooth_utils } // namespace chromeos |