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 <cstdlib>
21 #include <memory>
22 #include <sstream>
23 #include <vector>
24
25 #include <android-base/macros.h>
26 #include <util/map_ptr.h>
27
28 #include "androidfw/StringPiece.h"
29
30 #ifdef __ANDROID__
31 #define ANDROID_LOG(x) LOG(x)
32 #else
33 #define ANDROID_LOG(x) std::stringstream()
34 #endif
35
36 namespace android {
37 namespace util {
38
39 /**
40 * Makes a std::unique_ptr<> with the template parameter inferred by the
41 * compiler.
42 * This will be present in C++14 and can be removed then.
43 */
44 template <typename T, class... Args>
make_unique(Args &&...args)45 std::unique_ptr<T> make_unique(Args&&... args) {
46 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
47 }
48
49 // Based on std::unique_ptr, but uses free() to release malloc'ed memory
50 // without incurring the size increase of holding on to a custom deleter.
51 template <typename T>
52 class unique_cptr {
53 public:
54 using pointer = typename std::add_pointer<T>::type;
55
unique_cptr()56 constexpr unique_cptr() : ptr_(nullptr) {}
unique_cptr(std::nullptr_t)57 constexpr explicit unique_cptr(std::nullptr_t) : ptr_(nullptr) {}
unique_cptr(pointer ptr)58 explicit unique_cptr(pointer ptr) : ptr_(ptr) {}
unique_cptr(unique_cptr && o)59 unique_cptr(unique_cptr&& o) noexcept : ptr_(o.ptr_) { o.ptr_ = nullptr; }
60
~unique_cptr()61 ~unique_cptr() { std::free(reinterpret_cast<void*>(ptr_)); }
62
63 inline unique_cptr& operator=(unique_cptr&& o) noexcept {
64 if (&o == this) {
65 return *this;
66 }
67
68 std::free(reinterpret_cast<void*>(ptr_));
69 ptr_ = o.ptr_;
70 o.ptr_ = nullptr;
71 return *this;
72 }
73
74 inline unique_cptr& operator=(std::nullptr_t) {
75 std::free(reinterpret_cast<void*>(ptr_));
76 ptr_ = nullptr;
77 return *this;
78 }
79
release()80 pointer release() {
81 pointer result = ptr_;
82 ptr_ = nullptr;
83 return result;
84 }
85
get()86 inline pointer get() const { return ptr_; }
87
88 void reset(pointer ptr = pointer()) {
89 if (ptr == ptr_) {
90 return;
91 }
92
93 pointer old_ptr = ptr_;
94 ptr_ = ptr;
95 std::free(reinterpret_cast<void*>(old_ptr));
96 }
97
swap(unique_cptr & o)98 inline void swap(unique_cptr& o) { std::swap(ptr_, o.ptr_); }
99
100 inline explicit operator bool() const { return ptr_ != nullptr; }
101
102 inline typename std::add_lvalue_reference<T>::type operator*() const { return *ptr_; }
103
104 inline pointer operator->() const { return ptr_; }
105
106 inline bool operator==(const unique_cptr& o) const { return ptr_ == o.ptr_; }
107
108 inline bool operator!=(const unique_cptr& o) const { return ptr_ != o.ptr_; }
109
110 inline bool operator==(std::nullptr_t) const { return ptr_ == nullptr; }
111
112 inline bool operator!=(std::nullptr_t) const { return ptr_ != nullptr; }
113
114 private:
115 DISALLOW_COPY_AND_ASSIGN(unique_cptr);
116
117 pointer ptr_;
118 };
119
120 void ReadUtf16StringFromDevice(const uint16_t* src, size_t len, std::string* out);
121
122 // Converts a UTF-8 string to a UTF-16 string.
123 std::u16string Utf8ToUtf16(const StringPiece& utf8);
124
125 // Converts a UTF-16 string to a UTF-8 string.
126 std::string Utf16ToUtf8(const StringPiece16& utf16);
127
128 std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
129
130 template <typename T>
IsFourByteAligned(const incfs::map_ptr<T> & data)131 inline bool IsFourByteAligned(const incfs::map_ptr<T>& data) {
132 return ((size_t)data.unsafe_ptr() & 0x3U) == 0;
133 }
134
IsFourByteAligned(const void * data)135 inline bool IsFourByteAligned(const void* data) {
136 return ((size_t)data & 0x3U) == 0;
137 }
138
139 } // namespace util
140 } // namespace android
141
142 #endif /* UTIL_H_ */
143