CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libpng17/pngwio.c
Views: 1401
1/* pngwio.c - functions for data output2*3* Last changed in libpng 1.7.0 [(PENDING RELEASE)]4* Copyright (c) 1998-2002,2004,2006-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"21#define PNG_SRC_FILE PNG_SRC_FILE_pngwio2223#ifdef PNG_WRITE_SUPPORTED2425/* Write the data to whatever output you are using. The default routine26* writes to a file pointer. Note that this routine sometimes gets called27* with very small lengths, so you should implement some kind of simple28* buffering if you are using unbuffered writes. This should never be asked29* to write more than 64K on a 16 bit machine.30*/3132void /* PRIVATE */33png_write_data(png_structrp png_ptr, png_const_voidp data, png_size_t length)34{35/* This was guaranteed by prior versions of libpng, so app callbacks may36* assume it even though it isn't documented to be the case.37*/38debug(length > 0U);3940/* NOTE: write_data_fn must not change the buffer!41* This cast is required because of the API; changing the type of the42* callback would require every app to change the callback and that change43* would have to be conditional on the libpng version.44*/45if (png_ptr->rw_data_fn != NULL )46png_ptr->rw_data_fn(png_ptr,47png_constcast(png_bytep,png_voidcast(png_const_bytep,data)), length);4849else50png_app_error(png_ptr, "No write function");51}5253#ifdef PNG_STDIO_SUPPORTED54/* This is the function that does the actual writing of data. If you are55* not writing to a standard C stream, you should create a replacement56* write_data function and use it at run time with png_set_write_fn(), rather57* than changing the library.58*/59void PNGCBAPI60png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)61{62png_size_t check;6364if (png_ptr == NULL)65return;6667check = fwrite(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));6869if (check != length)70png_error(png_ptr, "Write Error");71}72#endif7374#ifdef PNG_WRITE_FLUSH_SUPPORTED75# ifdef PNG_STDIO_SUPPORTED76void PNGCBAPI77png_default_flush(png_structp png_ptr)78{79if (png_ptr == NULL)80return;8182fflush(png_voidcast(png_FILE_p, (png_ptr->io_ptr)));83}84# endif85#endif8687/* This function allows the application to supply new output functions for88* libpng if standard C streams aren't being used.89*90* This function takes as its arguments:91* png_ptr - pointer to a png output data structure92* io_ptr - pointer to user supplied structure containing info about93* the output functions. May be NULL.94* write_data_fn - pointer to a new output function that takes as its95* arguments a pointer to a png_struct, a pointer to96* data to be written, and a 32-bit unsigned int that is97* the number of bytes to be written. The new write98* function should call png_error(png_ptr, "Error msg")99* to exit and output any fatal error messages. May be100* NULL, in which case libpng's default function will101* be used.102* flush_data_fn - pointer to a new flush function that takes as its103* arguments a pointer to a png_struct. After a call to104* the flush function, there should be no data in any buffers105* or pending transmission. If the output method doesn't do106* any buffering of output, a function prototype must still be107* supplied although it doesn't have to do anything. If108* PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile109* time, output_flush_fn will be ignored, although it must be110* supplied for compatibility. May be NULL, in which case111* libpng's default function will be used, if112* PNG_WRITE_FLUSH_SUPPORTED is defined. This is not113* a good idea if io_ptr does not point to a standard114* *FILE structure.115*/116void PNGAPI117png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,118png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)119{120if (png_ptr == NULL)121return;122123if (png_ptr->read_struct)124{125png_app_error(png_ptr, "cannot set a write function on a read struct");126return;127}128129if (write_data_fn == NULL)130{131png_app_error(png_ptr,132"API change: png_set_write_fn requires a function");133return;134}135136png_ptr->io_ptr = io_ptr;137png_ptr->rw_data_fn = write_data_fn;138# ifdef PNG_WRITE_FLUSH_SUPPORTED139if (output_flush_fn != NULL)140png_ptr->output_flush_fn = output_flush_fn;141# else142PNG_UNUSED(output_flush_fn)143# endif /* WRITE_FLUSH */144}145#endif /* WRITE */146147148