CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/libpng17/pngrio.c
Views: 1401
1
2
/* pngrio.c - functions for data input
3
*
4
* Last changed in libpng 1.7.0 [(PENDING RELEASE)]
5
* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
6
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
*
9
* This code is released under the libpng license.
10
* For conditions of distribution and use, see the disclaimer
11
* and license in png.h
12
*
13
* This file provides a location for all input. Users who need
14
* special handling are expected to write a function that has the same
15
* arguments as this and performs a similar function, but that possibly
16
* has a different input method. Note that you shouldn't change this
17
* function, but rather write a replacement function and then make
18
* libpng use it at run time with png_set_read_fn(...).
19
*/
20
21
#include "pngpriv.h"
22
#define PNG_SRC_FILE PNG_SRC_FILE_pngrio
23
24
#ifdef PNG_READ_SUPPORTED
25
26
/* Read the data from whatever input you are using. The default routine
27
* reads from a file pointer. Note that this routine sometimes gets called
28
* with very small lengths, so you should implement some kind of simple
29
* buffering if you are using unbuffered reads. This should never be asked
30
* to read more than 64K on a 16 bit machine.
31
*/
32
void /* PRIVATE */
33
png_read_data(png_structrp png_ptr, png_voidp data, png_size_t length)
34
{
35
png_debug1(4, "reading %d bytes", (int)length);
36
37
/* This was guaranteed by prior versions of libpng, so app callbacks may
38
* assume it even though it isn't documented to be the case.
39
*/
40
debug(length > 0U);
41
42
if (png_ptr->rw_data_fn != NULL)
43
png_ptr->rw_data_fn(png_ptr, png_voidcast(png_bytep,data), length);
44
45
else
46
png_app_error(png_ptr, "No read function");
47
}
48
49
#ifdef PNG_STDIO_SUPPORTED
50
/* This is the function that does the actual reading of data. If you are
51
* not reading from a standard C stream, you should create a replacement
52
* read_data function and use it at run time with png_set_read_fn(), rather
53
* than changing the library.
54
*/
55
void PNGCBAPI
56
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
57
{
58
png_size_t check;
59
60
if (png_ptr == NULL)
61
return;
62
63
/* fread() returns 0 on error, so it is OK to store this in a png_size_t
64
* instead of an int, which is what fread() actually returns.
65
*/
66
check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));
67
68
if (check != length)
69
png_error(png_ptr, "Read Error");
70
}
71
#endif /* STDIO */
72
73
/* This function allows the application to supply a new input function
74
* for libpng if standard C streams aren't being used.
75
*
76
* This function takes as its arguments:
77
*
78
* png_ptr - pointer to a png input data structure
79
*
80
* io_ptr - pointer to user supplied structure containing info about
81
* the input functions. May be NULL.
82
*
83
* read_data_fn - pointer to a new input function that takes as its
84
* arguments a pointer to a png_struct, a pointer to
85
* a location where input data can be stored, and a 32-bit
86
* unsigned int that is the number of bytes to be read.
87
* To exit and output any fatal error messages the new write
88
* function should call png_error(png_ptr, "Error msg").
89
* May be NULL, in which case libpng's default function will
90
* be used.
91
*/
92
void PNGAPI
93
png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr,
94
png_rw_ptr read_data_fn)
95
{
96
if (png_ptr == NULL)
97
return;
98
99
if (!png_ptr->read_struct)
100
{
101
png_app_error(png_ptr, "cannot set a read function on a write struct");
102
return;
103
}
104
105
if (read_data_fn == NULL)
106
{
107
png_app_error(png_ptr, "API change: png_set_read_fn requires a function");
108
return;
109
}
110
111
png_ptr->io_ptr = io_ptr;
112
png_ptr->rw_data_fn = read_data_fn;
113
}
114
#endif /* READ */
115
116