blob: bc2e6990433c1f825b4f23faf7e0dbb3839902d8 [file] [log] [blame]
Adam Lesinski21efb682016-09-14 17:35:43 -07001/*
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 "compile/Png.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskicc73e992017-05-12 18:16:44 -070019#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080020#include "androidfw/StringPiece.h"
21
Adam Lesinski21efb682016-09-14 17:35:43 -070022#include "io/Io.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080023
Adam Lesinski46708052017-09-29 14:49:15 -070024using ::android::StringPiece;
25using ::android::base::StringPrintf;
Adam Lesinski21efb682016-09-14 17:35:43 -070026
27namespace aapt {
28
29static constexpr const char* kPngSignature = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a";
30
31// Useful helper function that encodes individual bytes into a uint32
32// at compile time.
33constexpr uint32_t u32(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 return (((uint32_t)a) << 24) | (((uint32_t)b) << 16) | (((uint32_t)c) << 8) |
35 ((uint32_t)d);
Adam Lesinski21efb682016-09-14 17:35:43 -070036}
37
38// Whitelist of PNG chunk types that we want to keep in the resulting PNG.
39enum PngChunkTypes {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 kPngChunkIHDR = u32(73, 72, 68, 82),
41 kPngChunkIDAT = u32(73, 68, 65, 84),
42 kPngChunkIEND = u32(73, 69, 78, 68),
43 kPngChunkPLTE = u32(80, 76, 84, 69),
44 kPngChunktRNS = u32(116, 82, 78, 83),
45 kPngChunksRGB = u32(115, 82, 71, 66),
Adam Lesinski21efb682016-09-14 17:35:43 -070046};
47
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048static uint32_t Peek32LE(const char* data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 uint32_t word = ((uint32_t)data[0]) & 0x000000ff;
50 word <<= 8;
51 word |= ((uint32_t)data[1]) & 0x000000ff;
52 word <<= 8;
53 word |= ((uint32_t)data[2]) & 0x000000ff;
54 word <<= 8;
55 word |= ((uint32_t)data[3]) & 0x000000ff;
56 return word;
Adam Lesinski21efb682016-09-14 17:35:43 -070057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059static bool IsPngChunkWhitelisted(uint32_t type) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 switch (type) {
Adam Lesinski21efb682016-09-14 17:35:43 -070061 case kPngChunkIHDR:
62 case kPngChunkIDAT:
63 case kPngChunkIEND:
64 case kPngChunkPLTE:
65 case kPngChunktRNS:
66 case kPngChunksRGB:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return true;
Adam Lesinski21efb682016-09-14 17:35:43 -070068 default:
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 return false;
70 }
Adam Lesinski21efb682016-09-14 17:35:43 -070071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073PngChunkFilter::PngChunkFilter(const StringPiece& data) : data_(data) {
74 if (util::StartsWith(data_, kPngSignature)) {
75 window_start_ = 0;
Adam Lesinski06460ef2017-03-14 18:52:13 -070076 window_end_ = kPngSignatureSize;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 } else {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -070078 error_msg_ = "file does not start with PNG signature";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 }
Adam Lesinski21efb682016-09-14 17:35:43 -070080}
81
Adam Lesinski06460ef2017-03-14 18:52:13 -070082bool PngChunkFilter::ConsumeWindow(const void** buffer, size_t* len) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 if (window_start_ != window_end_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 // We have bytes to give from our window.
Adam Lesinski06460ef2017-03-14 18:52:13 -070085 const size_t bytes_read = window_end_ - window_start_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 *buffer = data_.data() + window_start_;
87 *len = bytes_read;
88 window_start_ = window_end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 return true;
90 }
91 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -070092}
93
Adam Lesinski06460ef2017-03-14 18:52:13 -070094bool PngChunkFilter::Next(const void** buffer, size_t* len) {
Adam Lesinskicc73e992017-05-12 18:16:44 -070095 if (HadError()) {
Adam Lesinski21efb682016-09-14 17:35:43 -070096 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 }
98
99 // In case BackUp was called, we must consume the window.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 if (ConsumeWindow(buffer, len)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 return true;
102 }
103
104 // Advance the window as far as possible (until we meet a chunk that
105 // we want to strip).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 while (window_end_ < data_.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 // Chunk length (4 bytes) + type (4 bytes) + crc32 (4 bytes) = 12 bytes.
108 const size_t kMinChunkHeaderSize = 3 * sizeof(uint32_t);
109
110 // Is there enough room for a chunk header?
Adam Lesinskicc73e992017-05-12 18:16:44 -0700111 if (data_.size() - window_end_ < kMinChunkHeaderSize) {
112 error_msg_ = StringPrintf("Not enough space for a PNG chunk @ byte %zu/%zu", window_end_,
113 data_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 return false;
115 }
116
117 // Verify the chunk length.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 const uint32_t chunk_len = Peek32LE(data_.data() + window_end_);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700119 if ((size_t)chunk_len > data_.size() - window_end_ - kMinChunkHeaderSize) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 // Overflow.
Adam Lesinskicc73e992017-05-12 18:16:44 -0700121 const uint32_t chunk_type = Peek32LE(data_.data() + window_end_ + sizeof(uint32_t));
122 error_msg_ = StringPrintf(
123 "PNG chunk type %08x is too large: chunk length is %zu but chunk "
124 "starts at byte %zu/%zu",
125 chunk_type, (size_t)chunk_len, window_end_ + kMinChunkHeaderSize, data_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 return false;
127 }
128
129 // Do we strip this chunk?
Adam Lesinski06460ef2017-03-14 18:52:13 -0700130 const uint32_t chunk_type = Peek32LE(data_.data() + window_end_ + sizeof(uint32_t));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 if (IsPngChunkWhitelisted(chunk_type)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 // Advance the window to include this chunk.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 window_end_ += kMinChunkHeaderSize + chunk_len;
Adam Lesinskicc73e992017-05-12 18:16:44 -0700134
135 // Special case the IEND chunk, which MUST appear last and libpng stops parsing once it hits
136 // such a chunk (let's do the same).
137 if (chunk_type == kPngChunkIEND) {
138 // Truncate the data to the end of this chunk. There may be garbage trailing after
139 // (b/38169876)
140 data_ = data_.substr(0, window_end_);
141 break;
142 }
143
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 } else {
145 // We want to strip this chunk. If we accumulated a window,
146 // we must return the window now.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 if (window_start_ != window_end_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 break;
149 }
150
151 // The window is empty, so we can advance past this chunk
152 // and keep looking for the next good chunk,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 window_end_ += kMinChunkHeaderSize + chunk_len;
154 window_start_ = window_end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
156 }
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 if (ConsumeWindow(buffer, len)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 return true;
160 }
161 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700162}
163
Adam Lesinski06460ef2017-03-14 18:52:13 -0700164void PngChunkFilter::BackUp(size_t count) {
Adam Lesinskicc73e992017-05-12 18:16:44 -0700165 if (HadError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 return;
167 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 window_start_ -= count;
Adam Lesinski21efb682016-09-14 17:35:43 -0700169}
170
Adam Lesinski06460ef2017-03-14 18:52:13 -0700171bool PngChunkFilter::Rewind() {
Adam Lesinskicc73e992017-05-12 18:16:44 -0700172 if (HadError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 return false;
174 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700175 window_start_ = 0;
176 window_end_ = kPngSignatureSize;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 return true;
Adam Lesinski21efb682016-09-14 17:35:43 -0700178}
179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180} // namespace aapt