diff options
Diffstat (limited to 'jdatasrc.c')
-rw-r--r-- | jdatasrc.c | 36 |
1 files changed, 24 insertions, 12 deletions
@@ -5,8 +5,9 @@ * Copyright (C) 1994-1996, Thomas G. Lane. * Modified 2009-2011 by Guido Vollbeding. * libjpeg-turbo Modifications: - * Copyright (C) 2013, D. R. Commander. - * For conditions of distribution and use, see the accompanying README file. + * Copyright (C) 2013, 2016, D. R. Commander. + * For conditions of distribution and use, see the accompanying README.ijg + * file. * * This file contains decompression data source routines for the case of * reading JPEG data from memory or from a file (or any stdio stream). @@ -28,12 +29,12 @@ typedef struct { struct jpeg_source_mgr pub; /* public fields */ - FILE * infile; /* source stream */ - JOCTET * buffer; /* start of buffer */ + FILE *infile; /* source stream */ + JOCTET *buffer; /* start of buffer */ boolean start_of_file; /* have we gotten any data yet? */ } my_source_mgr; -typedef my_source_mgr * my_src_ptr; +typedef my_source_mgr *my_src_ptr; #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ @@ -161,7 +162,7 @@ fill_mem_input_buffer (j_decompress_ptr cinfo) METHODDEF(void) skip_input_data (j_decompress_ptr cinfo, long num_bytes) { - struct jpeg_source_mgr * src = cinfo->src; + struct jpeg_source_mgr *src = cinfo->src; /* Just a dumb implementation for now. Could use fseek() except * it doesn't work on pipes. Not clear that being smart is worth @@ -213,7 +214,7 @@ term_source (j_decompress_ptr cinfo) */ GLOBAL(void) -jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile) +jpeg_stdio_src (j_decompress_ptr cinfo, FILE *infile) { my_src_ptr src; @@ -221,8 +222,6 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile) * of JPEG images can be read from the same file by calling jpeg_stdio_src * only before the first one. (If we discarded the buffer at the end of * one image, we'd likely lose the start of the next one.) - * This makes it unsafe to use this manager and a different source - * manager serially with the same JPEG object. Caveat programmer. */ if (cinfo->src == NULL) { /* first time for this JPEG object? */ cinfo->src = (struct jpeg_source_mgr *) @@ -232,6 +231,14 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile) src->buffer = (JOCTET *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET)); + } else if (cinfo->src->init_source != init_source) { + /* It is unsafe to reuse the existing source manager unless it was created + * by this function. Otherwise, there is no guarantee that the opaque + * structure is the right size. Note that we could just create a new + * structure, but the old structure would not be freed until + * jpeg_destroy_decompress() was called. + */ + ERREXIT(cinfo, JERR_BUFFER_SIZE); } src = (my_src_ptr) cinfo->src; @@ -254,9 +261,9 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile) GLOBAL(void) jpeg_mem_src (j_decompress_ptr cinfo, - unsigned char * inbuffer, unsigned long insize) + const unsigned char *inbuffer, unsigned long insize) { - struct jpeg_source_mgr * src; + struct jpeg_source_mgr *src; if (inbuffer == NULL || insize == 0) /* Treat empty input as fatal error */ ERREXIT(cinfo, JERR_INPUT_EMPTY); @@ -269,6 +276,11 @@ jpeg_mem_src (j_decompress_ptr cinfo, cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(struct jpeg_source_mgr)); + } else if (cinfo->src->init_source != init_mem_source) { + /* It is unsafe to reuse the existing source manager unless it was created + * by this function. + */ + ERREXIT(cinfo, JERR_BUFFER_SIZE); } src = cinfo->src; @@ -278,6 +290,6 @@ jpeg_mem_src (j_decompress_ptr cinfo, src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->term_source = term_source; src->bytes_in_buffer = (size_t) insize; - src->next_input_byte = (JOCTET *) inbuffer; + src->next_input_byte = (const JOCTET *) inbuffer; } #endif |