pngrio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* pngrio.c - functions for data input
  2. *
  3. * Last changed in libpng 1.2.43 [February 25, 2010]
  4. * Copyright (c) 1998-2010 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 input. Users who need
  13. * special handling are expected to write a function that has the same
  14. * arguments as this and performs a similar function, but that possibly
  15. * has a different input method. Note that you shouldn't change this
  16. * function, but rather write a replacement function and then make
  17. * libpng use it at run time with png_set_read_fn(...).
  18. */
  19. #define PNG_INTERNAL
  20. #define PNG_NO_PEDANTIC_WARNINGS
  21. #include "png.h"
  22. #ifdef PNG_READ_SUPPORTED
  23. /* Read the data from whatever input you are using. The default routine
  24. * reads from a file pointer. Note that this routine sometimes gets called
  25. * with very small lengths, so you should implement some kind of simple
  26. * buffering if you are using unbuffered reads. This should never be asked
  27. * to read more then 64K on a 16 bit machine.
  28. */
  29. void /* PRIVATE */
  30. png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  31. {
  32. png_debug1(4, "reading %d bytes", (int)length);
  33. if (png_ptr->read_data_fn != NULL)
  34. (*(png_ptr->read_data_fn))(png_ptr, data, length);
  35. else
  36. png_error(png_ptr, "Call to NULL read function");
  37. }
  38. #ifdef PNG_STDIO_SUPPORTED
  39. /* This is the function that does the actual reading of data. If you are
  40. * not reading from a standard C stream, you should create a replacement
  41. * read_data function and use it at run time with png_set_read_fn(), rather
  42. * than changing the library.
  43. */
  44. #ifndef USE_FAR_KEYWORD
  45. void PNGAPI
  46. png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  47. {
  48. png_size_t check;
  49. if (png_ptr == NULL)
  50. return;
  51. /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  52. * instead of an int, which is what fread() actually returns.
  53. */
  54. #ifdef _WIN32_WCE
  55. if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
  56. check = 0;
  57. #else
  58. check = (png_size_t)fread(data, (png_size_t)1, length,
  59. (png_FILE_p)png_ptr->io_ptr);
  60. #endif
  61. if (check != length)
  62. png_error(png_ptr, "Read Error");
  63. }
  64. #else
  65. /* This is the model-independent version. Since the standard I/O library
  66. can't handle far buffers in the medium and small models, we have to copy
  67. the data.
  68. */
  69. #define NEAR_BUF_SIZE 1024
  70. #define MIN(a,b) (a <= b ? a : b)
  71. static void PNGAPI
  72. png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  73. {
  74. int check;
  75. png_byte *n_data;
  76. 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. n_data = (png_byte *)CVT_PTR_NOCHECK(data);
  81. io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
  82. if ((png_bytep)n_data == data)
  83. {
  84. #ifdef _WIN32_WCE
  85. if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check,
  86. NULL) )
  87. check = 0;
  88. #else
  89. check = fread(n_data, 1, length, io_ptr);
  90. #endif
  91. }
  92. else
  93. {
  94. png_byte buf[NEAR_BUF_SIZE];
  95. png_size_t read, remaining, err;
  96. check = 0;
  97. remaining = length;
  98. do
  99. {
  100. read = MIN(NEAR_BUF_SIZE, remaining);
  101. #ifdef _WIN32_WCE
  102. if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
  103. err = 0;
  104. #else
  105. err = fread(buf, (png_size_t)1, read, io_ptr);
  106. #endif
  107. png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  108. if (err != read)
  109. break;
  110. else
  111. check += err;
  112. data += read;
  113. remaining -= read;
  114. }
  115. while (remaining != 0);
  116. }
  117. if ((png_uint_32)check != (png_uint_32)length)
  118. png_error(png_ptr, "read Error");
  119. }
  120. #endif
  121. #endif
  122. /* This function allows the application to supply a new input function
  123. * for libpng if standard C streams aren't being used.
  124. *
  125. * This function takes as its arguments:
  126. * png_ptr - pointer to a png input data structure
  127. * io_ptr - pointer to user supplied structure containing info about
  128. * the input functions. May be NULL.
  129. * read_data_fn - pointer to a new input function that takes as its
  130. * arguments a pointer to a png_struct, a pointer to
  131. * a location where input data can be stored, and a 32-bit
  132. * unsigned int that is the number of bytes to be read.
  133. * To exit and output any fatal error messages the new write
  134. * function should call png_error(png_ptr, "Error msg").
  135. * May be NULL, in which case libpng's default function will
  136. * be used.
  137. */
  138. void PNGAPI
  139. png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  140. png_rw_ptr read_data_fn)
  141. {
  142. if (png_ptr == NULL)
  143. return;
  144. png_ptr->io_ptr = io_ptr;
  145. #ifdef PNG_STDIO_SUPPORTED
  146. if (read_data_fn != NULL)
  147. png_ptr->read_data_fn = read_data_fn;
  148. else
  149. png_ptr->read_data_fn = png_default_read_data;
  150. #else
  151. png_ptr->read_data_fn = read_data_fn;
  152. #endif
  153. /* It is an error to write to a read device */
  154. if (png_ptr->write_data_fn != NULL)
  155. {
  156. png_ptr->write_data_fn = NULL;
  157. png_warning(png_ptr,
  158. "It's an error to set both read_data_fn and write_data_fn in the ");
  159. png_warning(png_ptr,
  160. "same structure. Resetting write_data_fn to NULL.");
  161. }
  162. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  163. png_ptr->output_flush_fn = NULL;
  164. #endif
  165. }
  166. #endif /* PNG_READ_SUPPORTED */