Coverage Report

Created: 2025-06-22 06:59

/src/gdal/frmts/gtiff/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
#if defined(__x86_64__) || defined(_M_X64)
34
#include <emmintrin.h>
35
#endif
36
37
0
#define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
38
39
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
40
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
41
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
42
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
43
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
44
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
45
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
46
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
47
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
48
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
49
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
50
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
51
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
52
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
53
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc);
54
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc);
55
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
56
                              uint16_t s);
57
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
58
                               uint16_t s);
59
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
60
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
61
                               uint16_t s);
62
63
static int PredictorSetup(TIFF *tif)
64
0
{
65
0
    static const char module[] = "PredictorSetup";
66
67
0
    TIFFPredictorState *sp = PredictorState(tif);
68
0
    TIFFDirectory *td = &tif->tif_dir;
69
70
0
    switch (sp->predictor) /* no differencing */
71
0
    {
72
0
        case PREDICTOR_NONE:
73
0
            return 1;
74
0
        case PREDICTOR_HORIZONTAL:
75
0
            if (td->td_bitspersample != 8 && td->td_bitspersample != 16 &&
76
0
                td->td_bitspersample != 32 && td->td_bitspersample != 64)
77
0
            {
78
0
                TIFFErrorExtR(tif, module,
79
0
                              "Horizontal differencing \"Predictor\" not "
80
0
                              "supported with %" PRIu16 "-bit samples",
81
0
                              td->td_bitspersample);
82
0
                return 0;
83
0
            }
84
0
            break;
85
0
        case PREDICTOR_FLOATINGPOINT:
86
0
            if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP)
87
0
            {
88
0
                TIFFErrorExtR(
89
0
                    tif, module,
90
0
                    "Floating point \"Predictor\" not supported with %" PRIu16
91
0
                    " data format",
92
0
                    td->td_sampleformat);
93
0
                return 0;
94
0
            }
95
0
            if (td->td_bitspersample != 16 && td->td_bitspersample != 24 &&
96
0
                td->td_bitspersample != 32 && td->td_bitspersample != 64)
97
0
            { /* Should 64 be allowed? */
98
0
                TIFFErrorExtR(
99
0
                    tif, module,
100
0
                    "Floating point \"Predictor\" not supported with %" PRIu16
101
0
                    "-bit samples",
102
0
                    td->td_bitspersample);
103
0
                return 0;
104
0
            }
105
0
            break;
106
0
        default:
107
0
            TIFFErrorExtR(tif, module, "\"Predictor\" value %d not supported",
108
0
                          sp->predictor);
109
0
            return 0;
110
0
    }
111
0
    sp->stride =
112
0
        (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
113
0
                                                    : 1);
114
    /*
115
     * Calculate the scanline/tile-width size in bytes.
116
     */
117
0
    if (isTiled(tif))
118
0
        sp->rowsize = TIFFTileRowSize(tif);
119
0
    else
120
0
        sp->rowsize = TIFFScanlineSize(tif);
121
0
    if (sp->rowsize == 0)
122
0
        return 0;
123
124
0
    return 1;
125
0
}
126
127
static int PredictorSetupDecode(TIFF *tif)
128
0
{
129
0
    TIFFPredictorState *sp = PredictorState(tif);
130
0
    TIFFDirectory *td = &tif->tif_dir;
131
132
    /* Note: when PredictorSetup() fails, the effets of setupdecode() */
133
    /* will not be "canceled" so setupdecode() might be robust to */
134
    /* be called several times. */
135
0
    if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
136
0
        return 0;
137
138
0
    if (sp->predictor == 2)
139
0
    {
140
0
        switch (td->td_bitspersample)
141
0
        {
142
0
            case 8:
143
0
                sp->decodepfunc = horAcc8;
144
0
                break;
145
0
            case 16:
146
0
                sp->decodepfunc = horAcc16;
147
0
                break;
148
0
            case 32:
149
0
                sp->decodepfunc = horAcc32;
150
0
                break;
151
0
            case 64:
152
0
                sp->decodepfunc = horAcc64;
153
0
                break;
154
0
        }
155
        /*
156
         * Override default decoding method with one that does the
157
         * predictor stuff.
158
         */
159
0
        if (tif->tif_decoderow != PredictorDecodeRow)
160
0
        {
161
0
            sp->decoderow = tif->tif_decoderow;
162
0
            tif->tif_decoderow = PredictorDecodeRow;
163
0
            sp->decodestrip = tif->tif_decodestrip;
164
0
            tif->tif_decodestrip = PredictorDecodeTile;
165
0
            sp->decodetile = tif->tif_decodetile;
166
0
            tif->tif_decodetile = PredictorDecodeTile;
167
0
        }
168
169
        /*
170
         * If the data is horizontally differenced 16-bit data that
171
         * requires byte-swapping, then it must be byte swapped before
172
         * the accumulation step.  We do this with a special-purpose
173
         * routine and override the normal post decoding logic that
174
         * the library setup when the directory was read.
175
         */
176
0
        if (tif->tif_flags & TIFF_SWAB)
177
0
        {
178
0
            if (sp->decodepfunc == horAcc16)
179
0
            {
180
0
                sp->decodepfunc = swabHorAcc16;
181
0
                tif->tif_postdecode = _TIFFNoPostDecode;
182
0
            }
183
0
            else if (sp->decodepfunc == horAcc32)
184
0
            {
185
0
                sp->decodepfunc = swabHorAcc32;
186
0
                tif->tif_postdecode = _TIFFNoPostDecode;
187
0
            }
188
0
            else if (sp->decodepfunc == horAcc64)
189
0
            {
190
0
                sp->decodepfunc = swabHorAcc64;
191
0
                tif->tif_postdecode = _TIFFNoPostDecode;
192
0
            }
193
0
        }
194
0
    }
195
196
0
    else if (sp->predictor == 3)
197
0
    {
198
0
        sp->decodepfunc = fpAcc;
199
        /*
200
         * Override default decoding method with one that does the
201
         * predictor stuff.
202
         */
203
0
        if (tif->tif_decoderow != PredictorDecodeRow)
204
0
        {
205
0
            sp->decoderow = tif->tif_decoderow;
206
0
            tif->tif_decoderow = PredictorDecodeRow;
207
0
            sp->decodestrip = tif->tif_decodestrip;
208
0
            tif->tif_decodestrip = PredictorDecodeTile;
209
0
            sp->decodetile = tif->tif_decodetile;
210
0
            tif->tif_decodetile = PredictorDecodeTile;
211
0
        }
212
        /*
213
         * The data should not be swapped outside of the floating
214
         * point predictor, the accumulation routine should return
215
         * bytes in the native order.
216
         */
217
0
        if (tif->tif_flags & TIFF_SWAB)
218
0
        {
219
0
            tif->tif_postdecode = _TIFFNoPostDecode;
220
0
        }
221
0
    }
222
223
0
    return 1;
224
0
}
225
226
static int PredictorSetupEncode(TIFF *tif)
227
0
{
228
0
    TIFFPredictorState *sp = PredictorState(tif);
229
0
    TIFFDirectory *td = &tif->tif_dir;
230
231
0
    if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
232
0
        return 0;
233
234
0
    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
0
    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
        /*
309
         * The data should not be swapped outside of the floating
310
         * point predictor, the differentiation routine should return
311
         * bytes in the native order.
312
         */
313
0
        if (tif->tif_flags & TIFF_SWAB)
314
0
        {
315
0
            tif->tif_postdecode = _TIFFNoPostDecode;
316
0
        }
317
0
    }
318
319
0
    return 1;
320
0
}
321
322
#define REPEAT4(n, op)                                                         \
323
0
    switch (n)                                                                 \
324
0
    {                                                                          \
325
0
        default:                                                               \
326
0
        {                                                                      \
327
0
            tmsize_t i;                                                        \
328
0
            for (i = n - 4; i > 0; i--)                                        \
329
0
            {                                                                  \
330
0
                op;                                                            \
331
0
            }                                                                  \
332
0
        } /*-fallthrough*/                                                     \
333
0
        case 4:                                                                \
334
0
            op; /*-fallthrough*/                                               \
335
0
        case 3:                                                                \
336
0
            op; /*-fallthrough*/                                               \
337
0
        case 2:                                                                \
338
0
            op; /*-fallthrough*/                                               \
339
0
        case 1:                                                                \
340
0
            op; /*-fallthrough*/                                               \
341
0
        case 0:;                                                               \
342
0
    }
343
344
/* Remarks related to C standard compliance in all below functions : */
345
/* - to avoid any undefined behavior, we only operate on unsigned types */
346
/*   since the behavior of "overflows" is defined (wrap over) */
347
/* - when storing into the byte stream, we explicitly mask with 0xff so */
348
/*   as to make icc -check=conversions happy (not necessary by the standard) */
349
350
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
351
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
352
0
{
353
0
    tmsize_t stride = PredictorState(tif)->stride;
354
355
0
    uint8_t *cp = cp0;
356
0
    if ((cc % stride) != 0)
357
0
    {
358
0
        TIFFErrorExtR(tif, "horAcc8", "%s", "(cc%stride)!=0");
359
0
        return 0;
360
0
    }
361
362
0
    if (cc > stride)
363
0
    {
364
        /*
365
         * Pipeline the most common cases.
366
         */
367
0
        if (stride == 1)
368
0
        {
369
0
            uint32_t acc = cp[0];
370
0
            tmsize_t i = stride;
371
0
            for (; i < cc - 3; i += 4)
372
0
            {
373
0
                cp[i + 0] = (uint8_t)((acc += cp[i + 0]) & 0xff);
374
0
                cp[i + 1] = (uint8_t)((acc += cp[i + 1]) & 0xff);
375
0
                cp[i + 2] = (uint8_t)((acc += cp[i + 2]) & 0xff);
376
0
                cp[i + 3] = (uint8_t)((acc += cp[i + 3]) & 0xff);
377
0
            }
378
0
            for (; i < cc; i++)
379
0
            {
380
0
                cp[i + 0] = (uint8_t)((acc += cp[i + 0]) & 0xff);
381
0
            }
382
0
        }
383
0
        else if (stride == 3)
384
0
        {
385
0
            uint32_t cr = cp[0];
386
0
            uint32_t cg = cp[1];
387
0
            uint32_t cb = cp[2];
388
0
            tmsize_t i = stride;
389
0
            for (; i < cc; i += stride)
390
0
            {
391
0
                cp[i + 0] = (uint8_t)((cr += cp[i + 0]) & 0xff);
392
0
                cp[i + 1] = (uint8_t)((cg += cp[i + 1]) & 0xff);
393
0
                cp[i + 2] = (uint8_t)((cb += cp[i + 2]) & 0xff);
394
0
            }
395
0
        }
396
0
        else if (stride == 4)
397
0
        {
398
0
            uint32_t cr = cp[0];
399
0
            uint32_t cg = cp[1];
400
0
            uint32_t cb = cp[2];
401
0
            uint32_t ca = cp[3];
402
0
            tmsize_t i = stride;
403
0
            for (; i < cc; i += stride)
404
0
            {
405
0
                cp[i + 0] = (uint8_t)((cr += cp[i + 0]) & 0xff);
406
0
                cp[i + 1] = (uint8_t)((cg += cp[i + 1]) & 0xff);
407
0
                cp[i + 2] = (uint8_t)((cb += cp[i + 2]) & 0xff);
408
0
                cp[i + 3] = (uint8_t)((ca += cp[i + 3]) & 0xff);
409
0
            }
410
0
        }
411
0
        else
412
0
        {
413
0
            cc -= stride;
414
0
            do
415
0
            {
416
0
                REPEAT4(stride,
417
0
                        cp[stride] = (uint8_t)((cp[stride] + *cp) & 0xff);
418
0
                        cp++)
419
0
                cc -= stride;
420
0
            } while (cc > 0);
421
0
        }
422
0
    }
423
0
    return 1;
424
0
}
425
426
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
427
0
{
428
0
    uint16_t *wp = (uint16_t *)cp0;
429
0
    tmsize_t wc = cc / 2;
430
431
0
    TIFFSwabArrayOfShort(wp, wc);
432
0
    return horAcc16(tif, cp0, cc);
433
0
}
434
435
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
436
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
437
0
{
438
0
    tmsize_t stride = PredictorState(tif)->stride;
439
0
    uint16_t *wp = (uint16_t *)cp0;
440
0
    tmsize_t wc = cc / 2;
441
442
0
    if ((cc % (2 * stride)) != 0)
443
0
    {
444
0
        TIFFErrorExtR(tif, "horAcc16", "%s", "cc%(2*stride))!=0");
445
0
        return 0;
446
0
    }
447
448
0
    if (wc > stride)
449
0
    {
450
0
        wc -= stride;
451
0
        do
452
0
        {
453
0
            REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] +
454
0
                                                     (unsigned int)wp[0]) &
455
0
                                                    0xffff);
456
0
                    wp++)
457
0
            wc -= stride;
458
0
        } while (wc > 0);
459
0
    }
460
0
    return 1;
461
0
}
462
463
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
464
0
{
465
0
    uint32_t *wp = (uint32_t *)cp0;
466
0
    tmsize_t wc = cc / 4;
467
468
0
    TIFFSwabArrayOfLong(wp, wc);
469
0
    return horAcc32(tif, cp0, cc);
470
0
}
471
472
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
473
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
474
0
{
475
0
    tmsize_t stride = PredictorState(tif)->stride;
476
0
    uint32_t *wp = (uint32_t *)cp0;
477
0
    tmsize_t wc = cc / 4;
478
479
0
    if ((cc % (4 * stride)) != 0)
480
0
    {
481
0
        TIFFErrorExtR(tif, "horAcc32", "%s", "cc%(4*stride))!=0");
482
0
        return 0;
483
0
    }
484
485
0
    if (wc > stride)
486
0
    {
487
0
        wc -= stride;
488
0
        do
489
0
        {
490
0
            REPEAT4(stride, wp[stride] += wp[0]; wp++)
491
0
            wc -= stride;
492
0
        } while (wc > 0);
493
0
    }
494
0
    return 1;
495
0
}
496
497
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
498
0
{
499
0
    uint64_t *wp = (uint64_t *)cp0;
500
0
    tmsize_t wc = cc / 8;
501
502
0
    TIFFSwabArrayOfLong8(wp, wc);
503
0
    return horAcc64(tif, cp0, cc);
504
0
}
505
506
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
507
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
508
0
{
509
0
    tmsize_t stride = PredictorState(tif)->stride;
510
0
    uint64_t *wp = (uint64_t *)cp0;
511
0
    tmsize_t wc = cc / 8;
512
513
0
    if ((cc % (8 * stride)) != 0)
514
0
    {
515
0
        TIFFErrorExtR(tif, "horAcc64", "%s", "cc%(8*stride))!=0");
516
0
        return 0;
517
0
    }
518
519
0
    if (wc > stride)
520
0
    {
521
0
        wc -= stride;
522
0
        do
523
0
        {
524
0
            REPEAT4(stride, wp[stride] += wp[0]; wp++)
525
0
            wc -= stride;
526
0
        } while (wc > 0);
527
0
    }
528
0
    return 1;
529
0
}
530
531
/*
532
 * Floating point predictor accumulation routine.
533
 */
534
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc)
535
0
{
536
0
    tmsize_t stride = PredictorState(tif)->stride;
537
0
    uint32_t bps = tif->tif_dir.td_bitspersample / 8;
538
0
    tmsize_t wc = cc / bps;
539
0
    tmsize_t count = cc;
540
0
    uint8_t *cp = cp0;
541
0
    uint8_t *tmp;
542
543
0
    if (cc % (bps * stride) != 0)
544
0
    {
545
0
        TIFFErrorExtR(tif, "fpAcc", "%s", "cc%(bps*stride))!=0");
546
0
        return 0;
547
0
    }
548
549
0
    tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
550
0
    if (!tmp)
551
0
        return 0;
552
553
0
    if (stride == 1)
554
0
    {
555
        /* Optimization of general case */
556
0
#define OP                                                                     \
557
0
    do                                                                         \
558
0
    {                                                                          \
559
0
        cp[1] = (uint8_t)((cp[1] + cp[0]) & 0xff);                             \
560
0
        ++cp;                                                                  \
561
0
    } while (0)
562
0
        for (; count > 8; count -= 8)
563
0
        {
564
0
            OP;
565
0
            OP;
566
0
            OP;
567
0
            OP;
568
0
            OP;
569
0
            OP;
570
0
            OP;
571
0
            OP;
572
0
        }
573
0
        for (; count > 1; count -= 1)
574
0
        {
575
0
            OP;
576
0
        }
577
0
#undef OP
578
0
    }
579
0
    else
580
0
    {
581
0
        while (count > stride)
582
0
        {
583
0
            REPEAT4(stride, cp[stride] = (uint8_t)((cp[stride] + cp[0]) & 0xff);
584
0
                    cp++)
585
0
            count -= stride;
586
0
        }
587
0
    }
588
589
0
    _TIFFmemcpy(tmp, cp0, cc);
590
0
    cp = (uint8_t *)cp0;
591
0
    count = 0;
592
593
0
#if defined(__x86_64__) || defined(_M_X64)
594
0
    if (bps == 4)
595
0
    {
596
        /* Optimization of general case */
597
0
        for (; count + 15 < wc; count += 16)
598
0
        {
599
            /* Interlace 4*16 byte values */
600
601
0
            __m128i xmm0 =
602
0
                _mm_loadu_si128((const __m128i *)(tmp + count + 3 * wc));
603
0
            __m128i xmm1 =
604
0
                _mm_loadu_si128((const __m128i *)(tmp + count + 2 * wc));
605
0
            __m128i xmm2 =
606
0
                _mm_loadu_si128((const __m128i *)(tmp + count + 1 * wc));
607
0
            __m128i xmm3 =
608
0
                _mm_loadu_si128((const __m128i *)(tmp + count + 0 * wc));
609
            /* (xmm0_0, xmm1_0, xmm0_1, xmm1_1, xmm0_2, xmm1_2, ...) */
610
0
            __m128i tmp0 = _mm_unpacklo_epi8(xmm0, xmm1);
611
            /* (xmm0_8, xmm1_8, xmm0_9, xmm1_9, xmm0_10, xmm1_10, ...) */
612
0
            __m128i tmp1 = _mm_unpackhi_epi8(xmm0, xmm1);
613
            /* (xmm2_0, xmm3_0, xmm2_1, xmm3_1, xmm2_2, xmm3_2, ...) */
614
0
            __m128i tmp2 = _mm_unpacklo_epi8(xmm2, xmm3);
615
            /* (xmm2_8, xmm3_8, xmm2_9, xmm3_9, xmm2_10, xmm3_10, ...) */
616
0
            __m128i tmp3 = _mm_unpackhi_epi8(xmm2, xmm3);
617
            /* (xmm0_0, xmm1_0, xmm2_0, xmm3_0, xmm0_1, xmm1_1, xmm2_1, xmm3_1,
618
             * ...) */
619
0
            __m128i tmp2_0 = _mm_unpacklo_epi16(tmp0, tmp2);
620
0
            __m128i tmp2_1 = _mm_unpackhi_epi16(tmp0, tmp2);
621
0
            __m128i tmp2_2 = _mm_unpacklo_epi16(tmp1, tmp3);
622
0
            __m128i tmp2_3 = _mm_unpackhi_epi16(tmp1, tmp3);
623
0
            _mm_storeu_si128((__m128i *)(cp + 4 * count + 0 * 16), tmp2_0);
624
0
            _mm_storeu_si128((__m128i *)(cp + 4 * count + 1 * 16), tmp2_1);
625
0
            _mm_storeu_si128((__m128i *)(cp + 4 * count + 2 * 16), tmp2_2);
626
0
            _mm_storeu_si128((__m128i *)(cp + 4 * count + 3 * 16), tmp2_3);
627
0
        }
628
0
    }
629
0
#endif
630
631
0
    for (; count < wc; count++)
632
0
    {
633
0
        uint32_t byte;
634
0
        for (byte = 0; byte < bps; byte++)
635
0
        {
636
#if WORDS_BIGENDIAN
637
            cp[bps * count + byte] = tmp[byte * wc + count];
638
#else
639
0
            cp[bps * count + byte] = tmp[(bps - byte - 1) * wc + count];
640
0
#endif
641
0
        }
642
0
    }
643
0
    _TIFFfreeExt(tif, tmp);
644
0
    return 1;
645
0
}
646
647
/*
648
 * Decode a scanline and apply the predictor routine.
649
 */
650
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
651
                              uint16_t s)
652
0
{
653
0
    TIFFPredictorState *sp = PredictorState(tif);
654
655
0
    assert(sp != NULL);
656
0
    assert(sp->decoderow != NULL);
657
0
    assert(sp->decodepfunc != NULL);
658
659
0
    if ((*sp->decoderow)(tif, op0, occ0, s))
660
0
    {
661
0
        return (*sp->decodepfunc)(tif, op0, occ0);
662
0
    }
663
0
    else
664
0
        return 0;
665
0
}
666
667
/*
668
 * Decode a tile/strip and apply the predictor routine.
669
 * Note that horizontal differencing must be done on a
670
 * row-by-row basis.  The width of a "row" has already
671
 * been calculated at pre-decode time according to the
672
 * strip/tile dimensions.
673
 */
674
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
675
                               uint16_t s)
676
0
{
677
0
    TIFFPredictorState *sp = PredictorState(tif);
678
679
0
    assert(sp != NULL);
680
0
    assert(sp->decodetile != NULL);
681
682
0
    if ((*sp->decodetile)(tif, op0, occ0, s))
683
0
    {
684
0
        tmsize_t rowsize = sp->rowsize;
685
0
        assert(rowsize > 0);
686
0
        if ((occ0 % rowsize) != 0)
687
0
        {
688
0
            TIFFErrorExtR(tif, "PredictorDecodeTile", "%s",
689
0
                          "occ0%rowsize != 0");
690
0
            return 0;
691
0
        }
692
0
        assert(sp->decodepfunc != NULL);
693
0
        while (occ0 > 0)
694
0
        {
695
0
            if (!(*sp->decodepfunc)(tif, op0, rowsize))
696
0
                return 0;
697
0
            occ0 -= rowsize;
698
0
            op0 += rowsize;
699
0
        }
700
0
        return 1;
701
0
    }
702
0
    else
703
0
        return 0;
704
0
}
705
706
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
707
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
708
0
{
709
0
    TIFFPredictorState *sp = PredictorState(tif);
710
0
    tmsize_t stride = sp->stride;
711
0
    unsigned char *cp = (unsigned char *)cp0;
712
713
0
    if ((cc % stride) != 0)
714
0
    {
715
0
        TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%stride)!=0");
716
0
        return 0;
717
0
    }
718
719
0
    if (cc > stride)
720
0
    {
721
0
        cc -= stride;
722
        /*
723
         * Pipeline the most common cases.
724
         */
725
0
        if (stride == 3)
726
0
        {
727
0
            unsigned int r1, g1, b1;
728
0
            unsigned int r2 = cp[0];
729
0
            unsigned int g2 = cp[1];
730
0
            unsigned int b2 = cp[2];
731
0
            do
732
0
            {
733
0
                r1 = cp[3];
734
0
                cp[3] = (unsigned char)((r1 - r2) & 0xff);
735
0
                r2 = r1;
736
0
                g1 = cp[4];
737
0
                cp[4] = (unsigned char)((g1 - g2) & 0xff);
738
0
                g2 = g1;
739
0
                b1 = cp[5];
740
0
                cp[5] = (unsigned char)((b1 - b2) & 0xff);
741
0
                b2 = b1;
742
0
                cp += 3;
743
0
            } while ((cc -= 3) > 0);
744
0
        }
745
0
        else if (stride == 4)
746
0
        {
747
0
            unsigned int r1, g1, b1, a1;
748
0
            unsigned int r2 = cp[0];
749
0
            unsigned int g2 = cp[1];
750
0
            unsigned int b2 = cp[2];
751
0
            unsigned int a2 = cp[3];
752
0
            do
753
0
            {
754
0
                r1 = cp[4];
755
0
                cp[4] = (unsigned char)((r1 - r2) & 0xff);
756
0
                r2 = r1;
757
0
                g1 = cp[5];
758
0
                cp[5] = (unsigned char)((g1 - g2) & 0xff);
759
0
                g2 = g1;
760
0
                b1 = cp[6];
761
0
                cp[6] = (unsigned char)((b1 - b2) & 0xff);
762
0
                b2 = b1;
763
0
                a1 = cp[7];
764
0
                cp[7] = (unsigned char)((a1 - a2) & 0xff);
765
0
                a2 = a1;
766
0
                cp += 4;
767
0
            } while ((cc -= 4) > 0);
768
0
        }
769
0
        else
770
0
        {
771
0
            cp += cc - 1;
772
0
            do
773
0
            {
774
0
                REPEAT4(stride,
775
0
                        cp[stride] =
776
0
                            (unsigned char)((cp[stride] - cp[0]) & 0xff);
777
0
                        cp--)
778
0
            } while ((cc -= stride) > 0);
779
0
        }
780
0
    }
781
0
    return 1;
782
0
}
783
784
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
785
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
786
0
{
787
0
    TIFFPredictorState *sp = PredictorState(tif);
788
0
    tmsize_t stride = sp->stride;
789
0
    uint16_t *wp = (uint16_t *)cp0;
790
0
    tmsize_t wc = cc / 2;
791
792
0
    if ((cc % (2 * stride)) != 0)
793
0
    {
794
0
        TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%(2*stride))!=0");
795
0
        return 0;
796
0
    }
797
798
0
    if (wc > stride)
799
0
    {
800
0
        wc -= stride;
801
0
        wp += wc - 1;
802
0
        do
803
0
        {
804
0
            REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] -
805
0
                                                     (unsigned int)wp[0]) &
806
0
                                                    0xffff);
807
0
                    wp--)
808
0
            wc -= stride;
809
0
        } while (wc > 0);
810
0
    }
811
0
    return 1;
812
0
}
813
814
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
815
0
{
816
0
    uint16_t *wp = (uint16_t *)cp0;
817
0
    tmsize_t wc = cc / 2;
818
819
0
    if (!horDiff16(tif, cp0, cc))
820
0
        return 0;
821
822
0
    TIFFSwabArrayOfShort(wp, wc);
823
0
    return 1;
824
0
}
825
826
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
827
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
828
0
{
829
0
    TIFFPredictorState *sp = PredictorState(tif);
830
0
    tmsize_t stride = sp->stride;
831
0
    uint32_t *wp = (uint32_t *)cp0;
832
0
    tmsize_t wc = cc / 4;
833
834
0
    if ((cc % (4 * stride)) != 0)
835
0
    {
836
0
        TIFFErrorExtR(tif, "horDiff32", "%s", "(cc%(4*stride))!=0");
837
0
        return 0;
838
0
    }
839
840
0
    if (wc > stride)
841
0
    {
842
0
        wc -= stride;
843
0
        wp += wc - 1;
844
0
        do
845
0
        {
846
0
            REPEAT4(stride, wp[stride] -= wp[0]; wp--)
847
0
            wc -= stride;
848
0
        } while (wc > 0);
849
0
    }
850
0
    return 1;
851
0
}
852
853
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
854
0
{
855
0
    uint32_t *wp = (uint32_t *)cp0;
856
0
    tmsize_t wc = cc / 4;
857
858
0
    if (!horDiff32(tif, cp0, cc))
859
0
        return 0;
860
861
0
    TIFFSwabArrayOfLong(wp, wc);
862
0
    return 1;
863
0
}
864
865
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
866
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
867
0
{
868
0
    TIFFPredictorState *sp = PredictorState(tif);
869
0
    tmsize_t stride = sp->stride;
870
0
    uint64_t *wp = (uint64_t *)cp0;
871
0
    tmsize_t wc = cc / 8;
872
873
0
    if ((cc % (8 * stride)) != 0)
874
0
    {
875
0
        TIFFErrorExtR(tif, "horDiff64", "%s", "(cc%(8*stride))!=0");
876
0
        return 0;
877
0
    }
878
879
0
    if (wc > stride)
880
0
    {
881
0
        wc -= stride;
882
0
        wp += wc - 1;
883
0
        do
884
0
        {
885
0
            REPEAT4(stride, wp[stride] -= wp[0]; wp--)
886
0
            wc -= stride;
887
0
        } while (wc > 0);
888
0
    }
889
0
    return 1;
890
0
}
891
892
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
893
0
{
894
0
    uint64_t *wp = (uint64_t *)cp0;
895
0
    tmsize_t wc = cc / 8;
896
897
0
    if (!horDiff64(tif, cp0, cc))
898
0
        return 0;
899
900
0
    TIFFSwabArrayOfLong8(wp, wc);
901
0
    return 1;
902
0
}
903
904
/*
905
 * Floating point predictor differencing routine.
906
 */
907
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
908
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc)
909
0
{
910
0
    tmsize_t stride = PredictorState(tif)->stride;
911
0
    uint32_t bps = tif->tif_dir.td_bitspersample / 8;
912
0
    tmsize_t wc = cc / bps;
913
0
    tmsize_t count;
914
0
    uint8_t *cp = (uint8_t *)cp0;
915
0
    uint8_t *tmp;
916
917
0
    if ((cc % (bps * stride)) != 0)
918
0
    {
919
0
        TIFFErrorExtR(tif, "fpDiff", "%s", "(cc%(bps*stride))!=0");
920
0
        return 0;
921
0
    }
922
923
0
    tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
924
0
    if (!tmp)
925
0
        return 0;
926
927
0
    _TIFFmemcpy(tmp, cp0, cc);
928
0
    for (count = 0; count < wc; count++)
929
0
    {
930
0
        uint32_t byte;
931
0
        for (byte = 0; byte < bps; byte++)
932
0
        {
933
#if WORDS_BIGENDIAN
934
            cp[byte * wc + count] = tmp[bps * count + byte];
935
#else
936
0
            cp[(bps - byte - 1) * wc + count] = tmp[bps * count + byte];
937
0
#endif
938
0
        }
939
0
    }
940
0
    _TIFFfreeExt(tif, tmp);
941
942
0
    cp = (uint8_t *)cp0;
943
0
    cp += cc - stride - 1;
944
0
    for (count = cc; count > stride; count -= stride)
945
0
        REPEAT4(stride,
946
0
                cp[stride] = (unsigned char)((cp[stride] - cp[0]) & 0xff);
947
0
                cp--)
948
0
    return 1;
949
0
}
950
951
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
952
0
{
953
0
    static const char module[] = "PredictorEncodeRow";
954
0
    TIFFPredictorState *sp = PredictorState(tif);
955
0
    uint8_t *working_copy;
956
0
    int result_code;
957
958
0
    assert(sp != NULL);
959
0
    assert(sp->encodepfunc != NULL);
960
0
    assert(sp->encoderow != NULL);
961
962
    /*
963
     * Do predictor manipulation in a working buffer to avoid altering
964
     * the callers buffer, like for PredictorEncodeTile().
965
     * https://gitlab.com/libtiff/libtiff/-/issues/5
966
     */
967
0
    working_copy = (uint8_t *)_TIFFmallocExt(tif, cc);
968
0
    if (working_copy == NULL)
969
0
    {
970
0
        TIFFErrorExtR(tif, module,
971
0
                      "Out of memory allocating %" PRId64 " byte temp buffer.",
972
0
                      (int64_t)cc);
973
0
        return 0;
974
0
    }
975
0
    memcpy(working_copy, bp, cc);
976
977
0
    if (!(*sp->encodepfunc)(tif, working_copy, cc))
978
0
    {
979
0
        _TIFFfreeExt(tif, working_copy);
980
0
        return 0;
981
0
    }
982
0
    result_code = (*sp->encoderow)(tif, working_copy, cc, s);
983
0
    _TIFFfreeExt(tif, working_copy);
984
0
    return result_code;
985
0
}
986
987
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
988
                               uint16_t s)
989
0
{
990
0
    static const char module[] = "PredictorEncodeTile";
991
0
    TIFFPredictorState *sp = PredictorState(tif);
992
0
    uint8_t *working_copy;
993
0
    tmsize_t cc = cc0, rowsize;
994
0
    unsigned char *bp;
995
0
    int result_code;
996
997
0
    assert(sp != NULL);
998
0
    assert(sp->encodepfunc != NULL);
999
0
    assert(sp->encodetile != NULL);
1000
1001
    /*
1002
     * Do predictor manipulation in a working buffer to avoid altering
1003
     * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
1004
     */
1005
0
    working_copy = (uint8_t *)_TIFFmallocExt(tif, cc0);
1006
0
    if (working_copy == NULL)
1007
0
    {
1008
0
        TIFFErrorExtR(tif, module,
1009
0
                      "Out of memory allocating %" PRId64 " byte temp buffer.",
1010
0
                      (int64_t)cc0);
1011
0
        return 0;
1012
0
    }
1013
0
    memcpy(working_copy, bp0, cc0);
1014
0
    bp = working_copy;
1015
1016
0
    rowsize = sp->rowsize;
1017
0
    assert(rowsize > 0);
1018
0
    if ((cc0 % rowsize) != 0)
1019
0
    {
1020
0
        TIFFErrorExtR(tif, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0");
1021
0
        _TIFFfreeExt(tif, working_copy);
1022
0
        return 0;
1023
0
    }
1024
0
    while (cc > 0)
1025
0
    {
1026
0
        (*sp->encodepfunc)(tif, bp, rowsize);
1027
0
        cc -= rowsize;
1028
0
        bp += rowsize;
1029
0
    }
1030
0
    result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
1031
1032
0
    _TIFFfreeExt(tif, working_copy);
1033
1034
0
    return result_code;
1035
0
}
1036
1037
#define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */
1038
1039
static const TIFFField predictFields[] = {
1040
    {TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
1041
     FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL},
1042
};
1043
1044
static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap)
1045
0
{
1046
0
    TIFFPredictorState *sp = PredictorState(tif);
1047
1048
0
    assert(sp != NULL);
1049
0
    assert(sp->vsetparent != NULL);
1050
1051
0
    switch (tag)
1052
0
    {
1053
0
        case TIFFTAG_PREDICTOR:
1054
0
            sp->predictor = (uint16_t)va_arg(ap, uint16_vap);
1055
0
            TIFFSetFieldBit(tif, FIELD_PREDICTOR);
1056
0
            break;
1057
0
        default:
1058
0
            return (*sp->vsetparent)(tif, tag, ap);
1059
0
    }
1060
0
    tif->tif_flags |= TIFF_DIRTYDIRECT;
1061
0
    return 1;
1062
0
}
1063
1064
static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap)
1065
0
{
1066
0
    TIFFPredictorState *sp = PredictorState(tif);
1067
1068
0
    assert(sp != NULL);
1069
0
    assert(sp->vgetparent != NULL);
1070
1071
0
    switch (tag)
1072
0
    {
1073
0
        case TIFFTAG_PREDICTOR:
1074
0
            *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
1075
0
            break;
1076
0
        default:
1077
0
            return (*sp->vgetparent)(tif, tag, ap);
1078
0
    }
1079
0
    return 1;
1080
0
}
1081
1082
static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
1083
0
{
1084
0
    TIFFPredictorState *sp = PredictorState(tif);
1085
1086
0
    (void)flags;
1087
0
    if (TIFFFieldSet(tif, FIELD_PREDICTOR))
1088
0
    {
1089
0
        fprintf(fd, "  Predictor: ");
1090
0
        switch (sp->predictor)
1091
0
        {
1092
0
            case 1:
1093
0
                fprintf(fd, "none ");
1094
0
                break;
1095
0
            case 2:
1096
0
                fprintf(fd, "horizontal differencing ");
1097
0
                break;
1098
0
            case 3:
1099
0
                fprintf(fd, "floating point predictor ");
1100
0
                break;
1101
0
        }
1102
0
        fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
1103
0
    }
1104
0
    if (sp->printdir)
1105
0
        (*sp->printdir)(tif, fd, flags);
1106
0
}
1107
1108
int TIFFPredictorInit(TIFF *tif)
1109
0
{
1110
0
    TIFFPredictorState *sp = PredictorState(tif);
1111
1112
0
    assert(sp != 0);
1113
1114
    /*
1115
     * Merge codec-specific tag information.
1116
     */
1117
0
    if (!_TIFFMergeFields(tif, predictFields, TIFFArrayCount(predictFields)))
1118
0
    {
1119
0
        TIFFErrorExtR(tif, "TIFFPredictorInit",
1120
0
                      "Merging Predictor codec-specific tags failed");
1121
0
        return 0;
1122
0
    }
1123
1124
    /*
1125
     * Override parent get/set field methods.
1126
     */
1127
0
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
1128
0
    tif->tif_tagmethods.vgetfield =
1129
0
        PredictorVGetField; /* hook for predictor tag */
1130
0
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
1131
0
    tif->tif_tagmethods.vsetfield =
1132
0
        PredictorVSetField; /* hook for predictor tag */
1133
0
    sp->printdir = tif->tif_tagmethods.printdir;
1134
0
    tif->tif_tagmethods.printdir =
1135
0
        PredictorPrintDir; /* hook for predictor tag */
1136
1137
0
    sp->setupdecode = tif->tif_setupdecode;
1138
0
    tif->tif_setupdecode = PredictorSetupDecode;
1139
0
    sp->setupencode = tif->tif_setupencode;
1140
0
    tif->tif_setupencode = PredictorSetupEncode;
1141
1142
0
    sp->predictor = 1;      /* default value */
1143
0
    sp->encodepfunc = NULL; /* no predictor routine */
1144
0
    sp->decodepfunc = NULL; /* no predictor routine */
1145
0
    return 1;
1146
0
}
1147
1148
int TIFFPredictorCleanup(TIFF *tif)
1149
0
{
1150
0
    TIFFPredictorState *sp = PredictorState(tif);
1151
1152
0
    assert(sp != 0);
1153
1154
0
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
1155
0
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
1156
0
    tif->tif_tagmethods.printdir = sp->printdir;
1157
0
    tif->tif_setupdecode = sp->setupdecode;
1158
0
    tif->tif_setupencode = sp->setupencode;
1159
1160
0
    return 1;
1161
0
}