pngrio.c

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

Generated on Tue Jul 5 14:13:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1