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