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