[master] Revised png_get_int_32 and png_get_int_16 functions.

This commit is contained in:
Glenn Randers-Pehrson 2010-11-20 15:00:35 -06:00
parent 60ba227d57
commit 6bb02b28a1

View File

@ -1,7 +1,7 @@
/* pngrutil.c - utilities to read a PNG file /* pngrutil.c - utilities to read a PNG file
* *
* Last changed in libpng 1.4.5 [November 20, 2010] * Last changed in libpng 1.4.5 [%RDATE%]
* Copyright (c) 1998-2010 Glenn Randers-Pehrson * Copyright (c) 1998-2010 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@ -30,16 +30,15 @@ png_get_uint_31(png_structp png_ptr, png_bytep buf)
} }
#ifndef PNG_USE_READ_MACROS #ifndef PNG_USE_READ_MACROS
/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
png_uint_32 (PNGAPI png_int_32 PNGAPI
png_get_uint_32)(png_bytep buf) png_get_int_32(png_bytep buf)
{ {
png_uint_32 i = png_uint_32 uval = png_get_uint_32(buf);
((png_uint_32)(*(buf )) << 24) + if ((uval & 0x80000000L) == 0) /* non-negative */
((png_uint_32)(*(buf + 1)) << 16) + return uval;
((png_uint_32)(*(buf + 2)) << 8) +
((png_uint_32)(*(buf + 3)) ) ;
return (i); uval = (uval ^ 0xffffffffL) + 1; /* 2's complement: -x = ~x+1 */
return -(png_int_32)uval;
} }
/* Grab a signed 32-bit integer from a buffer in big-endian format. The /* Grab a signed 32-bit integer from a buffer in big-endian format. The
@ -48,25 +47,25 @@ png_get_uint_32)(png_bytep buf)
* the following code does a two's complement to native conversion. * the following code does a two's complement to native conversion.
*/ */
png_int_32 PNGAPI png_int_32 PNGAPI
png_get_int_32 (png_bytep buf) png_get_int_32(png_bytep buf)
{ {
png_uint_32 u = png_get_uint_32(buf); png_uint_32 uval = png_get_uint_32(buf);
if ((u & 0x80000000) == 0) /* non-negative */ if ((uval & 0x80000000L) == 0) /* non-negative */
return u; return uval;
u = (u ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ uval = (uval ^ 0xffffffffL) + 1; /* 2's complement: -x = ~x+1 */
return -(png_int_32)u; return -(png_int_32)uval;
} }
/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
png_uint_16 PNGAPI png_uint_16 PNGAPI
png_get_uint_16 (png_bytep buf) png_get_uint_16 (png_bytep buf)
{ {
png_uint_16 i = unsigned int val =
((png_uint_32)(*buf) << 8) + ((unsigned int)(*buf) << 8) +
((png_uint_32)(*(buf + 1))); ((unsigned int)(*(buf + 1)));
return (i); return (png_uint_16)val;
} }
#endif /* PNG_USE_READ_MACROS */ #endif /* PNG_USE_READ_MACROS */