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 base::expected<AssetManager2::ResourceName, NullOrIOError> ToResourceName(
34 const StringPoolRef& type_string_ref, const StringPoolRef& entry_string_ref,
35 const StringPiece& package_name);
36
37 // Formats a ResourceName to "package:type/entry_name".
38 std::string ToFormattedResourceString(const AssetManager2::ResourceName& resource_name);
39
fix_package_id(uint32_t resid,uint8_t package_id)40 inline uint32_t fix_package_id(uint32_t resid, uint8_t package_id) {
41 return (resid & 0x00ffffffu) | (static_cast<uint32_t>(package_id) << 24);
42 }
43
get_package_id(uint32_t resid)44 inline uint8_t get_package_id(uint32_t resid) {
45 return static_cast<uint8_t>((resid >> 24) & 0x000000ffu);
46 }
47
48 // The type ID is 1-based, so if the returned value is 0 it is invalid.
get_type_id(uint32_t resid)49 inline uint8_t get_type_id(uint32_t resid) {
50 return static_cast<uint8_t>((resid >> 16) & 0x000000ffu);
51 }
52
get_entry_id(uint32_t resid)53 inline uint16_t get_entry_id(uint32_t resid) {
54 return static_cast<uint16_t>(resid & 0x0000ffffu);
55 }
56
is_internal_resid(uint32_t resid)57 inline bool is_internal_resid(uint32_t resid) {
58 return (resid & 0xffff0000u) != 0 && (resid & 0x00ff0000u) == 0;
59 }
60
is_valid_resid(uint32_t resid)61 inline bool is_valid_resid(uint32_t resid) {
62 return (resid & 0x00ff0000u) != 0 && (resid & 0xff000000u) != 0;
63 }
64
make_resid(uint8_t package_id,uint8_t type_id,uint16_t entry_id)65 inline uint32_t make_resid(uint8_t package_id, uint8_t type_id, uint16_t entry_id) {
66 return (static_cast<uint32_t>(package_id) << 24) | (static_cast<uint32_t>(type_id) << 16) |
67 entry_id;
68 }
69
70 } // namespace android
71
72 #endif /* ANDROIDFW_RESOURCEUTILS_H */
73