Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "unflatten/ResChunkPullParser.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include <cstddef> |
| 20 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include "android-base/logging.h" |
| 22 | #include "androidfw/ResourceTypes.h" |
| 23 | |
| 24 | #include "util/Util.h" |
| 25 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | using android::ResChunk_header; |
| 29 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | ResChunkPullParser::Event ResChunkPullParser::Next() { |
| 31 | if (!IsGoodEvent(event_)) { |
| 32 | return event_; |
| 33 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | } // namespace aapt |