blob: c6ba87dfc2fb584bd56eb78574c9a4a8672bf1c3 [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
17#include <iterator>
18#include <map>
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080019#include <string>
20#include <vector>
21
22#include "androidfw/ResourceTypes.h"
23
24#include "idmap2/Idmap.h"
25#include "idmap2/Policies.h"
26#include "idmap2/Result.h"
27
28namespace android::idmap2 {
29
30namespace {
31
32const std::map<android::StringPiece, PolicyFlags> kStringToFlag = {
33 {"public", PolicyFlags::POLICY_PUBLIC},
34 {"product", PolicyFlags::POLICY_PRODUCT_PARTITION},
35 {"system", PolicyFlags::POLICY_SYSTEM_PARTITION},
36 {"vendor", PolicyFlags::POLICY_VENDOR_PARTITION},
Winsonb4100202019-02-06 12:05:32 -080037 {"signature", PolicyFlags::POLICY_SIGNATURE},
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080038};
39} // namespace
40
Mårten Kongstad49d835d2019-01-31 10:50:48 +010041Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies) {
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080042 PolicyBitmask bitmask = 0;
43 for (const std::string& policy : policies) {
44 const auto iter = kStringToFlag.find(policy);
45 if (iter != kStringToFlag.end()) {
46 bitmask |= iter->second;
47 } else {
Mårten Kongstad49d835d2019-01-31 10:50:48 +010048 return Error("unknown policy \"%s\"", policy.c_str());
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080049 }
50 }
51
52 return Result<PolicyBitmask>(bitmask);
53}
54
55} // namespace android::idmap2