dbf-halloween2015

diff libs/libpng/pngrio.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libpng/pngrio.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,166 @@
     1.4 +
     1.5 +/* pngrio.c - functions for data input
     1.6 + *
     1.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
     1.8 + * For conditions of distribution and use, see copyright notice in png.h
     1.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
    1.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.12 + *
    1.13 + * This file provides a location for all input.  Users who need
    1.14 + * special handling are expected to write a function that has the same
    1.15 + * arguments as this and performs a similar function, but that possibly
    1.16 + * has a different input method.  Note that you shouldn't change this
    1.17 + * function, but rather write a replacement function and then make
    1.18 + * libpng use it at run time with png_set_read_fn(...).
    1.19 + */
    1.20 +
    1.21 +#define PNG_INTERNAL
    1.22 +#include "png.h"
    1.23 +#if defined(PNG_READ_SUPPORTED)
    1.24 +
    1.25 +/* Read the data from whatever input you are using.  The default routine
    1.26 +   reads from a file pointer.  Note that this routine sometimes gets called
    1.27 +   with very small lengths, so you should implement some kind of simple
    1.28 +   buffering if you are using unbuffered reads.  This should never be asked
    1.29 +   to read more then 64K on a 16 bit machine. */
    1.30 +void /* PRIVATE */
    1.31 +png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
    1.32 +{
    1.33 +   png_debug1(4, "reading %d bytes\n", (int)length);
    1.34 +   if (png_ptr->read_data_fn != NULL)
    1.35 +      (*(png_ptr->read_data_fn))(png_ptr, data, length);
    1.36 +   else
    1.37 +      png_error(png_ptr, "Call to NULL read function");
    1.38 +}
    1.39 +
    1.40 +#if !defined(PNG_NO_STDIO)
    1.41 +/* This is the function that does the actual reading of data.  If you are
    1.42 +   not reading from a standard C stream, you should create a replacement
    1.43 +   read_data function and use it at run time with png_set_read_fn(), rather
    1.44 +   than changing the library. */
    1.45 +#ifndef USE_FAR_KEYWORD
    1.46 +void PNGAPI
    1.47 +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
    1.48 +{
    1.49 +   png_size_t check;
    1.50 +
    1.51 +   if (png_ptr == NULL) return;
    1.52 +   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
    1.53 +    * instead of an int, which is what fread() actually returns.
    1.54 +    */
    1.55 +#if defined(_WIN32_WCE)
    1.56 +   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
    1.57 +      check = 0;
    1.58 +#else
    1.59 +   check = (png_size_t)fread(data, (png_size_t)1, length,
    1.60 +      (png_FILE_p)png_ptr->io_ptr);
    1.61 +#endif
    1.62 +
    1.63 +   if (check != length)
    1.64 +      png_error(png_ptr, "Read Error");
    1.65 +}
    1.66 +#else
    1.67 +/* this is the model-independent version. Since the standard I/O library
    1.68 +   can't handle far buffers in the medium and small models, we have to copy
    1.69 +   the data.
    1.70 +*/
    1.71 +
    1.72 +#define NEAR_BUF_SIZE 1024
    1.73 +#define MIN(a,b) (a <= b ? a : b)
    1.74 +
    1.75 +static void PNGAPI
    1.76 +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
    1.77 +{
    1.78 +   int check;
    1.79 +   png_byte *n_data;
    1.80 +   png_FILE_p io_ptr;
    1.81 +
    1.82 +   if (png_ptr == NULL) return;
    1.83 +   /* Check if data really is near. If so, use usual code. */
    1.84 +   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
    1.85 +   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
    1.86 +   if ((png_bytep)n_data == data)
    1.87 +   {
    1.88 +#if defined(_WIN32_WCE)
    1.89 +      if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
    1.90 +         check = 0;
    1.91 +#else
    1.92 +      check = fread(n_data, 1, length, io_ptr);
    1.93 +#endif
    1.94 +   }
    1.95 +   else
    1.96 +   {
    1.97 +      png_byte buf[NEAR_BUF_SIZE];
    1.98 +      png_size_t read, remaining, err;
    1.99 +      check = 0;
   1.100 +      remaining = length;
   1.101 +      do
   1.102 +      {
   1.103 +         read = MIN(NEAR_BUF_SIZE, remaining);
   1.104 +#if defined(_WIN32_WCE)
   1.105 +         if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
   1.106 +            err = 0;
   1.107 +#else
   1.108 +         err = fread(buf, (png_size_t)1, read, io_ptr);
   1.109 +#endif
   1.110 +         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
   1.111 +         if (err != read)
   1.112 +            break;
   1.113 +         else
   1.114 +            check += err;
   1.115 +         data += read;
   1.116 +         remaining -= read;
   1.117 +      }
   1.118 +      while (remaining != 0);
   1.119 +   }
   1.120 +   if ((png_uint_32)check != (png_uint_32)length)
   1.121 +      png_error(png_ptr, "read Error");
   1.122 +}
   1.123 +#endif
   1.124 +#endif
   1.125 +
   1.126 +/* This function allows the application to supply a new input function
   1.127 +   for libpng if standard C streams aren't being used.
   1.128 +
   1.129 +   This function takes as its arguments:
   1.130 +   png_ptr      - pointer to a png input data structure
   1.131 +   io_ptr       - pointer to user supplied structure containing info about
   1.132 +                  the input functions.  May be NULL.
   1.133 +   read_data_fn - pointer to a new input function that takes as its
   1.134 +                  arguments a pointer to a png_struct, a pointer to
   1.135 +                  a location where input data can be stored, and a 32-bit
   1.136 +                  unsigned int that is the number of bytes to be read.
   1.137 +                  To exit and output any fatal error messages the new write
   1.138 +                  function should call png_error(png_ptr, "Error msg"). */
   1.139 +void PNGAPI
   1.140 +png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
   1.141 +   png_rw_ptr read_data_fn)
   1.142 +{
   1.143 +   if (png_ptr == NULL) return;
   1.144 +   png_ptr->io_ptr = io_ptr;
   1.145 +
   1.146 +#if !defined(PNG_NO_STDIO)
   1.147 +   if (read_data_fn != NULL)
   1.148 +      png_ptr->read_data_fn = read_data_fn;
   1.149 +   else
   1.150 +      png_ptr->read_data_fn = png_default_read_data;
   1.151 +#else
   1.152 +   png_ptr->read_data_fn = read_data_fn;
   1.153 +#endif
   1.154 +
   1.155 +   /* It is an error to write to a read device */
   1.156 +   if (png_ptr->write_data_fn != NULL)
   1.157 +   {
   1.158 +      png_ptr->write_data_fn = NULL;
   1.159 +      png_warning(png_ptr,
   1.160 +         "It's an error to set both read_data_fn and write_data_fn in the ");
   1.161 +      png_warning(png_ptr,
   1.162 +         "same structure.  Resetting write_data_fn to NULL.");
   1.163 +   }
   1.164 +
   1.165 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
   1.166 +   png_ptr->output_flush_fn = NULL;
   1.167 +#endif
   1.168 +}
   1.169 +#endif /* PNG_READ_SUPPORTED */