Coverage Report

Created: 2025-06-16 07:00

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