| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "dbus/string_util.h" | 5 #include "dbus/string_util.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 | 8 |
| 9 namespace dbus { | 9 namespace dbus { |
| 10 | 10 |
| 11 // This implementation is based upon D-Bus Specification Version 0.19. | |
| 12 bool IsValidObjectPath(const std::string& value) { | 11 bool IsValidObjectPath(const std::string& value) { |
| 12 // This implementation is based upon D-Bus Specification Version 0.19. |
| 13 |
| 14 const bool kCaseSensitive = true; |
| 15 |
| 13 // A valid object path begins with '/'. | 16 // A valid object path begins with '/'. |
| 14 if (!base::StartsWith(value, "/", base::CompareCase::SENSITIVE)) | 17 if (!base::StartsWithASCII(value, "/", kCaseSensitive)) |
| 15 return false; | 18 return false; |
| 16 | 19 |
| 17 // Elements are pieces delimited by '/'. For instance, "org", "chromium", | 20 // Elements are pieces delimited by '/'. For instance, "org", "chromium", |
| 18 // "Foo" are elements of "/org/chromium/Foo". | 21 // "Foo" are elements of "/org/chromium/Foo". |
| 19 int element_length = 0; | 22 int element_length = 0; |
| 20 for (size_t i = 1; i < value.size(); ++i) { | 23 for (size_t i = 1; i < value.size(); ++i) { |
| 21 const char c = value[i]; | 24 const char c = value[i]; |
| 22 if (c == '/') { | 25 if (c == '/') { |
| 23 // No element may be the empty string. | 26 // No element may be the empty string. |
| 24 if (element_length == 0) | 27 if (element_length == 0) |
| 25 return false; | 28 return false; |
| 26 element_length = 0; | 29 element_length = 0; |
| 27 } else { | 30 } else { |
| 28 // Each element must only contain "[A-Z][a-z][0-9]_". | 31 // Each element must only contain "[A-Z][a-z][0-9]_". |
| 29 const bool is_valid_character = | 32 const bool is_valid_character = |
| 30 ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || | 33 ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || |
| 31 ('0' <= c && c <= '9') || c == '_'; | 34 ('0' <= c && c <= '9') || c == '_'; |
| 32 if (!is_valid_character) | 35 if (!is_valid_character) |
| 33 return false; | 36 return false; |
| 34 element_length++; | 37 element_length++; |
| 35 } | 38 } |
| 36 } | 39 } |
| 37 | 40 |
| 38 // A trailing '/' character is not allowed unless the path is the root path. | 41 // A trailing '/' character is not allowed unless the path is the root path. |
| 39 if (value.size() > 1 && | 42 if (value.size() > 1 && base::EndsWith(value, "/", kCaseSensitive)) |
| 40 base::EndsWith(value, "/", base::CompareCase::SENSITIVE)) | |
| 41 return false; | 43 return false; |
| 42 | 44 |
| 43 return true; | 45 return true; |
| 44 } | 46 } |
| 45 | 47 |
| 46 } // namespace dbus | 48 } // namespace dbus |
| OLD | NEW |