/* pngrio.c - functions for data input1*2* Copyright (c) 2018-2025 Cosmin Truta3* Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson4* Copyright (c) 1996-1997 Andreas Dilger5* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.6*7* This code is released under the libpng license.8* For conditions of distribution and use, see the disclaimer9* and license in png.h10*11* This file provides a location for all input. Users who need12* special handling are expected to write a function that has the same13* arguments as this and performs a similar function, but that possibly14* has a different input method. Note that you shouldn't change this15* function, but rather write a replacement function and then make16* libpng use it at run time with png_set_read_fn(...).17*/1819#include "pngpriv.h"2021#ifdef PNG_READ_SUPPORTED2223/* Read the data from whatever input you are using. The default routine24* reads from a file pointer. Note that this routine sometimes gets called25* with very small lengths, so you should implement some kind of simple26* buffering if you are using unbuffered reads. This should never be asked27* to read more than 64K on a 16-bit machine.28*/29void /* PRIVATE */30png_read_data(png_structrp png_ptr, png_bytep data, size_t length)31{32png_debug1(4, "reading %d bytes", (int)length);3334if (png_ptr->read_data_fn != NULL)35(*(png_ptr->read_data_fn))(png_ptr, data, length);3637else38png_error(png_ptr, "Call to NULL read function");39}4041#ifdef PNG_STDIO_SUPPORTED42/* This is the function that does the actual reading of data. If you are43* not reading from a standard C stream, you should create a replacement44* read_data function and use it at run time with png_set_read_fn(), rather45* than changing the library.46*/47void PNGCBAPI48png_default_read_data(png_structp png_ptr, png_bytep data, size_t length)49{50size_t check;5152if (png_ptr == NULL)53return;5455/* fread() returns 0 on error, so it is OK to store this in a size_t56* instead of an int, which is what fread() actually returns.57*/58check = fread(data, 1, length, png_voidcast(FILE *, png_ptr->io_ptr));5960if (check != length)61png_error(png_ptr, "Read Error");62}63#endif6465/* This function allows the application to supply a new input function66* for libpng if standard C streams aren't being used.67*68* This function takes as its arguments:69*70* png_ptr - pointer to a png input data structure71*72* io_ptr - pointer to user supplied structure containing info about73* the input functions. May be NULL.74*75* read_data_fn - pointer to a new input function that takes as its76* arguments a pointer to a png_struct, a pointer to77* a location where input data can be stored, and a 32-bit78* unsigned int that is the number of bytes to be read.79* To exit and output any fatal error messages the new write80* function should call png_error(png_ptr, "Error msg").81* May be NULL, in which case libpng's default function will82* be used.83*/84void PNGAPI85png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr,86png_rw_ptr read_data_fn)87{88if (png_ptr == NULL)89return;9091png_ptr->io_ptr = io_ptr;9293#ifdef PNG_STDIO_SUPPORTED94if (read_data_fn != NULL)95png_ptr->read_data_fn = read_data_fn;9697else98png_ptr->read_data_fn = png_default_read_data;99#else100png_ptr->read_data_fn = read_data_fn;101#endif102103#ifdef PNG_WRITE_SUPPORTED104/* It is an error to write to a read device */105if (png_ptr->write_data_fn != NULL)106{107png_ptr->write_data_fn = NULL;108png_warning(png_ptr,109"Can't set both read_data_fn and write_data_fn in the"110" same structure");111}112#endif113114#ifdef PNG_WRITE_FLUSH_SUPPORTED115png_ptr->output_flush_fn = NULL;116#endif117}118#endif /* READ */119120121