Home | History | Annotate | Download | only in libidmap2
      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 #include "idmap2/Policies.h"
     18 
     19 #include <iterator>
     20 #include <string>
     21 #include <unordered_map>
     22 #include <vector>
     23 
     24 #include "androidfw/ResourceTypes.h"
     25 #include "idmap2/Idmap.h"
     26 #include "idmap2/Result.h"
     27 
     28 namespace android::idmap2 {
     29 
     30 Result<PolicyBitmask> PoliciesToBitmask(const std::vector<std::string>& policies) {
     31   static const std::unordered_map<android::StringPiece, PolicyFlags> kStringToFlag = {
     32       {kPolicyOdm, PolicyFlags::POLICY_ODM_PARTITION},
     33       {kPolicyOem, PolicyFlags::POLICY_OEM_PARTITION},
     34       {kPolicyPublic, PolicyFlags::POLICY_PUBLIC},
     35       {kPolicyProduct, PolicyFlags::POLICY_PRODUCT_PARTITION},
     36       {kPolicySignature, PolicyFlags::POLICY_SIGNATURE},
     37       {kPolicySystem, PolicyFlags::POLICY_SYSTEM_PARTITION},
     38       {kPolicyVendor, PolicyFlags::POLICY_VENDOR_PARTITION},
     39   };
     40 
     41   PolicyBitmask bitmask = 0;
     42   for (const std::string& policy : policies) {
     43     const auto iter = kStringToFlag.find(policy);
     44     if (iter != kStringToFlag.end()) {
     45       bitmask |= iter->second;
     46     } else {
     47       return Error("unknown policy \"%s\"", policy.c_str());
     48     }
     49   }
     50 
     51   return Result<PolicyBitmask>(bitmask);
     52 }
     53 
     54 std::vector<std::string> BitmaskToPolicies(const PolicyBitmask& bitmask) {
     55   std::vector<std::string> policies;
     56 
     57   if ((bitmask & PolicyFlags::POLICY_ODM_PARTITION) != 0) {
     58     policies.emplace_back(kPolicyOdm);
     59   }
     60 
     61   if ((bitmask & PolicyFlags::POLICY_OEM_PARTITION) != 0) {
     62     policies.emplace_back(kPolicyOem);
     63   }
     64 
     65   if ((bitmask & PolicyFlags::POLICY_PUBLIC) != 0) {
     66     policies.emplace_back(kPolicyPublic);
     67   }
     68 
     69   if ((bitmask & PolicyFlags::POLICY_PRODUCT_PARTITION) != 0) {
     70     policies.emplace_back(kPolicyProduct);
     71   }
     72 
     73   if ((bitmask & PolicyFlags::POLICY_SIGNATURE) != 0) {
     74     policies.emplace_back(kPolicySignature);
     75   }
     76 
     77   if ((bitmask & PolicyFlags::POLICY_SYSTEM_PARTITION) != 0) {
     78     policies.emplace_back(kPolicySystem);
     79   }
     80 
     81   if ((bitmask & PolicyFlags::POLICY_VENDOR_PARTITION) != 0) {
     82     policies.emplace_back(kPolicyVendor);
     83   }
     84 
     85   return policies;
     86 }
     87 
     88 }  // namespace android::idmap2
     89