Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Flags.h" |
| 18 | #include "ResourceTable.h" |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 19 | #include "ValueVisitor.h" |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 20 | #include "io/ZipArchive.h" |
| 21 | #include "process/IResourceTableConsumer.h" |
| 22 | #include "process/SymbolTable.h" |
| 23 | #include "unflatten/BinaryResourceParser.h" |
| 24 | |
| 25 | #include <android-base/macros.h> |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | class DiffContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 30 | public: |
| 31 | const std::string& getCompilationPackage() override { return mEmpty; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 33 | uint8_t getPackageId() override { return 0x0; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 35 | IDiagnostics* getDiagnostics() override { return &mDiagnostics; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 37 | NameMangler* getNameMangler() override { return &mNameMangler; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 39 | SymbolTable* getExternalSymbols() override { return &mSymbolTable; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 41 | bool verbose() override { return false; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 43 | int getMinSdkVersion() override { return 0; } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 45 | private: |
| 46 | std::string mEmpty; |
| 47 | StdErrDiagnostics mDiagnostics; |
| 48 | NameMangler mNameMangler = NameMangler(NameManglerPolicy{}); |
| 49 | SymbolTable mSymbolTable; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | class LoadedApk { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 53 | public: |
| 54 | LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk, |
| 55 | std::unique_ptr<ResourceTable> table) |
| 56 | : mSource(source), mApk(std::move(apk)), mTable(std::move(table)) {} |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 58 | io::IFileCollection* getFileCollection() { return mApk.get(); } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 60 | ResourceTable* getResourceTable() { return mTable.get(); } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 62 | const Source& getSource() { return mSource; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 64 | private: |
| 65 | Source mSource; |
| 66 | std::unique_ptr<io::IFileCollection> mApk; |
| 67 | std::unique_ptr<ResourceTable> mTable; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 69 | DISALLOW_COPY_AND_ASSIGN(LoadedApk); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 72 | static std::unique_ptr<LoadedApk> loadApkFromPath(IAaptContext* context, |
| 73 | const StringPiece& path) { |
| 74 | Source source(path); |
| 75 | std::string error; |
| 76 | std::unique_ptr<io::ZipFileCollection> apk = |
| 77 | io::ZipFileCollection::create(path, &error); |
| 78 | if (!apk) { |
| 79 | context->getDiagnostics()->error(DiagMessage(source) << error); |
| 80 | return {}; |
| 81 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 83 | io::IFile* file = apk->findFile("resources.arsc"); |
| 84 | if (!file) { |
| 85 | context->getDiagnostics()->error(DiagMessage(source) |
| 86 | << "no resources.arsc found"); |
| 87 | return {}; |
| 88 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 90 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 91 | if (!data) { |
| 92 | context->getDiagnostics()->error(DiagMessage(source) |
| 93 | << "could not open resources.arsc"); |
| 94 | return {}; |
| 95 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 97 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
| 98 | BinaryResourceParser parser(context, table.get(), source, data->data(), |
| 99 | data->size()); |
| 100 | if (!parser.parse()) { |
| 101 | return {}; |
| 102 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 104 | return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table)); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void emitDiffLine(const Source& source, const StringPiece& message) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 108 | std::cerr << source << ": " << message << "\n"; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 111 | static bool isSymbolVisibilityDifferent(const Symbol& symbolA, |
| 112 | const Symbol& symbolB) { |
| 113 | return symbolA.state != symbolB.state; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | template <typename Id> |
| 117 | static bool isIdDiff(const Symbol& symbolA, const Maybe<Id>& idA, |
| 118 | const Symbol& symbolB, const Maybe<Id>& idB) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 119 | if (symbolA.state == SymbolState::kPublic || |
| 120 | symbolB.state == SymbolState::kPublic) { |
| 121 | return idA != idB; |
| 122 | } |
| 123 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 126 | static bool emitResourceConfigValueDiff( |
| 127 | IAaptContext* context, LoadedApk* apkA, ResourceTablePackage* pkgA, |
| 128 | ResourceTableType* typeA, ResourceEntry* entryA, |
| 129 | ResourceConfigValue* configValueA, LoadedApk* apkB, |
| 130 | ResourceTablePackage* pkgB, ResourceTableType* typeB, ResourceEntry* entryB, |
| 131 | ResourceConfigValue* configValueB) { |
| 132 | Value* valueA = configValueA->value.get(); |
| 133 | Value* valueB = configValueB->value.get(); |
| 134 | if (!valueA->equals(valueB)) { |
| 135 | std::stringstream strStream; |
| 136 | strStream << "value " << pkgA->name << ":" << typeA->type << "/" |
| 137 | << entryA->name << " config=" << configValueA->config |
| 138 | << " does not match:\n"; |
| 139 | valueA->print(&strStream); |
| 140 | strStream << "\n vs \n"; |
| 141 | valueB->print(&strStream); |
| 142 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 143 | return true; |
| 144 | } |
| 145 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 148 | static bool emitResourceEntryDiff(IAaptContext* context, LoadedApk* apkA, |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 149 | ResourceTablePackage* pkgA, |
| 150 | ResourceTableType* typeA, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 151 | ResourceEntry* entryA, LoadedApk* apkB, |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 152 | ResourceTablePackage* pkgB, |
| 153 | ResourceTableType* typeB, |
| 154 | ResourceEntry* entryB) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 155 | bool diff = false; |
| 156 | for (std::unique_ptr<ResourceConfigValue>& configValueA : entryA->values) { |
| 157 | ResourceConfigValue* configValueB = entryB->findValue(configValueA->config); |
| 158 | if (!configValueB) { |
| 159 | std::stringstream strStream; |
| 160 | strStream << "missing " << pkgA->name << ":" << typeA->type << "/" |
| 161 | << entryA->name << " config=" << configValueA->config; |
| 162 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 163 | diff = true; |
| 164 | } else { |
| 165 | diff |= emitResourceConfigValueDiff(context, apkA, pkgA, typeA, entryA, |
| 166 | configValueA.get(), apkB, pkgB, typeB, |
| 167 | entryB, configValueB); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 168 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 169 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 171 | // Check for any newly added config values. |
| 172 | for (std::unique_ptr<ResourceConfigValue>& configValueB : entryB->values) { |
| 173 | ResourceConfigValue* configValueA = entryA->findValue(configValueB->config); |
| 174 | if (!configValueA) { |
| 175 | std::stringstream strStream; |
| 176 | strStream << "new config " << pkgB->name << ":" << typeB->type << "/" |
| 177 | << entryB->name << " config=" << configValueB->config; |
| 178 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 179 | diff = true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 180 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 181 | } |
| 182 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 185 | static bool emitResourceTypeDiff(IAaptContext* context, LoadedApk* apkA, |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 186 | ResourceTablePackage* pkgA, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 187 | ResourceTableType* typeA, LoadedApk* apkB, |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 188 | ResourceTablePackage* pkgB, |
| 189 | ResourceTableType* typeB) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 190 | bool diff = false; |
| 191 | for (std::unique_ptr<ResourceEntry>& entryA : typeA->entries) { |
| 192 | ResourceEntry* entryB = typeB->findEntry(entryA->name); |
| 193 | if (!entryB) { |
| 194 | std::stringstream strStream; |
| 195 | strStream << "missing " << pkgA->name << ":" << typeA->type << "/" |
| 196 | << entryA->name; |
| 197 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 198 | diff = true; |
| 199 | } else { |
| 200 | if (isSymbolVisibilityDifferent(entryA->symbolStatus, |
| 201 | entryB->symbolStatus)) { |
| 202 | std::stringstream strStream; |
| 203 | strStream << pkgA->name << ":" << typeA->type << "/" << entryA->name |
| 204 | << " has different visibility ("; |
| 205 | if (entryB->symbolStatus.state == SymbolState::kPublic) { |
| 206 | strStream << "PUBLIC"; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 207 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 208 | strStream << "PRIVATE"; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 209 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 210 | strStream << " vs "; |
| 211 | if (entryA->symbolStatus.state == SymbolState::kPublic) { |
| 212 | strStream << "PUBLIC"; |
| 213 | } else { |
| 214 | strStream << "PRIVATE"; |
| 215 | } |
| 216 | strStream << ")"; |
| 217 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 218 | diff = true; |
| 219 | } else if (isIdDiff(entryA->symbolStatus, entryA->id, |
| 220 | entryB->symbolStatus, entryB->id)) { |
| 221 | std::stringstream strStream; |
| 222 | strStream << pkgA->name << ":" << typeA->type << "/" << entryA->name |
| 223 | << " has different public ID ("; |
| 224 | if (entryB->id) { |
| 225 | strStream << "0x" << std::hex << entryB->id.value(); |
| 226 | } else { |
| 227 | strStream << "none"; |
| 228 | } |
| 229 | strStream << " vs "; |
| 230 | if (entryA->id) { |
| 231 | strStream << "0x " << std::hex << entryA->id.value(); |
| 232 | } else { |
| 233 | strStream << "none"; |
| 234 | } |
| 235 | strStream << ")"; |
| 236 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 237 | diff = true; |
| 238 | } |
| 239 | diff |= emitResourceEntryDiff(context, apkA, pkgA, typeA, entryA.get(), |
| 240 | apkB, pkgB, typeB, entryB); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 241 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 242 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 244 | // Check for any newly added entries. |
| 245 | for (std::unique_ptr<ResourceEntry>& entryB : typeB->entries) { |
| 246 | ResourceEntry* entryA = typeA->findEntry(entryB->name); |
| 247 | if (!entryA) { |
| 248 | std::stringstream strStream; |
| 249 | strStream << "new entry " << pkgB->name << ":" << typeB->type << "/" |
| 250 | << entryB->name; |
| 251 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 252 | diff = true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 253 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 254 | } |
| 255 | return diff; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static bool emitResourcePackageDiff(IAaptContext* context, LoadedApk* apkA, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 259 | ResourceTablePackage* pkgA, LoadedApk* apkB, |
| 260 | ResourceTablePackage* pkgB) { |
| 261 | bool diff = false; |
| 262 | for (std::unique_ptr<ResourceTableType>& typeA : pkgA->types) { |
| 263 | ResourceTableType* typeB = pkgB->findType(typeA->type); |
| 264 | if (!typeB) { |
| 265 | std::stringstream strStream; |
| 266 | strStream << "missing " << pkgA->name << ":" << typeA->type; |
| 267 | emitDiffLine(apkA->getSource(), strStream.str()); |
| 268 | diff = true; |
| 269 | } else { |
| 270 | if (isSymbolVisibilityDifferent(typeA->symbolStatus, |
| 271 | typeB->symbolStatus)) { |
| 272 | std::stringstream strStream; |
| 273 | strStream << pkgA->name << ":" << typeA->type |
| 274 | << " has different visibility ("; |
| 275 | if (typeB->symbolStatus.state == SymbolState::kPublic) { |
| 276 | strStream << "PUBLIC"; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 277 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 278 | strStream << "PRIVATE"; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 279 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 280 | strStream << " vs "; |
| 281 | if (typeA->symbolStatus.state == SymbolState::kPublic) { |
| 282 | strStream << "PUBLIC"; |
| 283 | } else { |
| 284 | strStream << "PRIVATE"; |
| 285 | } |
| 286 | strStream << ")"; |
| 287 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 288 | diff = true; |
| 289 | } else if (isIdDiff(typeA->symbolStatus, typeA->id, typeB->symbolStatus, |
| 290 | typeB->id)) { |
| 291 | std::stringstream strStream; |
| 292 | strStream << pkgA->name << ":" << typeA->type |
| 293 | << " has different public ID ("; |
| 294 | if (typeB->id) { |
| 295 | strStream << "0x" << std::hex << typeB->id.value(); |
| 296 | } else { |
| 297 | strStream << "none"; |
| 298 | } |
| 299 | strStream << " vs "; |
| 300 | if (typeA->id) { |
| 301 | strStream << "0x " << std::hex << typeA->id.value(); |
| 302 | } else { |
| 303 | strStream << "none"; |
| 304 | } |
| 305 | strStream << ")"; |
| 306 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 307 | diff = true; |
| 308 | } |
| 309 | diff |= emitResourceTypeDiff(context, apkA, pkgA, typeA.get(), apkB, pkgB, |
| 310 | typeB); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 311 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 312 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 313 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 314 | // Check for any newly added types. |
| 315 | for (std::unique_ptr<ResourceTableType>& typeB : pkgB->types) { |
| 316 | ResourceTableType* typeA = pkgA->findType(typeB->type); |
| 317 | if (!typeA) { |
| 318 | std::stringstream strStream; |
| 319 | strStream << "new type " << pkgB->name << ":" << typeB->type; |
| 320 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 321 | diff = true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 322 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 323 | } |
| 324 | return diff; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 327 | static bool emitResourceTableDiff(IAaptContext* context, LoadedApk* apkA, |
| 328 | LoadedApk* apkB) { |
| 329 | ResourceTable* tableA = apkA->getResourceTable(); |
| 330 | ResourceTable* tableB = apkB->getResourceTable(); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 331 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 332 | bool diff = false; |
| 333 | for (std::unique_ptr<ResourceTablePackage>& pkgA : tableA->packages) { |
| 334 | ResourceTablePackage* pkgB = tableB->findPackage(pkgA->name); |
| 335 | if (!pkgB) { |
| 336 | std::stringstream strStream; |
| 337 | strStream << "missing package " << pkgA->name; |
| 338 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 339 | diff = true; |
| 340 | } else { |
| 341 | if (pkgA->id != pkgB->id) { |
| 342 | std::stringstream strStream; |
| 343 | strStream << "package '" << pkgA->name << "' has different id ("; |
| 344 | if (pkgB->id) { |
| 345 | strStream << "0x" << std::hex << pkgB->id.value(); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 346 | } else { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 347 | strStream << "none"; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 348 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 349 | strStream << " vs "; |
| 350 | if (pkgA->id) { |
| 351 | strStream << "0x" << std::hex << pkgA->id.value(); |
| 352 | } else { |
| 353 | strStream << "none"; |
| 354 | } |
| 355 | strStream << ")"; |
| 356 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 357 | diff = true; |
| 358 | } |
| 359 | diff |= emitResourcePackageDiff(context, apkA, pkgA.get(), apkB, pkgB); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 360 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 361 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 362 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 363 | // Check for any newly added packages. |
| 364 | for (std::unique_ptr<ResourceTablePackage>& pkgB : tableB->packages) { |
| 365 | ResourceTablePackage* pkgA = tableA->findPackage(pkgB->name); |
| 366 | if (!pkgA) { |
| 367 | std::stringstream strStream; |
| 368 | strStream << "new package " << pkgB->name; |
| 369 | emitDiffLine(apkB->getSource(), strStream.str()); |
| 370 | diff = true; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 371 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 372 | } |
| 373 | return diff; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 376 | class ZeroingReferenceVisitor : public ValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 377 | public: |
| 378 | using ValueVisitor::visit; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 379 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 380 | void visit(Reference* ref) override { |
| 381 | if (ref->name && ref->id) { |
| 382 | if (ref->id.value().packageId() == 0x7f) { |
| 383 | ref->id = {}; |
| 384 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 385 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 386 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | static void zeroOutAppReferences(ResourceTable* table) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 390 | ZeroingReferenceVisitor visitor; |
| 391 | visitAllValuesInTable(table, &visitor); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 394 | int diff(const std::vector<StringPiece>& args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 395 | DiffContext context; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 396 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 397 | Flags flags; |
| 398 | if (!flags.parse("aapt2 diff", args, &std::cerr)) { |
| 399 | return 1; |
| 400 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 401 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 402 | if (flags.getArgs().size() != 2u) { |
| 403 | std::cerr << "must have two apks as arguments.\n\n"; |
| 404 | flags.usage("aapt2 diff", &std::cerr); |
| 405 | return 1; |
| 406 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 407 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 408 | std::unique_ptr<LoadedApk> apkA = |
| 409 | loadApkFromPath(&context, flags.getArgs()[0]); |
| 410 | std::unique_ptr<LoadedApk> apkB = |
| 411 | loadApkFromPath(&context, flags.getArgs()[1]); |
| 412 | if (!apkA || !apkB) { |
| 413 | return 1; |
| 414 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 415 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 416 | // Zero out Application IDs in references. |
| 417 | zeroOutAppReferences(apkA->getResourceTable()); |
| 418 | zeroOutAppReferences(apkB->getResourceTable()); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 419 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 420 | if (emitResourceTableDiff(&context, apkA.get(), apkB.get())) { |
| 421 | // We emitted a diff, so return 1 (failure). |
| 422 | return 1; |
| 423 | } |
| 424 | return 0; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 427 | } // namespace aapt |