michael@0: michael@0: /* pngrio.c - functions for data input michael@0: * michael@0: * Last changed in libpng 1.6.9 [February 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * This file provides a location for all input. Users who need michael@0: * special handling are expected to write a function that has the same michael@0: * arguments as this and performs a similar function, but that possibly michael@0: * has a different input method. Note that you shouldn't change this michael@0: * function, but rather write a replacement function and then make michael@0: * libpng use it at run time with png_set_read_fn(...). michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: michael@0: #ifdef PNG_READ_SUPPORTED michael@0: michael@0: /* Read the data from whatever input you are using. The default routine michael@0: * reads from a file pointer. Note that this routine sometimes gets called michael@0: * with very small lengths, so you should implement some kind of simple michael@0: * buffering if you are using unbuffered reads. This should never be asked michael@0: * to read more then 64K on a 16 bit machine. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length) michael@0: { michael@0: png_debug1(4, "reading %d bytes", (int)length); michael@0: michael@0: if (png_ptr->read_data_fn != NULL) michael@0: (*(png_ptr->read_data_fn))(png_ptr, data, length); michael@0: michael@0: else michael@0: png_error(png_ptr, "Call to NULL read function"); michael@0: } michael@0: michael@0: #ifdef PNG_STDIO_SUPPORTED michael@0: /* This is the function that does the actual reading of data. If you are michael@0: * not reading from a standard C stream, you should create a replacement michael@0: * read_data function and use it at run time with png_set_read_fn(), rather michael@0: * than changing the library. michael@0: */ michael@0: void PNGCBAPI michael@0: png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) michael@0: { michael@0: png_size_t check; michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* fread() returns 0 on error, so it is OK to store this in a png_size_t michael@0: * instead of an int, which is what fread() actually returns. michael@0: */ michael@0: check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr)); michael@0: michael@0: if (check != length) michael@0: png_error(png_ptr, "Read Error"); michael@0: } michael@0: #endif michael@0: michael@0: /* This function allows the application to supply a new input function michael@0: * for libpng if standard C streams aren't being used. michael@0: * michael@0: * This function takes as its arguments: michael@0: * michael@0: * png_ptr - pointer to a png input data structure michael@0: * michael@0: * io_ptr - pointer to user supplied structure containing info about michael@0: * the input functions. May be NULL. michael@0: * michael@0: * read_data_fn - pointer to a new input function that takes as its michael@0: * arguments a pointer to a png_struct, a pointer to michael@0: * a location where input data can be stored, and a 32-bit michael@0: * unsigned int that is the number of bytes to be read. michael@0: * To exit and output any fatal error messages the new write michael@0: * function should call png_error(png_ptr, "Error msg"). michael@0: * May be NULL, in which case libpng's default function will michael@0: * be used. michael@0: */ michael@0: void PNGAPI michael@0: png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr, michael@0: png_rw_ptr read_data_fn) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->io_ptr = io_ptr; michael@0: michael@0: #ifdef PNG_STDIO_SUPPORTED michael@0: if (read_data_fn != NULL) michael@0: png_ptr->read_data_fn = read_data_fn; michael@0: michael@0: else michael@0: png_ptr->read_data_fn = png_default_read_data; michael@0: #else michael@0: png_ptr->read_data_fn = read_data_fn; michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_SUPPORTED michael@0: /* It is an error to write to a read device */ michael@0: if (png_ptr->write_data_fn != NULL) michael@0: { michael@0: png_ptr->write_data_fn = NULL; michael@0: png_warning(png_ptr, michael@0: "Can't set both read_data_fn and write_data_fn in the" michael@0: " same structure"); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_FLUSH_SUPPORTED michael@0: png_ptr->output_flush_fn = NULL; michael@0: #endif michael@0: } michael@0: #endif /* PNG_READ_SUPPORTED */