blob: e9c381c5ba41f79eaaae9770cf7f0c7e222d2186 [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 *
Glenn Randers-Pehrsonf5ea1b72011-01-06 06:42:51 -06004 * Last changed in libpng 1.5.0 [January 6, 2011]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 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
29 * to read more then 64K on a 16 bit machine.
30 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050031void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050032png_read_data(png_structp png_ptr, png_bytep data, png_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-Pehrson6f6a91a2010-03-06 13:54:59 -060049# ifndef USE_FAR_KEYWORD
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -060050void PNGCBAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050051png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050052{
Andreas Dilger47a0c421997-05-16 02:46:07 -050053 png_size_t check;
Guy Schalnate5a37791996-06-05 15:50:50 -050054
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050055 if (png_ptr == NULL)
56 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050057
Andreas Dilger47a0c421997-05-16 02:46:07 -050058 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
59 * instead of an int, which is what fread() actually returns.
60 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050061 check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050062
Guy Schalnate5a37791996-06-05 15:50:50 -050063 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -050064 png_error(png_ptr, "Read Error");
Guy Schalnate5a37791996-06-05 15:50:50 -050065}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060066# else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050067/* This is the model-independent version. Since the standard I/O library
Guy Schalnate5a37791996-06-05 15:50:50 -050068 can't handle far buffers in the medium and small models, we have to copy
69 the data.
70*/
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060071
Guy Schalnate5a37791996-06-05 15:50:50 -050072#define NEAR_BUF_SIZE 1024
73#define MIN(a,b) (a <= b ? a : b)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060074
Glenn Randers-Pehrsoneae8e362010-03-12 17:36:53 -060075static void PNGCBAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050076png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050077{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050078 png_size_t check;
Guy Schalnate5a37791996-06-05 15:50:50 -050079 png_byte *n_data;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050080 png_FILE_p io_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050081
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050082 if (png_ptr == NULL)
83 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050084
Guy Schalnate5a37791996-06-05 15:50:50 -050085 /* Check if data really is near. If so, use usual code. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060086 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050087 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050088
Guy Schalnate5a37791996-06-05 15:50:50 -050089 if ((png_bytep)n_data == data)
Guy Schalnate5a37791996-06-05 15:50:50 -050090 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 check = fread(n_data, 1, length, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050092 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050093
Guy Schalnate5a37791996-06-05 15:50:50 -050094 else
95 {
96 png_byte buf[NEAR_BUF_SIZE];
97 png_size_t read, remaining, err;
98 check = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -050099 remaining = length;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500100
Guy Schalnate5a37791996-06-05 15:50:50 -0500101 do
102 {
103 read = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500104 err = fread(buf, 1, read, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500105 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500106
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500107 if (err != read)
Guy Schalnate5a37791996-06-05 15:50:50 -0500108 break;
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500109
Guy Schalnate5a37791996-06-05 15:50:50 -0500110 else
111 check += err;
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500112
Guy Schalnate5a37791996-06-05 15:50:50 -0500113 data += read;
114 remaining -= read;
115 }
116 while (remaining != 0);
117 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500118
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600119 if ((png_uint_32)check != (png_uint_32)length)
Guy Schalnate5a37791996-06-05 15:50:50 -0500120 png_error(png_ptr, "read Error");
Guy Schalnate5a37791996-06-05 15:50:50 -0500121}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600122# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600123#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500124
125/* This function allows the application to supply a new input function
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500126 * for libpng if standard C streams aren't being used.
127 *
128 * This function takes as its arguments:
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500129 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130 * png_ptr - pointer to a png input data structure
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500131 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500132 * io_ptr - pointer to user supplied structure containing info about
133 * the input functions. May be NULL.
Glenn Randers-Pehrson33893092010-10-23 13:20:18 -0500134 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500135 * read_data_fn - pointer to a new input function that takes as its
136 * arguments a pointer to a png_struct, a pointer to
137 * a location where input data can be stored, and a 32-bit
138 * unsigned int that is the number of bytes to be read.
139 * To exit and output any fatal error messages the new write
140 * function should call png_error(png_ptr, "Error msg").
141 * May be NULL, in which case libpng's default function will
142 * be used.
143 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500144void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500145png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
146 png_rw_ptr read_data_fn)
147{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500148 if (png_ptr == NULL)
149 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500150
Guy Schalnate5a37791996-06-05 15:50:50 -0500151 png_ptr->io_ptr = io_ptr;
152
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500153#ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500154 if (read_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500155 png_ptr->read_data_fn = read_data_fn;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500156
Guy Schalnate5a37791996-06-05 15:50:50 -0500157 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500158 png_ptr->read_data_fn = png_default_read_data;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600159#else
160 png_ptr->read_data_fn = read_data_fn;
161#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500162
163 /* It is an error to write to a read device */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500164 if (png_ptr->write_data_fn != NULL)
165 {
166 png_ptr->write_data_fn = NULL;
167 png_warning(png_ptr,
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500168 "Can't set both read_data_fn and write_data_fn in the"
169 " same structure");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500170 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500171
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500172#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500173 png_ptr->output_flush_fn = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500175}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600176#endif /* PNG_READ_SUPPORTED */