OSDN Git Service

Osu: fixed Mismatch between createFromParcel and writeToParcel
[android-x86/frameworks-base.git] / libs / androidfw / ChunkIterator.cpp
1 /*
2  * Copyright (C) 2016 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
17 #include "androidfw/Chunk.h"
18
19 #include "android-base/logging.h"
20
21 namespace android {
22
23 Chunk ChunkIterator::Next() {
24   CHECK(len_ != 0) << "called Next() after last chunk";
25
26   const ResChunk_header* this_chunk = next_chunk_;
27
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);
32
33   if (len_ != 0) {
34     // Prepare the next chunk.
35     VerifyNextChunk();
36   }
37   return Chunk(this_chunk);
38 }
39
40 // Returns false if there was an error.
41 bool ChunkIterator::VerifyNextChunk() {
42   const uintptr_t header_start = reinterpret_cast<uintptr_t>(next_chunk_);
43
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";
49     return false;
50   }
51
52   if (len_ < sizeof(ResChunk_header)) {
53     last_error_ = "not enough space for header";
54     return false;
55   }
56
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";
61     return false;
62   }
63
64   if (header_size > size) {
65     last_error_ = "header size is larger than entire chunk";
66     return false;
67   }
68
69   if (size > len_) {
70     last_error_ = "chunk size is bigger than given data";
71     return false;
72   }
73
74   if ((size | header_size) & 0x03) {
75     last_error_ = "header sizes are not aligned on 4-byte boundary";
76     return false;
77   }
78   return true;
79 }
80
81 }  // namespace android