blob: 8d92bd94b0ae2e303bfd2f485c091dcf17ceca2c [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070017#include "unflatten/ResChunkPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinski9431c472017-04-21 16:08:02 -070019#include <inttypes.h>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <cstddef>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "android-base/logging.h"
Adam Lesinski9431c472017-04-21 16:08:02 -070023#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "androidfw/ResourceTypes.h"
25
26#include "util/Util.h"
27
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
30using android::ResChunk_header;
Adam Lesinski9431c472017-04-21 16:08:02 -070031using android::base::StringPrintf;
32
33static std::string ChunkHeaderDump(const ResChunk_header* header) {
34 return StringPrintf("(type=%02" PRIx16 " header_size=%" PRIu16 " size=%" PRIu32 ")",
35 util::DeviceToHost16(header->type), util::DeviceToHost16(header->headerSize),
36 util::DeviceToHost32(header->size));
37}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039ResChunkPullParser::Event ResChunkPullParser::Next() {
40 if (!IsGoodEvent(event_)) {
41 return event_;
42 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 if (event_ == Event::kStartDocument) {
45 current_chunk_ = data_;
46 } else {
47 current_chunk_ =
48 (const ResChunk_header*)(((const char*)current_chunk_) +
49 util::DeviceToHost32(current_chunk_->size));
50 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 const std::ptrdiff_t diff = (const char*)current_chunk_ - (const char*)data_;
53 CHECK(diff >= 0) << "diff is negative";
54 const size_t offset = static_cast<const size_t>(diff);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 if (offset == len_) {
57 current_chunk_ = nullptr;
58 return (event_ = Event::kEndDocument);
59 } else if (offset + sizeof(ResChunk_header) > len_) {
60 error_ = "chunk is past the end of the document";
61 current_chunk_ = nullptr;
62 return (event_ = Event::kBadDocument);
63 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinski9431c472017-04-21 16:08:02 -070065 if (util::DeviceToHost16(current_chunk_->headerSize) < sizeof(ResChunk_header)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 error_ = "chunk has too small header";
67 current_chunk_ = nullptr;
68 return (event_ = Event::kBadDocument);
69 } else if (util::DeviceToHost32(current_chunk_->size) <
70 util::DeviceToHost16(current_chunk_->headerSize)) {
Adam Lesinski9431c472017-04-21 16:08:02 -070071 error_ = "chunk's total size is smaller than header " + ChunkHeaderDump(current_chunk_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 current_chunk_ = nullptr;
73 return (event_ = Event::kBadDocument);
74 } else if (offset + util::DeviceToHost32(current_chunk_->size) > len_) {
Adam Lesinski9431c472017-04-21 16:08:02 -070075 error_ = "chunk's data extends past the end of the document " + ChunkHeaderDump(current_chunk_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 current_chunk_ = nullptr;
77 return (event_ = Event::kBadDocument);
78 }
79 return (event_ = Event::kChunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080}
81
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082} // namespace aapt