Rev 1897 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1897 | Rev 3928 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* pngrio.c - functions for data input |
1 | /* pngrio.c - functions for data input |
2 | * |
2 | * |
3 | * Last changed in libpng 1.5.0 [January 6, 2011] |
3 | * Last changed in libpng 1.6.0 [February 14, 2013] |
4 | * Copyright (c) 1998-2011 Glenn Randers-Pehrson |
4 | * Copyright (c) 1998-2013 Glenn Randers-Pehrson |
5 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
5 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
6 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
6 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
7 | * |
7 | * |
8 | * This code is released under the libpng license. |
8 | * This code is released under the libpng license. |
9 | * For conditions of distribution and use, see the disclaimer |
9 | * For conditions of distribution and use, see the disclaimer |
Line 27... | Line 27... | ||
27 | * buffering if you are using unbuffered reads. This should never be asked |
27 | * buffering if you are using unbuffered reads. This should never be asked |
28 | * to read more then 64K on a 16 bit machine. |
28 | * to read more then 64K on a 16 bit machine. |
29 | */ |
29 | */ |
30 | void /* PRIVATE */ |
30 | void /* PRIVATE */ |
31 | png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
31 | png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length) |
32 | { |
32 | { |
33 | png_debug1(4, "reading %d bytes", (int)length); |
33 | png_debug1(4, "reading %d bytes", (int)length); |
34 | 34 | ||
Line 35... | Line 35... | ||
35 | if (png_ptr->read_data_fn != NULL) |
35 | if (png_ptr->read_data_fn != NULL) |
36 | (*(png_ptr->read_data_fn))(png_ptr, data, length); |
36 | (*(png_ptr->read_data_fn))(png_ptr, data, length); |
Line 44... | Line 44... | ||
44 | * not reading from a standard C stream, you should create a replacement |
44 | * not reading from a standard C stream, you should create a replacement |
45 | * read_data function and use it at run time with png_set_read_fn(), rather |
45 | * read_data function and use it at run time with png_set_read_fn(), rather |
46 | * than changing the library. |
46 | * than changing the library. |
47 | */ |
47 | */ |
48 | # ifndef USE_FAR_KEYWORD |
48 | void PNGCBAPI |
49 | void PNGCBAPI |
- | |
50 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
49 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
51 | { |
50 | { |
52 | png_size_t check; |
51 | png_size_t check; |
53 | 52 | ||
Line 56... | Line 55... | ||
56 | 55 | ||
Line 57... | Line 56... | ||
57 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t |
56 | /* fread() returns 0 on error, so it is OK to store this in a png_size_t |
58 | * instead of an int, which is what fread() actually returns. |
57 | * instead of an int, which is what fread() actually returns. |
59 | */ |
58 | */ |
60 | check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr); |
59 | check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr)); |
Line 61... | Line 60... | ||
61 | 60 | ||
62 | if (check != length) |
61 | if (check != length) |
63 | png_error(png_ptr, "Read Error"); |
62 | png_error(png_ptr, "Read Error"); |
64 | } |
- | |
65 | # else |
- | |
66 | /* This is the model-independent version. Since the standard I/O library |
- | |
67 | can't handle far buffers in the medium and small models, we have to copy |
- | |
68 | the data. |
- | |
69 | */ |
- | |
70 | - | ||
71 | #define NEAR_BUF_SIZE 1024 |
- | |
72 | #define MIN(a,b) (a <= b ? a : b) |
- | |
73 | - | ||
74 | static void PNGCBAPI |
- | |
75 | png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) |
- | |
76 | { |
- | |
77 | png_size_t check; |
- | |
78 | png_byte *n_data; |
- | |
79 | png_FILE_p io_ptr; |
- | |
80 | - | ||
81 | if (png_ptr == NULL) |
- | |
82 | return; |
- | |
83 | - | ||
84 | /* Check if data really is near. If so, use usual code. */ |
- | |
85 | n_data = (png_byte *)CVT_PTR_NOCHECK(data); |
- | |
86 | io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); |
- | |
87 | - | ||
88 | if ((png_bytep)n_data == data) |
- | |
89 | { |
- | |
90 | check = fread(n_data, 1, length, io_ptr); |
- | |
91 | } |
- | |
92 | - | ||
93 | else |
- | |
94 | { |
- | |
95 | png_byte buf[NEAR_BUF_SIZE]; |
- | |
96 | png_size_t read, remaining, err; |
- | |
97 | check = 0; |
- | |
98 | remaining = length; |
- | |
99 | - | ||
100 | do |
- | |
101 | { |
- | |
102 | read = MIN(NEAR_BUF_SIZE, remaining); |
- | |
103 | err = fread(buf, 1, read, io_ptr); |
- | |
104 | png_memcpy(data, buf, read); /* copy far buffer to near buffer */ |
- | |
105 | - | ||
106 | if (err != read) |
- | |
107 | break; |
- | |
108 | - | ||
109 | else |
- | |
110 | check += err; |
- | |
111 | - | ||
112 | data += read; |
- | |
113 | remaining -= read; |
- | |
114 | } |
- | |
115 | while (remaining != 0); |
- | |
116 | } |
- | |
117 | - | ||
118 | if ((png_uint_32)check != (png_uint_32)length) |
- | |
119 | png_error(png_ptr, "read Error"); |
- | |
120 | } |
- | |
121 | # endif |
63 | } |
Line 122... | Line 64... | ||
122 | #endif |
64 | #endif |
123 | 65 | ||
124 | /* This function allows the application to supply a new input function |
66 | /* This function allows the application to supply a new input function |
Line 140... | Line 82... | ||
140 | * May be NULL, in which case libpng's default function will |
82 | * May be NULL, in which case libpng's default function will |
141 | * be used. |
83 | * be used. |
142 | */ |
84 | */ |
143 | void PNGAPI |
85 | void PNGAPI |
144 | png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, |
86 | png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr, |
145 | png_rw_ptr read_data_fn) |
87 | png_rw_ptr read_data_fn) |
146 | { |
88 | { |
147 | if (png_ptr == NULL) |
89 | if (png_ptr == NULL) |
148 | return; |
90 | return; |
149 | 91 |