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