blob: 9856a00d7f67dd996ad4ad5e7b98321ef091f9b2 [file] [log] [blame]
Adam Lesinski59e04c62016-02-04 15:59:23 -08001/*
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 "ResourceTable.h"
18#include "ResourceUtils.h"
19#include "ValueVisitor.h"
20#include "proto/ProtoHelpers.h"
21#include "proto/ProtoSerialize.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080022
23#include <androidfw/ResourceTypes.h>
24
25namespace aapt {
26
27namespace {
28
29class ReferenceIdToNameVisitor : public ValueVisitor {
30public:
31 using ValueVisitor::visit;
32
33 ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceNameRef>* mapping) :
34 mMapping(mapping) {
35 assert(mMapping);
36 }
37
38 void visit(Reference* reference) override {
39 if (!reference->id || !reference->id.value().isValid()) {
40 return;
41 }
42
43 ResourceId id = reference->id.value();
44 auto cacheIter = mMapping->find(id);
45 if (cacheIter != mMapping->end()) {
46 reference->name = cacheIter->second.toResourceName();
47 }
48 }
49
50private:
51 const std::map<ResourceId, ResourceNameRef>* mMapping;
52};
53
54class PackagePbDeserializer {
55public:
56 PackagePbDeserializer(const android::ResStringPool* valuePool,
57 const android::ResStringPool* sourcePool,
58 const android::ResStringPool* symbolPool,
59 const Source& source, IDiagnostics* diag) :
60 mValuePool(valuePool), mSourcePool(sourcePool), mSymbolPool(symbolPool),
61 mSource(source), mDiag(diag) {
62 }
63
64public:
65 bool deserializeFromPb(const pb::Package& pbPackage, ResourceTable* table) {
66 Maybe<uint8_t> id;
67 if (pbPackage.has_package_id()) {
68 id = static_cast<uint8_t>(pbPackage.package_id());
69 }
70
71 std::map<ResourceId, ResourceNameRef> idIndex;
72
73 ResourceTablePackage* pkg = table->createPackage(
74 util::utf8ToUtf16(pbPackage.package_name()), id);
75 for (const pb::Type& pbType : pbPackage.types()) {
76 const ResourceType* resType = parseResourceType(util::utf8ToUtf16(pbType.name()));
77 if (!resType) {
78 mDiag->error(DiagMessage(mSource) << "unknown type '" << pbType.name() << "'");
79 return {};
80 }
81
82 ResourceTableType* type = pkg->findOrCreateType(*resType);
83
84 for (const pb::Entry& pbEntry : pbType.entries()) {
85 ResourceEntry* entry = type->findOrCreateEntry(util::utf8ToUtf16(pbEntry.name()));
86
87 // Deserialize the symbol status (public/private with source and comments).
88 if (pbEntry.has_symbol_status()) {
89 const pb::SymbolStatus& pbStatus = pbEntry.symbol_status();
90 if (pbStatus.has_source()) {
91 deserializeSourceFromPb(pbStatus.source(), *mSourcePool,
92 &entry->symbolStatus.source);
93 }
94
95 if (pbStatus.has_comment()) {
96 entry->symbolStatus.comment = util::utf8ToUtf16(pbStatus.comment());
97 }
98
99 SymbolState visibility = deserializeVisibilityFromPb(pbStatus.visibility());
100 entry->symbolStatus.state = visibility;
101
102 if (visibility == SymbolState::kPublic) {
103 // This is a public symbol, we must encode the ID now if there is one.
104 if (pbEntry.has_id()) {
105 entry->id = static_cast<uint16_t>(pbEntry.id());
106 }
107
108 if (type->symbolStatus.state != SymbolState::kPublic) {
109 // If the type has not been made public, do so now.
110 type->symbolStatus.state = SymbolState::kPublic;
111 if (pbType.has_id()) {
112 type->id = static_cast<uint8_t>(pbType.id());
113 }
114 }
115 } else if (visibility == SymbolState::kPrivate) {
116 if (type->symbolStatus.state == SymbolState::kUndefined) {
117 type->symbolStatus.state = SymbolState::kPrivate;
118 }
119 }
120 }
121
122 ResourceId resId(pbPackage.package_id(), pbType.id(), pbEntry.id());
123 if (resId.isValid()) {
124 idIndex[resId] = ResourceNameRef(pkg->name, type->type, entry->name);
125 }
126
127 for (const pb::ConfigValue& pbConfigValue : pbEntry.config_values()) {
128 const pb::ConfigDescription& pbConfig = pbConfigValue.config();
129
130 ConfigDescription config;
131 if (!deserializeConfigDescriptionFromPb(pbConfig, &config)) {
132 mDiag->error(DiagMessage(mSource) << "invalid configuration");
133 return {};
134 }
135
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800136 ResourceConfigValue* configValue = entry->findOrCreateValue(config,
137 pbConfig.product());
138 if (configValue->value) {
Adam Lesinski59e04c62016-02-04 15:59:23 -0800139 // Duplicate config.
140 mDiag->error(DiagMessage(mSource) << "duplicate configuration");
141 return {};
142 }
143
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800144 configValue->value = deserializeValueFromPb(pbConfigValue.value(),
145 config, &table->stringPool);
146 if (!configValue->value) {
Adam Lesinski59e04c62016-02-04 15:59:23 -0800147 return {};
148 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800149 }
150 }
151 }
152
153 ReferenceIdToNameVisitor visitor(&idIndex);
154 visitAllValuesInPackage(pkg, &visitor);
155 return true;
156 }
157
158private:
159 std::unique_ptr<Item> deserializeItemFromPb(const pb::Item& pbItem,
160 const ConfigDescription& config,
161 StringPool* pool) {
162 if (pbItem.has_ref()) {
163 const pb::Reference& pbRef = pbItem.ref();
164 std::unique_ptr<Reference> ref = util::make_unique<Reference>();
165 if (!deserializeReferenceFromPb(pbRef, ref.get())) {
166 return {};
167 }
168 return std::move(ref);
169
170 } else if (pbItem.has_prim()) {
171 const pb::Primitive& pbPrim = pbItem.prim();
172 android::Res_value prim = {};
173 prim.dataType = static_cast<uint8_t>(pbPrim.type());
174 prim.data = pbPrim.data();
175 return util::make_unique<BinaryPrimitive>(prim);
176
177 } else if (pbItem.has_id()) {
178 return util::make_unique<Id>();
179
180 } else if (pbItem.has_str()) {
181 const uint32_t idx = pbItem.str().idx();
182 StringPiece16 str = util::getString(*mValuePool, idx);
183
184 const android::ResStringPool_span* spans = mValuePool->styleAt(idx);
185 if (spans && spans->name.index != android::ResStringPool_span::END) {
186 StyleString styleStr = { str.toString() };
187 while (spans->name.index != android::ResStringPool_span::END) {
188 styleStr.spans.push_back(Span{
189 util::getString(*mValuePool, spans->name.index).toString(),
190 spans->firstChar,
191 spans->lastChar
192 });
193 spans++;
194 }
195 return util::make_unique<StyledString>(
196 pool->makeRef(styleStr, StringPool::Context{ 1, config }));
197 }
198 return util::make_unique<String>(
199 pool->makeRef(str, StringPool::Context{ 1, config }));
200
201 } else if (pbItem.has_raw_str()) {
202 const uint32_t idx = pbItem.raw_str().idx();
203 StringPiece16 str = util::getString(*mValuePool, idx);
204 return util::make_unique<RawString>(
205 pool->makeRef(str, StringPool::Context{ 1, config }));
206
207 } else if (pbItem.has_file()) {
208 const uint32_t idx = pbItem.file().path_idx();
209 StringPiece16 str = util::getString(*mValuePool, idx);
210 return util::make_unique<FileReference>(
211 pool->makeRef(str, StringPool::Context{ 0, config }));
212
213 } else {
214 mDiag->error(DiagMessage(mSource) << "unknown item");
215 }
216 return {};
217 }
218
219 std::unique_ptr<Value> deserializeValueFromPb(const pb::Value& pbValue,
220 const ConfigDescription& config,
221 StringPool* pool) {
222 const bool isWeak = pbValue.has_weak() ? pbValue.weak() : false;
223
224 std::unique_ptr<Value> value;
225 if (pbValue.has_item()) {
226 value = deserializeItemFromPb(pbValue.item(), config, pool);
227 if (!value) {
228 return {};
229 }
230
231 } else if (pbValue.has_compound_value()) {
232 const pb::CompoundValue pbCompoundValue = pbValue.compound_value();
233 if (pbCompoundValue.has_attr()) {
234 const pb::Attribute& pbAttr = pbCompoundValue.attr();
235 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(isWeak);
236 attr->typeMask = pbAttr.format_flags();
237 for (const pb::Attribute_Symbol& pbSymbol : pbAttr.symbols()) {
238 Attribute::Symbol symbol;
239 deserializeItemCommon(pbSymbol, &symbol.symbol);
240 if (!deserializeReferenceFromPb(pbSymbol.name(), &symbol.symbol)) {
241 return {};
242 }
243 symbol.value = pbSymbol.value();
244 attr->symbols.push_back(std::move(symbol));
245 }
246 value = std::move(attr);
247
248 } else if (pbCompoundValue.has_style()) {
249 const pb::Style& pbStyle = pbCompoundValue.style();
250 std::unique_ptr<Style> style = util::make_unique<Style>();
251 if (pbStyle.has_parent()) {
252 style->parent = Reference();
253 if (!deserializeReferenceFromPb(pbStyle.parent(), &style->parent.value())) {
254 return {};
255 }
256
257 if (pbStyle.has_parent_source()) {
258 Source parentSource;
259 deserializeSourceFromPb(pbStyle.parent_source(), *mSourcePool,
260 &parentSource);
261 style->parent.value().setSource(std::move(parentSource));
262 }
263 }
264
265 for (const pb::Style_Entry& pbEntry : pbStyle.entries()) {
266 Style::Entry entry;
267 deserializeItemCommon(pbEntry, &entry.key);
268 if (!deserializeReferenceFromPb(pbEntry.key(), &entry.key)) {
269 return {};
270 }
271
272 entry.value = deserializeItemFromPb(pbEntry.item(), config, pool);
273 if (!entry.value) {
274 return {};
275 }
276
277 deserializeItemCommon(pbEntry, entry.value.get());
278 style->entries.push_back(std::move(entry));
279 }
280 value = std::move(style);
281
282 } else if (pbCompoundValue.has_styleable()) {
283 const pb::Styleable& pbStyleable = pbCompoundValue.styleable();
284 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
285 for (const pb::Styleable_Entry& pbEntry : pbStyleable.entries()) {
286 Reference attrRef;
287 deserializeItemCommon(pbEntry, &attrRef);
288 deserializeReferenceFromPb(pbEntry.attr(), &attrRef);
289 styleable->entries.push_back(std::move(attrRef));
290 }
291 value = std::move(styleable);
292
293 } else if (pbCompoundValue.has_array()) {
294 const pb::Array& pbArray = pbCompoundValue.array();
295 std::unique_ptr<Array> array = util::make_unique<Array>();
296 for (const pb::Array_Entry& pbEntry : pbArray.entries()) {
297 std::unique_ptr<Item> item = deserializeItemFromPb(pbEntry.item(), config,
298 pool);
299 if (!item) {
300 return {};
301 }
302
303 deserializeItemCommon(pbEntry, item.get());
304 array->items.push_back(std::move(item));
305 }
306 value = std::move(array);
307
308 } else if (pbCompoundValue.has_plural()) {
309 const pb::Plural& pbPlural = pbCompoundValue.plural();
310 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
311 for (const pb::Plural_Entry& pbEntry : pbPlural.entries()) {
312 size_t pluralIdx = deserializePluralEnumFromPb(pbEntry.arity());
313 plural->values[pluralIdx] = deserializeItemFromPb(pbEntry.item(), config,
314 pool);
315 if (!plural->values[pluralIdx]) {
316 return {};
317 }
318
319 deserializeItemCommon(pbEntry, plural->values[pluralIdx].get());
320 }
321 value = std::move(plural);
322
323 } else {
324 mDiag->error(DiagMessage(mSource) << "unknown compound value");
325 return {};
326 }
327 } else {
328 mDiag->error(DiagMessage(mSource) << "unknown value");
329 return {};
330 }
331
332 assert(value && "forgot to set value");
333
334 value->setWeak(isWeak);
335 deserializeItemCommon(pbValue, value.get());
336 return value;
337 }
338
339 bool deserializeReferenceFromPb(const pb::Reference& pbRef, Reference* outRef) {
340 outRef->referenceType = deserializeReferenceTypeFromPb(pbRef.type());
341 outRef->privateReference = pbRef.private_();
342
343 if (!pbRef.has_id() && !pbRef.has_symbol_idx()) {
344 return false;
345 }
346
347 if (pbRef.has_id()) {
348 outRef->id = ResourceId(pbRef.id());
349 }
350
351 if (pbRef.has_symbol_idx()) {
352 StringPiece16 strSymbol = util::getString(*mSymbolPool, pbRef.symbol_idx());
353 ResourceNameRef nameRef;
354 if (!ResourceUtils::parseResourceName(strSymbol, &nameRef, nullptr)) {
355 mDiag->error(DiagMessage(mSource) << "invalid reference name '"
356 << strSymbol << "'");
357 return false;
358 }
359
360 outRef->name = nameRef.toResourceName();
361 }
362 return true;
363 }
364
365 template <typename T>
366 void deserializeItemCommon(const T& pbItem, Value* outValue) {
367 if (pbItem.has_source()) {
368 Source source;
369 deserializeSourceFromPb(pbItem.source(), *mSourcePool, &source);
370 outValue->setSource(std::move(source));
371 }
372
373 if (pbItem.has_comment()) {
374 outValue->setComment(util::utf8ToUtf16(pbItem.comment()));
375 }
376 }
377
378private:
379 const android::ResStringPool* mValuePool;
380 const android::ResStringPool* mSourcePool;
381 const android::ResStringPool* mSymbolPool;
382 const Source mSource;
383 IDiagnostics* mDiag;
384};
385
386} // namespace
387
388std::unique_ptr<ResourceTable> deserializeTableFromPb(const pb::ResourceTable& pbTable,
389 const Source& source,
390 IDiagnostics* diag) {
391 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
392
393 if (!pbTable.has_string_pool()) {
394 diag->error(DiagMessage(source) << "no string pool found");
395 return {};
396 }
397
398 android::ResStringPool valuePool;
399 android::status_t result = valuePool.setTo(pbTable.string_pool().data().data(),
400 pbTable.string_pool().data().size());
401 if (result != android::NO_ERROR) {
402 diag->error(DiagMessage(source) << "invalid string pool");
403 return {};
404 }
405
406 android::ResStringPool sourcePool;
407 if (pbTable.has_source_pool()) {
408 result = sourcePool.setTo(pbTable.source_pool().data().data(),
409 pbTable.source_pool().data().size());
410 if (result != android::NO_ERROR) {
411 diag->error(DiagMessage(source) << "invalid source pool");
412 return {};
413 }
414 }
415
416 android::ResStringPool symbolPool;
417 if (pbTable.has_symbol_pool()) {
418 result = symbolPool.setTo(pbTable.symbol_pool().data().data(),
419 pbTable.symbol_pool().data().size());
420 if (result != android::NO_ERROR) {
421 diag->error(DiagMessage(source) << "invalid symbol pool");
422 return {};
423 }
424 }
425
426 PackagePbDeserializer packagePbDeserializer(&valuePool, &sourcePool, &symbolPool, source, diag);
427 for (const pb::Package& pbPackage : pbTable.packages()) {
428 if (!packagePbDeserializer.deserializeFromPb(pbPackage, table.get())) {
429 return {};
430 }
431 }
432 return table;
433}
434
435std::unique_ptr<ResourceFile> deserializeCompiledFileFromPb(const pb::CompiledFile& pbFile,
436 const Source& source,
437 IDiagnostics* diag) {
438 std::unique_ptr<ResourceFile> file = util::make_unique<ResourceFile>();
439
440 ResourceNameRef nameRef;
441
442 // Need to create an lvalue here so that nameRef can point to something real.
443 std::u16string utf16Name = util::utf8ToUtf16(pbFile.resource_name());
444 if (!ResourceUtils::parseResourceName(utf16Name, &nameRef)) {
445 diag->error(DiagMessage(source) << "invalid resource name in compiled file header: "
446 << pbFile.resource_name());
447 return {};
448 }
449 file->name = nameRef.toResourceName();
450 file->source.path = pbFile.source_path();
451 deserializeConfigDescriptionFromPb(pbFile.config(), &file->config);
452
453 for (const pb::CompiledFile_Symbol& pbSymbol : pbFile.exported_symbols()) {
454 // Need to create an lvalue here so that nameRef can point to something real.
455 utf16Name = util::utf8ToUtf16(pbSymbol.resource_name());
456 if (!ResourceUtils::parseResourceName(utf16Name, &nameRef)) {
457 diag->error(DiagMessage(source) << "invalid resource name for exported symbol in "
458 "compiled file header: "
459 << pbFile.resource_name());
460 return {};
461 }
462 file->exportedSymbols.push_back(
463 SourcedResourceName{ nameRef.toResourceName(), pbSymbol.line_no() });
464 }
465 return file;
466}
467
468CompiledFileInputStream::CompiledFileInputStream(const void* data, size_t size) :
469 mIn(static_cast<const uint8_t*>(data), size), mPbFile(),
470 mData(static_cast<const uint8_t*>(data)), mSize(size) {
471}
472
473const pb::CompiledFile* CompiledFileInputStream::CompiledFile() {
474 if (!mPbFile) {
475 std::unique_ptr<pb::CompiledFile> pbFile = util::make_unique<pb::CompiledFile>();
476 uint64_t pbSize = 0u;
477 if (!mIn.ReadLittleEndian64(&pbSize)) {
478 return nullptr;
479 }
480 mIn.PushLimit(static_cast<int>(pbSize));
481 if (!pbFile->ParsePartialFromCodedStream(&mIn)) {
482 return nullptr;
483 }
484
485 const size_t padding = 4 - (pbSize & 0x03);
486 mData += sizeof(uint64_t) + pbSize + padding;
487 mSize -= sizeof(uint64_t) + pbSize + padding;
488 mPbFile = std::move(pbFile);
489 }
490 return mPbFile.get();
491}
492
493const void* CompiledFileInputStream::data() {
494 if (!mPbFile) {
495 if (!CompiledFile()) {
496 return nullptr;
497 }
498 }
499 return mData;
500}
501
502size_t CompiledFileInputStream::size() {
503 if (!mPbFile) {
504 if (!CompiledFile()) {
505 return 0;
506 }
507 }
508 return mSize;
509}
510
511} // namespace aapt