blob: 6aea3cf1de8fe52bacc552569d1b83ca8226e1c2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25/* pngrio.c - functions for data input
26 *
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file and, per its terms, should not be removed:
31 *
32 * Last changed in libpng 1.2.13 November 13, 2006
33 * For conditions of distribution and use, see copyright notice in png.h
34 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
35 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
36 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
37 *
38 * This file provides a location for all input. Users who need
39 * special handling are expected to write a function that has the same
40 * arguments as this and performs a similar function, but that possibly
41 * has a different input method. Note that you shouldn't change this
42 * function, but rather write a replacement function and then make
43 * libpng use it at run time with png_set_read_fn(...).
44 */
45
46#define PNG_INTERNAL
47#include "png.h"
48
49#if defined(PNG_READ_SUPPORTED)
50
51/* Read the data from whatever input you are using. The default routine
52 reads from a file pointer. Note that this routine sometimes gets called
53 with very small lengths, so you should implement some kind of simple
54 buffering if you are using unbuffered reads. This should never be asked
55 to read more then 64K on a 16 bit machine. */
56void /* PRIVATE */
57png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
58{
59 png_debug1(4,"reading %d bytes\n", (int)length);
60 if (png_ptr->read_data_fn != NULL)
61 (*(png_ptr->read_data_fn))(png_ptr, data, length);
62 else
63 png_error(png_ptr, "Call to NULL read function");
64}
65
66#if !defined(PNG_NO_STDIO)
67/* This is the function that does the actual reading of data. If you are
68 not reading from a standard C stream, you should create a replacement
69 read_data function and use it at run time with png_set_read_fn(), rather
70 than changing the library. */
71#ifndef USE_FAR_KEYWORD
72void PNGAPI
73png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
74{
75 png_size_t check;
76
77 if(png_ptr == NULL) return;
78 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
79 * instead of an int, which is what fread() actually returns.
80 */
81#if defined(_WIN32_WCE)
82 if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
83 check = 0;
84#else
85 check = (png_size_t)fread(data, (png_size_t)1, length,
86 (png_FILE_p)png_ptr->io_ptr);
87#endif
88
89 if (check != length)
90 png_error(png_ptr, "Read Error");
91}
92#else
93/* this is the model-independent version. Since the standard I/O library
94 can't handle far buffers in the medium and small models, we have to copy
95 the data.
96*/
97
98#define NEAR_BUF_SIZE 1024
99#define MIN(a,b) (a <= b ? a : b)
100
101static void PNGAPI
102png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
103{
104 int check;
105 png_byte *n_data;
106 png_FILE_p io_ptr;
107
108 if(png_ptr == NULL) return;
109 /* Check if data really is near. If so, use usual code. */
110 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
111 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
112 if ((png_bytep)n_data == data)
113 {
114#if defined(_WIN32_WCE)
115 if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
116 check = 0;
117#else
118 check = fread(n_data, 1, length, io_ptr);
119#endif
120 }
121 else
122 {
123 png_byte buf[NEAR_BUF_SIZE];
124 png_size_t read, remaining, err;
125 check = 0;
126 remaining = length;
127 do
128 {
129 read = MIN(NEAR_BUF_SIZE, remaining);
130#if defined(_WIN32_WCE)
131 if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
132 err = 0;
133#else
134 err = fread(buf, (png_size_t)1, read, io_ptr);
135#endif
136 png_memcpy(data, buf, read); /* copy far buffer to near buffer */
137 if(err != read)
138 break;
139 else
140 check += err;
141 data += read;
142 remaining -= read;
143 }
144 while (remaining != 0);
145 }
146 if ((png_uint_32)check != (png_uint_32)length)
147 png_error(png_ptr, "read Error");
148}
149#endif
150#endif
151
152/* This function allows the application to supply a new input function
153 for libpng if standard C streams aren't being used.
154
155 This function takes as its arguments:
156 png_ptr - pointer to a png input data structure
157 io_ptr - pointer to user supplied structure containing info about
158 the input functions. May be NULL.
159 read_data_fn - pointer to a new input function that takes as its
160 arguments a pointer to a png_struct, a pointer to
161 a location where input data can be stored, and a 32-bit
162 unsigned int that is the number of bytes to be read.
163 To exit and output any fatal error messages the new write
164 function should call png_error(png_ptr, "Error msg"). */
165void PNGAPI
166png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
167 png_rw_ptr read_data_fn)
168{
169 if(png_ptr == NULL) return;
170 png_ptr->io_ptr = io_ptr;
171
172#if !defined(PNG_NO_STDIO)
173 if (read_data_fn != NULL)
174 png_ptr->read_data_fn = read_data_fn;
175 else
176 png_ptr->read_data_fn = png_default_read_data;
177#else
178 png_ptr->read_data_fn = read_data_fn;
179#endif
180
181 /* It is an error to write to a read device */
182 if (png_ptr->write_data_fn != NULL)
183 {
184 png_ptr->write_data_fn = NULL;
185 png_warning(png_ptr,
186 "It's an error to set both read_data_fn and write_data_fn in the ");
187 png_warning(png_ptr,
188 "same structure. Resetting write_data_fn to NULL.");
189 }
190
191#if defined(PNG_WRITE_FLUSH_SUPPORTED)
192 png_ptr->output_flush_fn = NULL;
193#endif
194}
195#endif /* PNG_READ_SUPPORTED */