Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.2.1.x/jdatadst.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdatadst.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1996, Thomas G. Lane.
6
 * Modified 2009-2012 by Guido Vollbeding.
7
 * libjpeg-turbo Modifications:
8
 * Copyright (C) 2013, 2016, 2022, D. R. Commander.
9
 * For conditions of distribution and use, see the accompanying README.ijg
10
 * file.
11
 *
12
 * This file contains compression data destination routines for the case of
13
 * emitting JPEG data to memory or to a file (or any stdio stream).
14
 * While these routines are sufficient for most applications,
15
 * some will want to use a different destination manager.
16
 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
17
 * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
18
 * than 8 bits on your machine, you may need to do some tweaking.
19
 */
20
21
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
22
#include "jinclude.h"
23
#include "jpeglib.h"
24
#include "jerror.h"
25
26
27
/* Expanded data destination object for stdio output */
28
29
typedef struct {
30
  struct jpeg_destination_mgr pub; /* public fields */
31
32
  FILE *outfile;                /* target stream */
33
  JOCTET *buffer;               /* start of buffer */
34
} my_destination_mgr;
35
36
typedef my_destination_mgr *my_dest_ptr;
37
38
9.99k
#define OUTPUT_BUF_SIZE  4096   /* choose an efficiently fwrite'able size */
39
40
41
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
42
/* Expanded data destination object for memory output */
43
44
typedef struct {
45
  struct jpeg_destination_mgr pub; /* public fields */
46
47
  unsigned char **outbuffer;    /* target buffer */
48
  unsigned long *outsize;
49
  unsigned char *newbuffer;     /* newly allocated buffer */
50
  JOCTET *buffer;               /* start of buffer */
51
  size_t bufsize;
52
} my_mem_destination_mgr;
53
54
typedef my_mem_destination_mgr *my_mem_dest_ptr;
55
#endif
56
57
58
/*
59
 * Initialize destination --- called by jpeg_start_compress
60
 * before any data is actually written.
61
 */
62
63
METHODDEF(void)
64
init_destination(j_compress_ptr cinfo)
65
0
{
66
0
  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
67
68
  /* Allocate the output buffer --- it will be released when done with image */
69
0
  dest->buffer = (JOCTET *)
70
0
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
71
0
                                OUTPUT_BUF_SIZE * sizeof(JOCTET));
72
73
0
  dest->pub.next_output_byte = dest->buffer;
74
0
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
75
0
}
76
77
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
78
METHODDEF(void)
79
init_mem_destination(j_compress_ptr cinfo)
80
4.99k
{
81
  /* no work necessary here */
82
4.99k
}
83
#endif
84
85
86
/*
87
 * Empty the output buffer --- called whenever buffer fills up.
88
 *
89
 * In typical applications, this should write the entire output buffer
90
 * (ignoring the current state of next_output_byte & free_in_buffer),
91
 * reset the pointer & count to the start of the buffer, and return TRUE
92
 * indicating that the buffer has been dumped.
93
 *
94
 * In applications that need to be able to suspend compression due to output
95
 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
96
 * In this situation, the compressor will return to its caller (possibly with
97
 * an indication that it has not accepted all the supplied scanlines).  The
98
 * application should resume compression after it has made more room in the
99
 * output buffer.  Note that there are substantial restrictions on the use of
100
 * suspension --- see the documentation.
101
 *
102
 * When suspending, the compressor will back up to a convenient restart point
103
 * (typically the start of the current MCU). next_output_byte & free_in_buffer
104
 * indicate where the restart point will be if the current call returns FALSE.
105
 * Data beyond this point will be regenerated after resumption, so do not
106
 * write it out when emptying the buffer externally.
107
 */
108
109
METHODDEF(boolean)
110
empty_output_buffer(j_compress_ptr cinfo)
111
0
{
112
0
  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
113
114
0
  if (fwrite(dest->buffer, 1, OUTPUT_BUF_SIZE, dest->outfile) !=
115
0
      (size_t)OUTPUT_BUF_SIZE)
116
0
    ERREXIT(cinfo, JERR_FILE_WRITE);
117
118
0
  dest->pub.next_output_byte = dest->buffer;
119
0
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
120
121
0
  return TRUE;
122
0
}
123
124
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
125
METHODDEF(boolean)
126
empty_mem_output_buffer(j_compress_ptr cinfo)
127
2.43k
{
128
2.43k
  size_t nextsize;
129
2.43k
  JOCTET *nextbuffer;
130
2.43k
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
131
132
  /* Try to allocate new buffer with double size */
133
2.43k
  nextsize = dest->bufsize * 2;
134
2.43k
  nextbuffer = (JOCTET *)malloc(nextsize);
135
136
2.43k
  if (nextbuffer == NULL)
137
0
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
138
139
2.43k
  memcpy(nextbuffer, dest->buffer, dest->bufsize);
140
141
2.43k
  free(dest->newbuffer);
142
143
2.43k
  dest->newbuffer = nextbuffer;
144
145
2.43k
  dest->pub.next_output_byte = nextbuffer + dest->bufsize;
146
2.43k
  dest->pub.free_in_buffer = dest->bufsize;
147
148
2.43k
  dest->buffer = nextbuffer;
149
2.43k
  dest->bufsize = nextsize;
150
151
2.43k
  return TRUE;
152
2.43k
}
153
#endif
154
155
156
/*
157
 * Terminate destination --- called by jpeg_finish_compress
158
 * after all data has been written.  Usually needs to flush buffer.
159
 *
160
 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
161
 * application must deal with any cleanup that should happen even
162
 * for error exit.
163
 */
164
165
METHODDEF(void)
166
term_destination(j_compress_ptr cinfo)
167
0
{
168
0
  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
169
0
  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
170
171
  /* Write any data remaining in the buffer */
172
0
  if (datacount > 0) {
173
0
    if (fwrite(dest->buffer, 1, datacount, dest->outfile) != datacount)
174
0
      ERREXIT(cinfo, JERR_FILE_WRITE);
175
0
  }
176
0
  fflush(dest->outfile);
177
  /* Make sure we wrote the output file OK */
178
0
  if (ferror(dest->outfile))
179
0
    ERREXIT(cinfo, JERR_FILE_WRITE);
180
0
}
181
182
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
183
METHODDEF(void)
184
term_mem_destination(j_compress_ptr cinfo)
185
4.27k
{
186
4.27k
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
187
188
4.27k
  *dest->outbuffer = dest->buffer;
189
4.27k
  *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
190
4.27k
}
191
#endif
192
193
194
/*
195
 * Prepare for output to a stdio stream.
196
 * The caller must have already opened the stream, and is responsible
197
 * for closing it after finishing compression.
198
 */
199
200
GLOBAL(void)
201
jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
202
0
{
203
0
  my_dest_ptr dest;
204
205
  /* The destination object is made permanent so that multiple JPEG images
206
   * can be written to the same file without re-executing jpeg_stdio_dest.
207
   */
208
0
  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
209
0
    cinfo->dest = (struct jpeg_destination_mgr *)
210
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
211
0
                                  sizeof(my_destination_mgr));
212
0
  } else if (cinfo->dest->init_destination != init_destination) {
213
    /* It is unsafe to reuse the existing destination manager unless it was
214
     * created by this function.  Otherwise, there is no guarantee that the
215
     * opaque structure is the right size.  Note that we could just create a
216
     * new structure, but the old structure would not be freed until
217
     * jpeg_destroy_compress() was called.
218
     */
219
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
220
0
  }
221
222
0
  dest = (my_dest_ptr)cinfo->dest;
223
0
  dest->pub.init_destination = init_destination;
224
0
  dest->pub.empty_output_buffer = empty_output_buffer;
225
0
  dest->pub.term_destination = term_destination;
226
0
  dest->outfile = outfile;
227
0
}
228
229
230
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
231
/*
232
 * Prepare for output to a memory buffer.
233
 * The caller may supply an own initial buffer with appropriate size.
234
 * Otherwise, or when the actual data output exceeds the given size,
235
 * the library adapts the buffer size as necessary.
236
 * The standard library functions malloc/free are used for allocating
237
 * larger memory, so the buffer is available to the application after
238
 * finishing compression, and then the application is responsible for
239
 * freeing the requested memory.
240
 * Note:  An initial buffer supplied by the caller is expected to be
241
 * managed by the application.  The library does not free such buffer
242
 * when allocating a larger buffer.
243
 */
244
245
GLOBAL(void)
246
jpeg_mem_dest(j_compress_ptr cinfo, unsigned char **outbuffer,
247
              unsigned long *outsize)
248
4.99k
{
249
4.99k
  my_mem_dest_ptr dest;
250
251
4.99k
  if (outbuffer == NULL || outsize == NULL)     /* sanity check */
252
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
253
254
  /* The destination object is made permanent so that multiple JPEG images
255
   * can be written to the same buffer without re-executing jpeg_mem_dest.
256
   */
257
4.99k
  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
258
4.99k
    cinfo->dest = (struct jpeg_destination_mgr *)
259
4.99k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
260
4.99k
                                  sizeof(my_mem_destination_mgr));
261
4.99k
  } else if (cinfo->dest->init_destination != init_mem_destination) {
262
    /* It is unsafe to reuse the existing destination manager unless it was
263
     * created by this function.
264
     */
265
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
266
0
  }
267
268
4.99k
  dest = (my_mem_dest_ptr)cinfo->dest;
269
4.99k
  dest->pub.init_destination = init_mem_destination;
270
4.99k
  dest->pub.empty_output_buffer = empty_mem_output_buffer;
271
4.99k
  dest->pub.term_destination = term_mem_destination;
272
4.99k
  dest->outbuffer = outbuffer;
273
4.99k
  dest->outsize = outsize;
274
4.99k
  dest->newbuffer = NULL;
275
276
4.99k
  if (*outbuffer == NULL || *outsize == 0) {
277
    /* Allocate initial buffer */
278
4.99k
    dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
279
4.99k
    if (dest->newbuffer == NULL)
280
0
      ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
281
4.99k
    *outsize = OUTPUT_BUF_SIZE;
282
4.99k
  }
283
284
4.99k
  dest->pub.next_output_byte = dest->buffer = *outbuffer;
285
4.99k
  dest->pub.free_in_buffer = dest->bufsize = *outsize;
286
4.99k
}
287
#endif