nuclear@26: nuclear@26: /* pngrio.c - functions for data input nuclear@26: * nuclear@26: * Last changed in libpng 1.2.30 [August 15, 2008] nuclear@26: * For conditions of distribution and use, see copyright notice in png.h nuclear@26: * Copyright (c) 1998-2008 Glenn Randers-Pehrson nuclear@26: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) nuclear@26: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) nuclear@26: * nuclear@26: * This file provides a location for all input. Users who need nuclear@26: * special handling are expected to write a function that has the same nuclear@26: * arguments as this and performs a similar function, but that possibly nuclear@26: * has a different input method. Note that you shouldn't change this nuclear@26: * function, but rather write a replacement function and then make nuclear@26: * libpng use it at run time with png_set_read_fn(...). nuclear@26: */ nuclear@26: nuclear@26: #define PNG_INTERNAL nuclear@26: #include "png.h" nuclear@26: #if defined(PNG_READ_SUPPORTED) nuclear@26: nuclear@26: /* Read the data from whatever input you are using. The default routine nuclear@26: reads from a file pointer. Note that this routine sometimes gets called nuclear@26: with very small lengths, so you should implement some kind of simple nuclear@26: buffering if you are using unbuffered reads. This should never be asked nuclear@26: to read more then 64K on a 16 bit machine. */ nuclear@26: void /* PRIVATE */ nuclear@26: png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@26: { nuclear@26: png_debug1(4, "reading %d bytes\n", (int)length); nuclear@26: if (png_ptr->read_data_fn != NULL) nuclear@26: (*(png_ptr->read_data_fn))(png_ptr, data, length); nuclear@26: else nuclear@26: png_error(png_ptr, "Call to NULL read function"); nuclear@26: } nuclear@26: nuclear@26: #if !defined(PNG_NO_STDIO) nuclear@26: /* This is the function that does the actual reading of data. If you are nuclear@26: not reading from a standard C stream, you should create a replacement nuclear@26: read_data function and use it at run time with png_set_read_fn(), rather nuclear@26: than changing the library. */ nuclear@26: #ifndef USE_FAR_KEYWORD nuclear@26: void PNGAPI nuclear@26: png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@26: { nuclear@26: png_size_t check; nuclear@26: nuclear@26: if (png_ptr == NULL) return; nuclear@26: /* fread() returns 0 on error, so it is OK to store this in a png_size_t nuclear@26: * instead of an int, which is what fread() actually returns. nuclear@26: */ nuclear@26: #if defined(_WIN32_WCE) nuclear@26: if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) nuclear@26: check = 0; nuclear@26: #else nuclear@26: check = (png_size_t)fread(data, (png_size_t)1, length, nuclear@26: (png_FILE_p)png_ptr->io_ptr); nuclear@26: #endif nuclear@26: nuclear@26: if (check != length) nuclear@26: png_error(png_ptr, "Read Error"); nuclear@26: } nuclear@26: #else nuclear@26: /* this is the model-independent version. Since the standard I/O library nuclear@26: can't handle far buffers in the medium and small models, we have to copy nuclear@26: the data. nuclear@26: */ nuclear@26: nuclear@26: #define NEAR_BUF_SIZE 1024 nuclear@26: #define MIN(a,b) (a <= b ? a : b) nuclear@26: nuclear@26: static void PNGAPI nuclear@26: png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@26: { nuclear@26: int check; nuclear@26: png_byte *n_data; nuclear@26: png_FILE_p io_ptr; nuclear@26: nuclear@26: if (png_ptr == NULL) return; nuclear@26: /* Check if data really is near. If so, use usual code. */ nuclear@26: n_data = (png_byte *)CVT_PTR_NOCHECK(data); nuclear@26: io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); nuclear@26: if ((png_bytep)n_data == data) nuclear@26: { nuclear@26: #if defined(_WIN32_WCE) nuclear@26: if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) nuclear@26: check = 0; nuclear@26: #else nuclear@26: check = fread(n_data, 1, length, io_ptr); nuclear@26: #endif nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_byte buf[NEAR_BUF_SIZE]; nuclear@26: png_size_t read, remaining, err; nuclear@26: check = 0; nuclear@26: remaining = length; nuclear@26: do nuclear@26: { nuclear@26: read = MIN(NEAR_BUF_SIZE, remaining); nuclear@26: #if defined(_WIN32_WCE) nuclear@26: if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) ) nuclear@26: err = 0; nuclear@26: #else nuclear@26: err = fread(buf, (png_size_t)1, read, io_ptr); nuclear@26: #endif nuclear@26: png_memcpy(data, buf, read); /* copy far buffer to near buffer */ nuclear@26: if (err != read) nuclear@26: break; nuclear@26: else nuclear@26: check += err; nuclear@26: data += read; nuclear@26: remaining -= read; nuclear@26: } nuclear@26: while (remaining != 0); nuclear@26: } nuclear@26: if ((png_uint_32)check != (png_uint_32)length) nuclear@26: png_error(png_ptr, "read Error"); nuclear@26: } nuclear@26: #endif nuclear@26: #endif nuclear@26: nuclear@26: /* This function allows the application to supply a new input function nuclear@26: for libpng if standard C streams aren't being used. nuclear@26: nuclear@26: This function takes as its arguments: nuclear@26: png_ptr - pointer to a png input data structure nuclear@26: io_ptr - pointer to user supplied structure containing info about nuclear@26: the input functions. May be NULL. nuclear@26: read_data_fn - pointer to a new input function that takes as its nuclear@26: arguments a pointer to a png_struct, a pointer to nuclear@26: a location where input data can be stored, and a 32-bit nuclear@26: unsigned int that is the number of bytes to be read. nuclear@26: To exit and output any fatal error messages the new write nuclear@26: function should call png_error(png_ptr, "Error msg"). */ nuclear@26: void PNGAPI nuclear@26: png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, nuclear@26: png_rw_ptr read_data_fn) nuclear@26: { nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->io_ptr = io_ptr; nuclear@26: nuclear@26: #if !defined(PNG_NO_STDIO) nuclear@26: if (read_data_fn != NULL) nuclear@26: png_ptr->read_data_fn = read_data_fn; nuclear@26: else nuclear@26: png_ptr->read_data_fn = png_default_read_data; nuclear@26: #else nuclear@26: png_ptr->read_data_fn = read_data_fn; nuclear@26: #endif nuclear@26: nuclear@26: /* It is an error to write to a read device */ nuclear@26: if (png_ptr->write_data_fn != NULL) nuclear@26: { nuclear@26: png_ptr->write_data_fn = NULL; nuclear@26: png_warning(png_ptr, nuclear@26: "It's an error to set both read_data_fn and write_data_fn in the "); nuclear@26: png_warning(png_ptr, nuclear@26: "same structure. Resetting write_data_fn to NULL."); nuclear@26: } nuclear@26: nuclear@26: #if defined(PNG_WRITE_FLUSH_SUPPORTED) nuclear@26: png_ptr->output_flush_fn = NULL; nuclear@26: #endif nuclear@26: } nuclear@26: #endif /* PNG_READ_SUPPORTED */