| 1 | /* |
| 2 | * pngxio.c - libpng extension: I/O state query. |
| 3 | * |
| 4 | * Copyright (C) 2003-2017 Cosmin Truta. |
| 5 | * This software is distributed under the same licensing and warranty terms |
| 6 | * as libpng. |
| 7 | * |
| 8 | * NOTE: |
| 9 | * The functionality provided in this module has "graduated", and is now |
| 10 | * part of libpng. The original code, retrofitted as a back-port, has |
| 11 | * limitations: it is thread-unsafe, and it only allows one png_ptr object |
| 12 | * for reading and one for writing. |
| 13 | * |
| 14 | * CAUTION: |
| 15 | * libpng-1.4.5 is the earliest version whose I/O state implementation |
| 16 | * can be used reliably. |
| 17 | */ |
| 18 | |
| 19 | #include "pngxutil.h"cxx . . $ `<file>/src/pngxtern/pngxutil.h`/ |
| 20 | |
| 21 | #define PNGX_INTERNALcxx . . $ `src/pngxtern/pngxio.c:21:9`! |
| 22 | #include "pngxpriv.h"cxx . . $ `<file>/src/pngxtern/pngxpriv.h`/ |
| 23 | |
| 24 | |
| 25 | #if PNG_LIBPNG_VERcxx . . $ `<external>/449245770443910636/png.h:319:9`! < 10405 |
| 26 | |
| 27 | |
| 28 | static png_structp _pngxio_read_ptr; |
| 29 | static png_structp _pngxio_write_ptr; |
| 30 | |
| 31 | static png_rw_ptr _pngxio_read_fn; |
| 32 | static png_rw_ptr _pngxio_write_fn; |
| 33 | |
| 34 | static int _pngxio_read_io_state; |
| 35 | static int _pngxio_write_io_state; |
| 36 | |
| 37 | static png_byte _pngxio_read_crt_chunk_hdr[9]; |
| 38 | static png_byte _pngxio_write_crt_chunk_hdr[9]; |
| 39 | |
| 40 | static unsigned int _pngxio_read_crt_chunk_hdr_len; |
| 41 | static unsigned int _pngxio_write_crt_chunk_hdr_len; |
| 42 | |
| 43 | static png_uint_32 _pngxio_read_crt_len; |
| 44 | static png_uint_32 _pngxio_write_crt_len; |
| 45 | |
| 46 | static const char *_pngxio_errmsg_invalid_argument = |
| 47 | "[PNGXIO internal] invalid argument"; |
| 48 | |
| 49 | |
| 50 | /* Update io_state and call the user-supplied read/write functions. */ |
| 51 | void /* PRIVATE */ |
| 52 | pngxio_read_write(png_structp png_ptr, png_bytep data, size_t length) |
| 53 | { |
| 54 | png_rw_ptr io_data_fn; |
| 55 | int *io_state_ptr; |
| 56 | int io_state_op; |
| 57 | png_byte *crt_chunk_hdr; |
| 58 | unsigned int *crt_chunk_hdr_len_ptr; |
| 59 | png_uint_32 *crt_len_ptr; |
| 60 | |
| 61 | if (png_ptr == _pngxio_read_ptr) |
| 62 | { |
| 63 | io_data_fn = _pngxio_read_fn; |
| 64 | io_state_ptr = &_pngxio_read_io_state; |
| 65 | io_state_op = PNGX_IO_READING; |
| 66 | crt_chunk_hdr = _pngxio_read_crt_chunk_hdr; |
| 67 | crt_chunk_hdr_len_ptr = &_pngxio_read_crt_chunk_hdr_len; |
| 68 | crt_len_ptr = &_pngxio_read_crt_len; |
| 69 | } |
| 70 | else if (png_ptr == _pngxio_write_ptr) |
| 71 | { |
| 72 | io_data_fn = _pngxio_write_fn; |
| 73 | io_state_ptr = &_pngxio_write_io_state; |
| 74 | io_state_op = PNGX_IO_WRITING; |
| 75 | crt_chunk_hdr = _pngxio_write_crt_chunk_hdr; |
| 76 | crt_chunk_hdr_len_ptr = &_pngxio_write_crt_chunk_hdr_len; |
| 77 | crt_len_ptr = &_pngxio_write_crt_len; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | png_error(png_ptr, _pngxio_errmsg_invalid_argument); |
| 82 | /* NOTREACHED */ |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | switch (*io_state_ptr & PNGX_IO_MASK_LOC) |
| 87 | { |
| 88 | case PNGX_IO_SIGNATURE: |
| 89 | /* libpng must serialize the signature in a single I/O session. */ |
| 90 | PNGX_ASSERT(length <= 8); |
| 91 | io_data_fn(png_ptr, data, length); |
| 92 | *io_state_ptr = io_state_op | PNGX_IO_CHUNK_HDR; |
| 93 | *crt_chunk_hdr_len_ptr = 0; |
| 94 | return; |
| 95 | case PNGX_IO_CHUNK_HDR: |
| 96 | /* libpng must serialize the chunk header in a single I/O session. |
| 97 | * (This was done in libpng-1.2.30, but regressed in libpng-1.4.0, |
| 98 | * so we cannot rely on it here.) |
| 99 | */ |
| 100 | PNGX_ASSERT(length == 4 || length == 8); |
| 101 | PNGX_ASSERT(length + *crt_chunk_hdr_len_ptr <= 8); |
| 102 | if (io_state_op == PNGX_IO_READING) |
| 103 | { |
| 104 | if (*crt_chunk_hdr_len_ptr == 0) |
| 105 | io_data_fn(png_ptr, crt_chunk_hdr, 8); |
| 106 | memcpy(data, crt_chunk_hdr + *crt_chunk_hdr_len_ptr, length); |
| 107 | *crt_chunk_hdr_len_ptr += length; |
| 108 | if (*crt_chunk_hdr_len_ptr < 8) |
| 109 | return; |
| 110 | *crt_len_ptr = png_get_uint_32(crt_chunk_hdr); |
| 111 | /* memcpy(png_ptr->chunk_name, crt_chunk_hdr + 4, 4); */ |
| 112 | } |
| 113 | else /* io_state_op == PNGX_IO_WRITING */ |
| 114 | { |
| 115 | memcpy(crt_chunk_hdr + *crt_chunk_hdr_len_ptr, data, length); |
| 116 | *crt_chunk_hdr_len_ptr += length; |
| 117 | if (*crt_chunk_hdr_len_ptr < 8) |
| 118 | return; |
| 119 | *crt_len_ptr = png_get_uint_32(crt_chunk_hdr); |
| 120 | /* memcpy(png_ptr->chunk_name, crt_chunk_hdr + 4, 4); */ |
| 121 | io_data_fn(png_ptr, crt_chunk_hdr, 8); |
| 122 | } |
| 123 | *crt_chunk_hdr_len_ptr = 0; |
| 124 | *io_state_ptr = io_state_op | PNGX_IO_CHUNK_DATA; |
| 125 | return; |
| 126 | case PNGX_IO_CHUNK_DATA: |
| 127 | /* libpng may serialize the chunk data in multiple I/O sessions. */ |
| 128 | if (length == 0) |
| 129 | return; |
| 130 | if (*crt_len_ptr > 0) |
| 131 | { |
| 132 | PNGX_ASSERT(length <= *crt_len_ptr); |
| 133 | io_data_fn(png_ptr, data, length); |
| 134 | *crt_len_ptr -= length; |
| 135 | return; |
| 136 | } |
| 137 | *io_state_ptr = io_state_op | PNGX_IO_CHUNK_CRC; |
| 138 | /* FALLTHROUGH */ |
| 139 | case PNGX_IO_CHUNK_CRC: |
| 140 | /* libpng must serialize the chunk CRC in a single I/O session. */ |
| 141 | PNGX_ASSERT(length == 4); |
| 142 | io_data_fn(png_ptr, data, 4); |
| 143 | *io_state_ptr = io_state_op | PNGX_IO_CHUNK_HDR; |
| 144 | return; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* Get png_ptr->io_state. */ |
| 149 | png_uint_32 PNGAPI |
| 150 | pngx_get_io_state(png_structp png_ptr) |
| 151 | { |
| 152 | png_uint_32 io_state; |
| 153 | |
| 154 | if (png_ptr == _pngxio_read_ptr) |
| 155 | io_state = _pngxio_read_io_state; |
| 156 | else if (png_ptr == _pngxio_write_ptr) |
| 157 | io_state = _pngxio_write_io_state; |
| 158 | else |
| 159 | { |
| 160 | png_error(png_ptr, _pngxio_errmsg_invalid_argument); |
| 161 | /* NOTREACHED */ |
| 162 | io_state = PNGX_IO_NONE; |
| 163 | } |
| 164 | |
| 165 | return io_state; |
| 166 | } |
| 167 | |
| 168 | /* Get png_ptr->chunk_name. */ |
| 169 | png_bytep PNGAPI |
| 170 | pngx_get_io_chunk_name(png_structp png_ptr) |
| 171 | { |
| 172 | png_bytep chunk_name; |
| 173 | |
| 174 | if (png_ptr == _pngxio_read_ptr) |
| 175 | chunk_name = _pngxio_read_crt_chunk_hdr + 4; |
| 176 | else if (png_ptr == _pngxio_write_ptr) |
| 177 | chunk_name = _pngxio_write_crt_chunk_hdr + 4; |
| 178 | else |
| 179 | { |
| 180 | png_error(png_ptr, _pngxio_errmsg_invalid_argument); |
| 181 | /* NOTREACHED */ |
| 182 | chunk_name = NULL; |
| 183 | } |
| 184 | |
| 185 | return chunk_name; |
| 186 | } |
| 187 | |
| 188 | /* Wrap png_set_read_fn. */ |
| 189 | void PNGAPI |
| 190 | pngx_set_read_fn(png_structp png_ptr, png_voidp io_ptr, |
| 191 | png_rw_ptr read_data_fn) |
| 192 | { |
| 193 | _pngxio_read_ptr = png_ptr; |
| 194 | _pngxio_write_ptr = NULL; |
| 195 | _pngxio_read_fn = read_data_fn; |
| 196 | png_set_read_fn(png_ptr, io_ptr, pngxio_read_write); |
| 197 | _pngxio_read_io_state = PNGX_IO_READING | PNGX_IO_SIGNATURE; |
| 198 | } |
| 199 | |
| 200 | /* Wrap png_set_write_fn. */ |
| 201 | void PNGAPI |
| 202 | pngx_set_write_fn(png_structp png_ptr, png_voidp io_ptr, |
| 203 | png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) |
| 204 | { |
| 205 | _pngxio_write_ptr = png_ptr; |
| 206 | _pngxio_read_ptr = NULL; |
| 207 | _pngxio_write_fn = write_data_fn; |
| 208 | png_set_write_fn(png_ptr, io_ptr, pngxio_read_write, output_flush_fn); |
| 209 | _pngxio_write_io_state = PNGX_IO_WRITING | PNGX_IO_SIGNATURE; |
| 210 | } |
| 211 | |
| 212 | /* Wrap png_write_sig. */ |
| 213 | void PNGAPI |
| 214 | pngx_write_sig(png_structp png_ptr) |
| 215 | { |
| 216 | #if PNG_LIBPNG_VER >= 10400 |
| 217 | png_write_sig(png_ptr); |
| 218 | #else |
| 219 | /* png_write_sig is not exported from the earlier libpng versions. */ |
| 220 | static png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
| 221 | pngxio_read_write(png_ptr, png_signature, 8); |
| 222 | /* TODO: Take png_ptr->sig_bytes into account. */ |
| 223 | #endif |
| 224 | } |
| 225 | |
| 226 | |
| 227 | #endif /* PNG_LIBPNG_VER < 10405 */ |