00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define PNG_INTERNAL
00019 #include "png.h"
00020
00021
00022
00023
00024
00025
00026 void
00027 png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00028 {
00029 png_debug1(4,"reading %d bytes\n", (int)length);
00030 if (png_ptr->read_data_fn != NULL)
00031 (*(png_ptr->read_data_fn))(png_ptr, data, length);
00032 else
00033 png_error(png_ptr, "Call to NULL read function");
00034 }
00035
00036 #if !defined(PNG_NO_STDIO)
00037
00038
00039
00040
00041 #ifndef USE_FAR_KEYWORD
00042 void PNGAPI
00043 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00044 {
00045 png_size_t check;
00046
00047
00048
00049
00050 #if defined(_WIN32_WCE)
00051 if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00052 check = 0;
00053 #else
00054 check = (png_size_t)fread(data, (png_size_t)1, length,
00055 (png_FILE_p)png_ptr->io_ptr);
00056 #endif
00057
00058 if (check != length)
00059 png_error(png_ptr, "Read Error");
00060 }
00061 #else
00062
00063
00064
00065
00066
00067 #define NEAR_BUF_SIZE 1024
00068 #define MIN(a,b) (a <= b ? a : b)
00069
00070 static void
00071 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
00072 {
00073 int check;
00074 png_byte *n_data;
00075 png_FILE_p io_ptr;
00076
00077
00078 n_data = (png_byte *)CVT_PTR_NOCHECK(data);
00079 io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
00080 if ((png_bytep)n_data == data)
00081 {
00082 #if defined(_WIN32_WCE)
00083 if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
00084 check = 0;
00085 #else
00086 check = fread(n_data, 1, length, io_ptr);
00087 #endif
00088 }
00089 else
00090 {
00091 png_byte buf[NEAR_BUF_SIZE];
00092 png_size_t read, remaining, err;
00093 check = 0;
00094 remaining = length;
00095 do
00096 {
00097 read = MIN(NEAR_BUF_SIZE, remaining);
00098 #if defined(_WIN32_WCE)
00099 if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
00100 err = 0;
00101 #else
00102 err = fread(buf, (png_size_t)1, read, io_ptr);
00103 #endif
00104 png_memcpy(data, buf, read);
00105 if(err != read)
00106 break;
00107 else
00108 check += err;
00109 data += read;
00110 remaining -= read;
00111 }
00112 while (remaining != 0);
00113 }
00114 if ((png_uint_32)check != (png_uint_32)length)
00115 png_error(png_ptr, "read Error");
00116 }
00117 #endif
00118 #endif
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 void PNGAPI
00134 png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
00135 png_rw_ptr read_data_fn)
00136 {
00137 png_ptr->io_ptr = io_ptr;
00138
00139 #if !defined(PNG_NO_STDIO)
00140 if (read_data_fn != NULL)
00141 png_ptr->read_data_fn = read_data_fn;
00142 else
00143 png_ptr->read_data_fn = png_default_read_data;
00144 #else
00145 png_ptr->read_data_fn = read_data_fn;
00146 #endif
00147
00148
00149 if (png_ptr->write_data_fn != NULL)
00150 {
00151 png_ptr->write_data_fn = NULL;
00152 png_warning(png_ptr,
00153 "It's an error to set both read_data_fn and write_data_fn in the ");
00154 png_warning(png_ptr,
00155 "same structure. Resetting write_data_fn to NULL.");
00156 }
00157
00158 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
00159 png_ptr->output_flush_fn = NULL;
00160 #endif
00161 }