blob: f9fa69e4f2f2f8747ce311118fe5637d34d1dfcf [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-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 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-Pehrson3e61d792009-06-24 09:31:28 -05009 * This code is released under the zlib-libpng license.
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050010 * For conditions of distribution and use, see copyright notice, disclaimer,
11 * 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
Guy Schalnate5a37791996-06-05 15:50:50 -050021#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060022#if defined(PNG_READ_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050023#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060024
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);
Andreas Dilger47a0c421997-05-16 02:46:07 -050035 if (png_ptr->read_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050036 (*(png_ptr->read_data_fn))(png_ptr, data, length);
37 else
38 png_error(png_ptr, "Call to NULL read function");
39}
40
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -060041#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050042/* 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 -050043 * not reading from a standard C stream, you should create a replacement
44 * read_data function and use it at run time with png_set_read_fn(), rather
45 * than changing the library.
46 */
Guy Schalnate5a37791996-06-05 15:50:50 -050047#ifndef USE_FAR_KEYWORD
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -050048void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050049png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050050{
Andreas Dilger47a0c421997-05-16 02:46:07 -050051 png_size_t check;
Guy Schalnate5a37791996-06-05 15:50:50 -050052
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050053 if (png_ptr == NULL)
54 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050055 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
56 * instead of an int, which is what fread() actually returns.
57 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050058 check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050059
Guy Schalnate5a37791996-06-05 15:50:50 -050060 if (check != length)
Guy Schalnate5a37791996-06-05 15:50:50 -050061 png_error(png_ptr, "Read Error");
Guy Schalnate5a37791996-06-05 15:50:50 -050062}
63#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050064/* This is the model-independent version. Since the standard I/O library
Guy Schalnate5a37791996-06-05 15:50:50 -050065 can't handle far buffers in the medium and small models, we have to copy
66 the data.
67*/
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060068
Guy Schalnate5a37791996-06-05 15:50:50 -050069#define NEAR_BUF_SIZE 1024
70#define MIN(a,b) (a <= b ? a : b)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060071
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -060072static void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050073png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnate5a37791996-06-05 15:50:50 -050074{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050075 png_size_t check;
Guy Schalnate5a37791996-06-05 15:50:50 -050076 png_byte *n_data;
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050077 png_FILE_p io_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050078
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050079 if (png_ptr == NULL)
80 return;
Guy Schalnate5a37791996-06-05 15:50:50 -050081 /* Check if data really is near. If so, use usual code. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060082 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -050083 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050084 if ((png_bytep)n_data == data)
Guy Schalnate5a37791996-06-05 15:50:50 -050085 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 check = fread(n_data, 1, length, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050087 }
88 else
89 {
90 png_byte buf[NEAR_BUF_SIZE];
91 png_size_t read, remaining, err;
92 check = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -050093 remaining = length;
Guy Schalnate5a37791996-06-05 15:50:50 -050094 do
95 {
96 read = MIN(NEAR_BUF_SIZE, remaining);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050097 err = fread(buf, 1, read, io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -050098 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050099 if (err != read)
Guy Schalnate5a37791996-06-05 15:50:50 -0500100 break;
101 else
102 check += err;
103 data += read;
104 remaining -= read;
105 }
106 while (remaining != 0);
107 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600108 if ((png_uint_32)check != (png_uint_32)length)
Guy Schalnate5a37791996-06-05 15:50:50 -0500109 png_error(png_ptr, "read Error");
Guy Schalnate5a37791996-06-05 15:50:50 -0500110}
111#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600112#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500113
114/* This function allows the application to supply a new input function
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500115 * for libpng if standard C streams aren't being used.
116 *
117 * This function takes as its arguments:
118 * png_ptr - pointer to a png input data structure
119 * io_ptr - pointer to user supplied structure containing info about
120 * the input functions. May be NULL.
121 * read_data_fn - pointer to a new input function that takes as its
122 * arguments a pointer to a png_struct, a pointer to
123 * a location where input data can be stored, and a 32-bit
124 * unsigned int that is the number of bytes to be read.
125 * To exit and output any fatal error messages the new write
126 * function should call png_error(png_ptr, "Error msg").
127 * May be NULL, in which case libpng's default function will
128 * be used.
129 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500130void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500131png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
132 png_rw_ptr read_data_fn)
133{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500134 if (png_ptr == NULL)
135 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500136 png_ptr->io_ptr = io_ptr;
137
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600138#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 if (read_data_fn != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500140 png_ptr->read_data_fn = read_data_fn;
141 else
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500142 png_ptr->read_data_fn = png_default_read_data;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600143#else
144 png_ptr->read_data_fn = read_data_fn;
145#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500146
147 /* It is an error to write to a read device */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500148 if (png_ptr->write_data_fn != NULL)
149 {
150 png_ptr->write_data_fn = NULL;
151 png_warning(png_ptr,
152 "It's an error to set both read_data_fn and write_data_fn in the ");
153 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500154 "same structure. Resetting write_data_fn to NULL");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500155 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500156
157#if defined(PNG_WRITE_FLUSH_SUPPORTED)
158 png_ptr->output_flush_fn = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600159#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500160}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600161#endif /* PNG_READ_SUPPORTED */