Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/sdctd.c
Line
Count
Source
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* DCT decoding filter stream */
18
#include "memory_.h"
19
#include "stdio_.h"
20
#include "jpeglib_.h"
21
#include "jerror_.h"
22
#include "gdebug.h"
23
#include "gsmemory.h"
24
#include "strimpl.h"
25
#include "sdct.h"
26
#include "sjpeg.h"
27
28
/* ------ DCTDecode ------ */
29
30
/* JPEG source manager procedures */
31
static void
32
dctd_init_source(j_decompress_ptr dinfo)
33
12.1k
{
34
12.1k
}
35
static const JOCTET fake_eoi[2] =
36
{0xFF, JPEG_EOI};
37
static boolean
38
dctd_fill_input_buffer(j_decompress_ptr dinfo)
39
103k
{
40
103k
    jpeg_decompress_data *jddp =
41
103k
    (jpeg_decompress_data *) ((char *)dinfo -
42
103k
                              offset_of(jpeg_decompress_data, dinfo));
43
44
103k
    if (!jddp->input_eod)
45
101k
        return FALSE;    /* normal case: suspend processing */
46
    /* Reached end of source data without finding EOI */
47
1.60k
    WARNMS(dinfo, JWRN_JPEG_EOF);
48
    /* Insert a fake EOI marker */
49
1.60k
    dinfo->src->next_input_byte = fake_eoi;
50
1.60k
    dinfo->src->bytes_in_buffer = 2;
51
1.60k
    jddp->faked_eoi = true; /* so process routine doesn't use next_input_byte */
52
1.60k
    return TRUE;
53
103k
}
54
static void
55
dctd_skip_input_data(j_decompress_ptr dinfo, long num_bytes)
56
8.16k
{
57
8.16k
    struct jpeg_source_mgr *src = dinfo->src;
58
8.16k
    jpeg_decompress_data *jddp =
59
8.16k
    (jpeg_decompress_data *) ((char *)dinfo -
60
8.16k
                              offset_of(jpeg_decompress_data, dinfo));
61
62
8.16k
    if (num_bytes > 0) {
63
8.16k
        if (num_bytes > src->bytes_in_buffer) {
64
4.17k
            jddp->skip += num_bytes - src->bytes_in_buffer;
65
4.17k
            src->next_input_byte += src->bytes_in_buffer;
66
4.17k
            src->bytes_in_buffer = 0;
67
4.17k
            return;
68
4.17k
        }
69
3.99k
        src->next_input_byte += num_bytes;
70
3.99k
        src->bytes_in_buffer -= num_bytes;
71
3.99k
    }
72
8.16k
}
73
74
static void
75
dctd_term_source(j_decompress_ptr dinfo)
76
7.48k
{
77
7.48k
    jpeg_decompress_data *jddp =
78
7.48k
    (jpeg_decompress_data *) ((char *)dinfo -
79
7.48k
                              offset_of(jpeg_decompress_data, dinfo));
80
81
7.48k
    stream_dct_end_passthrough(jddp);
82
7.48k
    return;
83
7.48k
}
84
85
/* Set the defaults for the DCTDecode filter. */
86
static void
87
s_DCTD_set_defaults(stream_state * st)
88
24.6k
{
89
24.6k
    s_DCT_set_defaults(st);
90
24.6k
}
91
92
/* Initialize DCTDecode filter */
93
static int
94
s_DCTD_init(stream_state * st)
95
12.3k
{
96
12.3k
    stream_DCT_state *const ss = (stream_DCT_state *) st;
97
12.3k
    struct jpeg_source_mgr *src = &ss->data.decompress->source;
98
99
12.3k
    src->init_source = dctd_init_source;
100
12.3k
    src->fill_input_buffer = dctd_fill_input_buffer;
101
12.3k
    src->skip_input_data = dctd_skip_input_data;
102
12.3k
    src->term_source = dctd_term_source;
103
12.3k
    src->resync_to_restart = jpeg_resync_to_restart;  /* use default method */
104
12.3k
    ss->data.common->memory = ss->jpeg_memory;
105
12.3k
    ss->data.decompress->dinfo.src = src;
106
12.3k
    ss->data.decompress->skip = 0;
107
12.3k
    ss->data.decompress->input_eod = false;
108
12.3k
    ss->data.decompress->faked_eoi = false;
109
12.3k
    ss->phase = 0;
110
12.3k
    return 0;
111
12.3k
}
112
113
static int
114
compact_jpeg_buffer(stream_cursor_read *pr)
115
0
{
116
0
    byte *o, *i;
117
118
    /* Search backwards from the end for 2 consecutive 0xFFs */
119
0
    o = (byte *)pr->limit;
120
0
    while (o - pr->ptr >= 2) {
121
0
        if (*o-- == 0xFF) {
122
0
            if (*o == 0xFF)
123
0
                goto compact;
124
0
            o--;
125
0
        }
126
0
    }
127
0
    return 0;
128
0
compact:
129
0
    i = o-1;
130
0
    do {
131
        /* Skip i backwards over 0xFFs */
132
0
        while ((i != pr->ptr) && (*i == 0xFF))
133
0
            i--;
134
        /* Repeatedly copy from i to o */
135
0
        while (i != pr->ptr) {
136
0
            byte c = *i--;
137
0
            *o-- = c;
138
0
            if (c == 0xFF)
139
0
                break;
140
0
        }
141
0
    } while (i != pr->ptr);
142
143
0
    pr->ptr = o;
144
0
    return o - i;
145
0
}
146
147
static void
148
update_jpeg_header_height(JOCTET *d, size_t len, int height)
149
21.2k
{
150
21.2k
    int marker_len;
151
152
88.0k
    for (d += 2; len > 9 && d[0] == 0xFF; d += marker_len)
153
69.7k
    {
154
69.7k
        int declared_height;
155
156
69.7k
        marker_len = 2 + (d[2] << 8) + d[3];
157
69.7k
        if (marker_len > len)
158
2.97k
            break;
159
66.7k
        len -= marker_len;
160
161
        /* We can only safely rewrite non-differential SOF markers */
162
66.7k
        if (d[1] < 0xC0 || (0xC3 < d[1] && d[1] < 0xC9) || 0xCB < d[1])
163
57.9k
            continue;
164
165
8.78k
        declared_height = (d[5]<<8) | d[6];
166
8.78k
        if (declared_height == 0 || declared_height > height)
167
248
        {
168
248
            d[5] = height>>8;
169
248
            d[6] = height;
170
248
        }
171
8.78k
    }
172
21.2k
}
173
174
/* Process a buffer */
175
static int
176
s_DCTD_process(stream_state * st, stream_cursor_read * pr,
177
               stream_cursor_write * pw, bool last)
178
1.84M
{
179
1.84M
    stream_DCT_state *const ss = (stream_DCT_state *) st;
180
1.84M
    jpeg_decompress_data *jddp = ss->data.decompress;
181
1.84M
    struct jpeg_source_mgr *src = jddp->dinfo.src;
182
1.84M
    int code;
183
1.84M
    byte *Buf;
184
185
1.84M
    if_debug3m('w', st->memory, "[wdd]process avail=%u, skip=%u, last=%d\n",
186
1.84M
               (uint) (pr->limit - pr->ptr), (uint) jddp->skip, last);
187
1.84M
    if (jddp->skip != 0) {
188
16.8k
        long avail = pr->limit - pr->ptr;
189
190
16.8k
        if (avail < jddp->skip) {
191
13.1k
            if (jddp->PassThrough && jddp->PassThroughfn)
192
791
                (jddp->PassThroughfn)(jddp->device, (byte *)pr->ptr + 1, (byte *)pr->limit - (byte *)pr->ptr);
193
194
13.1k
            jddp->skip -= avail;
195
13.1k
            pr->ptr = pr->limit;
196
13.1k
            if (!last)
197
12.9k
                return 0;  /* need more data */
198
180
            jddp->skip = 0; /* don't skip past input EOD */
199
180
        }
200
3.91k
        Buf = (byte *)pr->ptr + 1;
201
3.91k
        pr->ptr += jddp->skip;
202
3.91k
        if (jddp->PassThrough && jddp->PassThroughfn)
203
265
            (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
204
205
3.91k
        jddp->skip = 0;
206
3.91k
    }
207
1.82M
    src->next_input_byte = pr->ptr + 1;
208
1.82M
    src->bytes_in_buffer = pr->limit - pr->ptr;
209
1.82M
    Buf = (byte *)pr->ptr + 1;
210
1.82M
    jddp->input_eod = last;
211
1.82M
    switch (ss->phase) {
212
24.4k
        case 0:   /* not initialized yet */
213
            /*
214
             * Adobe implementations seem to ignore leading garbage bytes,
215
             * even though neither the standard nor Adobe's own
216
             * documentation mention this.
217
             */
218
24.4k
            if (jddp->PassThrough && jddp->PassThroughfn && !jddp->StartedPassThrough) {
219
2.76k
                jddp->StartedPassThrough = 1;
220
2.76k
                (jddp->PassThroughfn)(jddp->device, NULL, 1);
221
2.76k
            }
222
38.8k
            while (pr->ptr < pr->limit && pr->ptr[1] != 0xff)
223
14.3k
                pr->ptr++;
224
24.4k
            if (pr->ptr == pr->limit) {
225
12.2k
                if (jddp->PassThrough && jddp->PassThroughfn)
226
2.77k
                    (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
227
12.2k
                return 0;
228
12.2k
            }
229
12.1k
            src->next_input_byte = pr->ptr + 1;
230
12.1k
            src->bytes_in_buffer = pr->limit - pr->ptr;
231
12.1k
            ss->phase = 1;
232
            /* falls through */
233
21.3k
        case 1:   /* reading header markers */
234
21.3k
            if (ss->data.common->Height != 0)
235
21.2k
            {
236
               /* Deliberate and naughty. We cast away a const pointer
237
                * here and write to a supposedly read-only stream. */
238
21.2k
                union { const byte *c; byte *u; } u;
239
21.2k
                u.c = pr->ptr+1;
240
21.2k
                update_jpeg_header_height(u.u, src->bytes_in_buffer, ss->data.common->Height);
241
21.2k
            }
242
21.3k
            if ((code = gs_jpeg_read_header(ss, TRUE)) < 0) {
243
3.29k
                code = ERRC;
244
3.29k
                goto error_out;
245
3.29k
            }
246
18.0k
            pr->ptr =
247
18.0k
                (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
248
18.0k
            switch (code) {
249
9.24k
                case JPEG_SUSPENDED:
250
9.24k
                    if (jddp->PassThrough && jddp->PassThroughfn)
251
1.11k
                        (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
252
9.24k
                    return 0;
253
                    /*case JPEG_HEADER_OK: */
254
18.0k
            }
255
256
            /*
257
             * Default the color transform if not set and check for
258
             * the Adobe marker and use Adobe's transform if the
259
             * marker is set.
260
             */
261
8.84k
            if (ss->ColorTransform == -1) {
262
8.84k
                if (jddp->dinfo.num_components == 3)
263
7.72k
                    ss->ColorTransform = 1;
264
1.11k
                else
265
1.11k
                    ss->ColorTransform = 0;
266
8.84k
            }
267
268
8.84k
            if (jddp->dinfo.saw_Adobe_marker)
269
5.10k
                ss->ColorTransform = jddp->dinfo.Adobe_transform;
270
271
8.84k
            switch (jddp->dinfo.num_components) {
272
7.72k
            case 3:
273
7.72k
                jddp->dinfo.jpeg_color_space =
274
7.72k
                    (ss->ColorTransform ? JCS_YCbCr : JCS_RGB);
275
                        /* out_color_space will default to JCS_RGB */
276
7.72k
                        break;
277
566
            case 4:
278
566
                jddp->dinfo.jpeg_color_space =
279
566
                    (ss->ColorTransform ? JCS_YCCK : JCS_CMYK);
280
                /* out_color_space will default to JCS_CMYK */
281
566
                break;
282
8.84k
            }
283
8.84k
            ss->phase = 2;
284
            /* falls through */
285
13.3k
        case 2:   /* start_decompress */
286
13.3k
            if ((code = gs_jpeg_start_decompress(ss)) < 0) {
287
63
                code = ERRC;
288
63
                goto error_out;
289
63
            }
290
13.3k
            pr->ptr =
291
13.3k
                (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
292
13.3k
            if (code == 0) {
293
4.54k
                if (jddp->PassThrough && jddp->PassThroughfn)
294
744
                    (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
295
4.54k
                return 0;
296
4.54k
            }
297
8.76k
            ss->scan_line_size =
298
8.76k
                jddp->dinfo.output_width * jddp->dinfo.output_components;
299
8.76k
            if_debug4m('w', ss->memory, "[wdd]width=%u, components=%d, scan_line_size=%u, min_out_size=%u\n",
300
8.76k
                       jddp->dinfo.output_width,
301
8.76k
                       jddp->dinfo.output_components,
302
8.76k
                       ss->scan_line_size, jddp->templat.min_out_size);
303
8.76k
            if (ss->scan_line_size > (uint) jddp->templat.min_out_size) {
304
                /* Create a spare buffer for oversize scanline */
305
429
                jddp->scanline_buffer =
306
429
                    gs_alloc_bytes_immovable(gs_memory_stable(jddp->memory),
307
429
                                             ss->scan_line_size,
308
429
                                         "s_DCTD_process(scanline_buffer)");
309
429
                if (jddp->scanline_buffer == NULL) {
310
0
                    code = ERRC;
311
0
                    goto error_out;
312
0
                }
313
429
            }
314
8.76k
            jddp->bytes_in_scanline = 0;
315
8.76k
            ss->phase = 3;
316
            /* falls through */
317
1.79M
        case 3:   /* reading data */
318
2.28M
          dumpbuffer:
319
2.28M
            if (jddp->bytes_in_scanline != 0) {
320
1.20M
                uint avail = pw->limit - pw->ptr;
321
1.20M
                uint tomove = min(jddp->bytes_in_scanline,
322
1.20M
                                  avail);
323
324
1.20M
                if_debug2m('w', ss->memory, "[wdd]moving %u/%u\n",
325
1.20M
                           tomove, avail);
326
1.20M
                memcpy(pw->ptr + 1, jddp->scanline_buffer +
327
1.20M
                       (ss->scan_line_size - jddp->bytes_in_scanline),
328
1.20M
                       tomove);
329
1.20M
                pw->ptr += tomove;
330
1.20M
                jddp->bytes_in_scanline -= tomove;
331
                /* calculate room after the copy,
332
                 * PXL typically provides room 1 exactly 1 scan, so avail == 0
333
                 * PDF/PS provide enough room, so avail >= 0
334
                 * XPS provides room ro complete image, and expects complet image copied
335
                 * PCL,PXL,PDF,PS copy 1 scan at a time.
336
                 */
337
1.20M
                avail -= tomove;
338
1.20M
                if ((jddp->bytes_in_scanline != 0) || /* no room for complete scan */
339
490k
                    ((jddp->bytes_in_scanline == 0) && (tomove > 0) && /* 1 scancopy completed */
340
490k
                     (avail < tomove) && /* still room for 1 more scan */
341
282k
                     (jddp->dinfo.output_height > jddp->dinfo.output_scanline))) /* more scans to do */
342
993k
                {
343
993k
                     if (jddp->PassThrough && jddp->PassThroughfn) {
344
92.9k
                        (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
345
92.9k
                    }
346
993k
                    return 1; /* need more room */
347
993k
                }
348
1.20M
            }
349
            /* while not done with image, decode 1 scan, otherwise fall into phase 4 */
350
2.59M
            while (jddp->dinfo.output_height > jddp->dinfo.output_scanline) {
351
2.58M
                int read;
352
2.58M
                byte *samples;
353
354
2.58M
                if (jddp->scanline_buffer != NULL)
355
507k
                    samples = jddp->scanline_buffer;
356
2.08M
                else {
357
2.08M
                    if ((uint) (pw->limit - pw->ptr) < ss->scan_line_size) {
358
709k
                        if (jddp->PassThrough && jddp->PassThroughfn) {
359
164k
                            (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
360
164k
                        }
361
709k
                        return 1; /* need more room */
362
709k
                    }
363
1.37M
                    samples = pw->ptr + 1;
364
1.37M
                }
365
1.87M
                read = gs_jpeg_read_scanlines(ss, &samples, 1);
366
1.87M
                if (read < 0) {
367
0
                    code = ERRC;
368
0
                    goto error_out;
369
0
                }
370
1.87M
                if_debug3m('w', ss->memory, "[wdd]read returns %d, used=%u, faked_eoi=%d\n",
371
1.87M
                           read,
372
1.87M
                           (uint) (src->next_input_byte - 1 - pr->ptr),
373
1.87M
                           (int)jddp->faked_eoi);
374
1.87M
                pr->ptr =
375
1.87M
                    (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
376
1.87M
                if (!read) {
377
                    /* We are suspending. If nothing was consumed, and the
378
                     * buffer was full, compact the data in the buffer. If
379
                     * this fails to save anything, then we'll never succeed;
380
                     * throw an error to avoid an infinite loop.
381
                     * The tricky part here is knowing "if the buffer is
382
                     * full"; we do that by comparing the number of bytes in
383
                     * the buffer with the min_in_size set for the stream.
384
                     */
385
                    /* TODO: If we ever find a file with valid data that trips
386
                     * this test, we should implement a scheme whereby we keep
387
                     * a local buffer and copy the data into it. The local
388
                     * buffer can be grown as required. */
389
84.3k
                    if ((src->next_input_byte-1 == pr->ptr) &&
390
84.3k
                        (pr->limit - pr->ptr >= ss->templat->min_in_size) &&
391
0
                        (compact_jpeg_buffer(pr) == 0)) {
392
0
                        code = ERRC;
393
0
                        goto error_out;
394
0
                    }
395
84.3k
                    if (jddp->PassThrough && jddp->PassThroughfn) {
396
15.5k
                        (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
397
15.5k
                    }
398
84.3k
                    return 0; /* need more data */
399
84.3k
                }
400
1.79M
                if (jddp->scanline_buffer != NULL) {
401
490k
                    jddp->bytes_in_scanline = ss->scan_line_size;
402
490k
                    goto dumpbuffer;
403
490k
                }
404
1.30M
                pw->ptr += ss->scan_line_size;
405
1.30M
            }
406
8.50k
            ss->phase = 4;
407
            /* falls through */
408
12.1k
        case 4:   /* end of image; scan for EOI */
409
12.1k
            {
410
                /* Slightly hacky: We want dctd_term_source() to call stream_dct_end_passthrough()
411
                 * to cope with certain error conditions, BUT not at this stage, because gs_jpeg_finish_decompress()
412
                 * is what can prompt libjpeg to flush through the last its input data, and we need the last of
413
                 * that input data pushed to the PassThroughfn call.
414
                 * Setting PassThrough to zero will prevent the final PassThroughfn happening during gs_jpeg_finish_decompress()
415
                 */
416
12.1k
                int pt = jddp->PassThrough;
417
12.1k
                jddp->PassThrough = 0;
418
12.1k
                if ((code = gs_jpeg_finish_decompress(ss)) < 0) {
419
1.02k
                    code = ERRC;
420
1.02k
                    goto error_out;
421
1.02k
                }
422
11.0k
                pr->ptr =
423
11.0k
                    (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
424
11.0k
                jddp->PassThrough = pt;
425
11.0k
                if (jddp->PassThrough && jddp->PassThroughfn)
426
1.77k
                    (jddp->PassThroughfn)(jddp->device, Buf, pr->ptr - (Buf - 1));
427
11.0k
                stream_dct_end_passthrough(jddp);
428
11.0k
                if (code == 0)
429
3.61k
                    return 0;
430
11.0k
            }
431
7.48k
            ss->phase = 5;
432
            /* falls through */
433
7.48k
        case 5:   /* we are DONE */
434
7.48k
            return EOFC;
435
1.82M
    }
436
    /* Default case can't happen.... */
437
0
    return ERRC;
438
439
4.38k
error_out:
440
4.38k
    stream_dct_end_passthrough(jddp);
441
4.38k
    return code;
442
1.82M
}
443
444
/* Stream template */
445
const stream_template s_DCTD_template =
446
{&st_DCT_state, s_DCTD_init, s_DCTD_process, 2000, 4000, NULL,
447
 s_DCTD_set_defaults
448
};