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 | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 17 | #include "format/binary/ResChunkPullParser.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "android-base/logging.h" |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | #include "androidfw/ResourceTypes.h" |
| 25 | |
| 26 | #include "util/Util.h" |
| 27 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | namespace aapt { |
| 29 | |
| 30 | using android::ResChunk_header; |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | static 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | ResChunkPullParser::Event ResChunkPullParser::Next() { |
| 40 | if (!IsGoodEvent(event_)) { |
| 41 | return event_; |
| 42 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | if (event_ == Event::kStartDocument) { |
| 45 | current_chunk_ = data_; |
| 46 | } else { |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 47 | current_chunk_ = (const ResChunk_header*)(((const char*)current_chunk_) + |
| 48 | util::DeviceToHost32(current_chunk_->size)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | const std::ptrdiff_t diff = (const char*)current_chunk_ - (const char*)data_; |
| 52 | CHECK(diff >= 0) << "diff is negative"; |
| 53 | const size_t offset = static_cast<const size_t>(diff); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | if (offset == len_) { |
| 56 | current_chunk_ = nullptr; |
| 57 | return (event_ = Event::kEndDocument); |
| 58 | } else if (offset + sizeof(ResChunk_header) > len_) { |
| 59 | error_ = "chunk is past the end of the document"; |
| 60 | current_chunk_ = nullptr; |
| 61 | return (event_ = Event::kBadDocument); |
| 62 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 63 | |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 64 | if (util::DeviceToHost16(current_chunk_->headerSize) < sizeof(ResChunk_header)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | error_ = "chunk has too small header"; |
| 66 | current_chunk_ = nullptr; |
| 67 | return (event_ = Event::kBadDocument); |
| 68 | } else if (util::DeviceToHost32(current_chunk_->size) < |
| 69 | util::DeviceToHost16(current_chunk_->headerSize)) { |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 70 | error_ = "chunk's total size is smaller than header " + ChunkHeaderDump(current_chunk_); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | current_chunk_ = nullptr; |
| 72 | return (event_ = Event::kBadDocument); |
| 73 | } else if (offset + util::DeviceToHost32(current_chunk_->size) > len_) { |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 74 | error_ = "chunk's data extends past the end of the document " + ChunkHeaderDump(current_chunk_); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | current_chunk_ = nullptr; |
| 76 | return (event_ = Event::kBadDocument); |
| 77 | } |
| 78 | return (event_ = Event::kChunk); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | } // namespace aapt |