summaryrefslogtreecommitdiffstats
path: root/tools/aapt2/ResourceTable.cpp
blob: c93ecc768022c6f5cddd3530d059d44c4d695664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ConfigDescription.h"
#include "Logger.h"
#include "NameMangler.h"
#include "ResourceTable.h"
#include "ResourceValues.h"
#include "Util.h"

#include <algorithm>
#include <androidfw/ResourceTypes.h>
#include <memory>
#include <string>
#include <tuple>

namespace aapt {

static bool compareConfigs(const ResourceConfigValue& lhs, const ConfigDescription& rhs) {
    return lhs.config < rhs;
}

static bool lessThanType(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
    return lhs->type < rhs;
}

static bool lessThanEntry(const std::unique_ptr<ResourceEntry>& lhs, const StringPiece16& rhs) {
    return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
}

ResourceTable::ResourceTable() : mPackageId(kUnsetPackageId) {
    // Make sure attrs always have type ID 1.
    findOrCreateType(ResourceType::kAttr)->typeId = 1;
}

std::unique_ptr<ResourceTableType>& ResourceTable::findOrCreateType(ResourceType type) {
    auto last = mTypes.end();
    auto iter = std::lower_bound(mTypes.begin(), last, type, lessThanType);
    if (iter != last) {
        if ((*iter)->type == type) {
            return *iter;
        }
    }
    return *mTypes.emplace(iter, new ResourceTableType{ type });
}

std::unique_ptr<ResourceEntry>& ResourceTable::findOrCreateEntry(
        std::unique_ptr<ResourceTableType>& type, const StringPiece16& name) {
    auto last = type->entries.end();
    auto iter = std::lower_bound(type->entries.begin(), last, name, lessThanEntry);
    if (iter != last) {
        if (name == (*iter)->name) {
            return *iter;
        }
    }
    return *type->entries.emplace(iter, new ResourceEntry{ name });
}

struct IsAttributeVisitor : ConstValueVisitor {
    bool isAttribute = false;

    void visit(const Attribute&, ValueVisitorArgs&) override {
        isAttribute = true;
    }

    operator bool() {
        return isAttribute;
    }
};

/**
 * The default handler for collisions. A return value of -1 means keep the
 * existing value, 0 means fail, and +1 means take the incoming value.
 */
static int defaultCollisionHandler(const Value& existing, const Value& incoming) {
    IsAttributeVisitor existingIsAttr, incomingIsAttr;
    existing.accept(existingIsAttr, {});
    incoming.accept(incomingIsAttr, {});

    if (!incomingIsAttr) {
        if (incoming.isWeak()) {
            // We're trying to add a weak resource but a resource
            // already exists. Keep the existing.
            return -1;
        } else if (existing.isWeak()) {
            // Override the weak resource with the new strong resource.
            return 1;
        }
        // The existing and incoming values are strong, this is an error
        // if the values are not both attributes.
        return 0;
    }

    if (!existingIsAttr) {
        if (existing.isWeak()) {
            // The existing value is not an attribute and it is weak,
            // so take the incoming attribute value.
            return 1;
        }
        // The existing value is not an attribute and it is strong,
        // so the incoming attribute value is an error.
        return 0;
    }

    //
    // Attribute specific handling. At this point we know both
    // values are attributes. Since we can declare and define
    // attributes all-over, we do special handling to see
    // which definition sticks.
    //
    const Attribute& existingAttr = static_cast<const Attribute&>(existing);
    const Attribute& incomingAttr = static_cast<const Attribute&>(incoming);
    if (existingAttr.typeMask == incomingAttr.typeMask) {
        // The two attributes are both DECLs, but they are plain attributes
        // with the same formats.
        // Keep the strongest one.
        return existingAttr.isWeak() ? 1 : -1;
    }

    if (existingAttr.isWeak() && existingAttr.typeMask == android::ResTable_map::TYPE_ANY) {
        // Any incoming attribute is better than this.
        return 1;
    }

    if (incomingAttr.isWeak() && incomingAttr.typeMask == android::ResTable_map::TYPE_ANY) {
        // The incoming attribute may be a USE instead of a DECL.
        // Keep the existing attribute.
        return -1;
    }
    return 0;
}

static constexpr const char16_t* kValidNameChars = u"._-";
static constexpr const char16_t* kValidNameMangledChars = u"._-$";

bool ResourceTable::addResource(const ResourceNameRef& name, const ConfigDescription& config,
                                const SourceLine& source, std::unique_ptr<Value> value) {
    return addResourceImpl(name, ResourceId{}, config, source, std::move(value), kValidNameChars);
}

bool ResourceTable::addResource(const ResourceNameRef& name, const ResourceId resId,
                                const ConfigDescription& config, const SourceLine& source,
                                std::unique_ptr<Value> value) {
    return addResourceImpl(name, resId, config, source, std::move(value), kValidNameChars);
}

bool ResourceTable::addResourceAllowMangled(const ResourceNameRef& name,
                                            const ConfigDescription& config,
                                            const SourceLine& source,
                                            std::unique_ptr<Value> value) {
    return addResourceImpl(name, ResourceId{}, config, source, std::move(value),
                           kValidNameMangledChars);
}

bool ResourceTable::addResourceImpl(const ResourceNameRef& name, const ResourceId resId,
                                    const ConfigDescription& config, const SourceLine& source,
                                    std::unique_ptr<Value> value, const char16_t* validChars) {
    if (!name.package.empty() && name.package != mPackage) {
        Logger::error(source)
                << "resource '"
                << name
                << "' has incompatible package. Must be '"
                << mPackage
                << "'."
                << std::endl;
        return false;
    }

    auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
    if (badCharIter != name.entry.end()) {
        Logger::error(source)
                << "resource '"
                << name
                << "' has invalid entry name '"
                << name.entry
                << "'. Invalid character '"
                << StringPiece16(badCharIter, 1)
                << "'."
                << std::endl;
        return false;
    }

    std::unique_ptr<ResourceTableType>& type = findOrCreateType(name.type);
    if (resId.isValid() && type->typeId != ResourceTableType::kUnsetTypeId &&
            type->typeId != resId.typeId()) {
        Logger::error(source)
                << "trying to add resource '"
                << name
                << "' with ID "
                << resId
                << " but type '"
                << type->type
                << "' already has ID "
                << std::hex << type->typeId << std::dec
                << "."
                << std::endl;
        return false;
    }

    std::unique_ptr<ResourceEntry>& entry = findOrCreateEntry(type, name.entry);
    if (resId.isValid() && entry->entryId != ResourceEntry::kUnsetEntryId &&
            entry->entryId != resId.entryId()) {
        Logger::error(source)
                << "trying to add resource '"
                << name
                << "' with ID "
                << resId
                << " but resource already has ID "
                << ResourceId(mPackageId, type->typeId, entry->entryId)
                << "."
                << std::endl;
        return false;
    }

    const auto endIter = std::end(entry->values);
    auto iter = std::lower_bound(std::begin(entry->values), endIter, config, compareConfigs);
    if (iter == endIter || iter->config != config) {
        // This resource did not exist before, add it.
        entry->values.insert(iter, ResourceConfigValue{ config, source, {}, std::move(value) });
    } else {
        int collisionResult = defaultCollisionHandler(*iter->value, *value);
        if (collisionResult > 0) {
            // Take the incoming value.
            *iter = ResourceConfigValue{ config, source, {}, std::move(value) };
        } else if (collisionResult == 0) {
            Logger::error(source)
                    << "duplicate value for resource '" << name << "' "
                    << "with config '" << iter->config << "'."
                    << std::endl;

            Logger::error(iter->source)
                    << "resource previously defined here."
                    << std::endl;
            return false;
        }
    }

    if (resId.isValid()) {
        type->typeId = resId.typeId();
        entry->entryId = resId.entryId();
    }
    return true;
}

bool ResourceTable::markPublic(const ResourceNameRef& name, const ResourceId resId,
                               const SourceLine& source) {
    return markPublicImpl(name, resId, source, kValidNameChars);
}

bool ResourceTable::markPublicAllowMangled(const ResourceNameRef& name, const ResourceId resId,
                                           const SourceLine& source) {
    return markPublicImpl(name, resId, source, kValidNameMangledChars);
}

bool ResourceTable::markPublicImpl(const ResourceNameRef& name, const ResourceId resId,
                                   const SourceLine& source, const char16_t* validChars) {
    if (!name.package.empty() && name.package != mPackage) {
        Logger::error(source)
                << "resource '"
                << name
                << "' has incompatible package. Must be '"
                << mPackage
                << "'."
            << std::endl;
        return false;
    }

    auto badCharIter = util::findNonAlphaNumericAndNotInSet(name.entry, validChars);
    if (badCharIter != name.entry.end()) {
        Logger::error(source)
                << "resource '"
                << name
                << "' has invalid entry name '"
                << name.entry
                << "'. Invalid character '"
                << StringPiece16(badCharIter, 1)
                << "'."
                << std::endl;
        return false;
    }

    std::unique_ptr<ResourceTableType>& type = findOrCreateType(name.type);
    if (resId.isValid() && type->typeId != ResourceTableType::kUnsetTypeId &&
            type->typeId != resId.typeId()) {
        Logger::error(source)
                << "trying to make resource '"
                << name
                << "' public with ID "
                << resId
                << " but type '"
                << type->type
                << "' already has ID "
                << std::hex << type->typeId << std::dec
                << "."
                << std::endl;
        return false;
    }

    std::unique_ptr<ResourceEntry>& entry = findOrCreateEntry(type, name.entry);
    if (resId.isValid() && entry->entryId != ResourceEntry::kUnsetEntryId &&
            entry->entryId != resId.entryId()) {
        Logger::error(source)
                << "trying to make resource '"
                << name
                << "' public with ID "
                << resId
                << " but resource already has ID "
                << ResourceId(mPackageId, type->typeId, entry->entryId)
                << "."
                << std::endl;
        return false;
    }

    type->publicStatus.isPublic = true;
    entry->publicStatus.isPublic = true;
    entry->publicStatus.source = source;

    if (resId.isValid()) {
        type->typeId = resId.typeId();
        entry->entryId = resId.entryId();
    }
    return true;
}

bool ResourceTable::merge(ResourceTable&& other) {
    const bool mangleNames = mPackage != other.getPackage();
    std::u16string mangledName;

    for (auto& otherType : other) {
        std::unique_ptr<ResourceTableType>& type = findOrCreateType(otherType->type);
        if (otherType->publicStatus.isPublic) {
            if (type->publicStatus.isPublic && type->typeId != otherType->typeId) {
                Logger::error() << "can not merge type '" << type->type
                                << "': conflicting public IDs "
                                << "(" << type->typeId << " vs " << otherType->typeId << ")."
                                << std::endl;
                return false;
            }
            type->publicStatus = std::move(otherType->publicStatus);
            type->typeId = otherType->typeId;
        }

        for (auto& otherEntry : otherType->entries) {
            const std::u16string* nameToAdd = &otherEntry->name;
            if (mangleNames) {
                mangledName = otherEntry->name;
                NameMangler::mangle(other.getPackage(), &mangledName);
                nameToAdd = &mangledName;
            }

            std::unique_ptr<ResourceEntry>& entry = findOrCreateEntry(type, *nameToAdd);
            if (otherEntry->publicStatus.isPublic) {
                if (entry->publicStatus.isPublic && entry->entryId != otherEntry->entryId) {
                    Logger::error() << "can not merge entry '" << type->type << "/" << entry->name
                                    << "': conflicting public IDs "
                                    << "(" << entry->entryId << " vs " << entry->entryId << ")."
                                    << std::endl;
                    return false;
                }
                entry->publicStatus = std::move(otherEntry->publicStatus);
                entry->entryId = otherEntry->entryId;
            }

            for (ResourceConfigValue& otherValue : otherEntry->values) {
                auto iter = std::lower_bound(entry->values.begin(), entry->values.end(),
                                             otherValue.config, compareConfigs);
                if (iter != entry->values.end() && iter->config == otherValue.config) {
                    int collisionResult = defaultCollisionHandler(*iter->value, *otherValue.value);
                    if (collisionResult > 0) {
                        // Take the incoming value.
                        iter->source = std::move(otherValue.source);
                        iter->comment = std::move(otherValue.comment);
                        iter->value = std::unique_ptr<Value>(otherValue.value->clone(&mValuePool));
                    } else if (collisionResult == 0) {
                        ResourceNameRef resourceName = { mPackage, type->type, entry->name };
                        Logger::error(otherValue.source)
                                << "resource '" << resourceName << "' has a conflicting value for "
                                << "configuration (" << otherValue.config << ")."
                                << std::endl;
                        Logger::note(iter->source) << "originally defined here." << std::endl;
                        return false;
                    }
                } else {
                    entry->values.insert(iter, ResourceConfigValue{
                            otherValue.config,
                            std::move(otherValue.source),
                            std::move(otherValue.comment),
                            std::unique_ptr<Value>(otherValue.value->clone(&mValuePool)),
                    });
                }
            }
        }
    }
    return true;
}

std::tuple<const ResourceTableType*, const ResourceEntry*>
ResourceTable::findResource(const ResourceNameRef& name) const {
    if (name.package != mPackage) {
        return {};
    }

    auto iter = std::lower_bound(mTypes.begin(), mTypes.end(), name.type, lessThanType);
    if (iter == mTypes.end() || (*iter)->type != name.type) {
        return {};
    }

    const std::unique_ptr<ResourceTableType>& type = *iter;
    auto iter2 = std::lower_bound(type->entries.begin(), type->entries.end(), name.entry,
                                  lessThanEntry);
    if (iter2 == type->entries.end() || name.entry != (*iter2)->name) {
        return {};
    }
    return std::make_tuple(iter->get(), iter2->get());
}

} // namespace aapt