1 /*
2 * memsrc.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains decompression data source routines for the case of
9 * reading JPEG data from a memory buffer that is preloaded with the entire
10 * JPEG file. This would not seem especially useful at first sight, but
11 * a number of people have asked for it.
12 * This is really just a stripped-down version of jdatasrc.c. Comparison
13 * of this code with jdatasrc.c may be helpful in seeing how to make
14 * custom source managers for other purposes.
15 */
16 
17 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jerror.h"
21 
22 
23 /* Expanded data source object for memory input */
24 
25 typedef struct {
26 struct jpeg_source_mgr pub; /* public fields */
27 
28 JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
29 } my_source_mgr;
30 
31 typedef my_source_mgr * my_src_ptr;
32 
33 
34 /*
35 * Initialize source --- called by jpeg_read_header
36 * before any data is actually read.
37 */
38 
39 METHODDEF(void)
init_source(j_decompress_ptr cinfo)40 init_source (j_decompress_ptr cinfo)
41 {
42 /* No work, since jpeg_memory_src set up the buffer pointer and count.
43 * Indeed, if we want to read multiple JPEG images from one buffer,
44 * this *must* not do anything to the pointer.
45 */
46 }
47 
48 
49 /*
50 * Fill the input buffer --- called whenever buffer is emptied.
51 *
52 * In this application, this routine should never be called; if it is called,
53 * the decompressor has overrun the end of the input buffer, implying we
54 * supplied an incomplete or corrupt JPEG datastream. A simple error exit
55 * might be the most appropriate response.
56 *
57 * But what we choose to do in this code is to supply dummy EOI markers
58 * in order to force the decompressor to finish processing and supply
59 * some sort of output image, no matter how corrupted.
60 */
61 
62 METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)63 fill_input_buffer (j_decompress_ptr cinfo)
64 {
65 my_src_ptr src = (my_src_ptr) cinfo->src;
66 
67 WARNMS(cinfo, JWRN_JPEG_EOF);
68 
69 /* Create a fake EOI marker */
70 src->eoi_buffer[0] = (JOCTET) 0xFF;
71 src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
72 src->pub.next_input_byte = src->eoi_buffer;
73 src->pub.bytes_in_buffer = 2;
74 
75 return TRUE;
76 }
77 
78 
79 /*
80 * Skip data --- used to skip over a potentially large amount of
81 * uninteresting data (such as an APPn marker).
82 *
83 * If we overrun the end of the buffer, we let fill_input_buffer deal with
84 * it. An extremely large skip could cause some time-wasting here, but
85 * it really isn't supposed to happen ... and the decompressor will never
86 * skip more than 64K anyway.
87 */
88 
89 METHODDEF(void)
skip_input_data(j_decompress_ptr cinfo,long num_bytes)90 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
91 {
92 my_src_ptr src = (my_src_ptr) cinfo->src;
93 
94 if (num_bytes > 0) {
95 while (num_bytes > (long) src->pub.bytes_in_buffer) {
96 num_bytes -= (long) src->pub.bytes_in_buffer;
97 (void) fill_input_buffer(cinfo);
98 /* note we assume that fill_input_buffer will never return FALSE,
99 * so suspension need not be handled.
100 */
101 }
102 src->pub.next_input_byte += (size_t) num_bytes;
103 src->pub.bytes_in_buffer -= (size_t) num_bytes;
104 }
105 }
106 
107 
108 /*
109 * An additional method that can be provided by data source modules is the
110 * resync_to_restart method for error recovery in the presence of RST markers.
111 * For the moment, this source module just uses the default resync method
112 * provided by the JPEG library. That method assumes that no backtracking
113 * is possible.
114 */
115 
116 
117 /*
118 * Terminate source --- called by jpeg_finish_decompress
119 * after all data has been read. Often a no-op.
120 *
121 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
122 * application must deal with any cleanup that should happen even
123 * for error exit.
124 */
125 
126 METHODDEF(void)
term_source(j_decompress_ptr cinfo)127 term_source (j_decompress_ptr cinfo)
128 {
129 /* no work necessary here */
130 }
131 
132 
133 /*
134 * Prepare for input from a memory buffer.
135 */
136 
137 GLOBAL(void)
jpeg_memory_src(j_decompress_ptr cinfo,const JOCTET * buffer,size_t bufsize)138 jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
139 {
140 my_src_ptr src;
141 
142 /* The source object is made permanent so that a series of JPEG images
143 * can be read from a single buffer by calling jpeg_memory_src
144 * only before the first one.
145 * This makes it unsafe to use this manager and a different source
146 * manager serially with the same JPEG object. Caveat programmer.
147 */
148 if (cinfo->src == NULL) { /* first time for this JPEG object? */
149 cinfo->src = (struct jpeg_source_mgr *)
150 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
151 SIZEOF(my_source_mgr));
152 }
153 
154 src = (my_src_ptr) cinfo->src;
155 src->pub.init_source = init_source;
156 src->pub.fill_input_buffer = fill_input_buffer;
157 src->pub.skip_input_data = skip_input_data;
158 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
159 src->pub.term_source = term_source;
160 
161 src->pub.next_input_byte = buffer;
162 src->pub.bytes_in_buffer = bufsize;
163 }
164