286N/A/*
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A/* pngwio.c - functions for data output
286N/A *
286N/A * This file is available under and governed by the GNU General Public
286N/A * License version 2 only, as published by the Free Software Foundation.
286N/A * However, the following notice accompanied the original version of this
286N/A * file and, per its terms, should not be removed:
286N/A *
286N/A * Last changed in libpng 1.5.0 [January 6, 2011]
286N/A * Copyright (c) 1998-2011 Glenn Randers-Pehrson
286N/A * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
286N/A * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
286N/A *
286N/A * This code is released under the libpng license.
286N/A * For conditions of distribution and use, see the disclaimer
286N/A * and license in png.h
286N/A *
286N/A * This file provides a location for all output. Users who need
286N/A * special handling are expected to write functions that have the same
286N/A * arguments as these and perform similar functions, but that possibly
286N/A * use different output methods. Note that you shouldn't change these
286N/A * functions, but rather write replacement functions and then change
286N/A * them at run time with png_set_write_fn(...).
286N/A */
286N/A
286N/A#include "pngpriv.h"
286N/A
286N/A#ifdef PNG_WRITE_SUPPORTED
286N/A
286N/A/* Write the data to whatever output you are using. The default routine
286N/A * writes to a file pointer. Note that this routine sometimes gets called
286N/A * with very small lengths, so you should implement some kind of simple
286N/A * buffering if you are using unbuffered writes. This should never be asked
286N/A * to write more than 64K on a 16 bit machine.
286N/A */
286N/A
524N/Avoid /* PRIVATE */
286N/Apng_write_data(png_structp png_ptr, png_const_bytep data, png_size_t length)
286N/A{
286N/A /* NOTE: write_data_fn must not change the buffer! */
286N/A if (png_ptr->write_data_fn != NULL )
286N/A (*(png_ptr->write_data_fn))(png_ptr, (png_bytep)data, length);
286N/A
286N/A else
286N/A png_error(png_ptr, "Call to NULL write function");
286N/A}
286N/A
286N/A#ifdef PNG_STDIO_SUPPORTED
286N/A/* This is the function that does the actual writing of data. If you are
286N/A * not writing to a standard C stream, you should create a replacement
286N/A * write_data function and use it at run time with png_set_write_fn(), rather
286N/A * than changing the library.
286N/A */
286N/A#ifndef USE_FAR_KEYWORD
286N/Avoid PNGCBAPI
286N/Apng_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
286N/A{
286N/A png_size_t check;
286N/A
286N/A if (png_ptr == NULL)
286N/A return;
286N/A
286N/A check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
286N/A
286N/A if (check != length)
286N/A png_error(png_ptr, "Write Error");
286N/A}
286N/A#else
286N/A/* This is the model-independent version. Since the standard I/O library
286N/A * can't handle far buffers in the medium and small models, we have to copy
286N/A * the data.
286N/A */
286N/A
286N/A#define NEAR_BUF_SIZE 1024
286N/A#define MIN(a,b) (a <= b ? a : b)
286N/A
286N/Avoid PNGCBAPI
286N/Apng_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
286N/A{
286N/A png_uint_32 check;
286N/A png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
286N/A png_FILE_p io_ptr;
286N/A
286N/A if (png_ptr == NULL)
286N/A return;
286N/A
286N/A /* Check if data really is near. If so, use usual code. */
286N/A near_data = (png_byte *)CVT_PTR_NOCHECK(data);
286N/A io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
286N/A
286N/A if ((png_bytep)near_data == data)
286N/A {
286N/A check = fwrite(near_data, 1, length, io_ptr);
286N/A }
286N/A
286N/A else
286N/A {
286N/A png_byte buf[NEAR_BUF_SIZE];
286N/A png_size_t written, remaining, err;
286N/A check = 0;
286N/A remaining = length;
286N/A
286N/A do
286N/A {
286N/A written = MIN(NEAR_BUF_SIZE, remaining);
286N/A png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
286N/A err = fwrite(buf, 1, written, io_ptr);
286N/A
286N/A if (err != written)
286N/A break;
286N/A
286N/A else
286N/A check += err;
286N/A
286N/A data += written;
286N/A remaining -= written;
286N/A }
286N/A while (remaining != 0);
286N/A }
286N/A
286N/A if (check != length)
286N/A png_error(png_ptr, "Write Error");
286N/A}
286N/A
286N/A#endif
286N/A#endif
286N/A
286N/A/* This function is called to output any data pending writing (normally
286N/A * to disk). After png_flush is called, there should be no data pending
286N/A * writing in any buffers.
286N/A */
286N/A#ifdef PNG_WRITE_FLUSH_SUPPORTED
286N/Avoid /* PRIVATE */
286N/Apng_flush(png_structp png_ptr)
286N/A{
286N/A if (png_ptr->output_flush_fn != NULL)
286N/A (*(png_ptr->output_flush_fn))(png_ptr);
286N/A}
286N/A
286N/A# ifdef PNG_STDIO_SUPPORTED
286N/Avoid PNGCBAPI
286N/Apng_default_flush(png_structp png_ptr)
286N/A{
286N/A png_FILE_p io_ptr;
286N/A
286N/A if (png_ptr == NULL)
286N/A return;
286N/A
286N/A io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
286N/A fflush(io_ptr);
286N/A}
286N/A# endif
286N/A#endif
286N/A
286N/A/* This function allows the application to supply new output functions for
286N/A * libpng if standard C streams aren't being used.
286N/A *
286N/A * This function takes as its arguments:
286N/A * png_ptr - pointer to a png output data structure
286N/A * io_ptr - pointer to user supplied structure containing info about
524N/A * the output functions. May be NULL.
286N/A * write_data_fn - pointer to a new output function that takes as its
286N/A * arguments a pointer to a png_struct, a pointer to
286N/A * data to be written, and a 32-bit unsigned int that is
286N/A * the number of bytes to be written. The new write
286N/A * function should call png_error(png_ptr, "Error msg")
286N/A * to exit and output any fatal error messages. May be
286N/A * NULL, in which case libpng's default function will
286N/A * be used.
286N/A * flush_data_fn - pointer to a new flush function that takes as its
286N/A * arguments a pointer to a png_struct. After a call to
286N/A * the flush function, there should be no data in any buffers
286N/A * or pending transmission. If the output method doesn't do
286N/A * any buffering of output, a function prototype must still be
286N/A * supplied although it doesn't have to do anything. If
286N/A * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
286N/A * time, output_flush_fn will be ignored, although it must be
286N/A * supplied for compatibility. May be NULL, in which case
286N/A * libpng's default function will be used, if
286N/A * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
286N/A * a good idea if io_ptr does not point to a standard
286N/A * *FILE structure.
286N/A */
286N/Avoid PNGAPI
286N/Apng_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
286N/A png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
286N/A{
286N/A if (png_ptr == NULL)
286N/A return;
286N/A
286N/A png_ptr->io_ptr = io_ptr;
286N/A
286N/A#ifdef PNG_STDIO_SUPPORTED
286N/A if (write_data_fn != NULL)
286N/A png_ptr->write_data_fn = write_data_fn;
286N/A
286N/A else
286N/A png_ptr->write_data_fn = png_default_write_data;
286N/A#else
286N/A png_ptr->write_data_fn = write_data_fn;
286N/A#endif
286N/A
286N/A#ifdef PNG_WRITE_FLUSH_SUPPORTED
286N/A# ifdef PNG_STDIO_SUPPORTED
286N/A
286N/A if (output_flush_fn != NULL)
286N/A png_ptr->output_flush_fn = output_flush_fn;
286N/A
286N/A else
286N/A png_ptr->output_flush_fn = png_default_flush;
286N/A
286N/A# else
286N/A png_ptr->output_flush_fn = output_flush_fn;
286N/A# endif
286N/A#endif /* PNG_WRITE_FLUSH_SUPPORTED */
286N/A
286N/A /* It is an error to read while writing a png file */
286N/A if (png_ptr->read_data_fn != NULL)
286N/A {
286N/A png_ptr->read_data_fn = NULL;
286N/A
286N/A png_warning(png_ptr,
286N/A "Can't set both read_data_fn and write_data_fn in the"
286N/A " same structure");
286N/A }
286N/A}
286N/A
286N/A#ifdef USE_FAR_KEYWORD
286N/A# ifdef _MSC_VER
286N/Avoid *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
286N/A{
286N/A void *near_ptr;
286N/A void FAR *far_ptr;
286N/A FP_OFF(near_ptr) = FP_OFF(ptr);
286N/A far_ptr = (void FAR *)near_ptr;
286N/A
286N/A if (check != 0)
286N/A if (FP_SEG(ptr) != FP_SEG(far_ptr))
286N/A png_error(png_ptr, "segment lost in conversion");
286N/A
286N/A return(near_ptr);
286N/A}
286N/A# else
286N/Avoid *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
286N/A{
286N/A void *near_ptr;
286N/A void FAR *far_ptr;
286N/A near_ptr = (void FAR *)ptr;
286N/A far_ptr = (void FAR *)near_ptr;
286N/A
286N/A if (check != 0)
286N/A if (far_ptr != ptr)
286N/A png_error(png_ptr, "segment lost in conversion");
286N/A
286N/A return(near_ptr);
286N/A}
286N/A# endif
286N/A#endif
286N/A#endif /* PNG_WRITE_SUPPORTED */
286N/A