blob: 6649288dfa4190c4f7a4972f1502279d601c2b78 [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>
19#include <sstream>
20#include <string>
21#include <vector>
22
23#include "androidfw/ResourceTypes.h"
24
25#include "idmap2/Idmap.h"
26#include "idmap2/Policies.h"
27#include "idmap2/Result.h"
28
29namespace android::idmap2 {
30
31namespace {
32
33const std::map<android::StringPiece, PolicyFlags> kStringToFlag = {
34 {"public", PolicyFlags::POLICY_PUBLIC},
35 {"product", PolicyFlags::POLICY_PRODUCT_PARTITION},
36 {"system", PolicyFlags::POLICY_SYSTEM_PARTITION},
37 {"vendor", PolicyFlags::POLICY_VENDOR_PARTITION},
Winsonb4100202019-02-06 12:05:32 -080038 {"signature", PolicyFlags::POLICY_SIGNATURE},
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080039};
40} // namespace
41
42Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies,
43 std::ostream& err) {
44 PolicyBitmask bitmask = 0;
45 for (const std::string& policy : policies) {
46 const auto iter = kStringToFlag.find(policy);
47 if (iter != kStringToFlag.end()) {
48 bitmask |= iter->second;
49 } else {
50 err << "error: unknown policy \"" << policy << "\"";
51 return kResultError;
52 }
53 }
54
55 return Result<PolicyBitmask>(bitmask);
56}
57
58} // namespace android::idmap2