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 
fix_package_id(uint32_t resid,uint8_t package_id)30 inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) {
31   return (resid & 0x00ffffffu) | (static_cast<uint32_t>(package_id) << 24);
32 }
33 
get_package_id(uint32_t resid)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.
get_type_id(uint32_t resid)39 inline uint8_t get_type_id(uint32_t resid) {
40   return static_cast<uint8_t>((resid >> 16) & 0x000000ffu);
41 }
42 
get_entry_id(uint32_t resid)43 inline uint16_t get_entry_id(uint32_t resid) {
44   return static_cast<uint16_t>(resid & 0x0000ffffu);
45 }
46 
is_internal_resid(uint32_t resid)47 inline bool is_internal_resid(uint32_t resid) {
48   return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0;
49 }
50 
is_valid_resid(uint32_t resid)51 inline bool is_valid_resid(uint32_t resid) {
52   return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0;
53 }
54 
make_resid(uint8_t package_id,uint8_t type_id,uint16_t entry_id)55 inline uint32_t make_resid(uint8_t package_id, uint8_t type_id, uint16_t entry_id) {
56   return (static_cast<uint32_t>(package_id) << 24) | (static_cast<uint32_t>(type_id) << 16) |
57          entry_id;
58 }
59 
60 }  // namespace android
61 
62 #endif /* ANDROIDFW_RESOURCEUTILS_H */
63