Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/tiff/libtiff/tif_aux.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1991-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
 * Auxiliary Support Routines.
29
 */
30
#include "tiffiop.h"
31
#include "tif_predict.h"
32
#include <math.h>
33
#include <float.h>
34
35
uint32_t
36
_TIFFMultiply32(TIFF* tif, uint32_t first, uint32_t second, const char* where)
37
0
{
38
0
  if (second && first > UINT32_MAX / second) {
39
0
    TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
40
0
    return 0;
41
0
  }
42
43
0
  return first * second;
44
0
}
45
46
uint64_t
47
_TIFFMultiply64(TIFF* tif, uint64_t first, uint64_t second, const char* where)
48
17.2k
{
49
17.2k
  if (second && first > UINT64_MAX / second) {
50
0
    TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
51
0
    return 0;
52
0
  }
53
54
17.2k
  return first * second;
55
17.2k
}
56
57
tmsize_t
58
_TIFFMultiplySSize(TIFF* tif, tmsize_t first, tmsize_t second, const char* where)
59
14.6k
{
60
14.6k
    if( first <= 0 || second <= 0 )
61
0
    {
62
0
        if( tif != NULL && where != NULL )
63
0
        {
64
0
            TIFFErrorExt(tif->tif_clientdata, where,
65
0
                        "Invalid argument to _TIFFMultiplySSize() in %s", where);
66
0
        }
67
0
        return 0;
68
0
    }
69
70
14.6k
    if( first > TIFF_TMSIZE_T_MAX / second )
71
0
    {
72
0
        if( tif != NULL && where != NULL )
73
0
        {
74
0
            TIFFErrorExt(tif->tif_clientdata, where,
75
0
                        "Integer overflow in %s", where);
76
0
        }
77
0
        return 0;
78
0
    }
79
14.6k
    return first * second;
80
14.6k
}
81
82
tmsize_t _TIFFCastUInt64ToSSize(TIFF* tif, uint64_t val, const char* module)
83
4.91k
{
84
4.91k
    if( val > (uint64_t)TIFF_TMSIZE_T_MAX )
85
0
    {
86
0
        if( tif != NULL && module != NULL )
87
0
        {
88
0
            TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
89
0
        }
90
0
        return 0;
91
0
    }
92
4.91k
    return (tmsize_t)val;
93
4.91k
}
94
95
void*
96
_TIFFCheckRealloc(TIFF* tif, void* buffer,
97
      tmsize_t nmemb, tmsize_t elem_size, const char* what)
98
12.7k
{
99
12.7k
  void* cp = NULL;
100
12.7k
        tmsize_t count = _TIFFMultiplySSize(tif, nmemb, elem_size, NULL);
101
  /*
102
   * Check for integer overflow.
103
   */
104
12.7k
  if (count != 0)
105
12.7k
  {
106
12.7k
    cp = _TIFFrealloc(buffer, count);
107
12.7k
  }
108
109
12.7k
  if (cp == NULL) {
110
0
    TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
111
0
           "Failed to allocate memory for %s "
112
0
           "(%"TIFF_SSIZE_FORMAT" elements of %"TIFF_SSIZE_FORMAT" bytes each)",
113
0
           what, nmemb, elem_size);
114
0
  }
115
116
12.7k
  return cp;
117
12.7k
}
118
119
void*
120
_TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
121
9.67k
{
122
9.67k
  return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);  
123
9.67k
}
124
125
static int
126
TIFFDefaultTransferFunction(TIFFDirectory* td)
127
0
{
128
0
  uint16_t **tf = td->td_transferfunction;
129
0
  tmsize_t i, n, nbytes;
130
131
0
  tf[0] = tf[1] = tf[2] = 0;
132
0
  if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
133
0
    return 0;
134
135
0
  n = ((tmsize_t)1)<<td->td_bitspersample;
136
0
  nbytes = n * sizeof (uint16_t);
137
0
        tf[0] = (uint16_t *)_TIFFmalloc(nbytes);
138
0
  if (tf[0] == NULL)
139
0
    return 0;
140
0
  tf[0][0] = 0;
141
0
  for (i = 1; i < n; i++) {
142
0
    double t = (double)i/((double) n-1.);
143
0
    tf[0][i] = (uint16_t)floor(65535. * pow(t, 2.2) + .5);
144
0
  }
145
146
0
  if (td->td_samplesperpixel - td->td_extrasamples > 1) {
147
0
                tf[1] = (uint16_t *)_TIFFmalloc(nbytes);
148
0
    if(tf[1] == NULL)
149
0
      goto bad;
150
0
    _TIFFmemcpy(tf[1], tf[0], nbytes);
151
0
                tf[2] = (uint16_t *)_TIFFmalloc(nbytes);
152
0
    if (tf[2] == NULL)
153
0
      goto bad;
154
0
    _TIFFmemcpy(tf[2], tf[0], nbytes);
155
0
  }
156
0
  return 1;
157
158
0
bad:
159
0
  if (tf[0])
160
0
    _TIFFfree(tf[0]);
161
0
  if (tf[1])
162
0
    _TIFFfree(tf[1]);
163
0
  if (tf[2])
164
0
    _TIFFfree(tf[2]);
165
0
  tf[0] = tf[1] = tf[2] = 0;
166
0
  return 0;
167
0
}
168
169
static int
170
TIFFDefaultRefBlackWhite(TIFFDirectory* td)
171
0
{
172
0
  int i;
173
174
0
        td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
175
0
  if (td->td_refblackwhite == NULL)
176
0
    return 0;
177
0
        if (td->td_photometric == PHOTOMETRIC_YCBCR) {
178
    /*
179
     * YCbCr (Class Y) images must have the ReferenceBlackWhite
180
     * tag set. Fix the broken images, which lacks that tag.
181
     */
182
0
    td->td_refblackwhite[0] = 0.0F;
183
0
    td->td_refblackwhite[1] = td->td_refblackwhite[3] =
184
0
      td->td_refblackwhite[5] = 255.0F;
185
0
    td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
186
0
  } else {
187
    /*
188
     * Assume RGB (Class R)
189
     */
190
0
    for (i = 0; i < 3; i++) {
191
0
        td->td_refblackwhite[2*i+0] = 0;
192
0
        td->td_refblackwhite[2*i+1] =
193
0
          (float)((1L<<td->td_bitspersample)-1L);
194
0
    }
195
0
  }
196
0
  return 1;
197
0
}
198
199
/*
200
 * Like TIFFGetField, but return any default
201
 * value if the tag is not present in the directory.
202
 *
203
 * NB:  We use the value in the directory, rather than
204
 *  explicit values so that defaults exist only one
205
 *  place in the library -- in TIFFDefaultDirectory.
206
 */
207
int
208
TIFFVGetFieldDefaulted(TIFF* tif, uint32_t tag, va_list ap)
209
0
{
210
0
  TIFFDirectory *td = &tif->tif_dir;
211
212
0
  if (TIFFVGetField(tif, tag, ap))
213
0
    return (1);
214
0
  switch (tag) {
215
0
  case TIFFTAG_SUBFILETYPE:
216
0
    *va_arg(ap, uint32_t *) = td->td_subfiletype;
217
0
    return (1);
218
0
  case TIFFTAG_BITSPERSAMPLE:
219
0
    *va_arg(ap, uint16_t *) = td->td_bitspersample;
220
0
    return (1);
221
0
  case TIFFTAG_THRESHHOLDING:
222
0
    *va_arg(ap, uint16_t *) = td->td_threshholding;
223
0
    return (1);
224
0
  case TIFFTAG_FILLORDER:
225
0
    *va_arg(ap, uint16_t *) = td->td_fillorder;
226
0
    return (1);
227
0
  case TIFFTAG_ORIENTATION:
228
0
    *va_arg(ap, uint16_t *) = td->td_orientation;
229
0
    return (1);
230
0
  case TIFFTAG_SAMPLESPERPIXEL:
231
0
    *va_arg(ap, uint16_t *) = td->td_samplesperpixel;
232
0
    return (1);
233
0
  case TIFFTAG_ROWSPERSTRIP:
234
0
    *va_arg(ap, uint32_t *) = td->td_rowsperstrip;
235
0
    return (1);
236
0
  case TIFFTAG_MINSAMPLEVALUE:
237
0
    *va_arg(ap, uint16_t *) = td->td_minsamplevalue;
238
0
    return (1);
239
0
  case TIFFTAG_MAXSAMPLEVALUE:
240
0
    *va_arg(ap, uint16_t *) = td->td_maxsamplevalue;
241
0
    return (1);
242
0
  case TIFFTAG_PLANARCONFIG:
243
0
    *va_arg(ap, uint16_t *) = td->td_planarconfig;
244
0
    return (1);
245
0
  case TIFFTAG_RESOLUTIONUNIT:
246
0
    *va_arg(ap, uint16_t *) = td->td_resolutionunit;
247
0
    return (1);
248
0
  case TIFFTAG_PREDICTOR:
249
0
    {
250
0
        TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
251
0
        if( sp == NULL )
252
0
        {
253
0
            TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
254
0
                         "Cannot get \"Predictor\" tag as plugin is not configured");
255
0
            *va_arg(ap, uint16_t*) = 0;
256
0
            return 0;
257
0
        }
258
0
        *va_arg(ap, uint16_t*) = (uint16_t) sp->predictor;
259
0
        return 1;
260
0
    }
261
0
  case TIFFTAG_DOTRANGE:
262
0
    *va_arg(ap, uint16_t *) = 0;
263
0
    *va_arg(ap, uint16_t *) = (1 << td->td_bitspersample) - 1;
264
0
    return (1);
265
0
  case TIFFTAG_INKSET:
266
0
    *va_arg(ap, uint16_t *) = INKSET_CMYK;
267
0
    return 1;
268
0
  case TIFFTAG_NUMBEROFINKS:
269
0
    *va_arg(ap, uint16_t *) = 4;
270
0
    return (1);
271
0
  case TIFFTAG_EXTRASAMPLES:
272
0
    *va_arg(ap, uint16_t *) = td->td_extrasamples;
273
0
    *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
274
0
    return (1);
275
0
  case TIFFTAG_MATTEING:
276
0
    *va_arg(ap, uint16_t *) =
277
0
        (td->td_extrasamples == 1 &&
278
0
         td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
279
0
    return (1);
280
0
  case TIFFTAG_TILEDEPTH:
281
0
    *va_arg(ap, uint32_t *) = td->td_tiledepth;
282
0
    return (1);
283
0
  case TIFFTAG_DATATYPE:
284
0
    *va_arg(ap, uint16_t *) = td->td_sampleformat - 1;
285
0
    return (1);
286
0
  case TIFFTAG_SAMPLEFORMAT:
287
0
    *va_arg(ap, uint16_t *) = td->td_sampleformat;
288
0
                return(1);
289
0
  case TIFFTAG_IMAGEDEPTH:
290
0
    *va_arg(ap, uint32_t *) = td->td_imagedepth;
291
0
    return (1);
292
0
  case TIFFTAG_YCBCRCOEFFICIENTS:
293
0
    {
294
      /* defaults are from CCIR Recommendation 601-1 */
295
0
      static const float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
296
0
      *va_arg(ap, const float **) = ycbcrcoeffs;
297
0
      return 1;
298
0
    }
299
0
  case TIFFTAG_YCBCRSUBSAMPLING:
300
0
    *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0];
301
0
    *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1];
302
0
    return (1);
303
0
  case TIFFTAG_YCBCRPOSITIONING:
304
0
    *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning;
305
0
    return (1);
306
0
  case TIFFTAG_WHITEPOINT:
307
0
    {
308
      /* TIFF 6.0 specification tells that it is no default
309
         value for the WhitePoint, but AdobePhotoshop TIFF
310
         Technical Note tells that it should be CIE D50. */
311
0
      static const float whitepoint[] = {
312
0
            D50_X0 / (D50_X0 + D50_Y0 + D50_Z0),
313
0
            D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0)
314
0
      };
315
0
      *va_arg(ap, const float **) = whitepoint;
316
0
      return 1;
317
0
    }
318
0
  case TIFFTAG_TRANSFERFUNCTION:
319
0
    if (!td->td_transferfunction[0] &&
320
0
        !TIFFDefaultTransferFunction(td)) {
321
0
      TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
322
0
      return (0);
323
0
    }
324
0
    *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
325
0
    if (td->td_samplesperpixel - td->td_extrasamples > 1) {
326
0
      *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
327
0
      *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
328
0
    }
329
0
    return (1);
330
0
  case TIFFTAG_REFERENCEBLACKWHITE:
331
0
    if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
332
0
      return (0);
333
0
    *va_arg(ap, const float **) = td->td_refblackwhite;
334
0
    return (1);
335
0
  }
336
0
  return 0;
337
0
}
338
339
/*
340
 * Like TIFFGetField, but return any default
341
 * value if the tag is not present in the directory.
342
 */
343
int
344
TIFFGetFieldDefaulted(TIFF* tif, uint32_t tag, ...)
345
0
{
346
0
  int ok;
347
0
  va_list ap;
348
349
0
  va_start(ap, tag);
350
0
  ok =  TIFFVGetFieldDefaulted(tif, tag, ap);
351
0
  va_end(ap);
352
0
  return (ok);
353
0
}
354
355
struct _Int64Parts {
356
  int32_t low, high;
357
};
358
359
typedef union {
360
  struct _Int64Parts part;
361
  int64_t value;
362
} _Int64;
363
364
float
365
_TIFFUInt64ToFloat(uint64_t ui64)
366
0
{
367
0
  _Int64 i;
368
369
0
  i.value = ui64;
370
0
  if (i.part.high >= 0) {
371
0
    return (float)i.value;
372
0
  } else {
373
0
    long double df;
374
0
    df = (long double)i.value;
375
0
    df += 18446744073709551616.0; /* adding 2**64 */
376
0
    return (float)df;
377
0
  }
378
0
}
379
380
double
381
_TIFFUInt64ToDouble(uint64_t ui64)
382
0
{
383
0
  _Int64 i;
384
385
0
  i.value = ui64;
386
0
  if (i.part.high >= 0) {
387
0
    return (double)i.value;
388
0
  } else {
389
0
    long double df;
390
0
    df = (long double)i.value;
391
0
    df += 18446744073709551616.0; /* adding 2**64 */
392
0
    return (double)df;
393
0
  }
394
0
}
395
396
float _TIFFClampDoubleToFloat( double val )
397
4.38k
{
398
4.38k
    if( val > FLT_MAX )
399
0
        return FLT_MAX;
400
4.38k
    if( val < -FLT_MAX )
401
0
        return -FLT_MAX;
402
4.38k
    return (float)val;
403
4.38k
}
404
405
int _TIFFSeekOK(TIFF* tif, toff_t off)
406
22.4k
{
407
    /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
408
    /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
409
22.4k
    return off <= (~(uint64_t)0) / 2 && TIFFSeekFile(tif, off, SEEK_SET) == off;
410
22.4k
}
411
412
/* vim: set ts=8 sts=8 sw=8 noet: */
413
/*
414
 * Local Variables:
415
 * mode: c
416
 * c-basic-offset: 8
417
 * fill-column: 78
418
 * End:
419
 */