Coverage Report

Created: 2025-07-01 06:26

/src/libjpeg-turbo.3.0.x/jdatadst-tj.c
Line
Count
Source (jump to first uncovered line)
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, 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
27
void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
28
                      size_t *outsize, boolean alloc);
29
30
31
1.96k
#define OUTPUT_BUF_SIZE  4096   /* choose an efficiently fwrite'able size */
32
33
34
/* Expanded data destination object for memory output */
35
36
typedef struct {
37
  struct jpeg_destination_mgr pub; /* public fields */
38
39
  unsigned char **outbuffer;    /* target buffer */
40
  size_t *outsize;
41
  unsigned char *newbuffer;     /* newly allocated buffer */
42
  JOCTET *buffer;               /* start of buffer */
43
  size_t bufsize;
44
  boolean alloc;
45
} my_mem_destination_mgr;
46
47
typedef my_mem_destination_mgr *my_mem_dest_ptr;
48
49
50
/*
51
 * Initialize destination --- called by jpeg_start_compress
52
 * before any data is actually written.
53
 */
54
55
METHODDEF(void)
56
init_mem_destination(j_compress_ptr cinfo)
57
13.3k
{
58
  /* no work necessary here */
59
13.3k
}
60
61
62
/*
63
 * Empty the output buffer --- called whenever buffer fills up.
64
 *
65
 * In typical applications, this should write the entire output buffer
66
 * (ignoring the current state of next_output_byte & free_in_buffer),
67
 * reset the pointer & count to the start of the buffer, and return TRUE
68
 * indicating that the buffer has been dumped.
69
 *
70
 * In applications that need to be able to suspend compression due to output
71
 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
72
 * In this situation, the compressor will return to its caller (possibly with
73
 * an indication that it has not accepted all the supplied scanlines).  The
74
 * application should resume compression after it has made more room in the
75
 * output buffer.  Note that there are substantial restrictions on the use of
76
 * suspension --- see the documentation.
77
 *
78
 * When suspending, the compressor will back up to a convenient restart point
79
 * (typically the start of the current MCU). next_output_byte & free_in_buffer
80
 * indicate where the restart point will be if the current call returns FALSE.
81
 * Data beyond this point will be regenerated after resumption, so do not
82
 * write it out when emptying the buffer externally.
83
 */
84
85
METHODDEF(boolean)
86
empty_mem_output_buffer(j_compress_ptr cinfo)
87
1.19k
{
88
1.19k
  size_t nextsize;
89
1.19k
  JOCTET *nextbuffer;
90
1.19k
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
91
92
1.19k
  if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
93
94
  /* Try to allocate new buffer with double size */
95
1.19k
  nextsize = dest->bufsize * 2;
96
1.19k
  nextbuffer = (JOCTET *)MALLOC(nextsize);
97
98
1.19k
  if (nextbuffer == NULL)
99
0
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
100
101
1.19k
  memcpy(nextbuffer, dest->buffer, dest->bufsize);
102
103
1.19k
  free(dest->newbuffer);
104
105
1.19k
  dest->newbuffer = nextbuffer;
106
107
1.19k
  dest->pub.next_output_byte = nextbuffer + dest->bufsize;
108
1.19k
  dest->pub.free_in_buffer = dest->bufsize;
109
110
1.19k
  dest->buffer = nextbuffer;
111
1.19k
  dest->bufsize = nextsize;
112
113
1.19k
  return TRUE;
114
1.19k
}
115
116
117
/*
118
 * Terminate destination --- called by jpeg_finish_compress
119
 * after all data has been written.  Usually needs to flush buffer.
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)
127
term_mem_destination(j_compress_ptr cinfo)
128
13.3k
{
129
13.3k
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
130
131
13.3k
  if (dest->alloc) *dest->outbuffer = dest->buffer;
132
13.3k
  *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
133
13.3k
}
134
135
136
/*
137
 * Prepare for output to a memory buffer.
138
 * The caller may supply an own initial buffer with appropriate size.
139
 * Otherwise, or when the actual data output exceeds the given size,
140
 * the library adapts the buffer size as necessary.
141
 * The standard library functions malloc/free are used for allocating
142
 * larger memory, so the buffer is available to the application after
143
 * finishing compression, and then the application is responsible for
144
 * freeing the requested memory.
145
 */
146
147
GLOBAL(void)
148
jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
149
                 size_t *outsize, boolean alloc)
150
36.6k
{
151
36.6k
  boolean reused = FALSE;
152
36.6k
  my_mem_dest_ptr dest;
153
154
36.6k
  if (outbuffer == NULL || outsize == NULL)     /* sanity check */
155
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
156
157
  /* The destination object is made permanent so that multiple JPEG images
158
   * can be written to the same buffer without re-executing jpeg_mem_dest.
159
   */
160
36.6k
  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
161
23.2k
    cinfo->dest = (struct jpeg_destination_mgr *)
162
23.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
163
23.2k
                                  sizeof(my_mem_destination_mgr));
164
23.2k
    dest = (my_mem_dest_ptr)cinfo->dest;
165
23.2k
    dest->newbuffer = NULL;
166
23.2k
    dest->buffer = NULL;
167
23.2k
  } else if (cinfo->dest->init_destination != init_mem_destination) {
168
    /* It is unsafe to reuse the existing destination manager unless it was
169
     * created by this function.
170
     */
171
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
172
0
  }
173
174
36.6k
  dest = (my_mem_dest_ptr)cinfo->dest;
175
36.6k
  dest->pub.init_destination = init_mem_destination;
176
36.6k
  dest->pub.empty_output_buffer = empty_mem_output_buffer;
177
36.6k
  dest->pub.term_destination = term_mem_destination;
178
36.6k
  if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
179
0
    reused = TRUE;
180
36.6k
  dest->outbuffer = outbuffer;
181
36.6k
  dest->outsize = outsize;
182
36.6k
  dest->alloc = alloc;
183
184
36.6k
  if (*outbuffer == NULL || *outsize == 0) {
185
1.96k
    if (alloc) {
186
      /* Allocate initial buffer */
187
1.96k
      dest->newbuffer = *outbuffer = (unsigned char *)MALLOC(OUTPUT_BUF_SIZE);
188
1.96k
      if (dest->newbuffer == NULL)
189
0
        ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
190
1.96k
      *outsize = OUTPUT_BUF_SIZE;
191
1.96k
    } else
192
0
      ERREXIT(cinfo, JERR_BUFFER_SIZE);
193
1.96k
  }
194
195
36.6k
  dest->pub.next_output_byte = dest->buffer = *outbuffer;
196
36.6k
  if (!reused)
197
36.6k
    dest->bufsize = *outsize;
198
36.6k
  dest->pub.free_in_buffer = dest->bufsize;
199
36.6k
}