1 /*
2 * Copyright (C) 2016 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 UTIL_H_
18 #define UTIL_H_
19
20 #include <android-base/macros.h>
21 #include <util/map_ptr.h>
22
23 #include <cstdlib>
24 #include <memory>
25 #include <vector>
26
27 #include "androidfw/BigBuffer.h"
28 #include "androidfw/ResourceTypes.h"
29 #include "androidfw/StringPiece.h"
30 #include "utils/ByteOrder.h"
31
32 #ifdef __ANDROID__
33 #define ANDROID_LOG(x) LOG(x)
34 #else
35 namespace android {
36 // No default logging for aapt2, as it's too noisy for a command line dev tool.
37 struct NullLogger {
38 template <class T>
39 friend const NullLogger& operator<<(const NullLogger& l, const T&) { return l; }
40 };
41 }
42 #define ANDROID_LOG(x) (android::NullLogger{})
43 #endif
44
45 namespace android {
46 namespace util {
47
48 /**
49 * Makes a std::unique_ptr<> with the template parameter inferred by the
50 * compiler.
51 * This will be present in C++14 and can be removed then.
52 */
53 template <typename T, class... Args>
make_unique(Args &&...args)54 std::unique_ptr<T> make_unique(Args&&... args) {
55 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
56 }
57
58 // Based on std::unique_ptr, but uses free() to release malloc'ed memory.
59 struct FreeDeleter {
operatorFreeDeleter60 void operator()(void* ptr) const {
61 ::free(ptr);
62 }
63 };
64 template <typename T>
65 using unique_cptr = std::unique_ptr<T, FreeDeleter>;
66
67 void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out);
68
69 // Converts a UTF-8 string to a UTF-16 string.
70 std::u16string Utf8ToUtf16(StringPiece utf8);
71
72 // Converts a UTF-16 string to a UTF-8 string.
73 std::string Utf16ToUtf8(StringPiece16 utf16);
74
75 // Converts a UTF8 string into Modified UTF8
76 std::string Utf8ToModifiedUtf8(std::string_view utf8);
77
78 // Converts a Modified UTF8 string into a UTF8 string
79 std::string ModifiedUtf8ToUtf8(std::string_view modified_utf8);
80
HostToDevice16(uint16_t value)81 inline uint16_t HostToDevice16(uint16_t value) {
82 return htods(value);
83 }
84
HostToDevice32(uint32_t value)85 inline uint32_t HostToDevice32(uint32_t value) {
86 return htodl(value);
87 }
88
DeviceToHost16(uint16_t value)89 inline uint16_t DeviceToHost16(uint16_t value) {
90 return dtohs(value);
91 }
92
DeviceToHost32(uint32_t value)93 inline uint32_t DeviceToHost32(uint32_t value) {
94 return dtohl(value);
95 }
96
97 std::vector<std::string> SplitAndLowercase(android::StringPiece str, char sep);
98
IsFourByteAligned(const void * data)99 inline bool IsFourByteAligned(const void* data) {
100 return ((uintptr_t)data & 0x3U) == 0;
101 }
102
103 template <typename T>
IsFourByteAligned(const incfs::map_ptr<T> & data)104 inline bool IsFourByteAligned(const incfs::map_ptr<T>& data) {
105 return IsFourByteAligned(data.unsafe_ptr());
106 }
107
108 // Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
109 // the conversion to UTF-16 happens within ResStringPool.
110 android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
111
112 // Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
113 // the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
114 // which maintains no state or cache. This means we must return an std::string copy.
115 std::string GetString(const android::ResStringPool& pool, size_t idx);
116
117 // Copies the entire BigBuffer into a single buffer.
118 std::unique_ptr<uint8_t[]> Copy(const android::BigBuffer& buffer);
119
120 } // namespace util
121 } // namespace android
122
123 #endif /* UTIL_H_ */
124