Home | History | Annotate | Download | only in androidfw
      1 /*
      2  * Copyright (C) 2017 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 ANDROIDFW_RESOURCEUTILS_H
     18 #define ANDROIDFW_RESOURCEUTILS_H
     19 
     20 #include "androidfw/StringPiece.h"
     21 
     22 namespace android {
     23 
     24 // Extracts the package, type, and name from a string of the format: [[package:]type/]name
     25 // Validation must be performed on each extracted piece.
     26 // Returns false if there was a syntax error.
     27 bool ExtractResourceName(const StringPiece& str, StringPiece* out_package, StringPiece* out_type,
     28                          StringPiece* out_entry);
     29 
     30 inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) {
     31   return resid | (static_cast<uint32_t>(package_id) << 24);
     32 }
     33 
     34 inline uint8_t get_package_id(uint32_t resid) {
     35   return static_cast<uint8_t>((resid >> 24) & 0x000000ffu);
     36 }
     37 
     38 // The type ID is 1-based, so if the returned value is 0 it is invalid.
     39 inline uint8_t get_type_id(uint32_t resid) {
     40   return static_cast<uint8_t>((resid >> 16) & 0x000000ffu);
     41 }
     42 
     43 inline uint16_t get_entry_id(uint32_t resid) { return static_cast<uint16_t>(resid & 0x0000ffffu); }
     44 
     45 inline bool is_internal_resid(uint32_t resid) {
     46   return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0;
     47 }
     48 
     49 inline bool is_valid_resid(uint32_t resid) {
     50   return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0;
     51 }
     52 
     53 inline uint32_t make_resid(uint8_t package_id, uint8_t type_id, uint16_t entry_id) {
     54   return (static_cast<uint32_t>(package_id) << 24) | (static_cast<uint32_t>(type_id) << 16) |
     55          entry_id;
     56 }
     57 
     58 }  // namespace android
     59 
     60 #endif /* ANDROIDFW_RESOURCEUTILS_H */
     61