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 
17 #ifndef IDMAP2_LIBIDMAP2_POLICIES_INCLUDE_IDMAP2_POLICIES_H_
18 #define IDMAP2_LIBIDMAP2_POLICIES_INCLUDE_IDMAP2_POLICIES_H_
19 
20 #include <array>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "android-base/stringprintf.h"
26 #include "androidfw/ResourceTypes.h"
27 #include "androidfw/StringPiece.h"
28 
29 using android::base::StringPrintf;
30 
31 using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask;
32 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
33 
34 namespace android::idmap2::policy {
35 
36 constexpr const char* kPolicyActor = "actor";
37 constexpr const char* kPolicyOdm = "odm";
38 constexpr const char* kPolicyOem = "oem";
39 constexpr const char* kPolicyProduct = "product";
40 constexpr const char* kPolicyPublic = "public";
41 constexpr const char* kPolicyConfigSignature = "config_signature";
42 constexpr const char* kPolicySignature = "signature";
43 constexpr const char* kPolicySystem = "system";
44 constexpr const char* kPolicyVendor = "vendor";
45 
46 inline static const std::array<std::pair<StringPiece, PolicyFlags>, 9> kPolicyStringToFlag = {
47     std::pair{kPolicyActor, PolicyFlags::ACTOR_SIGNATURE},
48     {kPolicyOdm, PolicyFlags::ODM_PARTITION},
49     {kPolicyOem, PolicyFlags::OEM_PARTITION},
50     {kPolicyProduct, PolicyFlags::PRODUCT_PARTITION},
51     {kPolicyPublic, PolicyFlags::PUBLIC},
52     {kPolicyConfigSignature, PolicyFlags::CONFIG_SIGNATURE},
53     {kPolicySignature, PolicyFlags::SIGNATURE},
54     {kPolicySystem, PolicyFlags::SYSTEM_PARTITION},
55     {kPolicyVendor, PolicyFlags::VENDOR_PARTITION},
56 };
57 
PoliciesToDebugString(PolicyBitmask policies)58 inline static std::string PoliciesToDebugString(PolicyBitmask policies) {
59   std::string str;
60   uint32_t remaining = policies;
61   for (auto const& policy : kPolicyStringToFlag) {
62     if ((policies & policy.second) != policy.second) {
63       continue;
64     }
65     if (!str.empty()) {
66       str.append("|");
67     }
68     str.append(policy.first.data());
69     remaining &= ~policy.second;
70   }
71   if (remaining != 0) {
72     if (!str.empty()) {
73       str.append("|");
74     }
75     str.append(StringPrintf("0x%08x", remaining));
76   }
77   return !str.empty() ? str : "none";
78 }
79 
80 }  // namespace android::idmap2::policy
81 
82 #endif  // IDMAP2_LIBIDMAP2_POLICIES_INCLUDE_IDMAP2_POLICIES_H_
83