Back to index

plt-scheme  4.2.1
pngrio.c
Go to the documentation of this file.
00001 
00002 /* pngrio.c - functions for data input
00003  *
00004  * libpng 1.2.5 - October 3, 2002
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2002 Glenn Randers-Pehrson
00007  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00008  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00009  *
00010  * This file provides a location for all input.  Users who need
00011  * special handling are expected to write a function that has the same
00012  * arguments as this and performs a similar function, but that possibly
00013  * has a different input method.  Note that you shouldn't change this
00014  * function, but rather write a replacement function and then make
00015  * libpng use it at run time with png_set_read_fn(...).
00016  */
00017 
00018 #define PNG_INTERNAL
00019 #include "png.h"
00020 
00021 /* Read the data from whatever input you are using.  The default routine
00022    reads from a file pointer.  Note that this routine sometimes gets called
00023    with very small lengths, so you should implement some kind of simple
00024    buffering if you are using unbuffered reads.  This should never be asked
00025    to read more then 64K on a 16 bit machine. */
00026 void /* PRIVATE */
00027 png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00028 {
00029    png_debug1(4,"reading %d bytes\n", (int)length);
00030    if (png_ptr->read_data_fn != NULL)
00031       (*(png_ptr->read_data_fn))(png_ptr, data, length);
00032    else
00033       png_error(png_ptr, "Call to NULL read function");
00034 }
00035 
00036 #if !defined(PNG_NO_STDIO)
00037 /* This is the function that does the actual reading of data.  If you are
00038    not reading from a standard C stream, you should create a replacement
00039    read_data function and use it at run time with png_set_read_fn(), rather
00040    than changing the library. */
00041 #ifndef USE_FAR_KEYWORD
00042 void PNGAPI
00043 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00044 {
00045    png_size_t check;
00046 
00047    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
00048     * instead of an int, which is what fread() actually returns.
00049     */
00050 #if defined(_WIN32_WCE)
00051    if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00052       check = 0;
00053 #else
00054    check = (png_size_t)fread(data, (png_size_t)1, length,
00055       (png_FILE_p)png_ptr->io_ptr);
00056 #endif
00057 
00058    if (check != length)
00059       png_error(png_ptr, "Read Error");
00060 }
00061 #else
00062 /* this is the model-independent version. Since the standard I/O library
00063    can't handle far buffers in the medium and small models, we have to copy
00064    the data.
00065 */
00066 
00067 #define NEAR_BUF_SIZE 1024
00068 #define MIN(a,b) (a <= b ? a : b)
00069 
00070 static void /* PRIVATE */
00071 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00072 {
00073    int check;
00074    png_byte *n_data;
00075    png_FILE_p io_ptr;
00076 
00077    /* Check if data really is near. If so, use usual code. */
00078    n_data = (png_byte *)CVT_PTR_NOCHECK(data);
00079    io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
00080    if ((png_bytep)n_data == data)
00081    {
00082 #if defined(_WIN32_WCE)
00083       if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00084          check = 0;
00085 #else
00086       check = fread(n_data, 1, length, io_ptr);
00087 #endif
00088    }
00089    else
00090    {
00091       png_byte buf[NEAR_BUF_SIZE];
00092       png_size_t read, remaining, err;
00093       check = 0;
00094       remaining = length;
00095       do
00096       {
00097          read = MIN(NEAR_BUF_SIZE, remaining);
00098 #if defined(_WIN32_WCE)
00099          if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
00100             err = 0;
00101 #else
00102          err = fread(buf, (png_size_t)1, read, io_ptr);
00103 #endif
00104          png_memcpy(data, buf, read); /* copy far buffer to near buffer */
00105          if(err != read)
00106             break;
00107          else
00108             check += err;
00109          data += read;
00110          remaining -= read;
00111       }
00112       while (remaining != 0);
00113    }
00114    if ((png_uint_32)check != (png_uint_32)length)
00115       png_error(png_ptr, "read Error");
00116 }
00117 #endif
00118 #endif
00119 
00120 /* This function allows the application to supply a new input function
00121    for libpng if standard C streams aren't being used.
00122 
00123    This function takes as its arguments:
00124    png_ptr      - pointer to a png input data structure
00125    io_ptr       - pointer to user supplied structure containing info about
00126                   the input functions.  May be NULL.
00127    read_data_fn - pointer to a new input function that takes as its
00128                   arguments a pointer to a png_struct, a pointer to
00129                   a location where input data can be stored, and a 32-bit
00130                   unsigned int that is the number of bytes to be read.
00131                   To exit and output any fatal error messages the new write
00132                   function should call png_error(png_ptr, "Error msg"). */
00133 void PNGAPI
00134 png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
00135    png_rw_ptr read_data_fn)
00136 {
00137    png_ptr->io_ptr = io_ptr;
00138 
00139 #if !defined(PNG_NO_STDIO)
00140    if (read_data_fn != NULL)
00141       png_ptr->read_data_fn = read_data_fn;
00142    else
00143       png_ptr->read_data_fn = png_default_read_data;
00144 #else
00145    png_ptr->read_data_fn = read_data_fn;
00146 #endif
00147 
00148    /* It is an error to write to a read device */
00149    if (png_ptr->write_data_fn != NULL)
00150    {
00151       png_ptr->write_data_fn = NULL;
00152       png_warning(png_ptr,
00153          "It's an error to set both read_data_fn and write_data_fn in the ");
00154       png_warning(png_ptr,
00155          "same structure.  Resetting write_data_fn to NULL.");
00156    }
00157 
00158 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00159    png_ptr->output_flush_fn = NULL;
00160 #endif
00161 }