Flags in T1 shall be unsigned (#840)
[openjpeg.git] / thirdparty / libtiff / tif_aux.c
index 272f0d9b68214e5810c41f0f193e78cd8380bc50..927150a49352deaf881631a3abd6e00e09887993 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_aux.c,v 1.20.2.3 2010-06-09 21:15:27 bfriesen Exp $ */
+/* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron Exp $ */
 
 /*
  * Copyright (c) 1991-1997 Sam Leffler
 #include "tif_predict.h"
 #include <math.h>
 
-tdata_t
-_TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
-                 size_t nmemb, size_t elem_size, const char* what)
+uint32
+_TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
 {
-       tdata_t cp = NULL;
-       tsize_t bytes = nmemb * elem_size;
+       uint32 bytes = first * second;
+
+       if (second && bytes / second != first) {
+               TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
+               bytes = 0;
+       }
+
+       return bytes;
+}
+
+uint64
+_TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
+{
+       uint64 bytes = first * second;
+
+       if (second && bytes / second != first) {
+               TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
+               bytes = 0;
+       }
+
+       return bytes;
+}
+
+void*
+_TIFFCheckRealloc(TIFF* tif, void* buffer,
+                 tmsize_t nmemb, tmsize_t elem_size, const char* what)
+{
+       void* cp = NULL;
+       tmsize_t bytes = nmemb * elem_size;
 
        /*
         * XXX: Check for integer overflow.
@@ -46,32 +72,33 @@ _TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
        if (nmemb && elem_size && bytes / elem_size == nmemb)
                cp = _TIFFrealloc(buffer, bytes);
 
-       if (cp == NULL)
+       if (cp == NULL) {
                TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                             "Failed to allocate memory for %s "
                             "(%ld elements of %ld bytes each)",
                             what,(long) nmemb, (long) elem_size);
+       }
 
        return cp;
 }
 
-tdata_t
-_TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
+void*
+_TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
 {
-       return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
+       return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);  
 }
 
 static int
 TIFFDefaultTransferFunction(TIFFDirectory* td)
 {
        uint16 **tf = td->td_transferfunction;
-       tsize_t i, n, nbytes;
+       tmsize_t i, n, nbytes;
 
        tf[0] = tf[1] = tf[2] = 0;
-       if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
+       if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
                return 0;
 
-       n = 1<<td->td_bitspersample;
+       n = ((tmsize_t)1)<<td->td_bitspersample;
        nbytes = n * sizeof (uint16);
        if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
                return 0;
@@ -140,7 +167,7 @@ TIFFDefaultRefBlackWhite(TIFFDirectory* td)
  *     place in the library -- in TIFFDefaultDirectory.
  */
 int
-TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
+TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
 {
        TIFFDirectory *td = &tif->tif_dir;
 
@@ -269,7 +296,7 @@ TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
  * value if the tag is not present in the directory.
  */
 int
-TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
+TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
 {
        int ok;
        va_list ap;
@@ -280,6 +307,47 @@ TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
        return (ok);
 }
 
+struct _Int64Parts {
+       int32 low, high;
+};
+
+typedef union {
+       struct _Int64Parts part;
+       int64 value;
+} _Int64;
+
+float
+_TIFFUInt64ToFloat(uint64 ui64)
+{
+       _Int64 i;
+
+       i.value = ui64;
+       if (i.part.high >= 0) {
+               return (float)i.value;
+       } else {
+               long double df;
+               df = (long double)i.value;
+               df += 18446744073709551616.0; /* adding 2**64 */
+               return (float)df;
+       }
+}
+
+double
+_TIFFUInt64ToDouble(uint64 ui64)
+{
+       _Int64 i;
+
+       i.value = ui64;
+       if (i.part.high >= 0) {
+               return (double)i.value;
+       } else {
+               long double df;
+               df = (long double)i.value;
+               df += 18446744073709551616.0; /* adding 2**64 */
+               return (double)df;
+       }
+}
+
 /* vim: set ts=8 sts=8 sw=8 noet: */
 /*
  * Local Variables: