[vlc-devel] [PATCH] modules/codec/jpeg.c Tighter definition of int types, etc

Wayne McDougall waynemcdougall at gmail.com
Thu Apr 21 12:38:45 CEST 2016


Tighter definition of int types, tightening of code, improved
change to type definition for win32 compilation

Thanks to jbkempf and apologies for my sloppiness

---
 modules/codec/jpeg.c | 81 +++++++++++++++++++++++-----------------------------
 1 file changed, 36 insertions(+), 45 deletions(-)

diff --git a/modules/codec/jpeg.c b/modules/codec/jpeg.c
index 3b82432..51510cd 100644
--- a/modules/codec/jpeg.c
+++ b/modules/codec/jpeg.c
@@ -184,7 +184,8 @@ static int OpenDecoder(vlc_object_t *p_this)
 /*
  * The following two functions are used to return 16 and 32 bit values from
  * the EXIF tag structure. That structure is borrowed from TIFF files
and may be
- * in big endian or little endian format. The endian parameter tells us which.
+ * in big endian or little endian format. The boolean b_bigEndian parameter
+ * is TRUE if the EXIF data is in big endian format, and FALSE for
little endian
  * Case Little Endian EXIF tag / Little Endian machine
  *   - just memcpy the tag structure into the value to return
  * Case Little Endian EXIF tag / Big Endian machine
@@ -200,18 +201,12 @@ static int OpenDecoder(vlc_object_t *p_this)
  * The claim is made that this is the best way to do it. Can you do better?
 */

-#define G_LITTLE_ENDIAN     1234
-#define G_BIG_ENDIAN        4321
-
-typedef unsigned int uint;
-typedef unsigned short ushort;
-
-LOCAL( unsigned short )
-de_get16( void * ptr, uint endian ) {
-    unsigned short val;
+LOCAL( uint16_t )
+de_get16( void * ptr, bool b_bigEndian ) {
+    uint16_t val;

     memcpy( &val, ptr, sizeof( val ) );
-    if ( endian == G_BIG_ENDIAN )
+    if ( b_bigEndian )
     {
         #ifndef WORDS_BIGENDIAN
         val = bswap16( val );
@@ -226,12 +221,12 @@ de_get16( void * ptr, uint endian ) {
     return val;
 }

-LOCAL( unsigned int )
-de_get32( void * ptr, uint endian ) {
+LOCAL( uint32_t )
+de_get32( void * ptr, bool b_bigEndian ) {
     unsigned int val;

     memcpy( &val, ptr, sizeof( val ) );
-    if ( endian == G_BIG_ENDIAN )
+    if ( b_bigEndian )
     {
         #ifndef WORDS_BIGENDIAN
         val = bswap32( val );
@@ -255,19 +250,18 @@ de_get32( void * ptr, uint endian ) {
  * the GdkPixbuf library, licensed under LGPLv2+.
  *   Copyright (C) 1999 Michael Zucchi, The Free Software Foundation
 */
-LOCAL( int )
+LOCAL( uint16_t )
 jpeg_GetOrientation( j_decompress_ptr cinfo )
 {

     uint i;                    /* index into working buffer */
-    ushort tag_type;           /* endianed tag type extracted from
tiff header */
-    uint ret;                  /* Return value */
-    uint offset;               /* de-endianed offset in various situations */
-    uint tags;                 /* number of tags in current ifd */
-    uint type;                 /* de-endianed type of tag */
-    uint count;                /* de-endianed count of elements in a tag */
-    uint tiff = 0;             /* offset to active tiff header */
-    uint endian = 0;           /* detected endian of data */
+    uint16_t tag_type;         /* endianed tag type extracted from
TIFF header */
+    uint16_t ret;              /* Return value */
+    uint32_t offset;           /* de-endianed offset in various situations */
+    uint16_t tags;             /* number of tags in current ifd */
+    uint16_t type;             /* de-endianed type of tag */
+    uint32_t count;            /* de-endianed count of elements in a tag */
+    bool b_bigEndian = FALSE;  /* detected endian of EXIF data */

     jpeg_saved_marker_ptr exif_marker;      /* Location of the Exif
APP1 marker */
     jpeg_saved_marker_ptr cmarker;          /* Location to check for
Exif APP1 marker */
@@ -275,9 +269,9 @@ jpeg_GetOrientation( j_decompress_ptr cinfo )
     const char leth[] = { 0x49, 0x49, 0x2a, 0x00 }; /* Little endian
TIFF header */
     const char beth[] = { 0x4d, 0x4d, 0x00, 0x2a }; /* Big endian
TIFF header */

-    #define EXIF_JPEG_MARKER    0xE1
+    #define EXIF_JPEG_MARKER    0xE1U
     #define EXIF_IDENT_STRING   "Exif\000\000"
-    #define EXIF_ORIENT_TAGID   0x112
+    #define EXIF_ORIENT_TAGID   0x112U

     /* check for Exif marker (also called the APP1 marker) */
     exif_marker = NULL;
@@ -325,44 +319,40 @@ jpeg_GetOrientation( j_decompress_ptr cinfo )

     while ( i < 16 )
     {
-        /* Little endian TIFF header */
-        if ( memcmp( &exif_marker->data[i], leth, 4 ) == 0 )
-        {
-            endian = G_LITTLE_ENDIAN;
-        }
-        /* Big endian TIFF header */
-        else
+        /* Is it a Big endian TIFF header?  */
         if ( memcmp( &exif_marker->data[i], beth, 4 ) == 0 )
         {
-            endian = G_BIG_ENDIAN;
+            b_bigEndian = TRUE;
         }
-        /* Keep looking through buffer */
+        /* Is it NOT a Little endian TIFF header? */
         else
+        if ( memcmp( &exif_marker->data[i], leth, 4 ) != 0 )
         {
+            /* Keep looking through buffer */
             i++;
             continue;
         }
         /* We have found either big or little endian TIFF header */
-        tiff = i;
         break;
     }

     /* So did we find a TIFF header or did we just hit end of buffer? */
-    if ( tiff == 0 )
-        return 0;
+    if ( i>15 )
+    {
+       return 0;
+    }

     /* Read out the offset pointer to IFD0 */
-    offset = de_get32( &exif_marker->data[i] + 4, endian );
+    offset = de_get32( &exif_marker->data[i] + 4, b_bigEndian );
     i = i + offset;

     /* Check that we still are within the buffer and can read the tag count */
-
     if ( ( i + 2 ) > exif_marker->data_length )
         return 0;

     /* Find out how many tags we have in IFD0. As per the TIFF spec, the first
        two bytes of the IFD contain a count of the number of tags. */
-    tags = de_get16( &exif_marker->data[i], endian );
+    tags = de_get16( &exif_marker->data[i], b_bigEndian );
     i = i + 2;

     /* Check that we still have enough data for all tags to check. The tags
@@ -374,12 +364,13 @@ jpeg_GetOrientation( j_decompress_ptr cinfo )
     /* Check through IFD0 for tags of interest */
     while ( tags-- )
     {
-        tag_type = de_get16( &exif_marker->data[i], endian );
+        tag_type = de_get16( &exif_marker->data[i], b_bigEndian );
+
         /* Is this the orientation tag? */
         if ( tag_type == EXIF_ORIENT_TAGID )
         {
-            type = de_get16( &exif_marker->data[i + 2], endian );
-            count = de_get32( &exif_marker->data[i + 4], endian );
+            type = de_get16( &exif_marker->data[i + 2], b_bigEndian );
+            count = de_get32( &exif_marker->data[i + 4], b_bigEndian );

             /* Check that type and count fields are OK. The orientation field
                will consist of a single (count=1) 2-byte integer (type=3). */
@@ -388,7 +379,7 @@ jpeg_GetOrientation( j_decompress_ptr cinfo )

             /* Return the orientation value. Within the 12-byte block, the
                pointer to the actual data is at offset 8. */
-            ret = de_get16( &exif_marker->data[i + 8], endian );
+            ret = de_get16( &exif_marker->data[i + 8], b_bigEndian );
             return ( ret <= 8 ) ? ret : 0;
         }
         /* move the pointer to the next 12-byte tag field. */
@@ -448,7 +439,7 @@ static picture_t *DecodeBlock(decoder_t *p_dec,
block_t **pp_block)
     p_dec->fmt_out.video.i_gmask = 0x0000ff00;
     p_dec->fmt_out.video.i_bmask = 0x00ff0000;

-    int i_otag; /* Orientation tag has valid range of 1-8. 1 is
normal orientation, 0 = unspecified = normal */
+    uint16_t i_otag; /* Orientation tag has valid range of 1-8. 1 is
normal orientation, 0 = unspecified = normal */
     i_otag = jpeg_GetOrientation( &p_sys->p_jpeg );
     if ( i_otag > 1 )
     {
-- 
2.1.4


More information about the vlc-devel mailing list