1/* pngwio.c - functions for data output2*3* Last changed in libpng 1.6.24 [August 4, 2016]4* Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson5* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)6* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)7*8* This code is released under the libpng license.9* For conditions of distribution and use, see the disclaimer10* and license in png.h11*12* This file provides a location for all output. Users who need13* special handling are expected to write functions that have the same14* arguments as these and perform similar functions, but that possibly15* use different output methods. Note that you shouldn't change these16* functions, but rather write replacement functions and then change17* them at run time with png_set_write_fn(...).18*/1920#include "pngpriv.h"2122#ifdef PNG_WRITE_SUPPORTED2324/* Write the data to whatever output you are using. The default routine25* writes to a file pointer. Note that this routine sometimes gets called26* with very small lengths, so you should implement some kind of simple27* buffering if you are using unbuffered writes. This should never be asked28* to write more than 64K on a 16-bit machine.29*/3031void /* PRIVATE */32png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)33{34/* NOTE: write_data_fn must not change the buffer! */35if (png_ptr->write_data_fn != NULL )36(*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),37length);3839else40png_error(png_ptr, "Call to NULL write function");41}4243#ifdef PNG_STDIO_SUPPORTED44/* This is the function that does the actual writing of data. If you are45* not writing to a standard C stream, you should create a replacement46* write_data function and use it at run time with png_set_write_fn(), rather47* than changing the library.48*/49void PNGCBAPI50png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)51{52png_size_t check;5354if (png_ptr == NULL)55return;5657check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));5859if (check != length)60png_error(png_ptr, "Write Error");61}62#endif6364/* This function is called to output any data pending writing (normally65* to disk). After png_flush is called, there should be no data pending66* writing in any buffers.67*/68#ifdef PNG_WRITE_FLUSH_SUPPORTED69void /* PRIVATE */70png_flush(png_structrp png_ptr)71{72if (png_ptr->output_flush_fn != NULL)73(*(png_ptr->output_flush_fn))(png_ptr);74}7576# ifdef PNG_STDIO_SUPPORTED77void PNGCBAPI78png_default_flush(png_structp png_ptr)79{80png_FILE_p io_ptr;8182if (png_ptr == NULL)83return;8485io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr));86fflush(io_ptr);87}88# endif89#endif9091/* This function allows the application to supply new output functions for92* libpng if standard C streams aren't being used.93*94* This function takes as its arguments:95* png_ptr - pointer to a png output data structure96* io_ptr - pointer to user supplied structure containing info about97* the output functions. May be NULL.98* write_data_fn - pointer to a new output function that takes as its99* arguments a pointer to a png_struct, a pointer to100* data to be written, and a 32-bit unsigned int that is101* the number of bytes to be written. The new write102* function should call png_error(png_ptr, "Error msg")103* to exit and output any fatal error messages. May be104* NULL, in which case libpng's default function will105* be used.106* flush_data_fn - pointer to a new flush function that takes as its107* arguments a pointer to a png_struct. After a call to108* the flush function, there should be no data in any buffers109* or pending transmission. If the output method doesn't do110* any buffering of output, a function prototype must still be111* supplied although it doesn't have to do anything. If112* PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile113* time, output_flush_fn will be ignored, although it must be114* supplied for compatibility. May be NULL, in which case115* libpng's default function will be used, if116* PNG_WRITE_FLUSH_SUPPORTED is defined. This is not117* a good idea if io_ptr does not point to a standard118* *FILE structure.119*/120void PNGAPI121png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,122png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)123{124if (png_ptr == NULL)125return;126127png_ptr->io_ptr = io_ptr;128129#ifdef PNG_STDIO_SUPPORTED130if (write_data_fn != NULL)131png_ptr->write_data_fn = write_data_fn;132133else134png_ptr->write_data_fn = png_default_write_data;135#else136png_ptr->write_data_fn = write_data_fn;137#endif138139#ifdef PNG_WRITE_FLUSH_SUPPORTED140# ifdef PNG_STDIO_SUPPORTED141142if (output_flush_fn != NULL)143png_ptr->output_flush_fn = output_flush_fn;144145else146png_ptr->output_flush_fn = png_default_flush;147148# else149png_ptr->output_flush_fn = output_flush_fn;150# endif151#else152PNG_UNUSED(output_flush_fn)153#endif /* WRITE_FLUSH */154155#ifdef PNG_READ_SUPPORTED156/* It is an error to read while writing a png file */157if (png_ptr->read_data_fn != NULL)158{159png_ptr->read_data_fn = NULL;160161png_warning(png_ptr,162"Can't set both read_data_fn and write_data_fn in the"163" same structure");164}165#endif166}167#endif /* WRITE */168169170