Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/tiff/libtiff/tif_predict.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library.
27
 *
28
 * Predictor Tag Support (used by multiple codecs).
29
 */
30
#include "tif_predict.h"
31
#include "tiffiop.h"
32
33
19.4k
#define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
34
35
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
36
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
37
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
38
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
39
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
40
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
41
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
42
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
43
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
44
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
45
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
46
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
47
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
48
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
49
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc);
50
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc);
51
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
52
                              uint16_t s);
53
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
54
                               uint16_t s);
55
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
56
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
57
                               uint16_t s);
58
59
static int PredictorSetup(TIFF *tif)
60
1.58k
{
61
1.58k
    static const char module[] = "PredictorSetup";
62
63
1.58k
    TIFFPredictorState *sp = PredictorState(tif);
64
1.58k
    TIFFDirectory *td = &tif->tif_dir;
65
66
1.58k
    switch (sp->predictor) /* no differencing */
67
1.58k
    {
68
1.58k
        case PREDICTOR_NONE:
69
1.58k
            return 1;
70
0
        case PREDICTOR_HORIZONTAL:
71
0
            if (td->td_bitspersample != 8 && td->td_bitspersample != 16 &&
72
0
                td->td_bitspersample != 32 && td->td_bitspersample != 64)
73
0
            {
74
0
                TIFFErrorExtR(tif, module,
75
0
                              "Horizontal differencing \"Predictor\" not "
76
0
                              "supported with %" PRIu16 "-bit samples",
77
0
                              td->td_bitspersample);
78
0
                return 0;
79
0
            }
80
0
            break;
81
0
        case PREDICTOR_FLOATINGPOINT:
82
0
            if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP)
83
0
            {
84
0
                TIFFErrorExtR(
85
0
                    tif, module,
86
0
                    "Floating point \"Predictor\" not supported with %" PRIu16
87
0
                    " data format",
88
0
                    td->td_sampleformat);
89
0
                return 0;
90
0
            }
91
0
            if (td->td_bitspersample != 16 && td->td_bitspersample != 24 &&
92
0
                td->td_bitspersample != 32 && td->td_bitspersample != 64)
93
0
            { /* Should 64 be allowed? */
94
0
                TIFFErrorExtR(
95
0
                    tif, module,
96
0
                    "Floating point \"Predictor\" not supported with %" PRIu16
97
0
                    "-bit samples",
98
0
                    td->td_bitspersample);
99
0
                return 0;
100
0
            }
101
0
            break;
102
0
        default:
103
0
            TIFFErrorExtR(tif, module, "\"Predictor\" value %d not supported",
104
0
                          sp->predictor);
105
0
            return 0;
106
1.58k
    }
107
0
    sp->stride =
108
0
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
109
0
                                                    : 1);
110
    /*
111
     * Calculate the scanline/tile-width size in bytes.
112
     */
113
0
    if (isTiled(tif))
114
0
        sp->rowsize = TIFFTileRowSize(tif);
115
0
    else
116
0
        sp->rowsize = TIFFScanlineSize(tif);
117
0
    if (sp->rowsize == 0)
118
0
        return 0;
119
120
0
    return 1;
121
0
}
122
123
static int PredictorSetupDecode(TIFF *tif)
124
0
{
125
0
    TIFFPredictorState *sp = PredictorState(tif);
126
0
    TIFFDirectory *td = &tif->tif_dir;
127
128
    /* Note: when PredictorSetup() fails, the effets of setupdecode() */
129
    /* will not be "canceled" so setupdecode() might be robust to */
130
    /* be called several times. */
131
0
    if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
132
0
        return 0;
133
134
0
    if (sp->predictor == 2)
135
0
    {
136
0
        switch (td->td_bitspersample)
137
0
        {
138
0
            case 8:
139
0
                sp->decodepfunc = horAcc8;
140
0
                break;
141
0
            case 16:
142
0
                sp->decodepfunc = horAcc16;
143
0
                break;
144
0
            case 32:
145
0
                sp->decodepfunc = horAcc32;
146
0
                break;
147
0
            case 64:
148
0
                sp->decodepfunc = horAcc64;
149
0
                break;
150
0
        }
151
        /*
152
         * Override default decoding method with one that does the
153
         * predictor stuff.
154
         */
155
0
        if (tif->tif_decoderow != PredictorDecodeRow)
156
0
        {
157
0
            sp->decoderow = tif->tif_decoderow;
158
0
            tif->tif_decoderow = PredictorDecodeRow;
159
0
            sp->decodestrip = tif->tif_decodestrip;
160
0
            tif->tif_decodestrip = PredictorDecodeTile;
161
0
            sp->decodetile = tif->tif_decodetile;
162
0
            tif->tif_decodetile = PredictorDecodeTile;
163
0
        }
164
165
        /*
166
         * If the data is horizontally differenced 16-bit data that
167
         * requires byte-swapping, then it must be byte swapped before
168
         * the accumulation step.  We do this with a special-purpose
169
         * routine and override the normal post decoding logic that
170
         * the library setup when the directory was read.
171
         */
172
0
        if (tif->tif_flags & TIFF_SWAB)
173
0
        {
174
0
            if (sp->decodepfunc == horAcc16)
175
0
            {
176
0
                sp->decodepfunc = swabHorAcc16;
177
0
                tif->tif_postdecode = _TIFFNoPostDecode;
178
0
            }
179
0
            else if (sp->decodepfunc == horAcc32)
180
0
            {
181
0
                sp->decodepfunc = swabHorAcc32;
182
0
                tif->tif_postdecode = _TIFFNoPostDecode;
183
0
            }
184
0
            else if (sp->decodepfunc == horAcc64)
185
0
            {
186
0
                sp->decodepfunc = swabHorAcc64;
187
0
                tif->tif_postdecode = _TIFFNoPostDecode;
188
0
            }
189
0
        }
190
0
    }
191
192
0
    else if (sp->predictor == 3)
193
0
    {
194
0
        sp->decodepfunc = fpAcc;
195
        /*
196
         * Override default decoding method with one that does the
197
         * predictor stuff.
198
         */
199
0
        if (tif->tif_decoderow != PredictorDecodeRow)
200
0
        {
201
0
            sp->decoderow = tif->tif_decoderow;
202
0
            tif->tif_decoderow = PredictorDecodeRow;
203
0
            sp->decodestrip = tif->tif_decodestrip;
204
0
            tif->tif_decodestrip = PredictorDecodeTile;
205
0
            sp->decodetile = tif->tif_decodetile;
206
0
            tif->tif_decodetile = PredictorDecodeTile;
207
0
        }
208
        /*
209
         * The data should not be swapped outside of the floating
210
         * point predictor, the accumulation routine should return
211
         * byres in the native order.
212
         */
213
0
        if (tif->tif_flags & TIFF_SWAB)
214
0
        {
215
0
            tif->tif_postdecode = _TIFFNoPostDecode;
216
0
        }
217
        /*
218
         * Allocate buffer to keep the decoded bytes before
219
         * rearranging in the right order
220
         */
221
0
    }
222
223
0
    return 1;
224
0
}
225
226
static int PredictorSetupEncode(TIFF *tif)
227
1.58k
{
228
1.58k
    TIFFPredictorState *sp = PredictorState(tif);
229
1.58k
    TIFFDirectory *td = &tif->tif_dir;
230
231
1.58k
    if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
232
0
        return 0;
233
234
1.58k
    if (sp->predictor == 2)
235
0
    {
236
0
        switch (td->td_bitspersample)
237
0
        {
238
0
            case 8:
239
0
                sp->encodepfunc = horDiff8;
240
0
                break;
241
0
            case 16:
242
0
                sp->encodepfunc = horDiff16;
243
0
                break;
244
0
            case 32:
245
0
                sp->encodepfunc = horDiff32;
246
0
                break;
247
0
            case 64:
248
0
                sp->encodepfunc = horDiff64;
249
0
                break;
250
0
        }
251
        /*
252
         * Override default encoding method with one that does the
253
         * predictor stuff.
254
         */
255
0
        if (tif->tif_encoderow != PredictorEncodeRow)
256
0
        {
257
0
            sp->encoderow = tif->tif_encoderow;
258
0
            tif->tif_encoderow = PredictorEncodeRow;
259
0
            sp->encodestrip = tif->tif_encodestrip;
260
0
            tif->tif_encodestrip = PredictorEncodeTile;
261
0
            sp->encodetile = tif->tif_encodetile;
262
0
            tif->tif_encodetile = PredictorEncodeTile;
263
0
        }
264
265
        /*
266
         * If the data is horizontally differenced 16-bit data that
267
         * requires byte-swapping, then it must be byte swapped after
268
         * the differentiation step.  We do this with a special-purpose
269
         * routine and override the normal post decoding logic that
270
         * the library setup when the directory was read.
271
         */
272
0
        if (tif->tif_flags & TIFF_SWAB)
273
0
        {
274
0
            if (sp->encodepfunc == horDiff16)
275
0
            {
276
0
                sp->encodepfunc = swabHorDiff16;
277
0
                tif->tif_postdecode = _TIFFNoPostDecode;
278
0
            }
279
0
            else if (sp->encodepfunc == horDiff32)
280
0
            {
281
0
                sp->encodepfunc = swabHorDiff32;
282
0
                tif->tif_postdecode = _TIFFNoPostDecode;
283
0
            }
284
0
            else if (sp->encodepfunc == horDiff64)
285
0
            {
286
0
                sp->encodepfunc = swabHorDiff64;
287
0
                tif->tif_postdecode = _TIFFNoPostDecode;
288
0
            }
289
0
        }
290
0
    }
291
292
1.58k
    else if (sp->predictor == 3)
293
0
    {
294
0
        sp->encodepfunc = fpDiff;
295
        /*
296
         * Override default encoding method with one that does the
297
         * predictor stuff.
298
         */
299
0
        if (tif->tif_encoderow != PredictorEncodeRow)
300
0
        {
301
0
            sp->encoderow = tif->tif_encoderow;
302
0
            tif->tif_encoderow = PredictorEncodeRow;
303
0
            sp->encodestrip = tif->tif_encodestrip;
304
0
            tif->tif_encodestrip = PredictorEncodeTile;
305
0
            sp->encodetile = tif->tif_encodetile;
306
0
            tif->tif_encodetile = PredictorEncodeTile;
307
0
        }
308
0
    }
309
310
1.58k
    return 1;
311
1.58k
}
312
313
#define REPEAT4(n, op)                                                         \
314
0
    switch (n)                                                                 \
315
0
    {                                                                          \
316
0
        default:                                                               \
317
0
        {                                                                      \
318
0
            tmsize_t i;                                                        \
319
0
            for (i = n - 4; i > 0; i--)                                        \
320
0
            {                                                                  \
321
0
                op;                                                            \
322
0
            }                                                                  \
323
0
        } /*-fallthrough*/                                                     \
324
0
        case 4:                                                                \
325
0
            op; /*-fallthrough*/                                               \
326
0
        case 3:                                                                \
327
0
            op; /*-fallthrough*/                                               \
328
0
        case 2:                                                                \
329
0
            op; /*-fallthrough*/                                               \
330
0
        case 1:                                                                \
331
0
            op; /*-fallthrough*/                                               \
332
0
        case 0:;                                                               \
333
0
    }
334
335
/* Remarks related to C standard compliance in all below functions : */
336
/* - to avoid any undefined behavior, we only operate on unsigned types */
337
/*   since the behavior of "overflows" is defined (wrap over) */
338
/* - when storing into the byte stream, we explicitly mask with 0xff so */
339
/*   as to make icc -check=conversions happy (not necessary by the standard) */
340
341
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
342
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
343
0
{
344
0
    tmsize_t stride = PredictorState(tif)->stride;
345
346
0
    unsigned char *cp = (unsigned char *)cp0;
347
0
    if ((cc % stride) != 0)
348
0
    {
349
0
        TIFFErrorExtR(tif, "horAcc8", "%s", "(cc%stride)!=0");
350
0
        return 0;
351
0
    }
352
353
0
    if (cc > stride)
354
0
    {
355
        /*
356
         * Pipeline the most common cases.
357
         */
358
0
        if (stride == 3)
359
0
        {
360
0
            unsigned int cr = cp[0];
361
0
            unsigned int cg = cp[1];
362
0
            unsigned int cb = cp[2];
363
0
            tmsize_t i = stride;
364
0
            for (; i < cc; i += stride)
365
0
            {
366
0
                cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
367
0
                cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
368
0
                cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
369
0
            }
370
0
        }
371
0
        else if (stride == 4)
372
0
        {
373
0
            unsigned int cr = cp[0];
374
0
            unsigned int cg = cp[1];
375
0
            unsigned int cb = cp[2];
376
0
            unsigned int ca = cp[3];
377
0
            tmsize_t i = stride;
378
0
            for (; i < cc; i += stride)
379
0
            {
380
0
                cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
381
0
                cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
382
0
                cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
383
0
                cp[i + 3] = (unsigned char)((ca += cp[i + 3]) & 0xff);
384
0
            }
385
0
        }
386
0
        else
387
0
        {
388
0
            cc -= stride;
389
0
            do
390
0
            {
391
0
                REPEAT4(stride,
392
0
                        cp[stride] = (unsigned char)((cp[stride] + *cp) & 0xff);
393
0
                        cp++)
394
0
                cc -= stride;
395
0
            } while (cc > 0);
396
0
        }
397
0
    }
398
0
    return 1;
399
0
}
400
401
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
402
0
{
403
0
    uint16_t *wp = (uint16_t *)cp0;
404
0
    tmsize_t wc = cc / 2;
405
406
0
    TIFFSwabArrayOfShort(wp, wc);
407
0
    return horAcc16(tif, cp0, cc);
408
0
}
409
410
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
411
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
412
0
{
413
0
    tmsize_t stride = PredictorState(tif)->stride;
414
0
    uint16_t *wp = (uint16_t *)cp0;
415
0
    tmsize_t wc = cc / 2;
416
417
0
    if ((cc % (2 * stride)) != 0)
418
0
    {
419
0
        TIFFErrorExtR(tif, "horAcc16", "%s", "cc%(2*stride))!=0");
420
0
        return 0;
421
0
    }
422
423
0
    if (wc > stride)
424
0
    {
425
0
        wc -= stride;
426
0
        do
427
0
        {
428
0
            REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] +
429
0
                                                     (unsigned int)wp[0]) &
430
0
                                                    0xffff);
431
0
                    wp++)
432
0
            wc -= stride;
433
0
        } while (wc > 0);
434
0
    }
435
0
    return 1;
436
0
}
437
438
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
439
0
{
440
0
    uint32_t *wp = (uint32_t *)cp0;
441
0
    tmsize_t wc = cc / 4;
442
443
0
    TIFFSwabArrayOfLong(wp, wc);
444
0
    return horAcc32(tif, cp0, cc);
445
0
}
446
447
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
448
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
449
0
{
450
0
    tmsize_t stride = PredictorState(tif)->stride;
451
0
    uint32_t *wp = (uint32_t *)cp0;
452
0
    tmsize_t wc = cc / 4;
453
454
0
    if ((cc % (4 * stride)) != 0)
455
0
    {
456
0
        TIFFErrorExtR(tif, "horAcc32", "%s", "cc%(4*stride))!=0");
457
0
        return 0;
458
0
    }
459
460
0
    if (wc > stride)
461
0
    {
462
0
        wc -= stride;
463
0
        do
464
0
        {
465
0
            REPEAT4(stride, wp[stride] += wp[0]; wp++)
466
0
            wc -= stride;
467
0
        } while (wc > 0);
468
0
    }
469
0
    return 1;
470
0
}
471
472
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
473
0
{
474
0
    uint64_t *wp = (uint64_t *)cp0;
475
0
    tmsize_t wc = cc / 8;
476
477
0
    TIFFSwabArrayOfLong8(wp, wc);
478
0
    return horAcc64(tif, cp0, cc);
479
0
}
480
481
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
482
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
483
0
{
484
0
    tmsize_t stride = PredictorState(tif)->stride;
485
0
    uint64_t *wp = (uint64_t *)cp0;
486
0
    tmsize_t wc = cc / 8;
487
488
0
    if ((cc % (8 * stride)) != 0)
489
0
    {
490
0
        TIFFErrorExtR(tif, "horAcc64", "%s", "cc%(8*stride))!=0");
491
0
        return 0;
492
0
    }
493
494
0
    if (wc > stride)
495
0
    {
496
0
        wc -= stride;
497
0
        do
498
0
        {
499
0
            REPEAT4(stride, wp[stride] += wp[0]; wp++)
500
0
            wc -= stride;
501
0
        } while (wc > 0);
502
0
    }
503
0
    return 1;
504
0
}
505
506
/*
507
 * Floating point predictor accumulation routine.
508
 */
509
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc)
510
0
{
511
0
    tmsize_t stride = PredictorState(tif)->stride;
512
0
    uint32_t bps = tif->tif_dir.td_bitspersample / 8;
513
0
    tmsize_t wc = cc / bps;
514
0
    tmsize_t count = cc;
515
0
    uint8_t *cp = (uint8_t *)cp0;
516
0
    uint8_t *tmp;
517
518
0
    if (cc % (bps * stride) != 0)
519
0
    {
520
0
        TIFFErrorExtR(tif, "fpAcc", "%s", "cc%(bps*stride))!=0");
521
0
        return 0;
522
0
    }
523
524
0
    tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
525
0
    if (!tmp)
526
0
        return 0;
527
528
0
    while (count > stride)
529
0
    {
530
0
        REPEAT4(stride,
531
0
                cp[stride] = (unsigned char)((cp[stride] + cp[0]) & 0xff);
532
0
                cp++)
533
0
        count -= stride;
534
0
    }
535
536
0
    _TIFFmemcpy(tmp, cp0, cc);
537
0
    cp = (uint8_t *)cp0;
538
0
    for (count = 0; count < wc; count++)
539
0
    {
540
0
        uint32_t byte;
541
0
        for (byte = 0; byte < bps; byte++)
542
0
        {
543
#if WORDS_BIGENDIAN
544
            cp[bps * count + byte] = tmp[byte * wc + count];
545
#else
546
0
            cp[bps * count + byte] = tmp[(bps - byte - 1) * wc + count];
547
0
#endif
548
0
        }
549
0
    }
550
0
    _TIFFfreeExt(tif, tmp);
551
0
    return 1;
552
0
}
553
554
/*
555
 * Decode a scanline and apply the predictor routine.
556
 */
557
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
558
                              uint16_t s)
559
0
{
560
0
    TIFFPredictorState *sp = PredictorState(tif);
561
562
0
    assert(sp != NULL);
563
0
    assert(sp->decoderow != NULL);
564
0
    assert(sp->decodepfunc != NULL);
565
566
0
    if ((*sp->decoderow)(tif, op0, occ0, s))
567
0
    {
568
0
        return (*sp->decodepfunc)(tif, op0, occ0);
569
0
    }
570
0
    else
571
0
        return 0;
572
0
}
573
574
/*
575
 * Decode a tile/strip and apply the predictor routine.
576
 * Note that horizontal differencing must be done on a
577
 * row-by-row basis.  The width of a "row" has already
578
 * been calculated at pre-decode time according to the
579
 * strip/tile dimensions.
580
 */
581
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
582
                               uint16_t s)
583
0
{
584
0
    TIFFPredictorState *sp = PredictorState(tif);
585
586
0
    assert(sp != NULL);
587
0
    assert(sp->decodetile != NULL);
588
589
0
    if ((*sp->decodetile)(tif, op0, occ0, s))
590
0
    {
591
0
        tmsize_t rowsize = sp->rowsize;
592
0
        assert(rowsize > 0);
593
0
        if ((occ0 % rowsize) != 0)
594
0
        {
595
0
            TIFFErrorExtR(tif, "PredictorDecodeTile", "%s",
596
0
                          "occ0%rowsize != 0");
597
0
            return 0;
598
0
        }
599
0
        assert(sp->decodepfunc != NULL);
600
0
        while (occ0 > 0)
601
0
        {
602
0
            if (!(*sp->decodepfunc)(tif, op0, rowsize))
603
0
                return 0;
604
0
            occ0 -= rowsize;
605
0
            op0 += rowsize;
606
0
        }
607
0
        return 1;
608
0
    }
609
0
    else
610
0
        return 0;
611
0
}
612
613
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
614
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
615
0
{
616
0
    TIFFPredictorState *sp = PredictorState(tif);
617
0
    tmsize_t stride = sp->stride;
618
0
    unsigned char *cp = (unsigned char *)cp0;
619
620
0
    if ((cc % stride) != 0)
621
0
    {
622
0
        TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%stride)!=0");
623
0
        return 0;
624
0
    }
625
626
0
    if (cc > stride)
627
0
    {
628
0
        cc -= stride;
629
        /*
630
         * Pipeline the most common cases.
631
         */
632
0
        if (stride == 3)
633
0
        {
634
0
            unsigned int r1, g1, b1;
635
0
            unsigned int r2 = cp[0];
636
0
            unsigned int g2 = cp[1];
637
0
            unsigned int b2 = cp[2];
638
0
            do
639
0
            {
640
0
                r1 = cp[3];
641
0
                cp[3] = (unsigned char)((r1 - r2) & 0xff);
642
0
                r2 = r1;
643
0
                g1 = cp[4];
644
0
                cp[4] = (unsigned char)((g1 - g2) & 0xff);
645
0
                g2 = g1;
646
0
                b1 = cp[5];
647
0
                cp[5] = (unsigned char)((b1 - b2) & 0xff);
648
0
                b2 = b1;
649
0
                cp += 3;
650
0
            } while ((cc -= 3) > 0);
651
0
        }
652
0
        else if (stride == 4)
653
0
        {
654
0
            unsigned int r1, g1, b1, a1;
655
0
            unsigned int r2 = cp[0];
656
0
            unsigned int g2 = cp[1];
657
0
            unsigned int b2 = cp[2];
658
0
            unsigned int a2 = cp[3];
659
0
            do
660
0
            {
661
0
                r1 = cp[4];
662
0
                cp[4] = (unsigned char)((r1 - r2) & 0xff);
663
0
                r2 = r1;
664
0
                g1 = cp[5];
665
0
                cp[5] = (unsigned char)((g1 - g2) & 0xff);
666
0
                g2 = g1;
667
0
                b1 = cp[6];
668
0
                cp[6] = (unsigned char)((b1 - b2) & 0xff);
669
0
                b2 = b1;
670
0
                a1 = cp[7];
671
0
                cp[7] = (unsigned char)((a1 - a2) & 0xff);
672
0
                a2 = a1;
673
0
                cp += 4;
674
0
            } while ((cc -= 4) > 0);
675
0
        }
676
0
        else
677
0
        {
678
0
            cp += cc - 1;
679
0
            do
680
0
            {
681
0
                REPEAT4(stride,
682
0
                        cp[stride] =
683
0
                            (unsigned char)((cp[stride] - cp[0]) & 0xff);
684
0
                        cp--)
685
0
            } while ((cc -= stride) > 0);
686
0
        }
687
0
    }
688
0
    return 1;
689
0
}
690
691
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
692
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
693
0
{
694
0
    TIFFPredictorState *sp = PredictorState(tif);
695
0
    tmsize_t stride = sp->stride;
696
0
    uint16_t *wp = (uint16_t *)cp0;
697
0
    tmsize_t wc = cc / 2;
698
699
0
    if ((cc % (2 * stride)) != 0)
700
0
    {
701
0
        TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%(2*stride))!=0");
702
0
        return 0;
703
0
    }
704
705
0
    if (wc > stride)
706
0
    {
707
0
        wc -= stride;
708
0
        wp += wc - 1;
709
0
        do
710
0
        {
711
0
            REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] -
712
0
                                                     (unsigned int)wp[0]) &
713
0
                                                    0xffff);
714
0
                    wp--)
715
0
            wc -= stride;
716
0
        } while (wc > 0);
717
0
    }
718
0
    return 1;
719
0
}
720
721
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
722
0
{
723
0
    uint16_t *wp = (uint16_t *)cp0;
724
0
    tmsize_t wc = cc / 2;
725
726
0
    if (!horDiff16(tif, cp0, cc))
727
0
        return 0;
728
729
0
    TIFFSwabArrayOfShort(wp, wc);
730
0
    return 1;
731
0
}
732
733
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
734
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
735
0
{
736
0
    TIFFPredictorState *sp = PredictorState(tif);
737
0
    tmsize_t stride = sp->stride;
738
0
    uint32_t *wp = (uint32_t *)cp0;
739
0
    tmsize_t wc = cc / 4;
740
741
0
    if ((cc % (4 * stride)) != 0)
742
0
    {
743
0
        TIFFErrorExtR(tif, "horDiff32", "%s", "(cc%(4*stride))!=0");
744
0
        return 0;
745
0
    }
746
747
0
    if (wc > stride)
748
0
    {
749
0
        wc -= stride;
750
0
        wp += wc - 1;
751
0
        do
752
0
        {
753
0
            REPEAT4(stride, wp[stride] -= wp[0]; wp--)
754
0
            wc -= stride;
755
0
        } while (wc > 0);
756
0
    }
757
0
    return 1;
758
0
}
759
760
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
761
0
{
762
0
    uint32_t *wp = (uint32_t *)cp0;
763
0
    tmsize_t wc = cc / 4;
764
765
0
    if (!horDiff32(tif, cp0, cc))
766
0
        return 0;
767
768
0
    TIFFSwabArrayOfLong(wp, wc);
769
0
    return 1;
770
0
}
771
772
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
773
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
774
0
{
775
0
    TIFFPredictorState *sp = PredictorState(tif);
776
0
    tmsize_t stride = sp->stride;
777
0
    uint64_t *wp = (uint64_t *)cp0;
778
0
    tmsize_t wc = cc / 8;
779
780
0
    if ((cc % (8 * stride)) != 0)
781
0
    {
782
0
        TIFFErrorExtR(tif, "horDiff64", "%s", "(cc%(8*stride))!=0");
783
0
        return 0;
784
0
    }
785
786
0
    if (wc > stride)
787
0
    {
788
0
        wc -= stride;
789
0
        wp += wc - 1;
790
0
        do
791
0
        {
792
0
            REPEAT4(stride, wp[stride] -= wp[0]; wp--)
793
0
            wc -= stride;
794
0
        } while (wc > 0);
795
0
    }
796
0
    return 1;
797
0
}
798
799
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
800
0
{
801
0
    uint64_t *wp = (uint64_t *)cp0;
802
0
    tmsize_t wc = cc / 8;
803
804
0
    if (!horDiff64(tif, cp0, cc))
805
0
        return 0;
806
807
0
    TIFFSwabArrayOfLong8(wp, wc);
808
0
    return 1;
809
0
}
810
811
/*
812
 * Floating point predictor differencing routine.
813
 */
814
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
815
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc)
816
0
{
817
0
    tmsize_t stride = PredictorState(tif)->stride;
818
0
    uint32_t bps = tif->tif_dir.td_bitspersample / 8;
819
0
    tmsize_t wc = cc / bps;
820
0
    tmsize_t count;
821
0
    uint8_t *cp = (uint8_t *)cp0;
822
0
    uint8_t *tmp;
823
824
0
    if ((cc % (bps * stride)) != 0)
825
0
    {
826
0
        TIFFErrorExtR(tif, "fpDiff", "%s", "(cc%(bps*stride))!=0");
827
0
        return 0;
828
0
    }
829
830
0
    tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
831
0
    if (!tmp)
832
0
        return 0;
833
834
0
    _TIFFmemcpy(tmp, cp0, cc);
835
0
    for (count = 0; count < wc; count++)
836
0
    {
837
0
        uint32_t byte;
838
0
        for (byte = 0; byte < bps; byte++)
839
0
        {
840
#if WORDS_BIGENDIAN
841
            cp[byte * wc + count] = tmp[bps * count + byte];
842
#else
843
0
            cp[(bps - byte - 1) * wc + count] = tmp[bps * count + byte];
844
0
#endif
845
0
        }
846
0
    }
847
0
    _TIFFfreeExt(tif, tmp);
848
849
0
    cp = (uint8_t *)cp0;
850
0
    cp += cc - stride - 1;
851
0
    for (count = cc; count > stride; count -= stride)
852
0
        REPEAT4(stride,
853
0
                cp[stride] = (unsigned char)((cp[stride] - cp[0]) & 0xff);
854
0
                cp--)
855
0
    return 1;
856
0
}
857
858
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
859
0
{
860
0
    TIFFPredictorState *sp = PredictorState(tif);
861
862
0
    assert(sp != NULL);
863
0
    assert(sp->encodepfunc != NULL);
864
0
    assert(sp->encoderow != NULL);
865
866
    /* XXX horizontal differencing alters user's data XXX */
867
0
    if (!(*sp->encodepfunc)(tif, bp, cc))
868
0
        return 0;
869
0
    return (*sp->encoderow)(tif, bp, cc, s);
870
0
}
871
872
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
873
                               uint16_t s)
874
0
{
875
0
    static const char module[] = "PredictorEncodeTile";
876
0
    TIFFPredictorState *sp = PredictorState(tif);
877
0
    uint8_t *working_copy;
878
0
    tmsize_t cc = cc0, rowsize;
879
0
    unsigned char *bp;
880
0
    int result_code;
881
882
0
    assert(sp != NULL);
883
0
    assert(sp->encodepfunc != NULL);
884
0
    assert(sp->encodetile != NULL);
885
886
    /*
887
     * Do predictor manipulation in a working buffer to avoid altering
888
     * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
889
     */
890
0
    working_copy = (uint8_t *)_TIFFmallocExt(tif, cc0);
891
0
    if (working_copy == NULL)
892
0
    {
893
0
        TIFFErrorExtR(tif, module,
894
0
                      "Out of memory allocating %" PRId64 " byte temp buffer.",
895
0
                      (int64_t)cc0);
896
0
        return 0;
897
0
    }
898
0
    memcpy(working_copy, bp0, cc0);
899
0
    bp = working_copy;
900
901
0
    rowsize = sp->rowsize;
902
0
    assert(rowsize > 0);
903
0
    if ((cc0 % rowsize) != 0)
904
0
    {
905
0
        TIFFErrorExtR(tif, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0");
906
0
        _TIFFfreeExt(tif, working_copy);
907
0
        return 0;
908
0
    }
909
0
    while (cc > 0)
910
0
    {
911
0
        (*sp->encodepfunc)(tif, bp, rowsize);
912
0
        cc -= rowsize;
913
0
        bp += rowsize;
914
0
    }
915
0
    result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
916
917
0
    _TIFFfreeExt(tif, working_copy);
918
919
0
    return result_code;
920
0
}
921
922
#define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */
923
924
static const TIFFField predictFields[] = {
925
    {TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
926
     TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL},
927
};
928
929
static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap)
930
12.2k
{
931
12.2k
    TIFFPredictorState *sp = PredictorState(tif);
932
933
12.2k
    assert(sp != NULL);
934
12.2k
    assert(sp->vsetparent != NULL);
935
936
12.2k
    switch (tag)
937
12.2k
    {
938
0
        case TIFFTAG_PREDICTOR:
939
0
            sp->predictor = (uint16_t)va_arg(ap, uint16_vap);
940
0
            TIFFSetFieldBit(tif, FIELD_PREDICTOR);
941
0
            break;
942
12.2k
        default:
943
12.2k
            return (*sp->vsetparent)(tif, tag, ap);
944
12.2k
    }
945
0
    tif->tif_flags |= TIFF_DIRTYDIRECT;
946
0
    return 1;
947
12.2k
}
948
949
static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap)
950
0
{
951
0
    TIFFPredictorState *sp = PredictorState(tif);
952
953
0
    assert(sp != NULL);
954
0
    assert(sp->vgetparent != NULL);
955
956
0
    switch (tag)
957
0
    {
958
0
        case TIFFTAG_PREDICTOR:
959
0
            *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
960
0
            break;
961
0
        default:
962
0
            return (*sp->vgetparent)(tif, tag, ap);
963
0
    }
964
0
    return 1;
965
0
}
966
967
static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
968
0
{
969
0
    TIFFPredictorState *sp = PredictorState(tif);
970
971
0
    (void)flags;
972
0
    if (TIFFFieldSet(tif, FIELD_PREDICTOR))
973
0
    {
974
0
        fprintf(fd, "  Predictor: ");
975
0
        switch (sp->predictor)
976
0
        {
977
0
            case 1:
978
0
                fprintf(fd, "none ");
979
0
                break;
980
0
            case 2:
981
0
                fprintf(fd, "horizontal differencing ");
982
0
                break;
983
0
            case 3:
984
0
                fprintf(fd, "floating point predictor ");
985
0
                break;
986
0
        }
987
0
        fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
988
0
    }
989
0
    if (sp->printdir)
990
0
        (*sp->printdir)(tif, fd, flags);
991
0
}
992
993
int TIFFPredictorInit(TIFF *tif)
994
2.03k
{
995
2.03k
    TIFFPredictorState *sp = PredictorState(tif);
996
997
2.03k
    assert(sp != 0);
998
999
    /*
1000
     * Merge codec-specific tag information.
1001
     */
1002
2.03k
    if (!_TIFFMergeFields(tif, predictFields, TIFFArrayCount(predictFields)))
1003
0
    {
1004
0
        TIFFErrorExtR(tif, "TIFFPredictorInit",
1005
0
                      "Merging Predictor codec-specific tags failed");
1006
0
        return 0;
1007
0
    }
1008
1009
    /*
1010
     * Override parent get/set field methods.
1011
     */
1012
2.03k
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
1013
2.03k
    tif->tif_tagmethods.vgetfield =
1014
2.03k
        PredictorVGetField; /* hook for predictor tag */
1015
2.03k
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
1016
2.03k
    tif->tif_tagmethods.vsetfield =
1017
2.03k
        PredictorVSetField; /* hook for predictor tag */
1018
2.03k
    sp->printdir = tif->tif_tagmethods.printdir;
1019
2.03k
    tif->tif_tagmethods.printdir =
1020
2.03k
        PredictorPrintDir; /* hook for predictor tag */
1021
1022
2.03k
    sp->setupdecode = tif->tif_setupdecode;
1023
2.03k
    tif->tif_setupdecode = PredictorSetupDecode;
1024
2.03k
    sp->setupencode = tif->tif_setupencode;
1025
2.03k
    tif->tif_setupencode = PredictorSetupEncode;
1026
1027
2.03k
    sp->predictor = 1;      /* default value */
1028
2.03k
    sp->encodepfunc = NULL; /* no predictor routine */
1029
2.03k
    sp->decodepfunc = NULL; /* no predictor routine */
1030
2.03k
    return 1;
1031
2.03k
}
1032
1033
int TIFFPredictorCleanup(TIFF *tif)
1034
2.03k
{
1035
2.03k
    TIFFPredictorState *sp = PredictorState(tif);
1036
1037
2.03k
    assert(sp != 0);
1038
1039
2.03k
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
1040
2.03k
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
1041
2.03k
    tif->tif_tagmethods.printdir = sp->printdir;
1042
2.03k
    tif->tif_setupdecode = sp->setupdecode;
1043
2.03k
    tif->tif_setupencode = sp->setupencode;
1044
1045
2.03k
    return 1;
1046
2.03k
}