2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
23 |
*/
|
|
24 |
|
|
25 |
/* pngrio.c - functions for data input
|
|
26 |
*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file and, per its terms, should not be removed:
|
|
31 |
*
|
51304
|
32 |
* Last changed in libpng 1.6.35 [July 15, 2018]
|
|
33 |
* Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson
|
2
|
34 |
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
|
35 |
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
|
36 |
*
|
10576
|
37 |
* This code is released under the libpng license.
|
|
38 |
* For conditions of distribution and use, see the disclaimer
|
|
39 |
* and license in png.h
|
|
40 |
*
|
2
|
41 |
* This file provides a location for all input. Users who need
|
|
42 |
* special handling are expected to write a function that has the same
|
|
43 |
* arguments as this and performs a similar function, but that possibly
|
|
44 |
* has a different input method. Note that you shouldn't change this
|
|
45 |
* function, but rather write a replacement function and then make
|
|
46 |
* libpng use it at run time with png_set_read_fn(...).
|
|
47 |
*/
|
|
48 |
|
10576
|
49 |
#include "pngpriv.h"
|
2
|
50 |
|
10576
|
51 |
#ifdef PNG_READ_SUPPORTED
|
2
|
52 |
|
|
53 |
/* Read the data from whatever input you are using. The default routine
|
10576
|
54 |
* reads from a file pointer. Note that this routine sometimes gets called
|
|
55 |
* with very small lengths, so you should implement some kind of simple
|
|
56 |
* buffering if you are using unbuffered reads. This should never be asked
|
35296
|
57 |
* to read more than 64K on a 16-bit machine.
|
10576
|
58 |
*/
|
2
|
59 |
void /* PRIVATE */
|
51304
|
60 |
png_read_data(png_structrp png_ptr, png_bytep data, size_t length)
|
2
|
61 |
{
|
10576
|
62 |
png_debug1(4, "reading %d bytes", (int)length);
|
|
63 |
|
2
|
64 |
if (png_ptr->read_data_fn != NULL)
|
|
65 |
(*(png_ptr->read_data_fn))(png_ptr, data, length);
|
10576
|
66 |
|
2
|
67 |
else
|
|
68 |
png_error(png_ptr, "Call to NULL read function");
|
|
69 |
}
|
|
70 |
|
10576
|
71 |
#ifdef PNG_STDIO_SUPPORTED
|
2
|
72 |
/* This is the function that does the actual reading of data. If you are
|
10576
|
73 |
* not reading from a standard C stream, you should create a replacement
|
|
74 |
* read_data function and use it at run time with png_set_read_fn(), rather
|
|
75 |
* than changing the library.
|
|
76 |
*/
|
|
77 |
void PNGCBAPI
|
51304
|
78 |
png_default_read_data(png_structp png_ptr, png_bytep data, size_t length)
|
2
|
79 |
{
|
51304
|
80 |
size_t check;
|
2
|
81 |
|
10576
|
82 |
if (png_ptr == NULL)
|
|
83 |
return;
|
|
84 |
|
51304
|
85 |
/* fread() returns 0 on error, so it is OK to store this in a size_t
|
2
|
86 |
* instead of an int, which is what fread() actually returns.
|
|
87 |
*/
|
29913
|
88 |
check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));
|
2
|
89 |
|
|
90 |
if (check != length)
|
|
91 |
png_error(png_ptr, "Read Error");
|
|
92 |
}
|
|
93 |
#endif
|
|
94 |
|
|
95 |
/* This function allows the application to supply a new input function
|
10576
|
96 |
* for libpng if standard C streams aren't being used.
|
|
97 |
*
|
|
98 |
* This function takes as its arguments:
|
|
99 |
*
|
|
100 |
* png_ptr - pointer to a png input data structure
|
|
101 |
*
|
|
102 |
* io_ptr - pointer to user supplied structure containing info about
|
|
103 |
* the input functions. May be NULL.
|
|
104 |
*
|
|
105 |
* read_data_fn - pointer to a new input function that takes as its
|
|
106 |
* arguments a pointer to a png_struct, a pointer to
|
|
107 |
* a location where input data can be stored, and a 32-bit
|
|
108 |
* unsigned int that is the number of bytes to be read.
|
|
109 |
* To exit and output any fatal error messages the new write
|
|
110 |
* function should call png_error(png_ptr, "Error msg").
|
|
111 |
* May be NULL, in which case libpng's default function will
|
|
112 |
* be used.
|
|
113 |
*/
|
2
|
114 |
void PNGAPI
|
29913
|
115 |
png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr,
|
43321
|
116 |
png_rw_ptr read_data_fn)
|
2
|
117 |
{
|
10576
|
118 |
if (png_ptr == NULL)
|
|
119 |
return;
|
|
120 |
|
2
|
121 |
png_ptr->io_ptr = io_ptr;
|
|
122 |
|
10576
|
123 |
#ifdef PNG_STDIO_SUPPORTED
|
2
|
124 |
if (read_data_fn != NULL)
|
|
125 |
png_ptr->read_data_fn = read_data_fn;
|
10576
|
126 |
|
2
|
127 |
else
|
|
128 |
png_ptr->read_data_fn = png_default_read_data;
|
|
129 |
#else
|
|
130 |
png_ptr->read_data_fn = read_data_fn;
|
|
131 |
#endif
|
|
132 |
|
29913
|
133 |
#ifdef PNG_WRITE_SUPPORTED
|
2
|
134 |
/* It is an error to write to a read device */
|
|
135 |
if (png_ptr->write_data_fn != NULL)
|
|
136 |
{
|
|
137 |
png_ptr->write_data_fn = NULL;
|
|
138 |
png_warning(png_ptr,
|
10576
|
139 |
"Can't set both read_data_fn and write_data_fn in the"
|
|
140 |
" same structure");
|
2
|
141 |
}
|
29913
|
142 |
#endif
|
2
|
143 |
|
10576
|
144 |
#ifdef PNG_WRITE_FLUSH_SUPPORTED
|
2
|
145 |
png_ptr->output_flush_fn = NULL;
|
|
146 |
#endif
|
|
147 |
}
|
29913
|
148 |
#endif /* READ */
|