blob: 0a0cecf1393238a56514a277f1c4a336748f8030 [file] [log] [blame]
Mårten Kongstadd10d06d2019-01-07 17:26:25 -08001/*
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 Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/Policies.h"
18
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080019#include <iterator>
20#include <map>
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080021#include <string>
22#include <vector>
23
24#include "androidfw/ResourceTypes.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080025#include "idmap2/Idmap.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080026#include "idmap2/Result.h"
27
28namespace android::idmap2 {
29
30namespace {
31
32const std::map<android::StringPiece, PolicyFlags> kStringToFlag = {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -080033 {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 Kongstadd10d06d2019-01-07 17:26:25 -080038};
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -080039
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080040} // namespace
41
Mårten Kongstad49d835d2019-01-31 10:50:48 +010042Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies) {
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080043 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 Kongstad49d835d2019-01-31 10:50:48 +010049 return Error("unknown policy \"%s\"", policy.c_str());
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080050 }
51 }
52
53 return Result<PolicyBitmask>(bitmask);
54}
55
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -080056std::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 Kongstadd10d06d2019-01-07 17:26:25 -080081} // namespace android::idmap2