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