string_util.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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
  3. // found in the LICENSE file.
  4. #include "dbus/string_util.h"
  5. #include <stddef.h>
  6. #include "base/strings/string_util.h"
  7. namespace dbus {
  8. // This implementation is based upon D-Bus Specification Version 0.19.
  9. bool IsValidObjectPath(const std::string& value) {
  10. // A valid object path begins with '/'.
  11. if (!base::StartsWith(value, "/", base::CompareCase::SENSITIVE))
  12. return false;
  13. // Elements are pieces delimited by '/'. For instance, "org", "chromium",
  14. // "Foo" are elements of "/org/chromium/Foo".
  15. int element_length = 0;
  16. for (size_t i = 1; i < value.size(); ++i) {
  17. const char c = value[i];
  18. if (c == '/') {
  19. // No element may be the empty string.
  20. if (element_length == 0)
  21. return false;
  22. element_length = 0;
  23. } else {
  24. // Each element must only contain "[A-Z][a-z][0-9]_".
  25. const bool is_valid_character =
  26. ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ||
  27. ('0' <= c && c <= '9') || c == '_';
  28. if (!is_valid_character)
  29. return false;
  30. element_length++;
  31. }
  32. }
  33. // A trailing '/' character is not allowed unless the path is the root path.
  34. if (value.size() > 1 &&
  35. base::EndsWith(value, "/", base::CompareCase::SENSITIVE))
  36. return false;
  37. return true;
  38. }
  39. } // namespace dbus