Support unrar64.dll
[xy_vsfilter.git] / src / libpng / pngwio.c
blob4ecab7df44d3982558541c246ae4a7c488922348
2 /* pngwio.c - functions for data output
4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10 * This file provides a location for all output. Users who need
11 * special handling are expected to write functions that have the same
12 * arguments as these and perform similar functions, but that possibly
13 * use different output methods. Note that you shouldn't change these
14 * functions, but rather write replacement functions and then change
15 * them at run time with png_set_write_fn(...).
18 #define PNG_INTERNAL
19 #include "png.h"
20 #ifdef PNG_WRITE_SUPPORTED
22 /* Write the data to whatever output you are using. The default routine
23 * writes to a file pointer. Note that this routine sometimes gets called
24 * with very small lengths, so you should implement some kind of simple
25 * buffering if you are using unbuffered writes. This should never be asked
26 * to write more than 64K on a 16 bit machine.
29 void /* PRIVATE */
30 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
32 if (png_ptr->write_data_fn != NULL )
33 (*(png_ptr->write_data_fn))(png_ptr, data, length);
34 else
35 png_error(png_ptr, "Call to NULL write function");
38 #if !defined(PNG_NO_STDIO)
39 /* This is the function that does the actual writing of data. If you are
40 * not writing to a standard C stream, you should create a replacement
41 * write_data function and use it at run time with png_set_write_fn(), rather
42 * than changing the library.
44 #ifndef USE_FAR_KEYWORD
45 void PNGAPI
46 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
48 png_uint_32 check;
50 if (png_ptr == NULL)
51 return;
52 #if defined(_WIN32_WCE)
53 if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
54 check = 0;
55 #else
56 check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
57 #endif
58 if (check != length)
59 png_error(png_ptr, "Write Error");
61 #else
62 /* This is the model-independent version. Since the standard I/O library
63 * can't handle far buffers in the medium and small models, we have to copy
64 * the data.
67 #define NEAR_BUF_SIZE 1024
68 #define MIN(a,b) (a <= b ? a : b)
70 void PNGAPI
71 png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
73 png_uint_32 check;
74 png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */
75 png_FILE_p io_ptr;
77 if (png_ptr == NULL)
78 return;
79 /* Check if data really is near. If so, use usual code. */
80 near_data = (png_byte *)CVT_PTR_NOCHECK(data);
81 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
82 if ((png_bytep)near_data == data)
84 #if defined(_WIN32_WCE)
85 if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
86 check = 0;
87 #else
88 check = fwrite(near_data, 1, length, io_ptr);
89 #endif
91 else
93 png_byte buf[NEAR_BUF_SIZE];
94 png_size_t written, remaining, err;
95 check = 0;
96 remaining = length;
99 written = MIN(NEAR_BUF_SIZE, remaining);
100 png_memcpy(buf, data, written); /* Copy far buffer to near buffer */
101 #if defined(_WIN32_WCE)
102 if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
103 err = 0;
104 #else
105 err = fwrite(buf, 1, written, io_ptr);
106 #endif
107 if (err != written)
108 break;
110 else
111 check += err;
113 data += written;
114 remaining -= written;
116 while (remaining != 0);
118 if (check != length)
119 png_error(png_ptr, "Write Error");
122 #endif
123 #endif
125 /* This function is called to output any data pending writing (normally
126 * to disk). After png_flush is called, there should be no data pending
127 * writing in any buffers.
129 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
130 void /* PRIVATE */
131 png_flush(png_structp png_ptr)
133 if (png_ptr->output_flush_fn != NULL)
134 (*(png_ptr->output_flush_fn))(png_ptr);
137 #if !defined(PNG_NO_STDIO)
138 void PNGAPI
139 png_default_flush(png_structp png_ptr)
141 #if !defined(_WIN32_WCE)
142 png_FILE_p io_ptr;
143 #endif
144 if (png_ptr == NULL)
145 return;
146 #if !defined(_WIN32_WCE)
147 io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
148 fflush(io_ptr);
149 #endif
151 #endif
152 #endif
154 /* This function allows the application to supply new output functions for
155 * libpng if standard C streams aren't being used.
157 * This function takes as its arguments:
158 * png_ptr - pointer to a png output data structure
159 * io_ptr - pointer to user supplied structure containing info about
160 * the output functions. May be NULL.
161 * write_data_fn - pointer to a new output function that takes as its
162 * arguments a pointer to a png_struct, a pointer to
163 * data to be written, and a 32-bit unsigned int that is
164 * the number of bytes to be written. The new write
165 * function should call png_error(png_ptr, "Error msg")
166 * to exit and output any fatal error messages. May be
167 * NULL, in which case libpng's default function will
168 * be used.
169 * flush_data_fn - pointer to a new flush function that takes as its
170 * arguments a pointer to a png_struct. After a call to
171 * the flush function, there should be no data in any buffers
172 * or pending transmission. If the output method doesn't do
173 * any buffering of ouput, a function prototype must still be
174 * supplied although it doesn't have to do anything. If
175 * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
176 * time, output_flush_fn will be ignored, although it must be
177 * supplied for compatibility. May be NULL, in which case
178 * libpng's default function will be used, if
179 * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not
180 * a good idea if io_ptr does not point to a standard
181 * *FILE structure.
183 void PNGAPI
184 png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
185 png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
187 if (png_ptr == NULL)
188 return;
190 png_ptr->io_ptr = io_ptr;
192 #if !defined(PNG_NO_STDIO)
193 if (write_data_fn != NULL)
194 png_ptr->write_data_fn = write_data_fn;
196 else
197 png_ptr->write_data_fn = png_default_write_data;
198 #else
199 png_ptr->write_data_fn = write_data_fn;
200 #endif
202 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
203 #if !defined(PNG_NO_STDIO)
204 if (output_flush_fn != NULL)
205 png_ptr->output_flush_fn = output_flush_fn;
207 else
208 png_ptr->output_flush_fn = png_default_flush;
209 #else
210 png_ptr->output_flush_fn = output_flush_fn;
211 #endif
212 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
214 /* It is an error to read while writing a png file */
215 if (png_ptr->read_data_fn != NULL)
217 png_ptr->read_data_fn = NULL;
218 png_warning(png_ptr,
219 "Attempted to set both read_data_fn and write_data_fn in");
220 png_warning(png_ptr,
221 "the same structure. Resetting read_data_fn to NULL.");
225 #if defined(USE_FAR_KEYWORD)
226 #if defined(_MSC_VER)
227 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
229 void *near_ptr;
230 void FAR *far_ptr;
231 FP_OFF(near_ptr) = FP_OFF(ptr);
232 far_ptr = (void FAR *)near_ptr;
234 if (check != 0)
235 if (FP_SEG(ptr) != FP_SEG(far_ptr))
236 png_error(png_ptr, "segment lost in conversion");
238 return(near_ptr);
240 # else
241 void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
243 void *near_ptr;
244 void FAR *far_ptr;
245 near_ptr = (void FAR *)ptr;
246 far_ptr = (void FAR *)near_ptr;
248 if (check != 0)
249 if (far_ptr != ptr)
250 png_error(png_ptr, "segment lost in conversion");
252 return(near_ptr);
254 # endif
255 # endif
256 #endif /* PNG_WRITE_SUPPORTED */