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