blob: 64c1b45391817e18dd9ad9b1b2a31a60bdfcc1b0 [file] [log] [blame]
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "UidRanges.h"
18
19#include "NetdConstants.h"
20
21#include <stdlib.h>
22
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090023#include <android-base/stringprintf.h>
24
25using android::base::StringAppendF;
26
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070027bool UidRanges::hasUid(uid_t uid) const {
28 auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), Range(uid, uid));
29 return (iter != mRanges.end() && iter->first == uid) ||
30 (iter != mRanges.begin() && (--iter)->second >= uid);
31}
32
33const std::vector<UidRanges::Range>& UidRanges::getRanges() const {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070034 return mRanges;
35}
36
37bool UidRanges::parseFrom(int argc, char* argv[]) {
38 mRanges.clear();
39 for (int i = 0; i < argc; ++i) {
40 if (!*argv[i]) {
41 // The UID string is empty.
42 return false;
43 }
44 char* endPtr;
45 uid_t uidStart = strtoul(argv[i], &endPtr, 0);
46 uid_t uidEnd;
47 if (!*endPtr) {
48 // Found a single UID. The range contains just the one UID.
49 uidEnd = uidStart;
50 } else if (*endPtr == '-') {
51 if (!*++endPtr) {
52 // Unexpected end of string.
53 return false;
54 }
55 uidEnd = strtoul(endPtr, &endPtr, 0);
56 if (*endPtr) {
57 // Illegal trailing chars.
58 return false;
59 }
60 if (uidEnd < uidStart) {
61 // Invalid order.
62 return false;
63 }
64 } else {
65 // Not a single uid, not a range. Found some other illegal char.
66 return false;
67 }
68 if (uidStart == INVALID_UID || uidEnd == INVALID_UID) {
69 // Invalid UIDs.
70 return false;
71 }
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070072 mRanges.push_back(Range(uidStart, uidEnd));
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070073 }
74 std::sort(mRanges.begin(), mRanges.end());
75 return true;
76}
77
78void UidRanges::add(const UidRanges& other) {
79 auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end());
80 std::inplace_merge(mRanges.begin(), middle, mRanges.end());
81}
82
83void UidRanges::remove(const UidRanges& other) {
84 auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(),
85 other.mRanges.end(), mRanges.begin());
86 mRanges.erase(end, mRanges.end());
87}
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090088
89std::string UidRanges::toString() const {
90 std::string s("UidRanges{ ");
91 for (Range range : mRanges) {
92 if (range.first != range.second) {
93 StringAppendF(&s, "%u-%u ", range.first, range.second);
94 } else {
95 StringAppendF(&s, "%u ", range.first);
96 }
97 }
98 StringAppendF(&s, "}");
99 return s;
100}