• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/AssetManager2.h"
21 #include "androidfw/StringPiece.h"
22 
23 namespace android {
24 
25 // Extracts the package, type, and name from a string of the format: [[package:]type/]name
26 // Validation must be performed on each extracted piece.
27 // Returns false if there was a syntax error.
28 bool ExtractResourceName(const StringPiece& str, StringPiece* out_package, StringPiece* out_type,
29                          StringPiece* out_entry);
30 
31 // Convert a type_string_ref, entry_string_ref, and package to AssetManager2::ResourceName.
32 // Useful for getting resource name without re-running AssetManager2::FindEntry searches.
33 bool ToResourceName(const StringPoolRef& type_string_ref,
34                     const StringPoolRef& entry_string_ref,
35                     const StringPiece& package_name,
36                     AssetManager2::ResourceName* out_name);
37 
38 // Formats a ResourceName to "package:type/entry_name".
39 std::string ToFormattedResourceString(AssetManager2::ResourceName* resource_name);
40 
fix_package_id(uint32_t resid,uint8_t package_id)41 inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) {
42   return (resid & 0x00ffffffu) | (static_cast<uint32_t>(package_id) << 24);
43 }
44 
get_package_id(uint32_t resid)45 inline uint8_t get_package_id(uint32_t resid) {
46   return static_cast<uint8_t>((resid >> 24) & 0x000000ffu);
47 }
48 
49 // The type ID is 1-based, so if the returned value is 0 it is invalid.
get_type_id(uint32_t resid)50 inline uint8_t get_type_id(uint32_t resid) {
51   return static_cast<uint8_t>((resid >> 16) & 0x000000ffu);
52 }
53 
get_entry_id(uint32_t resid)54 inline uint16_t get_entry_id(uint32_t resid) {
55   return static_cast<uint16_t>(resid & 0x0000ffffu);
56 }
57 
is_internal_resid(uint32_t resid)58 inline bool is_internal_resid(uint32_t resid) {
59   return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0;
60 }
61 
is_valid_resid(uint32_t resid)62 inline bool is_valid_resid(uint32_t resid) {
63   return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0;
64 }
65 
make_resid(uint8_t package_id,uint8_t type_id,uint16_t entry_id)66 inline uint32_t make_resid(uint8_t package_id, uint8_t type_id, uint16_t entry_id) {
67   return (static_cast<uint32_t>(package_id) << 24) | (static_cast<uint32_t>(type_id) << 16) |
68          entry_id;
69 }
70 
71 }  // namespace android
72 
73 #endif /* ANDROIDFW_RESOURCEUTILS_H */
74