blob: 7f71015c4802ef47a0318b70b4cdd741391cf79d [file] [log] [blame]
hashimoto@chromium.org5fdbcf72012-06-05 13:15:50 +09001// 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
5#include "dbus/string_util.h"
6
avi0ad0ce02015-12-23 03:12:45 +09007#include <stddef.h>
8
avi@chromium.orgffcdb952013-06-11 16:27:01 +09009#include "base/strings/string_util.h"
hashimoto@chromium.org5fdbcf72012-06-05 13:15:50 +090010
11namespace dbus {
12
brettwad743062015-07-17 02:49:29 +090013// This implementation is based upon D-Bus Specification Version 0.19.
hashimoto@chromium.org5fdbcf72012-06-05 13:15:50 +090014bool IsValidObjectPath(const std::string& value) {
hashimoto@chromium.org5fdbcf72012-06-05 13:15:50 +090015 // A valid object path begins with '/'.
brettwad743062015-07-17 02:49:29 +090016 if (!base::StartsWith(value, "/", base::CompareCase::SENSITIVE))
hashimoto@chromium.org5fdbcf72012-06-05 13:15:50 +090017 return false;
18
19 // Elements are pieces delimited by '/'. For instance, "org", "chromium",
20 // "Foo" are elements of "/org/chromium/Foo".
21 int element_length = 0;
22 for (size_t i = 1; i < value.size(); ++i) {
23 const char c = value[i];
24 if (c == '/') {
25 // No element may be the empty string.
26 if (element_length == 0)
27 return false;
28 element_length = 0;
29 } else {
30 // Each element must only contain "[A-Z][a-z][0-9]_".
31 const bool is_valid_character =
32 ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ||
33 ('0' <= c && c <= '9') || c == '_';
34 if (!is_valid_character)
35 return false;
36 element_length++;
37 }
38 }
39
40 // A trailing '/' character is not allowed unless the path is the root path.
brettwad743062015-07-17 02:49:29 +090041 if (value.size() > 1 &&
42 base::EndsWith(value, "/", base::CompareCase::SENSITIVE))
hashimoto@chromium.org5fdbcf72012-06-05 13:15:50 +090043 return false;
44
45 return true;
46}
47
48} // namespace dbus