Coverage Report

Created: 2026-04-12 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/jdatadst-tj.c
Line
Count
Source
1
/*
2
 * jdatadst-tj.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) 2011, 2014, 2016, 2019, 2022-2023, 2025-2026, 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
#define JPEG_INTERNALS
23
#include "jinclude.h"
24
#include "jpeglib.h"
25
#include "jerror.h"
26
#if !defined(_MSC_VER) || _MSC_VER > 1600
27
#include <stdint.h>
28
#endif
29
30
void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
31
                      size_t *outsize, boolean alloc);
32
33
34
40.5k
#define OUTPUT_BUF_SIZE  4096   /* choose an efficiently fwrite'able size */
35
36
37
/* Expanded data destination object for memory output */
38
39
typedef struct {
40
  struct jpeg_destination_mgr pub; /* public fields */
41
42
  unsigned char **outbuffer;    /* target buffer */
43
  size_t *outsize;
44
  unsigned char *newbuffer;     /* newly allocated buffer */
45
  JOCTET *buffer;               /* start of buffer */
46
  size_t bufsize;
47
  boolean alloc;
48
} my_mem_destination_mgr;
49
50
typedef my_mem_destination_mgr *my_mem_dest_ptr;
51
52
53
/*
54
 * Initialize destination --- called by jpeg_start_compress
55
 * before any data is actually written.
56
 */
57
58
METHODDEF(void)
59
init_mem_destination(j_compress_ptr cinfo)
60
238k
{
61
  /* no work necessary here */
62
238k
}
63
64
65
/*
66
 * Empty the output buffer --- called whenever buffer fills up.
67
 *
68
 * In typical applications, this should write the entire output buffer
69
 * (ignoring the current state of next_output_byte & free_in_buffer),
70
 * reset the pointer & count to the start of the buffer, and return TRUE
71
 * indicating that the buffer has been dumped.
72
 *
73
 * In applications that need to be able to suspend compression due to output
74
 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
75
 * In this situation, the compressor will return to its caller (possibly with
76
 * an indication that it has not accepted all the supplied scanlines).  The
77
 * application should resume compression after it has made more room in the
78
 * output buffer.  Note that there are substantial restrictions on the use of
79
 * suspension --- see the documentation.
80
 *
81
 * When suspending, the compressor will back up to a convenient restart point
82
 * (typically the start of the current MCU). next_output_byte & free_in_buffer
83
 * indicate where the restart point will be if the current call returns FALSE.
84
 * Data beyond this point will be regenerated after resumption, so do not
85
 * write it out when emptying the buffer externally.
86
 */
87
88
METHODDEF(boolean)
89
empty_mem_output_buffer(j_compress_ptr cinfo)
90
211k
{
91
211k
  size_t nextsize;
92
211k
  JOCTET *nextbuffer;
93
211k
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
94
95
211k
  if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
96
97
  /* Try to allocate new buffer with double size
98
   *
99
   * NOTE: The following check isn't actually necessary.  On 64-bit systems,
100
   * the maximum theoretical JPEG size is
101
   * 65500 * 65500 * cinfo->num_components * sizeof(DCTELEM) bytes, which is of
102
   * course much less than 8 exabytes (SIZE_MAX / 2).  On 32-bit systems,
103
   * malloc() will never return a buffer >= 2 GB, so the malloc() call will
104
   * fail before 32-bit integer overflow/wraparound can occur.  The sole
105
   * purpose of this code is to shut up automated code analysis tools.
106
   */
107
211k
  if (dest->bufsize > SIZE_MAX / 2)
108
0
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 13);
109
211k
  nextsize = dest->bufsize * 2;
110
211k
  nextbuffer = (JOCTET *)MALLOC(nextsize);
111
112
211k
  if (nextbuffer == NULL)
113
0
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 12);
114
115
211k
  memcpy(nextbuffer, dest->buffer, dest->bufsize);
116
117
211k
  free(dest->newbuffer);
118
119
211k
  dest->newbuffer = nextbuffer;
120
121
211k
  dest->pub.next_output_byte = nextbuffer + dest->bufsize;
122
211k
  dest->pub.free_in_buffer = dest->bufsize;
123
124
211k
  dest->buffer = nextbuffer;
125
211k
  dest->bufsize = nextsize;
126
127
211k
  return TRUE;
128
211k
}
129
130
131
/*
132
 * Terminate destination --- called by jpeg_finish_compress
133
 * after all data has been written.  Usually needs to flush buffer.
134
 *
135
 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
136
 * application must deal with any cleanup that should happen even
137
 * for error exit.
138
 */
139
140
METHODDEF(void)
141
term_mem_destination(j_compress_ptr cinfo)
142
238k
{
143
238k
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
144
145
238k
  if (dest->alloc) *dest->outbuffer = dest->buffer;
146
238k
  *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
147
238k
}
148
149
150
/*
151
 * Prepare for output to a memory buffer.
152
 * The caller may supply an own initial buffer with appropriate size.
153
 * Otherwise, or when the actual data output exceeds the given size,
154
 * the library adapts the buffer size as necessary.
155
 * The standard library functions malloc/free are used for allocating
156
 * larger memory, so the buffer is available to the application after
157
 * finishing compression, and then the application is responsible for
158
 * freeing the requested memory.
159
 */
160
161
GLOBAL(void)
162
jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
163
                 size_t *outsize, boolean alloc)
164
763k
{
165
763k
  boolean reused = FALSE;
166
763k
  my_mem_dest_ptr dest;
167
168
763k
  if (outbuffer == NULL || outsize == NULL)     /* sanity check */
169
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
170
171
  /* The destination object is made permanent so that multiple JPEG images
172
   * can be written to the same buffer without re-executing jpeg_mem_dest.
173
   */
174
763k
  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
175
520k
    cinfo->dest = (struct jpeg_destination_mgr *)
176
520k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
177
520k
                                  sizeof(my_mem_destination_mgr));
178
520k
    dest = (my_mem_dest_ptr)cinfo->dest;
179
520k
    dest->newbuffer = NULL;
180
520k
    dest->buffer = NULL;
181
520k
  } else if (cinfo->dest->init_destination != init_mem_destination) {
182
    /* It is unsafe to reuse the existing destination manager unless it was
183
     * created by this function.
184
     */
185
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
186
0
  }
187
188
763k
  dest = (my_mem_dest_ptr)cinfo->dest;
189
763k
  dest->pub.init_destination = init_mem_destination;
190
763k
  dest->pub.empty_output_buffer = empty_mem_output_buffer;
191
763k
  dest->pub.term_destination = term_mem_destination;
192
763k
  if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
193
0
    reused = TRUE;
194
763k
  dest->outbuffer = outbuffer;
195
763k
  dest->outsize = outsize;
196
763k
  dest->alloc = alloc;
197
198
763k
  if (*outbuffer == NULL || (*outsize == 0 && !reused)) {
199
40.5k
    if (alloc) {
200
      /* Allocate initial buffer */
201
40.5k
      dest->newbuffer = *outbuffer = (unsigned char *)MALLOC(OUTPUT_BUF_SIZE);
202
40.5k
      if (dest->newbuffer == NULL)
203
0
        ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
204
40.5k
      *outsize = OUTPUT_BUF_SIZE;
205
40.5k
    } else
206
0
      ERREXIT(cinfo, JERR_BUFFER_SIZE);
207
40.5k
  } else
208
723k
    dest->newbuffer = *outbuffer;
209
210
763k
  dest->pub.next_output_byte = dest->buffer = *outbuffer;
211
763k
  if (!reused)
212
763k
    dest->bufsize = *outsize;
213
763k
  dest->pub.free_in_buffer = dest->bufsize;
214
763k
}