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