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