blob: 7946358101bb894710530a871ae4879a1307d232 [file] [log] [blame]
Guy Schalnate5a37791996-06-05 15:50:50 -05001
2/* pngrio.c - functions for data input
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Cosmin Trutaa8738932018-07-28 18:47:21 -04004 * Copyright (c) 2018 Cosmin Truta
Cosmin Truta46aedd82018-07-15 23:58:00 -04005 * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson
Cosmin Trutaa8738932018-07-28 18:47:21 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050013 * This file provides a location for all input. Users who need
14 * special handling are expected to write a function that has the same
15 * arguments as this and performs a similar function, but that possibly
16 * has a different input method. Note that you shouldn't change this
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060017 * function, but rather write a replacement function and then make
18 * libpng use it at run time with png_set_read_fn(...).
19 */
Guy Schalnate5a37791996-06-05 15:50:50 -050020
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050021#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060022
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060023#ifdef PNG_READ_SUPPORTED
24
Guy Schalnate5a37791996-06-05 15:50:50 -050025/* Read the data from whatever input you are using. The default routine
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050026 * reads from a file pointer. Note that this routine sometimes gets called
27 * with very small lengths, so you should implement some kind of simple
28 * buffering if you are using unbuffered reads. This should never be asked
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -050029 * to read more than 64K on a 16-bit machine.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050030 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050031void /* PRIVATE */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -040032png_read_data(png_structrp png_ptr, png_bytep data, size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050033{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050034 png_debug1(4, "reading %d bytes", (int)length);
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050035
Andreas Dilger47a0c421997-05-16 02:46:07 -050036 if (png_ptr->read_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050037 (*(png_ptr->read_data_fn))(png_ptr, data, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050038
Guy Schalnate5a37791996-06-05 15:50:50 -050039 else
40 png_error(png_ptr, "Call to NULL read function");
41}
42
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050043#ifdef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050044/* This is the function that does the actual reading of data. If you are
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050045 * not reading from a standard C stream, you should create a replacement
46 * read_data function and use it at run time with png_set_read_fn(), rather
47 * than changing the library.
48 */
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -060049void PNGCBAPI
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -040050png_default_read_data(png_structp png_ptr, png_bytep data, size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050051{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -040052 size_t check;
Guy Schalnate5a37791996-06-05 15:50:50 -050053
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050054 if (png_ptr == NULL)
55 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050056
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -040057 /* fread() returns 0 on error, so it is OK to store this in a size_t
Andreas Dilger47a0c421997-05-16 02:46:07 -050058 * instead of an int, which is what fread() actually returns.
59 */
John Bowlerbaeb6d12011-11-26 18:21:02 -060060 check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));
Andreas Dilger47a0c421997-05-16 02:46:07 -050061
Guy Schalnate5a37791996-06-05 15:50:50 -050062 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -050063 png_error(png_ptr, "Read Error");
Guy Schalnate5a37791996-06-05 15:50:50 -050064}
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060065#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050066
67/* This function allows the application to supply a new input function
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050068 * for libpng if standard C streams aren't being used.
69 *
70 * This function takes as its arguments:
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -050071 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050072 * png_ptr - pointer to a png input data structure
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -050073 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050074 * io_ptr - pointer to user supplied structure containing info about
75 * the input functions. May be NULL.
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -050076 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050077 * read_data_fn - pointer to a new input function that takes as its
78 * arguments a pointer to a png_struct, a pointer to
79 * a location where input data can be stored, and a 32-bit
80 * unsigned int that is the number of bytes to be read.
81 * To exit and output any fatal error messages the new write
82 * function should call png_error(png_ptr, "Error msg").
83 * May be NULL, in which case libpng's default function will
84 * be used.
85 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060087png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -050088 png_rw_ptr read_data_fn)
Guy Schalnate5a37791996-06-05 15:50:50 -050089{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050090 if (png_ptr == NULL)
91 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050092
Guy Schalnate5a37791996-06-05 15:50:50 -050093 png_ptr->io_ptr = io_ptr;
94
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050095#ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -050096 if (read_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050097 png_ptr->read_data_fn = read_data_fn;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050098
Guy Schalnate5a37791996-06-05 15:50:50 -050099 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500100 png_ptr->read_data_fn = png_default_read_data;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600101#else
102 png_ptr->read_data_fn = read_data_fn;
103#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500104
Glenn Randers-Pehrson88ecac62013-12-28 12:52:59 -0600105#ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500106 /* It is an error to write to a read device */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500107 if (png_ptr->write_data_fn != NULL)
108 {
109 png_ptr->write_data_fn = NULL;
110 png_warning(png_ptr,
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500111 "Can't set both read_data_fn and write_data_fn in the"
112 " same structure");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500113 }
Glenn Randers-Pehrson88ecac62013-12-28 12:52:59 -0600114#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500115
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500116#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500117 png_ptr->output_flush_fn = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600118#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500119}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600120#endif /* READ */