1/* PDFlib GmbH cvsid: $Id: pngrio.c 14574 2005-10-29 16:27:43Z bonefish $ */
2
3/* pngrio.c - functions for data input
4 *
5 * libpng 1.2.5 - October 3, 2002
6 * For conditions of distribution and use, see copyright notice in png.h
7 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
8 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
9 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10 *
11 * This file provides a location for all input.  Users who need
12 * special handling are expected to write a function that has the same
13 * arguments as this and performs a similar function, but that possibly
14 * has a different input method.  Note that you shouldn't change this
15 * function, but rather write a replacement function and then make
16 * libpng use it at run time with png_set_read_fn(...).
17 */
18
19#define PNG_INTERNAL
20#include "png.h"
21
22/* Read the data from whatever input you are using.  The default routine
23   reads from a file pointer.  Note that this routine sometimes gets called
24   with very small lengths, so you should implement some kind of simple
25   buffering if you are using unbuffered reads.  This should never be asked
26   to read more then 64K on a 16 bit machine. */
27void /* PRIVATE */
28png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
29{
30   png_debug1(4,"reading %d bytes\n", (int)length);
31   if (png_ptr->read_data_fn != NULL)
32      (*(png_ptr->read_data_fn))(png_ptr, data, length);
33   else
34      png_error(png_ptr, "Call to NULL read function");
35}
36
37#if !defined(PNG_NO_STDIO)
38/* This is the function that does the actual reading of data.  If you are
39   not reading from a standard C stream, you should create a replacement
40   read_data function and use it at run time with png_set_read_fn(), rather
41   than changing the library. */
42#ifndef USE_FAR_KEYWORD
43void PNGAPI
44png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
45{
46   png_size_t check;
47
48   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
49    * instead of an int, which is what fread() actually returns.
50    */
51/* PDFlib GmbH:
52#if defined(_WIN32_WCE)
53   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
54      check = 0;
55#else
56*/
57   check = (png_size_t)fread(data, (png_size_t)1, length,
58      (png_FILE_p)png_ptr->io_ptr);
59/* #endif PDFlib GmbH: */
60
61   if (check != length)
62      png_error(png_ptr, "Read Error");
63}
64#else
65/* this is the model-independent version. Since the standard I/O library
66   can't handle far buffers in the medium and small models, we have to copy
67   the data.
68*/
69
70#define NEAR_BUF_SIZE 1024
71#define MIN(a,b) (a <= b ? a : b)
72
73static void /* PRIVATE */
74png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
75{
76   int check;
77   png_byte *n_data;
78   png_FILE_p io_ptr;
79
80   /* Check if data really is near. If so, use usual code. */
81   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
82   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
83   if ((png_bytep)n_data == data)
84   {
85/* PDFlib GmbH:
86#if defined(_WIN32_WCE)
87      if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
88         check = 0;
89#else
90*/
91      check = fread(n_data, 1, length, io_ptr);
92/* #endif PDFlib GmbH: */
93   }
94   else
95   {
96      png_byte buf[NEAR_BUF_SIZE];
97      png_size_t read, remaining, err;
98      check = 0;
99      remaining = length;
100      do
101      {
102         read = MIN(NEAR_BUF_SIZE, remaining);
103/* PDFlib GmbH:
104#if defined(_WIN32_WCE)
105         if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
106            err = 0;
107#else
108*/
109         err = fread(buf, (png_size_t)1, read, io_ptr);
110/* #endif PDFlib GmbH: */
111         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
112         if(err != read)
113            break;
114         else
115            check += err;
116         data += read;
117         remaining -= read;
118      }
119      while (remaining != 0);
120   }
121   if ((png_uint_32)check != (png_uint_32)length)
122      png_error(png_ptr, "read Error");
123}
124#endif
125#endif
126
127/* This function allows the application to supply a new input function
128   for libpng if standard C streams aren't being used.
129
130   This function takes as its arguments:
131   png_ptr      - pointer to a png input data structure
132   io_ptr       - pointer to user supplied structure containing info about
133                  the input functions.  May be NULL.
134   read_data_fn - pointer to a new input function that takes as its
135                  arguments a pointer to a png_struct, a pointer to
136                  a location where input data can be stored, and a 32-bit
137                  unsigned int that is the number of bytes to be read.
138                  To exit and output any fatal error messages the new write
139                  function should call png_error(png_ptr, "Error msg"). */
140void PNGAPI
141png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
142   png_rw_ptr read_data_fn)
143{
144   png_ptr->io_ptr = io_ptr;
145
146#if !defined(PNG_NO_STDIO)
147   if (read_data_fn != NULL)
148      png_ptr->read_data_fn = read_data_fn;
149   else
150      png_ptr->read_data_fn = png_default_read_data;
151#else
152   png_ptr->read_data_fn = read_data_fn;
153#endif
154
155   /* It is an error to write to a read device */
156   if (png_ptr->write_data_fn != NULL)
157   {
158      png_ptr->write_data_fn = NULL;
159      png_warning(png_ptr,
160         "It's an error to set both read_data_fn and write_data_fn in the ");
161      png_warning(png_ptr,
162         "same structure.  Resetting write_data_fn to NULL.");
163   }
164
165#if defined(PNG_WRITE_FLUSH_SUPPORTED)
166   png_ptr->output_flush_fn = NULL;
167#endif
168}
169