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 | |
Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 24 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 26 | #include "build/version.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 28 | #include "text/Unicode.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 29 | #include "text/Utf8Iterator.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 30 | #include "util/BigBuffer.h" |
| 31 | #include "util/Maybe.h" |
Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 32 | #include "utils/Unicode.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 34 | using ::aapt::text::Utf8Iterator; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 35 | using ::android::StringPiece; |
| 36 | using ::android::StringPiece16; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | namespace aapt { |
| 39 | namespace util { |
| 40 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame^] | 41 | // Package name and shared user id would be used as a part of the file name. |
| 42 | // Limits size to 223 and reserves 32 for the OS. |
| 43 | // See frameworks/base/core/java/android/content/pm/parsing/ParsingPackageUtils.java |
| 44 | constexpr static const size_t kMaxPackageNameSize = 223; |
| 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | static std::vector<std::string> SplitAndTransform( |
| 47 | const StringPiece& str, char sep, const std::function<char(char)>& f) { |
| 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); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 54 | parts.emplace_back(str.substr(start, current).to_string()); |
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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | std::vector<std::string> Split(const StringPiece& str, char sep) { |
| 65 | return SplitAndTransform(str, sep, nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) { |
| 69 | return SplitAndTransform(str, sep, ::tolower); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | bool StartsWith(const StringPiece& str, const StringPiece& prefix) { |
| 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | bool EndsWith(const StringPiece& str, const StringPiece& suffix) { |
| 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 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 86 | StringPiece TrimLeadingWhitespace(const StringPiece& str) { |
| 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 | |
| 100 | StringPiece TrimTrailingWhitespace(const StringPiece& str) { |
| 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | StringPiece TrimWhitespace(const StringPiece& str) { |
| 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 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 133 | static int IsJavaNameImpl(const StringPiece& str) { |
| 134 | int pieces = 0; |
| 135 | for (const StringPiece& piece : Tokenize(str, '.')) { |
| 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | bool IsJavaClassName(const 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | bool IsJavaPackageName(const 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 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 152 | static int IsAndroidNameImpl(const StringPiece& str) { |
| 153 | int pieces = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | for (const 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 | |
| 176 | bool IsAndroidPackageName(const 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 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame^] | 183 | bool IsAndroidSharedUserId(const android::StringPiece& package_name, |
| 184 | const android::StringPiece& shared_user_id) { |
| 185 | if (shared_user_id.size() > kMaxPackageNameSize) { |
| 186 | return false; |
| 187 | } |
| 188 | return shared_user_id.empty() || IsAndroidNameImpl(shared_user_id) > 1 || |
| 189 | package_name == "android"; |
| 190 | } |
| 191 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 192 | bool IsAndroidSplitName(const StringPiece& str) { |
| 193 | return IsAndroidNameImpl(str) > 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package, |
| 197 | const StringPiece& classname) { |
| 198 | if (classname.empty()) { |
| 199 | return {}; |
| 200 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 201 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | if (util::IsJavaClassName(classname)) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 203 | return classname.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | if (package.empty()) { |
| 207 | return {}; |
| 208 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 210 | std::string result = package.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | if (classname.data()[0] != '.') { |
| 212 | result += '.'; |
| 213 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | result.append(classname.data(), classname.size()); |
| 216 | if (!IsJavaClassName(result)) { |
| 217 | return {}; |
| 218 | } |
| 219 | return result; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 222 | const char* GetToolName() { |
| 223 | static const char* const sToolName = "Android Asset Packaging Tool (aapt)"; |
| 224 | return sToolName; |
| 225 | } |
| 226 | |
| 227 | std::string GetToolFingerprint() { |
| 228 | // DO NOT UPDATE, this is more of a marketing version. |
| 229 | static const char* const sMajorVersion = "2"; |
| 230 | |
| 231 | // Update minor version whenever a feature or flag is added. |
| 232 | static const char* const sMinorVersion = "19"; |
| 233 | |
| 234 | // The build id of aapt2 binary. |
| 235 | static const std::string sBuildId = android::build::GetBuildNumber(); |
| 236 | |
| 237 | return android::base::StringPrintf("%s.%s-%s", sMajorVersion, sMinorVersion, sBuildId.c_str()); |
| 238 | } |
| 239 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | static size_t ConsumeDigits(const char* start, const char* end) { |
| 241 | const char* c = start; |
| 242 | for (; c != end && *c >= '0' && *c <= '9'; c++) { |
| 243 | } |
| 244 | return static_cast<size_t>(c - start); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | bool VerifyJavaStringFormat(const StringPiece& str) { |
| 248 | const char* c = str.begin(); |
| 249 | const char* const end = str.end(); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 250 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | size_t arg_count = 0; |
| 252 | bool nonpositional = false; |
| 253 | while (c != end) { |
| 254 | if (*c == '%' && c + 1 < end) { |
| 255 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 256 | |
Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 257 | if (*c == '%' || *c == 'n') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | c++; |
| 259 | continue; |
| 260 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 261 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | arg_count++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 263 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 264 | size_t num_digits = ConsumeDigits(c, end); |
| 265 | if (num_digits > 0) { |
| 266 | c += num_digits; |
| 267 | if (c != end && *c != '$') { |
| 268 | // The digits were a size, but not a positional argument. |
| 269 | nonpositional = true; |
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 | } else if (*c == '<') { |
| 272 | // Reusing last argument, bad idea since positions can be moved around |
| 273 | // during translation. |
| 274 | nonpositional = true; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 275 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | c++; |
| 277 | |
| 278 | // Optionally we can have a $ after |
| 279 | if (c != end && *c == '$') { |
| 280 | c++; |
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 | } else { |
| 283 | nonpositional = true; |
| 284 | } |
| 285 | |
| 286 | // Ignore size, width, flags, etc. |
| 287 | while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' || |
| 288 | *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) { |
| 289 | c++; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * This is a shortcut to detect strings that are going to Time.format() |
| 294 | * instead of String.format() |
| 295 | * |
| 296 | * Comparison of String.format() and Time.format() args: |
| 297 | * |
| 298 | * String: ABC E GH ST X abcdefgh nost x |
| 299 | * Time: DEFGHKMS W Za d hkm s w yz |
| 300 | * |
| 301 | * Therefore we know it's definitely Time if we have: |
| 302 | * DFKMWZkmwyz |
| 303 | */ |
| 304 | if (c != end) { |
| 305 | switch (*c) { |
| 306 | case 'D': |
| 307 | case 'F': |
| 308 | case 'K': |
| 309 | case 'M': |
| 310 | case 'W': |
| 311 | case 'Z': |
| 312 | case 'k': |
| 313 | case 'm': |
| 314 | case 'w': |
| 315 | case 'y': |
| 316 | case 'z': |
| 317 | return true; |
| 318 | } |
| 319 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | if (c != end) { |
| 323 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 324 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (arg_count > 1 && nonpositional) { |
| 328 | // Multiple arguments were specified, but some or all were non positional. |
| 329 | // Translated |
| 330 | // strings may rearrange the order of the arguments, which will break the |
| 331 | // string. |
| 332 | return false; |
| 333 | } |
| 334 | return true; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 337 | std::string Utf8ToModifiedUtf8(const std::string& utf8) { |
| 338 | // Java uses Modified UTF-8 which only supports the 1, 2, and 3 byte formats of UTF-8. To encode |
| 339 | // 4 byte UTF-8 codepoints, Modified UTF-8 allows the use of surrogate pairs in the same format |
| 340 | // of CESU-8 surrogate pairs. Calculate the size of the utf8 string with all 4 byte UTF-8 |
| 341 | // codepoints replaced with 2 3 byte surrogate pairs |
| 342 | size_t modified_size = 0; |
| 343 | const size_t size = utf8.size(); |
| 344 | for (size_t i = 0; i < size; i++) { |
| 345 | if (((uint8_t) utf8[i] >> 4) == 0xF) { |
| 346 | modified_size += 6; |
| 347 | i += 3; |
| 348 | } else { |
| 349 | modified_size++; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Early out if no 4 byte codepoints are found |
| 354 | if (size == modified_size) { |
| 355 | return utf8; |
| 356 | } |
| 357 | |
| 358 | std::string output; |
| 359 | output.reserve(modified_size); |
| 360 | for (size_t i = 0; i < size; i++) { |
| 361 | if (((uint8_t) utf8[i] >> 4) == 0xF) { |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 362 | int32_t codepoint = utf32_from_utf8_at(utf8.data(), size, i, nullptr); |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 363 | |
| 364 | // Calculate the high and low surrogates as UTF-16 would |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 365 | int32_t high = ((codepoint - 0x10000) / 0x400) + 0xD800; |
| 366 | int32_t low = ((codepoint - 0x10000) % 0x400) + 0xDC00; |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 367 | |
| 368 | // Encode each surrogate in UTF-8 |
| 369 | output.push_back((char) (0xE4 | ((high >> 12) & 0xF))); |
| 370 | output.push_back((char) (0x80 | ((high >> 6) & 0x3F))); |
| 371 | output.push_back((char) (0x80 | (high & 0x3F))); |
| 372 | output.push_back((char) (0xE4 | ((low >> 12) & 0xF))); |
| 373 | output.push_back((char) (0x80 | ((low >> 6) & 0x3F))); |
| 374 | output.push_back((char) (0x80 | (low & 0x3F))); |
| 375 | i += 3; |
| 376 | } else { |
| 377 | output.push_back(utf8[i]); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return output; |
| 382 | } |
| 383 | |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 384 | std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8) { |
| 385 | // The UTF-8 representation will have a byte length less than or equal to the Modified UTF-8 |
| 386 | // representation. |
| 387 | std::string output; |
| 388 | output.reserve(modified_utf8.size()); |
| 389 | |
| 390 | size_t index = 0; |
| 391 | const size_t modified_size = modified_utf8.size(); |
| 392 | while (index < modified_size) { |
| 393 | size_t next_index; |
| 394 | int32_t high_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, index, |
| 395 | &next_index); |
| 396 | if (high_surrogate < 0) { |
| 397 | return {}; |
| 398 | } |
| 399 | |
| 400 | // Check that the first codepoint is within the high surrogate range |
| 401 | if (high_surrogate >= 0xD800 && high_surrogate <= 0xDB7F) { |
| 402 | int32_t low_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, next_index, |
| 403 | &next_index); |
| 404 | if (low_surrogate < 0) { |
| 405 | return {}; |
| 406 | } |
| 407 | |
| 408 | // Check that the second codepoint is within the low surrogate range |
| 409 | if (low_surrogate >= 0xDC00 && low_surrogate <= 0xDFFF) { |
| 410 | const char32_t codepoint = (char32_t) (((high_surrogate - 0xD800) * 0x400) |
| 411 | + (low_surrogate - 0xDC00) + 0x10000); |
| 412 | |
| 413 | // The decoded codepoint should represent a 4 byte, UTF-8 character |
| 414 | const size_t utf8_length = (size_t) utf32_to_utf8_length(&codepoint, 1); |
| 415 | if (utf8_length != 4) { |
| 416 | return {}; |
| 417 | } |
| 418 | |
| 419 | // Encode the UTF-8 representation of the codepoint into the string |
| 420 | char* start = &output[output.size()]; |
| 421 | output.resize(output.size() + utf8_length); |
| 422 | utf32_to_utf8((char32_t*) &codepoint, 1, start, utf8_length + 1); |
| 423 | |
| 424 | index = next_index; |
| 425 | continue; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // Append non-surrogate pairs to the output string |
| 430 | for (size_t i = index; i < next_index; i++) { |
| 431 | output.push_back(modified_utf8[i]); |
| 432 | } |
| 433 | index = next_index; |
| 434 | } |
| 435 | return output; |
| 436 | } |
| 437 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 438 | std::u16string Utf8ToUtf16(const StringPiece& utf8) { |
| 439 | ssize_t utf16_length = utf8_to_utf16_length( |
| 440 | reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length()); |
| 441 | if (utf16_length <= 0) { |
| 442 | return {}; |
| 443 | } |
| 444 | |
| 445 | std::u16string utf16; |
| 446 | utf16.resize(utf16_length); |
| 447 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), |
| 448 | &*utf16.begin(), utf16_length + 1); |
| 449 | return utf16; |
| 450 | } |
| 451 | |
| 452 | std::string Utf16ToUtf8(const StringPiece16& utf16) { |
| 453 | ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length()); |
| 454 | if (utf8_length <= 0) { |
| 455 | return {}; |
| 456 | } |
| 457 | |
| 458 | std::string utf8; |
| 459 | utf8.resize(utf8_length); |
| 460 | utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1); |
| 461 | return utf8; |
| 462 | } |
| 463 | |
| 464 | bool WriteAll(std::ostream& out, const BigBuffer& buffer) { |
| 465 | for (const auto& b : buffer) { |
| 466 | if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) { |
| 467 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 468 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 469 | } |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) { |
| 474 | std::unique_ptr<uint8_t[]> data = |
| 475 | std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]); |
| 476 | uint8_t* p = data.get(); |
| 477 | for (const auto& block : buffer) { |
| 478 | memcpy(p, block.buffer.get(), block.size); |
| 479 | p += block.size; |
| 480 | } |
| 481 | return data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 484 | typename Tokenizer::iterator& Tokenizer::iterator::operator++() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 485 | const char* start = token_.end(); |
| 486 | const char* end = str_.end(); |
| 487 | if (start == end) { |
| 488 | end_ = true; |
| 489 | token_.assign(token_.end(), 0); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 490 | return *this; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | start += 1; |
| 494 | const char* current = start; |
| 495 | while (current != end) { |
| 496 | if (*current == separator_) { |
| 497 | token_.assign(start, current - start); |
| 498 | return *this; |
| 499 | } |
| 500 | ++current; |
| 501 | } |
| 502 | token_.assign(start, end - start); |
| 503 | return *this; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | bool Tokenizer::iterator::operator==(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 507 | // We check equality here a bit differently. |
| 508 | // We need to know that the addresses are the same. |
| 509 | return token_.begin() == rhs.token_.begin() && |
| 510 | token_.end() == rhs.token_.end() && end_ == rhs.end_; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | bool Tokenizer::iterator::operator!=(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 514 | return !(*this == rhs); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 517 | Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 518 | : str_(s), separator_(sep), token_(tok), end_(end) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 519 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 520 | Tokenizer::Tokenizer(const StringPiece& str, char sep) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 521 | : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)), |
| 522 | end_(str, sep, StringPiece(str.end(), 0), true) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 523 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 524 | bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix, |
| 525 | StringPiece* out_entry, StringPiece* out_suffix) { |
| 526 | const StringPiece res_prefix("res/"); |
| 527 | if (!StartsWith(path, res_prefix)) { |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | StringPiece::const_iterator last_occurence = path.end(); |
| 532 | for (auto iter = path.begin() + res_prefix.size(); iter != path.end(); |
| 533 | ++iter) { |
| 534 | if (*iter == '/') { |
| 535 | last_occurence = iter; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 536 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 537 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 538 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 539 | if (last_occurence == path.end()) { |
| 540 | return false; |
| 541 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 542 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 543 | auto iter = std::find(last_occurence, path.end(), '.'); |
| 544 | *out_suffix = StringPiece(iter, path.end() - iter); |
| 545 | *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1); |
| 546 | *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1); |
| 547 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 550 | StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) { |
| 551 | size_t len; |
| 552 | const char16_t* str = pool.stringAt(idx, &len); |
| 553 | if (str != nullptr) { |
| 554 | return StringPiece16(str, len); |
| 555 | } |
| 556 | return StringPiece16(); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 559 | std::string GetString(const android::ResStringPool& pool, size_t idx) { |
| 560 | size_t len; |
| 561 | const char* str = pool.string8At(idx, &len); |
| 562 | if (str != nullptr) { |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 563 | return ModifiedUtf8ToUtf8(std::string(str, len)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 564 | } |
| 565 | return Utf16ToUtf8(GetString16(pool, idx)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 568 | } // namespace util |
| 569 | } // namespace aapt |