/* pngwio.c - functions for data output1*2* Copyright (c) 2018-2025 Cosmin Truta3* Copyright (c) 1998-2002,2004,2006-2014,2016,2018 Glenn Randers-Pehrson4* Copyright (c) 1996-1997 Andreas Dilger5* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.6*7* This code is released under the libpng license.8* For conditions of distribution and use, see the disclaimer9* and license in png.h10*11* This file provides a location for all output. Users who need12* special handling are expected to write functions that have the same13* arguments as these and perform similar functions, but that possibly14* use different output methods. Note that you shouldn't change these15* functions, but rather write replacement functions and then change16* them at run time with png_set_write_fn(...).17*/1819#include "pngpriv.h"2021#ifdef PNG_WRITE_SUPPORTED2223/* Write the data to whatever output you are using. The default routine24* writes to a file pointer. Note that this routine sometimes gets called25* with very small lengths, so you should implement some kind of simple26* buffering if you are using unbuffered writes. This should never be asked27* to write more than 64K on a 16-bit machine.28*/2930void /* PRIVATE */31png_write_data(png_structrp png_ptr, png_const_bytep data, size_t length)32{33/* NOTE: write_data_fn must not change the buffer! */34if (png_ptr->write_data_fn != NULL )35(*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),36length);3738else39png_error(png_ptr, "Call to NULL write function");40}4142#ifdef PNG_STDIO_SUPPORTED43/* This is the function that does the actual writing of data. If you are44* not writing to a standard C stream, you should create a replacement45* write_data function and use it at run time with png_set_write_fn(), rather46* than changing the library.47*/48void PNGCBAPI49png_default_write_data(png_structp png_ptr, png_bytep data, size_t length)50{51size_t check;5253if (png_ptr == NULL)54return;5556check = fwrite(data, 1, length, (FILE *)png_ptr->io_ptr);5758if (check != length)59png_error(png_ptr, "Write Error");60}61#endif6263/* This function is called to output any data pending writing (normally64* to disk). After png_flush is called, there should be no data pending65* writing in any buffers.66*/67#ifdef PNG_WRITE_FLUSH_SUPPORTED68void /* PRIVATE */69png_flush(png_structrp png_ptr)70{71if (png_ptr->output_flush_fn != NULL)72(*(png_ptr->output_flush_fn))(png_ptr);73}7475# ifdef PNG_STDIO_SUPPORTED76void PNGCBAPI77png_default_flush(png_structp png_ptr)78{79FILE *io_ptr;8081if (png_ptr == NULL)82return;8384io_ptr = png_voidcast(FILE *, png_ptr->io_ptr);85fflush(io_ptr);86}87# endif88#endif8990/* This function allows the application to supply new output functions for91* libpng if standard C streams aren't being used.92*93* This function takes as its arguments:94* png_ptr - pointer to a png output data structure95* io_ptr - pointer to user supplied structure containing info about96* the output functions. May be NULL.97* write_data_fn - pointer to a new output function that takes as its98* arguments a pointer to a png_struct, a pointer to99* data to be written, and a 32-bit unsigned int that is100* the number of bytes to be written. The new write101* function should call png_error(png_ptr, "Error msg")102* to exit and output any fatal error messages. May be103* NULL, in which case libpng's default function will104* be used.105* flush_data_fn - pointer to a new flush function that takes as its106* arguments a pointer to a png_struct. After a call to107* the flush function, there should be no data in any buffers108* or pending transmission. If the output method doesn't do109* any buffering of output, a function prototype must still be110* supplied although it doesn't have to do anything. If111* PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile112* time, output_flush_fn will be ignored, although it must be113* supplied for compatibility. May be NULL, in which case114* libpng's default function will be used, if115* PNG_WRITE_FLUSH_SUPPORTED is defined. This is not116* a good idea if io_ptr does not point to a standard117* *FILE structure.118*/119void PNGAPI120png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,121png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)122{123if (png_ptr == NULL)124return;125126png_ptr->io_ptr = io_ptr;127128#ifdef PNG_STDIO_SUPPORTED129if (write_data_fn != NULL)130png_ptr->write_data_fn = write_data_fn;131132else133png_ptr->write_data_fn = png_default_write_data;134#else135png_ptr->write_data_fn = write_data_fn;136#endif137138#ifdef PNG_WRITE_FLUSH_SUPPORTED139# ifdef PNG_STDIO_SUPPORTED140141if (output_flush_fn != NULL)142png_ptr->output_flush_fn = output_flush_fn;143144else145png_ptr->output_flush_fn = png_default_flush;146147# else148png_ptr->output_flush_fn = output_flush_fn;149# endif150#else151PNG_UNUSED(output_flush_fn)152#endif /* WRITE_FLUSH */153154#ifdef PNG_READ_SUPPORTED155/* It is an error to read while writing a png file */156if (png_ptr->read_data_fn != NULL)157{158png_ptr->read_data_fn = NULL;159160png_warning(png_ptr,161"Can't set both read_data_fn and write_data_fn in the"162" same structure");163}164#endif165}166#endif /* WRITE */167168169