pngwio.c 5.5 KB

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