| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include "util/Util.h" |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <ostream> |
| 21 | #include <string> |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| Mark Punzalan | ec79566 | 2023-07-25 22:12:40 +0000 | [diff] [blame] | 24 | #include "android-base/parseint.h" |
| Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
| Jeremy Meyer | 7f592a8 | 2021-11-01 21:06:20 +0000 | [diff] [blame] | 26 | #include "android-base/strings.h" |
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 27 | #include "androidfw/BigBuffer.h" |
| Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | #include "androidfw/StringPiece.h" |
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 29 | #include "androidfw/Util.h" |
| Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 30 | #include "build/version.h" |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 31 | #include "text/Unicode.h" |
| Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 32 | #include "text/Utf8Iterator.h" |
| Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 33 | #include "utils/Unicode.h" |
| Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 34 | |
| Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 35 | using ::aapt::text::Utf8Iterator; |
| Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 36 | using ::android::StringPiece; |
| 37 | using ::android::StringPiece16; |
| Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 38 | |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | namespace aapt { |
| 40 | namespace util { |
| 41 | |
| Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 42 | // Package name and shared user id would be used as a part of the file name. |
| 43 | // Limits size to 223 and reserves 32 for the OS. |
| 44 | // See frameworks/base/core/java/android/content/pm/parsing/ParsingPackageUtils.java |
| 45 | constexpr static const size_t kMaxPackageNameSize = 223; |
| 46 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 47 | static std::vector<std::string> SplitAndTransform(StringPiece str, char sep, char (*f)(char)) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | std::vector<std::string> parts; |
| 49 | const StringPiece::const_iterator end = std::end(str); |
| 50 | StringPiece::const_iterator start = std::begin(str); |
| 51 | StringPiece::const_iterator current; |
| 52 | do { |
| 53 | current = std::find(start, end, sep); |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 54 | parts.emplace_back(start, current); |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | if (f) { |
| 56 | std::string& part = parts.back(); |
| 57 | std::transform(part.begin(), part.end(), part.begin(), f); |
| 58 | } |
| 59 | start = current + 1; |
| 60 | } while (current != end); |
| 61 | return parts; |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 64 | std::vector<std::string> Split(StringPiece str, char sep) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | return SplitAndTransform(str, sep, nullptr); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 68 | std::vector<std::string> SplitAndLowercase(StringPiece str, char sep) { |
| 69 | return SplitAndTransform(str, sep, [](char c) -> char { return ::tolower(c); }); |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 72 | bool StartsWith(StringPiece str, StringPiece prefix) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | if (str.size() < prefix.size()) { |
| 74 | return false; |
| 75 | } |
| 76 | return str.substr(0, prefix.size()) == prefix; |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 77 | } |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 79 | bool EndsWith(StringPiece str, StringPiece suffix) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | if (str.size() < suffix.size()) { |
| 81 | return false; |
| 82 | } |
| 83 | return str.substr(str.size() - suffix.size(), suffix.size()) == suffix; |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 86 | StringPiece TrimLeadingWhitespace(StringPiece str) { |
| Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 87 | if (str.size() == 0 || str.data() == nullptr) { |
| 88 | return str; |
| 89 | } |
| 90 | |
| 91 | const char* start = str.data(); |
| 92 | const char* end = start + str.length(); |
| 93 | |
| 94 | while (start != end && isspace(*start)) { |
| 95 | start++; |
| 96 | } |
| 97 | return StringPiece(start, end - start); |
| 98 | } |
| 99 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 100 | StringPiece TrimTrailingWhitespace(StringPiece str) { |
| Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 101 | if (str.size() == 0 || str.data() == nullptr) { |
| 102 | return str; |
| 103 | } |
| 104 | |
| 105 | const char* start = str.data(); |
| 106 | const char* end = start + str.length(); |
| 107 | |
| 108 | while (end != start && isspace(*(end - 1))) { |
| 109 | end--; |
| 110 | } |
| 111 | return StringPiece(start, end - start); |
| 112 | } |
| 113 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 114 | StringPiece TrimWhitespace(StringPiece str) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | if (str.size() == 0 || str.data() == nullptr) { |
| 116 | return str; |
| 117 | } |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 118 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | const char* start = str.data(); |
| 120 | const char* end = str.data() + str.length(); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 121 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | while (start != end && isspace(*start)) { |
| 123 | start++; |
| 124 | } |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 125 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | while (end != start && isspace(*(end - 1))) { |
| 127 | end--; |
| 128 | } |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 129 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | return StringPiece(start, end - start); |
| Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 133 | static int IsJavaNameImpl(StringPiece str) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 134 | int pieces = 0; |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 135 | for (StringPiece piece : Tokenize(str, '.')) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 136 | pieces++; |
| 137 | if (!text::IsJavaIdentifier(piece)) { |
| 138 | return -1; |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 141 | return pieces; |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 144 | bool IsJavaClassName(StringPiece str) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 145 | return IsJavaNameImpl(str) >= 2; |
| Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 148 | bool IsJavaPackageName(StringPiece str) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 149 | return IsJavaNameImpl(str) >= 1; |
| 150 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 152 | static int IsAndroidNameImpl(StringPiece str) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 153 | int pieces = 0; |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 154 | for (StringPiece piece : Tokenize(str, '.')) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | if (piece.empty()) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 156 | return -1; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 159 | const char first_character = piece.data()[0]; |
| 160 | if (!::isalpha(first_character)) { |
| 161 | return -1; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 164 | bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool { |
| 165 | return ::isalnum(c) || c == '_'; |
| 166 | }); |
| 167 | |
| 168 | if (!valid) { |
| 169 | return -1; |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | } |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 171 | pieces++; |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | } |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 173 | return pieces; |
| 174 | } |
| 175 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 176 | bool IsAndroidPackageName(StringPiece str) { |
| Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 177 | if (str.size() > kMaxPackageNameSize) { |
| 178 | return false; |
| 179 | } |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 180 | return IsAndroidNameImpl(str) > 1 || str == "android"; |
| 181 | } |
| 182 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 183 | bool IsAndroidSharedUserId(android::StringPiece package_name, android::StringPiece shared_user_id) { |
| Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 184 | if (shared_user_id.size() > kMaxPackageNameSize) { |
| 185 | return false; |
| 186 | } |
| 187 | return shared_user_id.empty() || IsAndroidNameImpl(shared_user_id) > 1 || |
| 188 | package_name == "android"; |
| 189 | } |
| 190 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 191 | bool IsAndroidSplitName(StringPiece str) { |
| Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 192 | return IsAndroidNameImpl(str) > 0; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 195 | std::optional<std::string> GetFullyQualifiedClassName(StringPiece package, StringPiece classname) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | if (classname.empty()) { |
| 197 | return {}; |
| 198 | } |
| Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 199 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 200 | if (util::IsJavaClassName(classname)) { |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 201 | return std::string(classname); |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | } |
| Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 203 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | if (package.empty()) { |
| 205 | return {}; |
| 206 | } |
| Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 207 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 208 | std::string result{package}; |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | if (classname.data()[0] != '.') { |
| 210 | result += '.'; |
| 211 | } |
| Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 212 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | result.append(classname.data(), classname.size()); |
| 214 | if (!IsJavaClassName(result)) { |
| 215 | return {}; |
| 216 | } |
| 217 | return result; |
| Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 220 | const char* GetToolName() { |
| 221 | static const char* const sToolName = "Android Asset Packaging Tool (aapt)"; |
| 222 | return sToolName; |
| 223 | } |
| 224 | |
| 225 | std::string GetToolFingerprint() { |
| 226 | // DO NOT UPDATE, this is more of a marketing version. |
| 227 | static const char* const sMajorVersion = "2"; |
| 228 | |
| 229 | // Update minor version whenever a feature or flag is added. |
| 230 | static const char* const sMinorVersion = "19"; |
| 231 | |
| 232 | // The build id of aapt2 binary. |
| Mark Punzalan | ec79566 | 2023-07-25 22:12:40 +0000 | [diff] [blame] | 233 | static const std::string sBuildId = [] { |
| 234 | std::string buildNumber = android::build::GetBuildNumber(); |
| Jeremy Meyer | 7f592a8 | 2021-11-01 21:06:20 +0000 | [diff] [blame] | 235 | |
| Mark Punzalan | ec79566 | 2023-07-25 22:12:40 +0000 | [diff] [blame] | 236 | if (android::base::StartsWith(buildNumber, "eng.")) { |
| 237 | // android::build::GetBuildNumber() returns something like "eng.user.20230725.214219" where |
| 238 | // the latter two parts are "yyyyMMdd.HHmmss" at build time. Use "yyyyMM" in the fingerprint. |
| 239 | std::vector<std::string> parts = util::Split(buildNumber, '.'); |
| 240 | int buildYear; |
| 241 | int buildMonth; |
| 242 | if (parts.size() < 3 || parts[2].length() < 6 || |
| 243 | !android::base::ParseInt(parts[2].substr(0, 4), &buildYear) || |
| 244 | !android::base::ParseInt(parts[2].substr(4, 2), &buildMonth)) { |
| 245 | // Fallback to localtime() if GetBuildNumber() returns an unexpected output. |
| 246 | time_t now = time(0); |
| 247 | tm* ltm = localtime(&now); |
| 248 | buildYear = 1900 + ltm->tm_year; |
| 249 | buildMonth = 1 + ltm->tm_mon; |
| 250 | } |
| Jeremy Meyer | 7f592a8 | 2021-11-01 21:06:20 +0000 | [diff] [blame] | 251 | |
| Mark Punzalan | ec79566 | 2023-07-25 22:12:40 +0000 | [diff] [blame] | 252 | buildNumber = android::base::StringPrintf("eng.%04d%02d", buildYear, buildMonth); |
| 253 | } |
| 254 | return buildNumber; |
| 255 | }(); |
| Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 256 | |
| 257 | return android::base::StringPrintf("%s.%s-%s", sMajorVersion, sMinorVersion, sBuildId.c_str()); |
| 258 | } |
| 259 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | static size_t ConsumeDigits(const char* start, const char* end) { |
| 261 | const char* c = start; |
| 262 | for (; c != end && *c >= '0' && *c <= '9'; c++) { |
| 263 | } |
| 264 | return static_cast<size_t>(c - start); |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 267 | bool VerifyJavaStringFormat(StringPiece str) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | const char* c = str.begin(); |
| 269 | const char* const end = str.end(); |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 270 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | size_t arg_count = 0; |
| 272 | bool nonpositional = false; |
| 273 | while (c != end) { |
| 274 | if (*c == '%' && c + 1 < end) { |
| 275 | c++; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 276 | |
| Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 277 | if (*c == '%' || *c == 'n') { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | c++; |
| 279 | continue; |
| 280 | } |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 281 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | arg_count++; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 283 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 284 | size_t num_digits = ConsumeDigits(c, end); |
| 285 | if (num_digits > 0) { |
| 286 | c += num_digits; |
| 287 | if (c != end && *c != '$') { |
| 288 | // The digits were a size, but not a positional argument. |
| 289 | nonpositional = true; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 290 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | } else if (*c == '<') { |
| 292 | // Reusing last argument, bad idea since positions can be moved around |
| 293 | // during translation. |
| 294 | nonpositional = true; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 295 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | c++; |
| 297 | |
| 298 | // Optionally we can have a $ after |
| 299 | if (c != end && *c == '$') { |
| 300 | c++; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 301 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | } else { |
| 303 | nonpositional = true; |
| 304 | } |
| 305 | |
| 306 | // Ignore size, width, flags, etc. |
| 307 | while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' || |
| 308 | *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) { |
| 309 | c++; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * This is a shortcut to detect strings that are going to Time.format() |
| 314 | * instead of String.format() |
| 315 | * |
| 316 | * Comparison of String.format() and Time.format() args: |
| 317 | * |
| 318 | * String: ABC E GH ST X abcdefgh nost x |
| 319 | * Time: DEFGHKMS W Za d hkm s w yz |
| 320 | * |
| 321 | * Therefore we know it's definitely Time if we have: |
| 322 | * DFKMWZkmwyz |
| 323 | */ |
| 324 | if (c != end) { |
| 325 | switch (*c) { |
| 326 | case 'D': |
| 327 | case 'F': |
| 328 | case 'K': |
| 329 | case 'M': |
| 330 | case 'W': |
| 331 | case 'Z': |
| 332 | case 'k': |
| 333 | case 'm': |
| 334 | case 'w': |
| 335 | case 'y': |
| 336 | case 'z': |
| 337 | return true; |
| 338 | } |
| 339 | } |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | if (c != end) { |
| 343 | c++; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 344 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (arg_count > 1 && nonpositional) { |
| 348 | // Multiple arguments were specified, but some or all were non positional. |
| 349 | // Translated |
| 350 | // strings may rearrange the order of the arguments, which will break the |
| 351 | // string. |
| 352 | return false; |
| 353 | } |
| 354 | return true; |
| Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 357 | std::u16string Utf8ToUtf16(StringPiece utf8) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | ssize_t utf16_length = utf8_to_utf16_length( |
| 359 | reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length()); |
| 360 | if (utf16_length <= 0) { |
| 361 | return {}; |
| 362 | } |
| 363 | |
| 364 | std::u16string utf16; |
| 365 | utf16.resize(utf16_length); |
| 366 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), |
| 367 | &*utf16.begin(), utf16_length + 1); |
| 368 | return utf16; |
| 369 | } |
| 370 | |
| 371 | std::string Utf16ToUtf8(const StringPiece16& utf16) { |
| 372 | ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length()); |
| 373 | if (utf8_length <= 0) { |
| 374 | return {}; |
| 375 | } |
| 376 | |
| 377 | std::string utf8; |
| 378 | utf8.resize(utf8_length); |
| 379 | utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1); |
| 380 | return utf8; |
| 381 | } |
| 382 | |
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 383 | bool WriteAll(std::ostream& out, const android::BigBuffer& buffer) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 384 | for (const auto& b : buffer) { |
| 385 | if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) { |
| 386 | return false; |
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 387 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | } |
| 389 | return true; |
| 390 | } |
| 391 | |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 392 | typename Tokenizer::iterator& Tokenizer::iterator::operator++() { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 393 | const char* start = token_.end(); |
| 394 | const char* end = str_.end(); |
| 395 | if (start == end) { |
| 396 | end_ = true; |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 397 | token_ = StringPiece(token_.end(), 0); |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 398 | return *this; |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | start += 1; |
| 402 | const char* current = start; |
| 403 | while (current != end) { |
| 404 | if (*current == separator_) { |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 405 | token_ = StringPiece(start, current - start); |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | return *this; |
| 407 | } |
| 408 | ++current; |
| 409 | } |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 410 | token_ = StringPiece(start, end - start); |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 411 | return *this; |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | bool Tokenizer::iterator::operator==(const iterator& rhs) const { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 415 | // We check equality here a bit differently. |
| 416 | // We need to know that the addresses are the same. |
| 417 | return token_.begin() == rhs.token_.begin() && |
| 418 | token_.end() == rhs.token_.end() && end_ == rhs.end_; |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | bool Tokenizer::iterator::operator!=(const iterator& rhs) const { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 422 | return !(*this == rhs); |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 425 | Tokenizer::iterator::iterator(StringPiece s, char sep, StringPiece tok, bool end) |
| 426 | : str_(s), separator_(sep), token_(tok), end_(end) { |
| 427 | } |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 428 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 429 | Tokenizer::Tokenizer(StringPiece str, char sep) |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 430 | : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)), |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 431 | end_(str, sep, StringPiece(str.end(), 0), true) { |
| 432 | } |
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 433 | |
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 434 | bool ExtractResFilePathParts(StringPiece path, StringPiece* out_prefix, StringPiece* out_entry, |
| 435 | StringPiece* out_suffix) { |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 436 | const StringPiece res_prefix("res/"); |
| 437 | if (!StartsWith(path, res_prefix)) { |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | StringPiece::const_iterator last_occurence = path.end(); |
| 442 | for (auto iter = path.begin() + res_prefix.size(); iter != path.end(); |
| 443 | ++iter) { |
| 444 | if (*iter == '/') { |
| 445 | last_occurence = iter; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 446 | } |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 447 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 448 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 449 | if (last_occurence == path.end()) { |
| 450 | return false; |
| 451 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 452 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 453 | auto iter = std::find(last_occurence, path.end(), '.'); |
| 454 | *out_suffix = StringPiece(iter, path.end() - iter); |
| 455 | *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1); |
| 456 | *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1); |
| 457 | return true; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 460 | } // namespace util |
| 461 | } // namespace aapt |