/* * Copyright (C) 2006 Apple Computer, Inc. * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. * * Portions are Copyright (C) 2001 mozilla.org * * Other contributors: * Stuart Parmenter <[email protected]> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Alternatively, the contents of this file may be used under the terms * of either the Mozilla Public License Version 1.1, found at * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html * (the "GPL"), in which case the provisions of the MPL or the GPL are * applicable instead of those above. If you wish to allow use of your * version of this file only under the terms of one of those two * licenses (the MPL or the GPL) and not to allow others to use your * version of this file under the LGPL, indicate your decision by * deletingthe provisions above and replace them with the notice and * other provisions required by the MPL or the GPL, as the case may be. * If you do not delete the provisions above, a recipient may use your * version of this file under any of the LGPL, the MPL or the GPL. */ #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/351564777): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif #include "third_party/blink/renderer/platform/image-decoders/png/png_image_reader.h" #include <memory> #include "base/numerics/checked_math.h" #include "third_party/blink/renderer/platform/image-decoders/fast_shared_buffer_reader.h" #include "third_party/blink/renderer/platform/image-decoders/png/png_image_decoder.h" #include "third_party/blink/renderer/platform/image-decoders/segment_reader.h" #include "zlib.h" namespace { inline blink::PNGImageDecoder* imageDecoder(png_structp png) { … } void PNGAPI pngHeaderAvailable(png_structp png, png_infop) { … } void PNGAPI pngRowAvailable(png_structp png, png_bytep row, png_uint_32 rowIndex, int state) { … } void PNGAPI pngFrameComplete(png_structp png, png_infop) { … } void PNGAPI pngFailed(png_structp png, png_const_charp) { … } } // namespace namespace blink { PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, wtf_size_t initial_offset) : … { … } PNGImageReader::~PNGImageReader() { … } // This method reads from the FastSharedBufferReader, starting at offset, // and returns |length| bytes in the form of a pointer to a const png_byte*. // This function is used to make it easy to access data from the reader in a // png friendly way, and pass it to libpng for decoding. // // Pre-conditions before using this: // - |reader|.size() >= |read_offset| + |length| // - |buffer|.size() >= |length| // - |length| <= |kPngReadBufferSize| // // The reason for the last two precondition is that currently the png signature // plus IHDR chunk (8B + 25B = 33B) is the largest chunk that is read using this // method. If the data is not consecutive, it is stored in |buffer|, which must // have the size of (at least) |length|, but there's no need for it to be larger // than |kPngReadBufferSize|. static constexpr wtf_size_t kPngReadBufferSize = …; const png_byte* ReadAsConstPngBytep(const FastSharedBufferReader& reader, wtf_size_t read_offset, wtf_size_t length, char* buffer) { … } bool PNGImageReader::ShouldDecodeWithNewPNG(wtf_size_t index) const { … } // Return false on a fatal error. bool PNGImageReader::Decode(SegmentReader& data, wtf_size_t index) { … } void PNGImageReader::StartFrameDecoding(const FastSharedBufferReader& reader, wtf_size_t index) { … } // Determine if the bytes 4 to 7 of |chunk| indicate that it is a |tag| chunk. // - The length of |chunk| must be >= 8 // - The length of |tag| must be = 4 static inline bool IsChunk(const png_byte* chunk, const char* tag) { … } bool PNGImageReader::ProgressivelyDecodeFirstFrame( const FastSharedBufferReader& reader) { … } void PNGImageReader::ProcessFdatChunkAsIdat(png_uint_32 fdat_length) { … } void PNGImageReader::DecodeFrame(const FastSharedBufferReader& reader, wtf_size_t index) { … } // Compute the CRC and compare to the stored value. static bool CheckCrc(const FastSharedBufferReader& reader, wtf_size_t chunk_start, wtf_size_t chunk_length) { … } bool PNGImageReader::CheckSequenceNumber(const png_byte* position) { … } // Return false if there was a fatal error; true otherwise. bool PNGImageReader::Parse(SegmentReader& data, ParseQuery query) { … } // If |length| == 0, read until the stream ends. Return number of bytes // processed. wtf_size_t PNGImageReader::ProcessData(const FastSharedBufferReader& reader, wtf_size_t offset, wtf_size_t length) { … } // Process up to the start of the IDAT with libpng. // Return false for a fatal error. True otherwise. bool PNGImageReader::ParseSize(const FastSharedBufferReader& reader) { … } void PNGImageReader::ClearDecodeState(wtf_size_t index) { … } const PNGImageReader::FrameInfo& PNGImageReader::GetFrameInfo( wtf_size_t index) const { … } // Extract the fcTL frame control info and store it in new_frame_. The length // check on the fcTL data has been done by the calling code. bool PNGImageReader::ParseFrameInfo(const png_byte* data) { … } } // namespace blink