nuclear@0: nuclear@0: /* pngwio.c - functions for data output nuclear@0: * nuclear@0: * Last changed in libpng 1.2.30 [August 15, 2008] nuclear@0: * For conditions of distribution and use, see copyright notice in png.h nuclear@0: * Copyright (c) 1998-2008 Glenn Randers-Pehrson nuclear@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) nuclear@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) nuclear@0: * nuclear@0: * This file provides a location for all output. Users who need nuclear@0: * special handling are expected to write functions that have the same nuclear@0: * arguments as these and perform similar functions, but that possibly nuclear@0: * use different output methods. Note that you shouldn't change these nuclear@0: * functions, but rather write replacement functions and then change nuclear@0: * them at run time with png_set_write_fn(...). nuclear@0: */ nuclear@0: nuclear@0: #define PNG_INTERNAL nuclear@0: #include "png.h" nuclear@0: #ifdef PNG_WRITE_SUPPORTED nuclear@0: nuclear@0: /* Write the data to whatever output you are using. The default routine nuclear@0: writes to a file pointer. Note that this routine sometimes gets called nuclear@0: with very small lengths, so you should implement some kind of simple nuclear@0: buffering if you are using unbuffered writes. This should never be asked nuclear@0: to write more than 64K on a 16 bit machine. */ nuclear@0: nuclear@0: void /* PRIVATE */ nuclear@0: png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@0: { nuclear@0: if (png_ptr->write_data_fn != NULL ) nuclear@0: (*(png_ptr->write_data_fn))(png_ptr, data, length); nuclear@0: else nuclear@0: png_error(png_ptr, "Call to NULL write function"); nuclear@0: } nuclear@0: nuclear@0: #if !defined(PNG_NO_STDIO) nuclear@0: /* This is the function that does the actual writing of data. If you are nuclear@0: not writing to a standard C stream, you should create a replacement nuclear@0: write_data function and use it at run time with png_set_write_fn(), rather nuclear@0: than changing the library. */ nuclear@0: #ifndef USE_FAR_KEYWORD nuclear@0: void PNGAPI nuclear@0: png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@0: { nuclear@0: png_uint_32 check; nuclear@0: nuclear@0: if (png_ptr == NULL) return; nuclear@0: #if defined(_WIN32_WCE) nuclear@0: if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) ) nuclear@0: check = 0; nuclear@0: #else nuclear@0: check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); nuclear@0: #endif nuclear@0: if (check != length) nuclear@0: png_error(png_ptr, "Write Error"); nuclear@0: } nuclear@0: #else nuclear@0: /* this is the model-independent version. Since the standard I/O library nuclear@0: can't handle far buffers in the medium and small models, we have to copy nuclear@0: the data. nuclear@0: */ nuclear@0: nuclear@0: #define NEAR_BUF_SIZE 1024 nuclear@0: #define MIN(a,b) (a <= b ? a : b) nuclear@0: nuclear@0: void PNGAPI nuclear@0: png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) nuclear@0: { nuclear@0: png_uint_32 check; nuclear@0: png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ nuclear@0: png_FILE_p io_ptr; nuclear@0: nuclear@0: if (png_ptr == NULL) return; nuclear@0: /* Check if data really is near. If so, use usual code. */ nuclear@0: near_data = (png_byte *)CVT_PTR_NOCHECK(data); nuclear@0: io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); nuclear@0: if ((png_bytep)near_data == data) nuclear@0: { nuclear@0: #if defined(_WIN32_WCE) nuclear@0: if ( !WriteFile(io_ptr, near_data, length, &check, NULL) ) nuclear@0: check = 0; nuclear@0: #else nuclear@0: check = fwrite(near_data, 1, length, io_ptr); nuclear@0: #endif nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: png_byte buf[NEAR_BUF_SIZE]; nuclear@0: png_size_t written, remaining, err; nuclear@0: check = 0; nuclear@0: remaining = length; nuclear@0: do nuclear@0: { nuclear@0: written = MIN(NEAR_BUF_SIZE, remaining); nuclear@0: png_memcpy(buf, data, written); /* copy far buffer to near buffer */ nuclear@0: #if defined(_WIN32_WCE) nuclear@0: if ( !WriteFile(io_ptr, buf, written, &err, NULL) ) nuclear@0: err = 0; nuclear@0: #else nuclear@0: err = fwrite(buf, 1, written, io_ptr); nuclear@0: #endif nuclear@0: if (err != written) nuclear@0: break; nuclear@0: else nuclear@0: check += err; nuclear@0: data += written; nuclear@0: remaining -= written; nuclear@0: } nuclear@0: while (remaining != 0); nuclear@0: } nuclear@0: if (check != length) nuclear@0: png_error(png_ptr, "Write Error"); nuclear@0: } nuclear@0: nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: /* This function is called to output any data pending writing (normally nuclear@0: to disk). After png_flush is called, there should be no data pending nuclear@0: writing in any buffers. */ nuclear@0: #if defined(PNG_WRITE_FLUSH_SUPPORTED) nuclear@0: void /* PRIVATE */ nuclear@0: png_flush(png_structp png_ptr) nuclear@0: { nuclear@0: if (png_ptr->output_flush_fn != NULL) nuclear@0: (*(png_ptr->output_flush_fn))(png_ptr); nuclear@0: } nuclear@0: nuclear@0: #if !defined(PNG_NO_STDIO) nuclear@0: void PNGAPI nuclear@0: png_default_flush(png_structp png_ptr) nuclear@0: { nuclear@0: #if !defined(_WIN32_WCE) nuclear@0: png_FILE_p io_ptr; nuclear@0: #endif nuclear@0: if (png_ptr == NULL) return; nuclear@0: #if !defined(_WIN32_WCE) nuclear@0: io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); nuclear@0: if (io_ptr != NULL) nuclear@0: fflush(io_ptr); nuclear@0: #endif nuclear@0: } nuclear@0: #endif nuclear@0: #endif nuclear@0: nuclear@0: /* This function allows the application to supply new output functions for nuclear@0: libpng if standard C streams aren't being used. nuclear@0: nuclear@0: This function takes as its arguments: nuclear@0: png_ptr - pointer to a png output data structure nuclear@0: io_ptr - pointer to user supplied structure containing info about nuclear@0: the output functions. May be NULL. nuclear@0: write_data_fn - pointer to a new output function that takes as its nuclear@0: arguments a pointer to a png_struct, a pointer to nuclear@0: data to be written, and a 32-bit unsigned int that is nuclear@0: the number of bytes to be written. The new write nuclear@0: function should call png_error(png_ptr, "Error msg") nuclear@0: to exit and output any fatal error messages. nuclear@0: flush_data_fn - pointer to a new flush function that takes as its nuclear@0: arguments a pointer to a png_struct. After a call to nuclear@0: the flush function, there should be no data in any buffers nuclear@0: or pending transmission. If the output method doesn't do nuclear@0: any buffering of ouput, a function prototype must still be nuclear@0: supplied although it doesn't have to do anything. If nuclear@0: PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile nuclear@0: time, output_flush_fn will be ignored, although it must be nuclear@0: supplied for compatibility. */ nuclear@0: void PNGAPI nuclear@0: png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, nuclear@0: png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) nuclear@0: { nuclear@0: if (png_ptr == NULL) return; nuclear@0: png_ptr->io_ptr = io_ptr; nuclear@0: nuclear@0: #if !defined(PNG_NO_STDIO) nuclear@0: if (write_data_fn != NULL) nuclear@0: png_ptr->write_data_fn = write_data_fn; nuclear@0: else nuclear@0: png_ptr->write_data_fn = png_default_write_data; nuclear@0: #else nuclear@0: png_ptr->write_data_fn = write_data_fn; nuclear@0: #endif nuclear@0: nuclear@0: #if defined(PNG_WRITE_FLUSH_SUPPORTED) nuclear@0: #if !defined(PNG_NO_STDIO) nuclear@0: if (output_flush_fn != NULL) nuclear@0: png_ptr->output_flush_fn = output_flush_fn; nuclear@0: else nuclear@0: png_ptr->output_flush_fn = png_default_flush; nuclear@0: #else nuclear@0: png_ptr->output_flush_fn = output_flush_fn; nuclear@0: #endif nuclear@0: #endif /* PNG_WRITE_FLUSH_SUPPORTED */ nuclear@0: nuclear@0: /* It is an error to read while writing a png file */ nuclear@0: if (png_ptr->read_data_fn != NULL) nuclear@0: { nuclear@0: png_ptr->read_data_fn = NULL; nuclear@0: png_warning(png_ptr, nuclear@0: "Attempted to set both read_data_fn and write_data_fn in"); nuclear@0: png_warning(png_ptr, nuclear@0: "the same structure. Resetting read_data_fn to NULL."); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #if defined(USE_FAR_KEYWORD) nuclear@0: #if defined(_MSC_VER) nuclear@0: void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) nuclear@0: { nuclear@0: void *near_ptr; nuclear@0: void FAR *far_ptr; nuclear@0: FP_OFF(near_ptr) = FP_OFF(ptr); nuclear@0: far_ptr = (void FAR *)near_ptr; nuclear@0: if (check != 0) nuclear@0: if (FP_SEG(ptr) != FP_SEG(far_ptr)) nuclear@0: png_error(png_ptr, "segment lost in conversion"); nuclear@0: return(near_ptr); nuclear@0: } nuclear@0: # else nuclear@0: void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) nuclear@0: { nuclear@0: void *near_ptr; nuclear@0: void FAR *far_ptr; nuclear@0: near_ptr = (void FAR *)ptr; nuclear@0: far_ptr = (void FAR *)near_ptr; nuclear@0: if (check != 0) nuclear@0: if (far_ptr != ptr) nuclear@0: png_error(png_ptr, "segment lost in conversion"); nuclear@0: return(near_ptr); nuclear@0: } nuclear@0: # endif nuclear@0: # endif nuclear@0: #endif /* PNG_WRITE_SUPPORTED */