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