michael@0: michael@0: /* pngwio.c - functions for data output michael@0: * michael@0: * Last changed in libpng 1.6.9 [February 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * This file provides a location for all output. Users who need michael@0: * special handling are expected to write functions that have the same michael@0: * arguments as these and perform similar functions, but that possibly michael@0: * use different output methods. Note that you shouldn't change these michael@0: * functions, but rather write replacement functions and then change michael@0: * them at run time with png_set_write_fn(...). michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: michael@0: #ifdef PNG_WRITE_SUPPORTED michael@0: michael@0: /* Write the data to whatever output you are using. The default routine michael@0: * writes to a file pointer. Note that this routine sometimes gets called michael@0: * with very small lengths, so you should implement some kind of simple michael@0: * buffering if you are using unbuffered writes. This should never be asked michael@0: * to write more than 64K on a 16 bit machine. michael@0: */ michael@0: michael@0: void /* PRIVATE */ michael@0: png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length) michael@0: { michael@0: /* NOTE: write_data_fn must not change the buffer! */ michael@0: if (png_ptr->write_data_fn != NULL ) michael@0: (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data), michael@0: length); michael@0: michael@0: else michael@0: png_error(png_ptr, "Call to NULL write function"); michael@0: } michael@0: michael@0: #ifdef PNG_STDIO_SUPPORTED michael@0: /* This is the function that does the actual writing of data. If you are michael@0: * not writing to a standard C stream, you should create a replacement michael@0: * write_data function and use it at run time with png_set_write_fn(), rather michael@0: * than changing the library. michael@0: */ michael@0: void PNGCBAPI michael@0: png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) michael@0: { michael@0: png_size_t check; michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); michael@0: michael@0: if (check != length) michael@0: png_error(png_ptr, "Write Error"); michael@0: } michael@0: #endif michael@0: michael@0: /* This function is called to output any data pending writing (normally michael@0: * to disk). After png_flush is called, there should be no data pending michael@0: * writing in any buffers. michael@0: */ michael@0: #ifdef PNG_WRITE_FLUSH_SUPPORTED michael@0: void /* PRIVATE */ michael@0: png_flush(png_structrp png_ptr) michael@0: { michael@0: if (png_ptr->output_flush_fn != NULL) michael@0: (*(png_ptr->output_flush_fn))(png_ptr); michael@0: } michael@0: michael@0: # ifdef PNG_STDIO_SUPPORTED michael@0: void PNGCBAPI michael@0: png_default_flush(png_structp png_ptr) michael@0: { michael@0: png_FILE_p io_ptr; michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr)); michael@0: fflush(io_ptr); michael@0: } michael@0: # endif michael@0: #endif michael@0: michael@0: /* This function allows the application to supply new output functions for michael@0: * libpng if standard C streams aren't being used. michael@0: * michael@0: * This function takes as its arguments: michael@0: * png_ptr - pointer to a png output data structure michael@0: * io_ptr - pointer to user supplied structure containing info about michael@0: * the output functions. May be NULL. michael@0: * write_data_fn - pointer to a new output function that takes as its michael@0: * arguments a pointer to a png_struct, a pointer to michael@0: * data to be written, and a 32-bit unsigned int that is michael@0: * the number of bytes to be written. The new write michael@0: * function should call png_error(png_ptr, "Error msg") michael@0: * to exit and output any fatal error messages. May be michael@0: * NULL, in which case libpng's default function will michael@0: * be used. michael@0: * flush_data_fn - pointer to a new flush function that takes as its michael@0: * arguments a pointer to a png_struct. After a call to michael@0: * the flush function, there should be no data in any buffers michael@0: * or pending transmission. If the output method doesn't do michael@0: * any buffering of output, a function prototype must still be michael@0: * supplied although it doesn't have to do anything. If michael@0: * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile michael@0: * time, output_flush_fn will be ignored, although it must be michael@0: * supplied for compatibility. May be NULL, in which case michael@0: * libpng's default function will be used, if michael@0: * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not michael@0: * a good idea if io_ptr does not point to a standard michael@0: * *FILE structure. michael@0: */ michael@0: void PNGAPI michael@0: png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr, michael@0: png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->io_ptr = io_ptr; michael@0: michael@0: #ifdef PNG_STDIO_SUPPORTED michael@0: if (write_data_fn != NULL) michael@0: png_ptr->write_data_fn = write_data_fn; michael@0: michael@0: else michael@0: png_ptr->write_data_fn = png_default_write_data; michael@0: #else michael@0: png_ptr->write_data_fn = write_data_fn; michael@0: #endif michael@0: michael@0: #ifdef PNG_WRITE_FLUSH_SUPPORTED michael@0: # ifdef PNG_STDIO_SUPPORTED michael@0: michael@0: if (output_flush_fn != NULL) michael@0: png_ptr->output_flush_fn = output_flush_fn; michael@0: michael@0: else michael@0: png_ptr->output_flush_fn = png_default_flush; michael@0: michael@0: # else michael@0: png_ptr->output_flush_fn = output_flush_fn; michael@0: # endif michael@0: #else michael@0: PNG_UNUSED(output_flush_fn) michael@0: #endif /* PNG_WRITE_FLUSH_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_SUPPORTED michael@0: /* It is an error to read while writing a png file */ michael@0: if (png_ptr->read_data_fn != NULL) michael@0: { michael@0: png_ptr->read_data_fn = NULL; michael@0: michael@0: png_warning(png_ptr, michael@0: "Can't set both read_data_fn and write_data_fn in the" michael@0: " same structure"); michael@0: } michael@0: #endif michael@0: } michael@0: #endif /* PNG_WRITE_SUPPORTED */