1*a67afe4dSAndroid Build Coastguard Worker
2*a67afe4dSAndroid Build Coastguard Worker /* pngwio.c - functions for data output
3*a67afe4dSAndroid Build Coastguard Worker *
4*a67afe4dSAndroid Build Coastguard Worker * Copyright (c) 2018 Cosmin Truta
5*a67afe4dSAndroid Build Coastguard Worker * Copyright (c) 1998-2002,2004,2006-2014,2016,2018 Glenn Randers-Pehrson
6*a67afe4dSAndroid Build Coastguard Worker * Copyright (c) 1996-1997 Andreas Dilger
7*a67afe4dSAndroid Build Coastguard Worker * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
8*a67afe4dSAndroid Build Coastguard Worker *
9*a67afe4dSAndroid Build Coastguard Worker * This code is released under the libpng license.
10*a67afe4dSAndroid Build Coastguard Worker * For conditions of distribution and use, see the disclaimer
11*a67afe4dSAndroid Build Coastguard Worker * and license in png.h
12*a67afe4dSAndroid Build Coastguard Worker *
13*a67afe4dSAndroid Build Coastguard Worker * This file provides a location for all output. Users who need
14*a67afe4dSAndroid Build Coastguard Worker * special handling are expected to write functions that have the same
15*a67afe4dSAndroid Build Coastguard Worker * arguments as these and perform similar functions, but that possibly
16*a67afe4dSAndroid Build Coastguard Worker * use different output methods. Note that you shouldn't change these
17*a67afe4dSAndroid Build Coastguard Worker * functions, but rather write replacement functions and then change
18*a67afe4dSAndroid Build Coastguard Worker * them at run time with png_set_write_fn(...).
19*a67afe4dSAndroid Build Coastguard Worker */
20*a67afe4dSAndroid Build Coastguard Worker
21*a67afe4dSAndroid Build Coastguard Worker #include "pngpriv.h"
22*a67afe4dSAndroid Build Coastguard Worker
23*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_WRITE_SUPPORTED
24*a67afe4dSAndroid Build Coastguard Worker
25*a67afe4dSAndroid Build Coastguard Worker /* Write the data to whatever output you are using. The default routine
26*a67afe4dSAndroid Build Coastguard Worker * writes to a file pointer. Note that this routine sometimes gets called
27*a67afe4dSAndroid Build Coastguard Worker * with very small lengths, so you should implement some kind of simple
28*a67afe4dSAndroid Build Coastguard Worker * buffering if you are using unbuffered writes. This should never be asked
29*a67afe4dSAndroid Build Coastguard Worker * to write more than 64K on a 16-bit machine.
30*a67afe4dSAndroid Build Coastguard Worker */
31*a67afe4dSAndroid Build Coastguard Worker
32*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_write_data(png_structrp png_ptr,png_const_bytep data,size_t length)33*a67afe4dSAndroid Build Coastguard Worker png_write_data(png_structrp png_ptr, png_const_bytep data, size_t length)
34*a67afe4dSAndroid Build Coastguard Worker {
35*a67afe4dSAndroid Build Coastguard Worker /* NOTE: write_data_fn must not change the buffer! */
36*a67afe4dSAndroid Build Coastguard Worker if (png_ptr->write_data_fn != NULL )
37*a67afe4dSAndroid Build Coastguard Worker (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),
38*a67afe4dSAndroid Build Coastguard Worker length);
39*a67afe4dSAndroid Build Coastguard Worker
40*a67afe4dSAndroid Build Coastguard Worker else
41*a67afe4dSAndroid Build Coastguard Worker png_error(png_ptr, "Call to NULL write function");
42*a67afe4dSAndroid Build Coastguard Worker }
43*a67afe4dSAndroid Build Coastguard Worker
44*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_STDIO_SUPPORTED
45*a67afe4dSAndroid Build Coastguard Worker /* This is the function that does the actual writing of data. If you are
46*a67afe4dSAndroid Build Coastguard Worker * not writing to a standard C stream, you should create a replacement
47*a67afe4dSAndroid Build Coastguard Worker * write_data function and use it at run time with png_set_write_fn(), rather
48*a67afe4dSAndroid Build Coastguard Worker * than changing the library.
49*a67afe4dSAndroid Build Coastguard Worker */
50*a67afe4dSAndroid Build Coastguard Worker void PNGCBAPI
png_default_write_data(png_structp png_ptr,png_bytep data,size_t length)51*a67afe4dSAndroid Build Coastguard Worker png_default_write_data(png_structp png_ptr, png_bytep data, size_t length)
52*a67afe4dSAndroid Build Coastguard Worker {
53*a67afe4dSAndroid Build Coastguard Worker size_t check;
54*a67afe4dSAndroid Build Coastguard Worker
55*a67afe4dSAndroid Build Coastguard Worker if (png_ptr == NULL)
56*a67afe4dSAndroid Build Coastguard Worker return;
57*a67afe4dSAndroid Build Coastguard Worker
58*a67afe4dSAndroid Build Coastguard Worker check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
59*a67afe4dSAndroid Build Coastguard Worker
60*a67afe4dSAndroid Build Coastguard Worker if (check != length)
61*a67afe4dSAndroid Build Coastguard Worker png_error(png_ptr, "Write Error");
62*a67afe4dSAndroid Build Coastguard Worker }
63*a67afe4dSAndroid Build Coastguard Worker #endif
64*a67afe4dSAndroid Build Coastguard Worker
65*a67afe4dSAndroid Build Coastguard Worker /* This function is called to output any data pending writing (normally
66*a67afe4dSAndroid Build Coastguard Worker * to disk). After png_flush is called, there should be no data pending
67*a67afe4dSAndroid Build Coastguard Worker * writing in any buffers.
68*a67afe4dSAndroid Build Coastguard Worker */
69*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_WRITE_FLUSH_SUPPORTED
70*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_flush(png_structrp png_ptr)71*a67afe4dSAndroid Build Coastguard Worker png_flush(png_structrp png_ptr)
72*a67afe4dSAndroid Build Coastguard Worker {
73*a67afe4dSAndroid Build Coastguard Worker if (png_ptr->output_flush_fn != NULL)
74*a67afe4dSAndroid Build Coastguard Worker (*(png_ptr->output_flush_fn))(png_ptr);
75*a67afe4dSAndroid Build Coastguard Worker }
76*a67afe4dSAndroid Build Coastguard Worker
77*a67afe4dSAndroid Build Coastguard Worker # ifdef PNG_STDIO_SUPPORTED
78*a67afe4dSAndroid Build Coastguard Worker void PNGCBAPI
png_default_flush(png_structp png_ptr)79*a67afe4dSAndroid Build Coastguard Worker png_default_flush(png_structp png_ptr)
80*a67afe4dSAndroid Build Coastguard Worker {
81*a67afe4dSAndroid Build Coastguard Worker png_FILE_p io_ptr;
82*a67afe4dSAndroid Build Coastguard Worker
83*a67afe4dSAndroid Build Coastguard Worker if (png_ptr == NULL)
84*a67afe4dSAndroid Build Coastguard Worker return;
85*a67afe4dSAndroid Build Coastguard Worker
86*a67afe4dSAndroid Build Coastguard Worker io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr));
87*a67afe4dSAndroid Build Coastguard Worker fflush(io_ptr);
88*a67afe4dSAndroid Build Coastguard Worker }
89*a67afe4dSAndroid Build Coastguard Worker # endif
90*a67afe4dSAndroid Build Coastguard Worker #endif
91*a67afe4dSAndroid Build Coastguard Worker
92*a67afe4dSAndroid Build Coastguard Worker /* This function allows the application to supply new output functions for
93*a67afe4dSAndroid Build Coastguard Worker * libpng if standard C streams aren't being used.
94*a67afe4dSAndroid Build Coastguard Worker *
95*a67afe4dSAndroid Build Coastguard Worker * This function takes as its arguments:
96*a67afe4dSAndroid Build Coastguard Worker * png_ptr - pointer to a png output data structure
97*a67afe4dSAndroid Build Coastguard Worker * io_ptr - pointer to user supplied structure containing info about
98*a67afe4dSAndroid Build Coastguard Worker * the output functions. May be NULL.
99*a67afe4dSAndroid Build Coastguard Worker * write_data_fn - pointer to a new output function that takes as its
100*a67afe4dSAndroid Build Coastguard Worker * arguments a pointer to a png_struct, a pointer to
101*a67afe4dSAndroid Build Coastguard Worker * data to be written, and a 32-bit unsigned int that is
102*a67afe4dSAndroid Build Coastguard Worker * the number of bytes to be written. The new write
103*a67afe4dSAndroid Build Coastguard Worker * function should call png_error(png_ptr, "Error msg")
104*a67afe4dSAndroid Build Coastguard Worker * to exit and output any fatal error messages. May be
105*a67afe4dSAndroid Build Coastguard Worker * NULL, in which case libpng's default function will
106*a67afe4dSAndroid Build Coastguard Worker * be used.
107*a67afe4dSAndroid Build Coastguard Worker * flush_data_fn - pointer to a new flush function that takes as its
108*a67afe4dSAndroid Build Coastguard Worker * arguments a pointer to a png_struct. After a call to
109*a67afe4dSAndroid Build Coastguard Worker * the flush function, there should be no data in any buffers
110*a67afe4dSAndroid Build Coastguard Worker * or pending transmission. If the output method doesn't do
111*a67afe4dSAndroid Build Coastguard Worker * any buffering of output, a function prototype must still be
112*a67afe4dSAndroid Build Coastguard Worker * supplied although it doesn't have to do anything. If
113*a67afe4dSAndroid Build Coastguard Worker * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
114*a67afe4dSAndroid Build Coastguard Worker * time, output_flush_fn will be ignored, although it must be
115*a67afe4dSAndroid Build Coastguard Worker * supplied for compatibility. May be NULL, in which case
116*a67afe4dSAndroid Build Coastguard Worker * libpng's default function will be used, if
117*a67afe4dSAndroid Build Coastguard Worker * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
118*a67afe4dSAndroid Build Coastguard Worker * a good idea if io_ptr does not point to a standard
119*a67afe4dSAndroid Build Coastguard Worker * *FILE structure.
120*a67afe4dSAndroid Build Coastguard Worker */
121*a67afe4dSAndroid Build Coastguard Worker void PNGAPI
png_set_write_fn(png_structrp png_ptr,png_voidp io_ptr,png_rw_ptr write_data_fn,png_flush_ptr output_flush_fn)122*a67afe4dSAndroid Build Coastguard Worker png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr,
123*a67afe4dSAndroid Build Coastguard Worker png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
124*a67afe4dSAndroid Build Coastguard Worker {
125*a67afe4dSAndroid Build Coastguard Worker if (png_ptr == NULL)
126*a67afe4dSAndroid Build Coastguard Worker return;
127*a67afe4dSAndroid Build Coastguard Worker
128*a67afe4dSAndroid Build Coastguard Worker png_ptr->io_ptr = io_ptr;
129*a67afe4dSAndroid Build Coastguard Worker
130*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_STDIO_SUPPORTED
131*a67afe4dSAndroid Build Coastguard Worker if (write_data_fn != NULL)
132*a67afe4dSAndroid Build Coastguard Worker png_ptr->write_data_fn = write_data_fn;
133*a67afe4dSAndroid Build Coastguard Worker
134*a67afe4dSAndroid Build Coastguard Worker else
135*a67afe4dSAndroid Build Coastguard Worker png_ptr->write_data_fn = png_default_write_data;
136*a67afe4dSAndroid Build Coastguard Worker #else
137*a67afe4dSAndroid Build Coastguard Worker png_ptr->write_data_fn = write_data_fn;
138*a67afe4dSAndroid Build Coastguard Worker #endif
139*a67afe4dSAndroid Build Coastguard Worker
140*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_WRITE_FLUSH_SUPPORTED
141*a67afe4dSAndroid Build Coastguard Worker # ifdef PNG_STDIO_SUPPORTED
142*a67afe4dSAndroid Build Coastguard Worker
143*a67afe4dSAndroid Build Coastguard Worker if (output_flush_fn != NULL)
144*a67afe4dSAndroid Build Coastguard Worker png_ptr->output_flush_fn = output_flush_fn;
145*a67afe4dSAndroid Build Coastguard Worker
146*a67afe4dSAndroid Build Coastguard Worker else
147*a67afe4dSAndroid Build Coastguard Worker png_ptr->output_flush_fn = png_default_flush;
148*a67afe4dSAndroid Build Coastguard Worker
149*a67afe4dSAndroid Build Coastguard Worker # else
150*a67afe4dSAndroid Build Coastguard Worker png_ptr->output_flush_fn = output_flush_fn;
151*a67afe4dSAndroid Build Coastguard Worker # endif
152*a67afe4dSAndroid Build Coastguard Worker #else
153*a67afe4dSAndroid Build Coastguard Worker PNG_UNUSED(output_flush_fn)
154*a67afe4dSAndroid Build Coastguard Worker #endif /* WRITE_FLUSH */
155*a67afe4dSAndroid Build Coastguard Worker
156*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_SUPPORTED
157*a67afe4dSAndroid Build Coastguard Worker /* It is an error to read while writing a png file */
158*a67afe4dSAndroid Build Coastguard Worker if (png_ptr->read_data_fn != NULL)
159*a67afe4dSAndroid Build Coastguard Worker {
160*a67afe4dSAndroid Build Coastguard Worker png_ptr->read_data_fn = NULL;
161*a67afe4dSAndroid Build Coastguard Worker
162*a67afe4dSAndroid Build Coastguard Worker png_warning(png_ptr,
163*a67afe4dSAndroid Build Coastguard Worker "Can't set both read_data_fn and write_data_fn in the"
164*a67afe4dSAndroid Build Coastguard Worker " same structure");
165*a67afe4dSAndroid Build Coastguard Worker }
166*a67afe4dSAndroid Build Coastguard Worker #endif
167*a67afe4dSAndroid Build Coastguard Worker }
168*a67afe4dSAndroid Build Coastguard Worker #endif /* WRITE */
169