1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* pngwio.c - functions for data output
  26  *
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file and, per its terms, should not be removed:
  31  *
  32  * Last changed in libpng 1.6.15 [November 20, 2014]
  33  * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson
  34  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  35  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  36  *
  37  * This code is released under the libpng license.
  38  * For conditions of distribution and use, see the disclaimer
  39  * and license in png.h
  40  *
  41  * This file provides a location for all output.  Users who need
  42  * special handling are expected to write functions that have the same
  43  * arguments as these and perform similar functions, but that possibly
  44  * use different output methods.  Note that you shouldn't change these
  45  * functions, but rather write replacement functions and then change
  46  * them at run time with png_set_write_fn(...).
  47  */
  48 
  49 #include "pngpriv.h"
  50 
  51 #ifdef PNG_WRITE_SUPPORTED
  52 
  53 /* Write the data to whatever output you are using.  The default routine
  54  * writes to a file pointer.  Note that this routine sometimes gets called
  55  * with very small lengths, so you should implement some kind of simple
  56  * buffering if you are using unbuffered writes.  This should never be asked
  57  * to write more than 64K on a 16-bit machine.
  58  */
  59 
  60 void /* PRIVATE */
  61 png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
  62 {
  63    /* NOTE: write_data_fn must not change the buffer! */
  64    if (png_ptr->write_data_fn != NULL )
  65       (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),
  66          length);
  67 
  68    else
  69       png_error(png_ptr, "Call to NULL write function");
  70 }
  71 
  72 #ifdef PNG_STDIO_SUPPORTED
  73 /* This is the function that does the actual writing of data.  If you are
  74  * not writing to a standard C stream, you should create a replacement
  75  * write_data function and use it at run time with png_set_write_fn(), rather
  76  * than changing the library.
  77  */
  78 void PNGCBAPI
  79 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  80 {
  81    png_size_t check;
  82 
  83    if (png_ptr == NULL)
  84       return;
  85 
  86    check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
  87 
  88    if (check != length)
  89       png_error(png_ptr, "Write Error");
  90 }
  91 #endif
  92 
  93 /* This function is called to output any data pending writing (normally
  94  * to disk).  After png_flush is called, there should be no data pending
  95  * writing in any buffers.
  96  */
  97 #ifdef PNG_WRITE_FLUSH_SUPPORTED
  98 void /* PRIVATE */
  99 png_flush(png_structrp png_ptr)
 100 {
 101    if (png_ptr->output_flush_fn != NULL)
 102       (*(png_ptr->output_flush_fn))(png_ptr);
 103 }
 104 
 105 #  ifdef PNG_STDIO_SUPPORTED
 106 void PNGCBAPI
 107 png_default_flush(png_structp png_ptr)
 108 {
 109    png_FILE_p io_ptr;
 110 
 111    if (png_ptr == NULL)
 112       return;
 113 
 114    io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr));
 115    fflush(io_ptr);
 116 }
 117 #  endif
 118 #endif
 119 
 120 /* This function allows the application to supply new output functions for
 121  * libpng if standard C streams aren't being used.
 122  *
 123  * This function takes as its arguments:
 124  * png_ptr       - pointer to a png output data structure
 125  * io_ptr        - pointer to user supplied structure containing info about
 126  *                 the output functions.  May be NULL.
 127  * write_data_fn - pointer to a new output function that takes as its
 128  *                 arguments a pointer to a png_struct, a pointer to
 129  *                 data to be written, and a 32-bit unsigned int that is
 130  *                 the number of bytes to be written.  The new write
 131  *                 function should call png_error(png_ptr, "Error msg")
 132  *                 to exit and output any fatal error messages.  May be
 133  *                 NULL, in which case libpng's default function will
 134  *                 be used.
 135  * flush_data_fn - pointer to a new flush function that takes as its
 136  *                 arguments a pointer to a png_struct.  After a call to
 137  *                 the flush function, there should be no data in any buffers
 138  *                 or pending transmission.  If the output method doesn't do
 139  *                 any buffering of output, a function prototype must still be
 140  *                 supplied although it doesn't have to do anything.  If
 141  *                 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
 142  *                 time, output_flush_fn will be ignored, although it must be
 143  *                 supplied for compatibility.  May be NULL, in which case
 144  *                 libpng's default function will be used, if
 145  *                 PNG_WRITE_FLUSH_SUPPORTED is defined.  This is not
 146  *                 a good idea if io_ptr does not point to a standard
 147  *                 *FILE structure.
 148  */
 149 void PNGAPI
 150 png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,
 151     png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
 152 {
 153    if (png_ptr == NULL)
 154       return;
 155 
 156    png_ptr->io_ptr = io_ptr;
 157 
 158 #ifdef PNG_STDIO_SUPPORTED
 159    if (write_data_fn != NULL)
 160       png_ptr->write_data_fn = write_data_fn;
 161 
 162    else
 163       png_ptr->write_data_fn = png_default_write_data;
 164 #else
 165    png_ptr->write_data_fn = write_data_fn;
 166 #endif
 167 
 168 #ifdef PNG_WRITE_FLUSH_SUPPORTED
 169 #  ifdef PNG_STDIO_SUPPORTED
 170 
 171    if (output_flush_fn != NULL)
 172       png_ptr->output_flush_fn = output_flush_fn;
 173 
 174    else
 175       png_ptr->output_flush_fn = png_default_flush;
 176 
 177 #  else
 178    png_ptr->output_flush_fn = output_flush_fn;
 179 #  endif
 180 #else
 181    PNG_UNUSED(output_flush_fn)
 182 #endif /* WRITE_FLUSH */
 183 
 184 #ifdef PNG_READ_SUPPORTED
 185    /* It is an error to read while writing a png file */
 186    if (png_ptr->read_data_fn != NULL)
 187    {
 188       png_ptr->read_data_fn = NULL;
 189 
 190       png_warning(png_ptr,
 191           "Can't set both read_data_fn and write_data_fn in the"
 192           " same structure");
 193    }
 194 #endif
 195 }
 196 #endif /* WRITE */