pngrio.c 5.2 KB

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