2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "androidfw/Chunk.h"
19 #include "android-base/logging.h"
23 Chunk ChunkIterator::Next() {
24 CHECK(len_ != 0) << "called Next() after last chunk";
26 const ResChunk_header* this_chunk = next_chunk_;
28 // We've already checked the values of this_chunk, so safely increment.
29 next_chunk_ = reinterpret_cast<const ResChunk_header*>(
30 reinterpret_cast<const uint8_t*>(this_chunk) + dtohl(this_chunk->size));
31 len_ -= dtohl(this_chunk->size);
34 // Prepare the next chunk.
37 return Chunk(this_chunk);
40 // Returns false if there was an error.
41 bool ChunkIterator::VerifyNextChunk() {
42 const uintptr_t header_start = reinterpret_cast<uintptr_t>(next_chunk_);
44 // This data must be 4-byte aligned, since we directly
45 // access 32-bit words, which must be aligned on
46 // certain architectures.
47 if (header_start & 0x03) {
48 last_error_ = "header not aligned on 4-byte boundary";
52 if (len_ < sizeof(ResChunk_header)) {
53 last_error_ = "not enough space for header";
57 const size_t header_size = dtohs(next_chunk_->headerSize);
58 const size_t size = dtohl(next_chunk_->size);
59 if (header_size < sizeof(ResChunk_header)) {
60 last_error_ = "header size too small";
64 if (header_size > size) {
65 last_error_ = "header size is larger than entire chunk";
70 last_error_ = "chunk size is bigger than given data";
74 if ((size | header_size) & 0x03) {
75 last_error_ = "header sizes are not aligned on 4-byte boundary";
81 } // namespace android