image/decoders/nsPNGDecoder.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef nsPNGDecoder_h__
michael@0 8 #define nsPNGDecoder_h__
michael@0 9
michael@0 10 #include "Decoder.h"
michael@0 11
michael@0 12 #include "gfxTypes.h"
michael@0 13
michael@0 14 #include "nsCOMPtr.h"
michael@0 15
michael@0 16 #include "png.h"
michael@0 17
michael@0 18 #include "qcms.h"
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace image {
michael@0 22 class RasterImage;
michael@0 23
michael@0 24 class nsPNGDecoder : public Decoder
michael@0 25 {
michael@0 26 public:
michael@0 27 nsPNGDecoder(RasterImage &aImage);
michael@0 28 virtual ~nsPNGDecoder();
michael@0 29
michael@0 30 virtual void InitInternal();
michael@0 31 virtual void WriteInternal(const char* aBuffer, uint32_t aCount, DecodeStrategy aStrategy);
michael@0 32 virtual Telemetry::ID SpeedHistogram();
michael@0 33
michael@0 34 void CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
michael@0 35 int32_t width, int32_t height,
michael@0 36 gfxImageFormat format);
michael@0 37 void EndImageFrame();
michael@0 38
michael@0 39 // Check if PNG is valid ICO (32bpp RGBA)
michael@0 40 // http://blogs.msdn.com/b/oldnewthing/archive/2010/10/22/10079192.aspx
michael@0 41 bool IsValidICO() const
michael@0 42 {
michael@0 43 // If there are errors in the call to png_get_IHDR, the error_callback in
michael@0 44 // nsPNGDecoder.cpp is called. In this error callback we do a longjmp, so
michael@0 45 // we need to save the jump buffer here. Oterwise we'll end up without a
michael@0 46 // proper callstack.
michael@0 47 if (setjmp(png_jmpbuf(mPNG))) {
michael@0 48 // We got here from a longjmp call indirectly from png_get_IHDR
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 png_uint_32
michael@0 53 png_width, // Unused
michael@0 54 png_height; // Unused
michael@0 55
michael@0 56 int png_bit_depth,
michael@0 57 png_color_type;
michael@0 58
michael@0 59 if (png_get_IHDR(mPNG, mInfo, &png_width, &png_height, &png_bit_depth,
michael@0 60 &png_color_type, nullptr, nullptr, nullptr)) {
michael@0 61
michael@0 62 return ((png_color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
michael@0 63 png_color_type == PNG_COLOR_TYPE_RGB) &&
michael@0 64 png_bit_depth == 8);
michael@0 65 } else {
michael@0 66 return false;
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 public:
michael@0 71 png_structp mPNG;
michael@0 72 png_infop mInfo;
michael@0 73 nsIntRect mFrameRect;
michael@0 74 uint8_t *mCMSLine;
michael@0 75 uint8_t *interlacebuf;
michael@0 76 qcms_profile *mInProfile;
michael@0 77 qcms_transform *mTransform;
michael@0 78
michael@0 79 gfxImageFormat format;
michael@0 80
michael@0 81 // For size decodes
michael@0 82 uint8_t mSizeBytes[8]; // Space for width and height, both 4 bytes
michael@0 83 uint32_t mHeaderBytesRead;
michael@0 84
michael@0 85 // whether CMS or premultiplied alpha are forced off
michael@0 86 uint32_t mCMSMode;
michael@0 87
michael@0 88 uint8_t mChannels;
michael@0 89 bool mFrameHasNoAlpha;
michael@0 90 bool mFrameIsHidden;
michael@0 91 bool mDisablePremultipliedAlpha;
michael@0 92
michael@0 93 struct AnimFrameInfo
michael@0 94 {
michael@0 95 AnimFrameInfo();
michael@0 96 #ifdef PNG_APNG_SUPPORTED
michael@0 97 AnimFrameInfo(png_structp aPNG, png_infop aInfo);
michael@0 98 #endif
michael@0 99
michael@0 100 FrameBlender::FrameDisposalMethod mDispose;
michael@0 101 FrameBlender::FrameBlendMethod mBlend;
michael@0 102 int32_t mTimeout;
michael@0 103 };
michael@0 104
michael@0 105 AnimFrameInfo mAnimInfo;
michael@0 106
michael@0 107 // The number of frames we've finished.
michael@0 108 uint32_t mNumFrames;
michael@0 109
michael@0 110 /*
michael@0 111 * libpng callbacks
michael@0 112 *
michael@0 113 * We put these in the class so that they can access protected members.
michael@0 114 */
michael@0 115 static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr);
michael@0 116 static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row,
michael@0 117 png_uint_32 row_num, int pass);
michael@0 118 #ifdef PNG_APNG_SUPPORTED
michael@0 119 static void PNGAPI frame_info_callback(png_structp png_ptr,
michael@0 120 png_uint_32 frame_num);
michael@0 121 #endif
michael@0 122 static void PNGAPI end_callback(png_structp png_ptr, png_infop info_ptr);
michael@0 123 static void PNGAPI error_callback(png_structp png_ptr,
michael@0 124 png_const_charp error_msg);
michael@0 125 static void PNGAPI warning_callback(png_structp png_ptr,
michael@0 126 png_const_charp warning_msg);
michael@0 127
michael@0 128 // This is defined in the PNG spec as an invariant. We use it to
michael@0 129 // do manual validation without libpng.
michael@0 130 static const uint8_t pngSignatureBytes[];
michael@0 131 };
michael@0 132
michael@0 133 } // namespace image
michael@0 134 } // namespace mozilla
michael@0 135
michael@0 136 #endif // nsPNGDecoder_h__

mercurial