Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 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 "compile/Png.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 20 | #include "androidfw/StringPiece.h" |
| 21 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 22 | #include "io/Io.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 23 | |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 24 | using ::android::StringPiece; |
| 25 | using ::android::base::StringPrintf; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | static 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. |
| 33 | constexpr uint32_t u32(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | return (((uint32_t)a) << 24) | (((uint32_t)b) << 16) | (((uint32_t)c) << 8) | |
| 35 | ((uint32_t)d); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // Whitelist of PNG chunk types that we want to keep in the resulting PNG. |
| 39 | enum PngChunkTypes { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | static uint32_t Peek32LE(const char* data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | static bool IsPngChunkWhitelisted(uint32_t type) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | switch (type) { |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 61 | case kPngChunkIHDR: |
| 62 | case kPngChunkIDAT: |
| 63 | case kPngChunkIEND: |
| 64 | case kPngChunkPLTE: |
| 65 | case kPngChunktRNS: |
| 66 | case kPngChunksRGB: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | return true; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 68 | default: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | return false; |
| 70 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | PngChunkFilter::PngChunkFilter(const StringPiece& data) : data_(data) { |
| 74 | if (util::StartsWith(data_, kPngSignature)) { |
| 75 | window_start_ = 0; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 76 | window_end_ = kPngSignatureSize; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | } else { |
Adam Lesinski | 60d9c2f | 2017-05-17 16:07:45 -0700 | [diff] [blame] | 78 | error_msg_ = "file does not start with PNG signature"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 82 | bool PngChunkFilter::ConsumeWindow(const void** buffer, size_t* len) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | if (window_start_ != window_end_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | // We have bytes to give from our window. |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 85 | const size_t bytes_read = window_end_ - window_start_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | *buffer = data_.data() + window_start_; |
| 87 | *len = bytes_read; |
| 88 | window_start_ = window_end_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 94 | bool PngChunkFilter::Next(const void** buffer, size_t* len) { |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 95 | if (HadError()) { |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 96 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // In case BackUp was called, we must consume the window. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | if (ConsumeWindow(buffer, len)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
| 104 | // Advance the window as far as possible (until we meet a chunk that |
| 105 | // we want to strip). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | while (window_end_ < data_.size()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | // 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 Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 111 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Verify the chunk length. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | const uint32_t chunk_len = Peek32LE(data_.data() + window_end_); |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 119 | if ((size_t)chunk_len > data_.size() - window_end_ - kMinChunkHeaderSize) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | // Overflow. |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 121 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // Do we strip this chunk? |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 130 | const uint32_t chunk_type = Peek32LE(data_.data() + window_end_ + sizeof(uint32_t)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | if (IsPngChunkWhitelisted(chunk_type)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | // Advance the window to include this chunk. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | window_end_ += kMinChunkHeaderSize + chunk_len; |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 134 | |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | } else { |
| 145 | // We want to strip this chunk. If we accumulated a window, |
| 146 | // we must return the window now. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | if (window_start_ != window_end_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 153 | window_end_ += kMinChunkHeaderSize + chunk_len; |
| 154 | window_start_ = window_end_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | if (ConsumeWindow(buffer, len)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 164 | void PngChunkFilter::BackUp(size_t count) { |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 165 | if (HadError()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | return; |
| 167 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | window_start_ -= count; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 171 | bool PngChunkFilter::Rewind() { |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 172 | if (HadError()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 175 | window_start_ = 0; |
| 176 | window_end_ = kPngSignatureSize; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | return true; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } // namespace aapt |