| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_UTIL_H |
| 18 |
#define AAPT_UTIL_H |
| 19 |
|
| 20 |
#include <functional> |
| 21 |
#include <memory> |
| 22 |
#include <ostream> |
| 23 |
#include <string> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "androidfw/ResourceTypes.h" |
| 27 |
#include "androidfw/StringPiece.h" |
| 28 |
#include "utils/ByteOrder.h" |
| 29 |
|
| 30 |
#include "util/BigBuffer.h" |
| 31 |
|
| 32 |
#ifdef _WIN32 |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#ifdef ERROR |
| 39 |
#undef ERROR |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
|
| 43 |
namespace aapt { |
| 44 |
namespace util { |
| 45 |
|
| 46 |
template <typename T> |
| 47 |
struct Range { |
| 48 |
T start; |
| 49 |
T end; |
| 50 |
}; |
| 51 |
|
| 52 |
std::vector<std::string> Split(const android::StringPiece& str, char sep); |
| 53 |
std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep); |
| 54 |
|
| 55 |
|
| 56 |
bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix); |
| 57 |
|
| 58 |
|
| 59 |
bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
android::StringPiece TrimWhitespace(const android::StringPiece& str); |
| 72 |
|
| 73 |
|
| 74 |
bool IsJavaClassName(const android::StringPiece& str); |
| 75 |
|
| 76 |
|
| 77 |
bool IsJavaPackageName(const android::StringPiece& str); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
bool IsAndroidPackageName(const android::StringPiece& str); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
bool IsAndroidSplitName(const android::StringPiece& str); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
bool IsAndroidSharedUserId(const android::StringPiece& package_name, |
| 98 |
const android::StringPiece& shared_user_id); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
std::optional<std::string> GetFullyQualifiedClassName(const android::StringPiece& package, |
| 108 |
const android::StringPiece& class_name); |
| 109 |
|
| 110 |
|
| 111 |
const char* GetToolName(); |
| 112 |
|
| 113 |
|
| 114 |
std::string GetToolFingerprint(); |
| 115 |
|
| 116 |
template <typename T> |
| 117 |
typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) { |
| 118 |
if (a < b) { |
| 119 |
return -1; |
| 120 |
} else if (a > b) { |
| 121 |
return 1; |
| 122 |
} |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
template <typename T, class... Args> |
| 129 |
std::unique_ptr<T> make_unique(Args&&... args) { |
| 130 |
return std::unique_ptr<T>(new T{std::forward<Args>(args)...}); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
template <typename Container> |
| 135 |
::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container, |
| 136 |
const char* sep) { |
| 137 |
using std::begin; |
| 138 |
using std::end; |
| 139 |
const auto begin_iter = begin(container); |
| 140 |
const auto end_iter = end(container); |
| 141 |
return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& { |
| 142 |
for (auto iter = begin_iter; iter != end_iter; ++iter) { |
| 143 |
if (iter != begin_iter) { |
| 144 |
out << sep; |
| 145 |
} |
| 146 |
out << *iter; |
| 147 |
} |
| 148 |
return out; |
| 149 |
}; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
std::string GetString(const android::ResStringPool& pool, size_t idx); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
bool VerifyJavaStringFormat(const android::StringPiece& str); |
| 166 |
|
| 167 |
bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces, |
| 168 |
std::string* out_str, std::string* out_error); |
| 169 |
|
| 170 |
class StringBuilder { |
| 171 |
public: |
| 172 |
StringBuilder() = default; |
| 173 |
|
| 174 |
StringBuilder& Append(const android::StringPiece& str); |
| 175 |
const std::string& ToString() const; |
| 176 |
const std::string& Error() const; |
| 177 |
bool IsEmpty() const; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
size_t Utf16Len() const; |
| 183 |
|
| 184 |
explicit operator bool() const; |
| 185 |
|
| 186 |
private: |
| 187 |
std::string str_; |
| 188 |
size_t utf16_len_ = 0; |
| 189 |
bool quote_ = false; |
| 190 |
bool trailing_space_ = false; |
| 191 |
bool last_char_was_escape_ = false; |
| 192 |
std::string error_; |
| 193 |
}; |
| 194 |
|
| 195 |
inline const std::string& StringBuilder::ToString() const { |
| 196 |
return str_; |
| 197 |
} |
| 198 |
|
| 199 |
inline const std::string& StringBuilder::Error() const { |
| 200 |
return error_; |
| 201 |
} |
| 202 |
|
| 203 |
inline bool StringBuilder::IsEmpty() const { |
| 204 |
return str_.empty(); |
| 205 |
} |
| 206 |
|
| 207 |
inline size_t StringBuilder::Utf16Len() const { |
| 208 |
return utf16_len_; |
| 209 |
} |
| 210 |
|
| 211 |
inline StringBuilder::operator bool() const { |
| 212 |
return error_.empty(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
std::string Utf8ToModifiedUtf8(const std::string& utf8); |
| 217 |
std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8); |
| 218 |
|
| 219 |
|
| 220 |
std::u16string Utf8ToUtf16(const android::StringPiece& utf8); |
| 221 |
std::string Utf16ToUtf8(const android::StringPiece16& utf16); |
| 222 |
|
| 223 |
|
| 224 |
bool WriteAll(std::ostream& out, const BigBuffer& buffer); |
| 225 |
|
| 226 |
|
| 227 |
std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
class Tokenizer { |
| 232 |
public: |
| 233 |
class iterator { |
| 234 |
public: |
| 235 |
using reference = android::StringPiece&; |
| 236 |
using value_type = android::StringPiece; |
| 237 |
using difference_type = size_t; |
| 238 |
using pointer = android::StringPiece*; |
| 239 |
using iterator_category = std::forward_iterator_tag; |
| 240 |
|
| 241 |
iterator(const iterator&) = default; |
| 242 |
iterator& operator=(const iterator&) = default; |
| 243 |
|
| 244 |
iterator& operator++(); |
| 245 |
|
| 246 |
android::StringPiece operator*() { return token_; } |
| 247 |
bool operator==(const iterator& rhs) const; |
| 248 |
bool operator!=(const iterator& rhs) const; |
| 249 |
|
| 250 |
private: |
| 251 |
friend class Tokenizer; |
| 252 |
|
| 253 |
iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end); |
| 254 |
|
| 255 |
android::StringPiece str_; |
| 256 |
char separator_; |
| 257 |
android::StringPiece token_; |
| 258 |
bool end_; |
| 259 |
}; |
| 260 |
|
| 261 |
Tokenizer(const android::StringPiece& str, char sep); |
| 262 |
|
| 263 |
iterator begin() const { |
| 264 |
return begin_; |
| 265 |
} |
| 266 |
|
| 267 |
iterator end() const { |
| 268 |
return end_; |
| 269 |
} |
| 270 |
|
| 271 |
private: |
| 272 |
const iterator begin_; |
| 273 |
const iterator end_; |
| 274 |
}; |
| 275 |
|
| 276 |
inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { |
| 277 |
return Tokenizer(str, sep); |
| 278 |
} |
| 279 |
|
| 280 |
inline uint16_t HostToDevice16(uint16_t value) { |
| 281 |
return htods(value); |
| 282 |
} |
| 283 |
|
| 284 |
inline uint32_t HostToDevice32(uint32_t value) { |
| 285 |
return htodl(value); |
| 286 |
} |
| 287 |
|
| 288 |
inline uint16_t DeviceToHost16(uint16_t value) { |
| 289 |
return dtohs(value); |
| 290 |
} |
| 291 |
|
| 292 |
inline uint32_t DeviceToHost32(uint32_t value) { |
| 293 |
return dtohl(value); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix, |
| 304 |
android::StringPiece* out_entry, android::StringPiece* out_suffix); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
inline ::std::ostream& operator<<(::std::ostream& out, |
| 311 |
const ::std::function<::std::ostream&(::std::ostream&)>& f) { |
| 312 |
return f(out); |
| 313 |
} |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
#endif |