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