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


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:18