media/libpng/pngrio.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1
michael@0 2 /* pngrio.c - functions for data input
michael@0 3 *
michael@0 4 * Last changed in libpng 1.6.9 [February 6, 2014]
michael@0 5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
michael@0 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
michael@0 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
michael@0 8 *
michael@0 9 * This code is released under the libpng license.
michael@0 10 * For conditions of distribution and use, see the disclaimer
michael@0 11 * and license in png.h
michael@0 12 *
michael@0 13 * This file provides a location for all input. Users who need
michael@0 14 * special handling are expected to write a function that has the same
michael@0 15 * arguments as this and performs a similar function, but that possibly
michael@0 16 * has a different input method. Note that you shouldn't change this
michael@0 17 * function, but rather write a replacement function and then make
michael@0 18 * libpng use it at run time with png_set_read_fn(...).
michael@0 19 */
michael@0 20
michael@0 21 #include "pngpriv.h"
michael@0 22
michael@0 23 #ifdef PNG_READ_SUPPORTED
michael@0 24
michael@0 25 /* Read the data from whatever input you are using. The default routine
michael@0 26 * reads from a file pointer. Note that this routine sometimes gets called
michael@0 27 * with very small lengths, so you should implement some kind of simple
michael@0 28 * buffering if you are using unbuffered reads. This should never be asked
michael@0 29 * to read more then 64K on a 16 bit machine.
michael@0 30 */
michael@0 31 void /* PRIVATE */
michael@0 32 png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length)
michael@0 33 {
michael@0 34 png_debug1(4, "reading %d bytes", (int)length);
michael@0 35
michael@0 36 if (png_ptr->read_data_fn != NULL)
michael@0 37 (*(png_ptr->read_data_fn))(png_ptr, data, length);
michael@0 38
michael@0 39 else
michael@0 40 png_error(png_ptr, "Call to NULL read function");
michael@0 41 }
michael@0 42
michael@0 43 #ifdef PNG_STDIO_SUPPORTED
michael@0 44 /* This is the function that does the actual reading of data. If you are
michael@0 45 * not reading from a standard C stream, you should create a replacement
michael@0 46 * read_data function and use it at run time with png_set_read_fn(), rather
michael@0 47 * than changing the library.
michael@0 48 */
michael@0 49 void PNGCBAPI
michael@0 50 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
michael@0 51 {
michael@0 52 png_size_t check;
michael@0 53
michael@0 54 if (png_ptr == NULL)
michael@0 55 return;
michael@0 56
michael@0 57 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
michael@0 58 * instead of an int, which is what fread() actually returns.
michael@0 59 */
michael@0 60 check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));
michael@0 61
michael@0 62 if (check != length)
michael@0 63 png_error(png_ptr, "Read Error");
michael@0 64 }
michael@0 65 #endif
michael@0 66
michael@0 67 /* This function allows the application to supply a new input function
michael@0 68 * for libpng if standard C streams aren't being used.
michael@0 69 *
michael@0 70 * This function takes as its arguments:
michael@0 71 *
michael@0 72 * png_ptr - pointer to a png input data structure
michael@0 73 *
michael@0 74 * io_ptr - pointer to user supplied structure containing info about
michael@0 75 * the input functions. May be NULL.
michael@0 76 *
michael@0 77 * read_data_fn - pointer to a new input function that takes as its
michael@0 78 * arguments a pointer to a png_struct, a pointer to
michael@0 79 * a location where input data can be stored, and a 32-bit
michael@0 80 * unsigned int that is the number of bytes to be read.
michael@0 81 * To exit and output any fatal error messages the new write
michael@0 82 * function should call png_error(png_ptr, "Error msg").
michael@0 83 * May be NULL, in which case libpng's default function will
michael@0 84 * be used.
michael@0 85 */
michael@0 86 void PNGAPI
michael@0 87 png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr,
michael@0 88 png_rw_ptr read_data_fn)
michael@0 89 {
michael@0 90 if (png_ptr == NULL)
michael@0 91 return;
michael@0 92
michael@0 93 png_ptr->io_ptr = io_ptr;
michael@0 94
michael@0 95 #ifdef PNG_STDIO_SUPPORTED
michael@0 96 if (read_data_fn != NULL)
michael@0 97 png_ptr->read_data_fn = read_data_fn;
michael@0 98
michael@0 99 else
michael@0 100 png_ptr->read_data_fn = png_default_read_data;
michael@0 101 #else
michael@0 102 png_ptr->read_data_fn = read_data_fn;
michael@0 103 #endif
michael@0 104
michael@0 105 #ifdef PNG_WRITE_SUPPORTED
michael@0 106 /* It is an error to write to a read device */
michael@0 107 if (png_ptr->write_data_fn != NULL)
michael@0 108 {
michael@0 109 png_ptr->write_data_fn = NULL;
michael@0 110 png_warning(png_ptr,
michael@0 111 "Can't set both read_data_fn and write_data_fn in the"
michael@0 112 " same structure");
michael@0 113 }
michael@0 114 #endif
michael@0 115
michael@0 116 #ifdef PNG_WRITE_FLUSH_SUPPORTED
michael@0 117 png_ptr->output_flush_fn = NULL;
michael@0 118 #endif
michael@0 119 }
michael@0 120 #endif /* PNG_READ_SUPPORTED */

mercurial