blob: 5d71ff315874f647b442d63daca48923af0425e0 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080019#include <cstddef>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "android-base/logging.h"
22#include "androidfw/ResourceTypes.h"
23
24#include "util/Util.h"
25
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026namespace aapt {
27
28using android::ResChunk_header;
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030ResChunkPullParser::Event ResChunkPullParser::Next() {
31 if (!IsGoodEvent(event_)) {
32 return event_;
33 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 if (event_ == Event::kStartDocument) {
36 current_chunk_ = data_;
37 } else {
38 current_chunk_ =
39 (const ResChunk_header*)(((const char*)current_chunk_) +
40 util::DeviceToHost32(current_chunk_->size));
41 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 const std::ptrdiff_t diff = (const char*)current_chunk_ - (const char*)data_;
44 CHECK(diff >= 0) << "diff is negative";
45 const size_t offset = static_cast<const size_t>(diff);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 if (offset == len_) {
48 current_chunk_ = nullptr;
49 return (event_ = Event::kEndDocument);
50 } else if (offset + sizeof(ResChunk_header) > len_) {
51 error_ = "chunk is past the end of the document";
52 current_chunk_ = nullptr;
53 return (event_ = Event::kBadDocument);
54 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 if (util::DeviceToHost16(current_chunk_->headerSize) <
57 sizeof(ResChunk_header)) {
58 error_ = "chunk has too small header";
59 current_chunk_ = nullptr;
60 return (event_ = Event::kBadDocument);
61 } else if (util::DeviceToHost32(current_chunk_->size) <
62 util::DeviceToHost16(current_chunk_->headerSize)) {
63 error_ = "chunk's total size is smaller than header";
64 current_chunk_ = nullptr;
65 return (event_ = Event::kBadDocument);
66 } else if (offset + util::DeviceToHost32(current_chunk_->size) > len_) {
67 error_ = "chunk's data extends past the end of the document";
68 current_chunk_ = nullptr;
69 return (event_ = Event::kBadDocument);
70 }
71 return (event_ = Event::kChunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072}
73
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074} // namespace aapt