Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/Policies.h" |
| 18 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 19 | #include <iterator> |
| 20 | #include <map> |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "androidfw/ResourceTypes.h" |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 25 | #include "idmap2/Idmap.h" |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 26 | #include "idmap2/Result.h" |
| 27 | |
| 28 | namespace android::idmap2 { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | const std::map<android::StringPiece, PolicyFlags> kStringToFlag = { |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 33 | {kPolicyPublic, PolicyFlags::POLICY_PUBLIC}, |
| 34 | {kPolicyProduct, PolicyFlags::POLICY_PRODUCT_PARTITION}, |
| 35 | {kPolicySystem, PolicyFlags::POLICY_SYSTEM_PARTITION}, |
| 36 | {kPolicyVendor, PolicyFlags::POLICY_VENDOR_PARTITION}, |
| 37 | {kPolicySignature, PolicyFlags::POLICY_SIGNATURE}, |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 38 | }; |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 39 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 40 | } // namespace |
| 41 | |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 42 | Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies) { |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 43 | PolicyBitmask bitmask = 0; |
| 44 | for (const std::string& policy : policies) { |
| 45 | const auto iter = kStringToFlag.find(policy); |
| 46 | if (iter != kStringToFlag.end()) { |
| 47 | bitmask |= iter->second; |
| 48 | } else { |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 49 | return Error("unknown policy \"%s\"", policy.c_str()); |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | return Result<PolicyBitmask>(bitmask); |
| 54 | } |
| 55 | |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 56 | std::vector<std::string> BitmaskToPolicies(const PolicyBitmask& bitmask) { |
| 57 | std::vector<std::string> policies; |
| 58 | if ((bitmask & PolicyFlags::POLICY_PUBLIC) != 0) { |
| 59 | policies.emplace_back(kPolicyPublic); |
| 60 | } |
| 61 | |
| 62 | if ((bitmask & PolicyFlags::POLICY_PRODUCT_PARTITION) != 0) { |
| 63 | policies.emplace_back(kPolicyProduct); |
| 64 | } |
| 65 | |
| 66 | if ((bitmask & PolicyFlags::POLICY_SYSTEM_PARTITION) != 0) { |
| 67 | policies.emplace_back(kPolicySystem); |
| 68 | } |
| 69 | |
| 70 | if ((bitmask & PolicyFlags::POLICY_VENDOR_PARTITION) != 0) { |
| 71 | policies.emplace_back(kPolicyVendor); |
| 72 | } |
| 73 | |
| 74 | if ((bitmask & PolicyFlags::POLICY_SIGNATURE) != 0) { |
| 75 | policies.emplace_back(kPolicySignature); |
| 76 | } |
| 77 | |
| 78 | return policies; |
| 79 | } |
| 80 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 81 | } // namespace android::idmap2 |