Fix various issues reported by the UB sanitizers

Most of these involved left shifting a negative number, which is
technically undefined (although every modern compiler I'm aware of
will implement this by treating the signed integer as a 2's complement
unsigned integer-- the LEFT_SHIFT() macro just makes this behavior
explicit in order to shut up ubsan.)  This also fixes a couple of
non-issues in the entropy codecs, whereby the sanitizer reported an
out-of-bounds index in the 4th argument of jpeg_make_d_derived_tbl().
In those cases, the index was actually out of bounds (caused by a
malformed JPEG image), but jpeg_make_d_derived_tbl() would have caught
the error and aborted prior to actually using the invalid address.  Here
again, the fix was to make our intentions explicit so as to shut up
ubsan.
This commit is contained in:
DRC
2015-09-21 12:57:41 -05:00
parent 54792ba340
commit 8e9cef2e6f
9 changed files with 121 additions and 90 deletions

View File

@@ -28,6 +28,11 @@ pixels. The "plain" upsampling routines are normally only used when
decompressing a non-YCbCr JPEG image, but they are also used when decompressing
a JPEG image whose scaled output height is 1.
[5] Fixed various negative left shifts and other issues reported by the GCC and
Clang undefined behavior sanitizers. None of these was known to pose a
security threat, but removing the warnings makes it easier to detect actual
security issues, should they arise in the future.
1.4.1
=====

View File

@@ -306,7 +306,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
}
/* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
(*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
(*block)[0] = (JCOEF) LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
}
return TRUE;

View File

@@ -91,6 +91,7 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
{
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
int ci, blkn, dctbl, actbl;
d_derived_tbl **pdtbl;
jpeg_component_info * compptr;
/* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
@@ -107,10 +108,10 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
actbl = compptr->ac_tbl_no;
/* Compute derived values for Huffman tables */
/* We may do this more than once for a table, but it's not expensive */
jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
& entropy->dc_derived_tbls[dctbl]);
jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
& entropy->ac_derived_tbls[actbl]);
pdtbl = entropy->dc_derived_tbls + dctbl;
jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, pdtbl);
pdtbl = entropy->ac_derived_tbls + actbl;
jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, pdtbl);
/* Initialize DC predictions to 0 */
entropy->saved.last_dc_val[ci] = 0;
}
@@ -490,7 +491,8 @@ jpeg_huff_decode (bitread_working_state * state,
#define AVOID_TABLES
#ifdef AVOID_TABLES
#define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1)))
#define NEG_1 ((unsigned int)-1)
#define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((NEG_1)<<(s)) + 1)))
#else

View File

@@ -3,8 +3,8 @@
*
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1995-1997, Thomas G. Lane.
* It was modified by The libjpeg-turbo Project to include only code relevant
* to libjpeg-turbo.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, D. R. Commander.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains Huffman entropy decoding routines for progressive JPEG.
@@ -96,6 +96,7 @@ start_pass_phuff_decoder (j_decompress_ptr cinfo)
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
boolean is_DC_band, bad;
int ci, coefi, tbl;
d_derived_tbl **pdtbl;
int *coef_bit_ptr;
jpeg_component_info * compptr;
@@ -168,13 +169,13 @@ start_pass_phuff_decoder (j_decompress_ptr cinfo)
if (is_DC_band) {
if (cinfo->Ah == 0) { /* DC refinement needs no table */
tbl = compptr->dc_tbl_no;
jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
& entropy->derived_tbls[tbl]);
pdtbl = entropy->derived_tbls + tbl;
jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
}
} else {
tbl = compptr->ac_tbl_no;
jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
& entropy->derived_tbls[tbl]);
pdtbl = entropy->derived_tbls + tbl;
jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
/* remember the single active table */
entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
}
@@ -203,7 +204,8 @@ start_pass_phuff_decoder (j_decompress_ptr cinfo)
#define AVOID_TABLES
#ifdef AVOID_TABLES
#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
#define NEG_1 ((unsigned)-1)
#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((NEG_1)<<(s)) + 1) : (x))
#else
@@ -336,7 +338,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
s += state.last_dc_val[ci];
state.last_dc_val[ci] = s;
/* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
(*block)[0] = (JCOEF) (s << Al);
(*block)[0] = (JCOEF) LEFT_SHIFT(s, Al);
}
/* Completed MCU, so update state */
@@ -404,7 +406,7 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
r = GET_BITS(s);
s = HUFF_EXTEND(r, s);
/* Scale and output coefficient in natural (dezigzagged) order */
(*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
(*block)[jpeg_natural_order[k]] = (JCOEF) LEFT_SHIFT(s, Al);
} else {
if (r == 15) { /* ZRL */
k += 15; /* skip 15 zeroes in band */
@@ -495,8 +497,8 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
int Se = cinfo->Se;
int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
register int s, k, r;
unsigned int EOBRUN;
JBLOCKROW block;

View File

@@ -1,8 +1,10 @@
/*
* jfdctint.c
*
* This file was part of the Independent JPEG Group's software.
* Copyright (C) 1991-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, D. R. Commander
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains a slow-but-accurate integer implementation of the
@@ -170,8 +172,8 @@ jpeg_fdct_islow (DCTELEM * data)
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
dataptr[0] = (DCTELEM) LEFT_SHIFT(tmp10 + tmp11, PASS1_BITS);
dataptr[4] = (DCTELEM) LEFT_SHIFT(tmp10 - tmp11, PASS1_BITS);
z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),

View File

@@ -1,9 +1,11 @@
/*
* jidctint.c
*
* This file was part of the Independent JPEG Group's software.
* Copyright (C) 1991-1998, Thomas G. Lane.
* Modification developed 2002-2009 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, D. R. Commander
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains a slow-but-accurate integer implementation of the
@@ -205,7 +207,8 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
inptr[DCTSIZE*7] == 0) {
/* AC terms all zero */
int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
int dcval = LEFT_SHIFT(DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]),
PASS1_BITS);
wsptr[DCTSIZE*0] = dcval;
wsptr[DCTSIZE*1] = dcval;
@@ -235,8 +238,8 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
tmp0 = (z2 + z3) << CONST_BITS;
tmp1 = (z2 - z3) << CONST_BITS;
tmp0 = LEFT_SHIFT(z2 + z3, CONST_BITS);
tmp1 = LEFT_SHIFT(z2 - z3, CONST_BITS);
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
@@ -337,8 +340,8 @@ jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
tmp0 = LEFT_SHIFT((INT32) wsptr[0] + (INT32) wsptr[4], CONST_BITS);
tmp1 = LEFT_SHIFT((INT32) wsptr[0] - (INT32) wsptr[4], CONST_BITS);
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
@@ -444,7 +447,7 @@ jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp13 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp13 <<= CONST_BITS;
tmp13 = LEFT_SHIFT(tmp13, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp13 += ONE << (CONST_BITS-PASS1_BITS-1);
@@ -499,7 +502,7 @@ jpeg_idct_7x7 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp13 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp13 <<= CONST_BITS;
tmp13 = LEFT_SHIFT(tmp13, CONST_BITS);
z1 = (INT32) wsptr[2];
z2 = (INT32) wsptr[4];
@@ -593,7 +596,7 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp0 += ONE << (CONST_BITS-PASS1_BITS-1);
tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
@@ -611,9 +614,9 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */
tmp0 = tmp1 + ((z1 + z2) << CONST_BITS);
tmp2 = tmp1 + ((z3 - z2) << CONST_BITS);
tmp1 = (z1 - z2 - z3) << PASS1_BITS;
tmp0 = tmp1 + LEFT_SHIFT(z1 + z2, CONST_BITS);
tmp2 = tmp1 + LEFT_SHIFT(z3 - z2, CONST_BITS);
tmp1 = LEFT_SHIFT(z1 - z2 - z3, PASS1_BITS);
/* Final output stage */
@@ -635,7 +638,7 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
tmp2 = (INT32) wsptr[4];
tmp10 = MULTIPLY(tmp2, FIX(0.707106781)); /* c4 */
tmp1 = tmp0 + tmp10;
@@ -651,9 +654,9 @@ jpeg_idct_6x6 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z2 = (INT32) wsptr[3];
z3 = (INT32) wsptr[5];
tmp1 = MULTIPLY(z1 + z3, FIX(0.366025404)); /* c5 */
tmp0 = tmp1 + ((z1 + z2) << CONST_BITS);
tmp2 = tmp1 + ((z3 - z2) << CONST_BITS);
tmp1 = (z1 - z2 - z3) << CONST_BITS;
tmp0 = tmp1 + LEFT_SHIFT(z1 + z2, CONST_BITS);
tmp2 = tmp1 + LEFT_SHIFT(z3 - z2, CONST_BITS);
tmp1 = LEFT_SHIFT(z1 - z2 - z3, CONST_BITS);
/* Final output stage */
@@ -714,7 +717,7 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp12 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp12 <<= CONST_BITS;
tmp12 = LEFT_SHIFT(tmp12, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp12 += ONE << (CONST_BITS-PASS1_BITS-1);
tmp0 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
@@ -724,7 +727,7 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z3 = tmp12 + z2;
tmp10 = z3 + z1;
tmp11 = z3 - z1;
tmp12 -= z2 << 2;
tmp12 -= LEFT_SHIFT(z2, 2);
/* Odd part */
@@ -754,7 +757,7 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp12 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp12 <<= CONST_BITS;
tmp12 = LEFT_SHIFT(tmp12, CONST_BITS);
tmp0 = (INT32) wsptr[2];
tmp1 = (INT32) wsptr[4];
z1 = MULTIPLY(tmp0 + tmp1, FIX(0.790569415)); /* (c2+c4)/2 */
@@ -762,7 +765,7 @@ jpeg_idct_5x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z3 = tmp12 + z2;
tmp10 = z3 + z1;
tmp11 = z3 - z1;
tmp12 -= z2 << 2;
tmp12 -= LEFT_SHIFT(z2, 2);
/* Odd part */
@@ -828,7 +831,7 @@ jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp0 += ONE << (CONST_BITS-PASS1_BITS-1);
tmp2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
@@ -858,7 +861,7 @@ jpeg_idct_3x3 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
tmp2 = (INT32) wsptr[2];
tmp12 = MULTIPLY(tmp2, FIX(0.707106781)); /* c2 */
tmp10 = tmp0 + tmp12;
@@ -919,7 +922,7 @@ jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp0 += ONE << (CONST_BITS-PASS1_BITS-1);
@@ -983,7 +986,7 @@ jpeg_idct_9x9 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
z1 = (INT32) wsptr[2];
z2 = (INT32) wsptr[4];
@@ -1091,7 +1094,7 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
z3 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
z3 <<= CONST_BITS;
z3 = LEFT_SHIFT(z3, CONST_BITS);
/* Add fudge factor here for final descale. */
z3 += ONE << (CONST_BITS-PASS1_BITS-1);
z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
@@ -1100,8 +1103,8 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp10 = z3 + z1;
tmp11 = z3 - z2;
tmp22 = RIGHT_SHIFT(z3 - ((z1 - z2) << 1), /* c0 = (c4-c8)*2 */
CONST_BITS-PASS1_BITS);
tmp22 = RIGHT_SHIFT(z3 - LEFT_SHIFT(z1 - z2, 1),
CONST_BITS-PASS1_BITS); /* c0 = (c4-c8)*2 */
z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -1126,7 +1129,7 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp13 = z2 - z4;
tmp12 = MULTIPLY(tmp13, FIX(0.309016994)); /* (c3-c7)/2 */
z5 = z3 << CONST_BITS;
z5 = LEFT_SHIFT(z3, CONST_BITS);
z2 = MULTIPLY(tmp11, FIX(0.951056516)); /* (c3+c7)/2 */
z4 = z5 + tmp12;
@@ -1135,9 +1138,9 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp14 = MULTIPLY(z1, FIX(0.221231742)) - z2 + z4; /* c9 */
z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */
z4 = z5 - tmp12 - (tmp13 << (CONST_BITS - 1));
z4 = z5 - tmp12 - LEFT_SHIFT(tmp13, CONST_BITS - 1);
tmp12 = (z1 - tmp13 - z3) << PASS1_BITS;
tmp12 = LEFT_SHIFT(z1 - tmp13 - z3, PASS1_BITS);
tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */
tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */
@@ -1166,14 +1169,14 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
z3 <<= CONST_BITS;
z3 = LEFT_SHIFT(z3, CONST_BITS);
z4 = (INT32) wsptr[4];
z1 = MULTIPLY(z4, FIX(1.144122806)); /* c4 */
z2 = MULTIPLY(z4, FIX(0.437016024)); /* c8 */
tmp10 = z3 + z1;
tmp11 = z3 - z2;
tmp22 = z3 - ((z1 - z2) << 1); /* c0 = (c4-c8)*2 */
tmp22 = z3 - LEFT_SHIFT(z1 - z2, 1); /* c0 = (c4-c8)*2 */
z2 = (INT32) wsptr[2];
z3 = (INT32) wsptr[6];
@@ -1192,7 +1195,7 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z1 = (INT32) wsptr[1];
z2 = (INT32) wsptr[3];
z3 = (INT32) wsptr[5];
z3 <<= CONST_BITS;
z3 = LEFT_SHIFT(z3, CONST_BITS);
z4 = (INT32) wsptr[7];
tmp11 = z2 + z4;
@@ -1207,9 +1210,9 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp14 = MULTIPLY(z1, FIX(0.221231742)) - z2 + z4; /* c9 */
z2 = MULTIPLY(tmp11, FIX(0.587785252)); /* (c1-c9)/2 */
z4 = z3 - tmp12 - (tmp13 << (CONST_BITS - 1));
z4 = z3 - tmp12 - LEFT_SHIFT(tmp13, CONST_BITS - 1);
tmp12 = ((z1 - tmp13) << CONST_BITS) - z3;
tmp12 = LEFT_SHIFT(z1 - tmp13, CONST_BITS) - z3;
tmp11 = MULTIPLY(z1, FIX(1.260073511)) - z2 - z4; /* c3 */
tmp13 = MULTIPLY(z1, FIX(0.642039522)) - z2 + z4; /* c7 */
@@ -1286,7 +1289,7 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp10 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp10 <<= CONST_BITS;
tmp10 = LEFT_SHIFT(tmp10, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp10 += ONE << (CONST_BITS-PASS1_BITS-1);
@@ -1359,7 +1362,7 @@ jpeg_idct_11x11 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp10 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp10 <<= CONST_BITS;
tmp10 = LEFT_SHIFT(tmp10, CONST_BITS);
z1 = (INT32) wsptr[2];
z2 = (INT32) wsptr[4];
@@ -1480,7 +1483,7 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
z3 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
z3 <<= CONST_BITS;
z3 = LEFT_SHIFT(z3, CONST_BITS);
/* Add fudge factor here for final descale. */
z3 += ONE << (CONST_BITS-PASS1_BITS-1);
@@ -1492,9 +1495,9 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
z4 = MULTIPLY(z1, FIX(1.366025404)); /* c2 */
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
z2 <<= CONST_BITS;
z2 = LEFT_SHIFT(z2, CONST_BITS);
tmp12 = z1 - z2;
@@ -1563,7 +1566,7 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
z3 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
z3 <<= CONST_BITS;
z3 = LEFT_SHIFT(z3, CONST_BITS);
z4 = (INT32) wsptr[4];
z4 = MULTIPLY(z4, FIX(1.224744871)); /* c4 */
@@ -1573,9 +1576,9 @@ jpeg_idct_12x12 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z1 = (INT32) wsptr[2];
z4 = MULTIPLY(z1, FIX(1.366025404)); /* c2 */
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
z2 = (INT32) wsptr[6];
z2 <<= CONST_BITS;
z2 = LEFT_SHIFT(z2, CONST_BITS);
tmp12 = z1 - z2;
@@ -1696,7 +1699,7 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
/* Add fudge factor here for final descale. */
z1 += ONE << (CONST_BITS-PASS1_BITS-1);
@@ -1784,7 +1787,7 @@ jpeg_idct_13x13 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
z2 = (INT32) wsptr[2];
z3 = (INT32) wsptr[4];
@@ -1924,7 +1927,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
/* Add fudge factor here for final descale. */
z1 += ONE << (CONST_BITS-PASS1_BITS-1);
z4 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
@@ -1936,8 +1939,8 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp11 = z1 + z3;
tmp12 = z1 - z4;
tmp23 = RIGHT_SHIFT(z1 - ((z2 + z3 - z4) << 1), /* c0 = (c4+c12-c8)*2 */
CONST_BITS-PASS1_BITS);
tmp23 = RIGHT_SHIFT(z1 - LEFT_SHIFT(z2 + z3 - z4, 1),
CONST_BITS-PASS1_BITS); /* c0 = (c4+c12-c8)*2 */
z1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
z2 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -1962,7 +1965,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
z3 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
z4 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
tmp13 = z4 << CONST_BITS;
tmp13 = LEFT_SHIFT(z4, CONST_BITS);
tmp14 = z1 + z3;
tmp11 = MULTIPLY(z1 + z2, FIX(1.334852607)); /* c3 */
@@ -1981,7 +1984,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp14 += z4 + tmp13 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */
tmp15 += z4 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */
tmp13 = (z1 - z3) << PASS1_BITS;
tmp13 = LEFT_SHIFT(z1 - z3, PASS1_BITS);
/* Final output stage */
@@ -2011,7 +2014,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
z4 = (INT32) wsptr[4];
z2 = MULTIPLY(z4, FIX(1.274162392)); /* c4 */
z3 = MULTIPLY(z4, FIX(0.314692123)); /* c12 */
@@ -2021,7 +2024,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp11 = z1 + z3;
tmp12 = z1 - z4;
tmp23 = z1 - ((z2 + z3 - z4) << 1); /* c0 = (c4+c12-c8)*2 */
tmp23 = z1 - LEFT_SHIFT(z2 + z3 - z4, 1); /* c0 = (c4+c12-c8)*2 */
z1 = (INT32) wsptr[2];
z2 = (INT32) wsptr[6];
@@ -2046,7 +2049,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
z2 = (INT32) wsptr[3];
z3 = (INT32) wsptr[5];
z4 = (INT32) wsptr[7];
z4 <<= CONST_BITS;
z4 = LEFT_SHIFT(z4, CONST_BITS);
tmp14 = z1 + z3;
tmp11 = MULTIPLY(z1 + z2, FIX(1.334852607)); /* c3 */
@@ -2064,7 +2067,7 @@ jpeg_idct_14x14 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp14 += tmp13 + z4 - MULTIPLY(z3, FIX(1.6906431334)); /* c1+c9-c11 */
tmp15 += tmp13 + MULTIPLY(z2, FIX(0.674957567)); /* c1+c11-c5 */
tmp13 = ((z1 - z3) << CONST_BITS) + z4;
tmp13 = LEFT_SHIFT(z1 - z3, CONST_BITS) + z4;
/* Final output stage */
@@ -2150,7 +2153,7 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
/* Add fudge factor here for final descale. */
z1 += ONE << (CONST_BITS-PASS1_BITS-1);
@@ -2163,7 +2166,7 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp12 = z1 - tmp10;
tmp13 = z1 + tmp11;
z1 -= (tmp11 - tmp10) << 1; /* c0 = (c6-c12)*2 */
z1 -= LEFT_SHIFT(tmp11 - tmp10, 1); /* c0 = (c6-c12)*2 */
z4 = z2 - z3;
z3 += z2;
@@ -2243,7 +2246,7 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
z1 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
z1 <<= CONST_BITS;
z1 = LEFT_SHIFT(z1, CONST_BITS);
z2 = (INT32) wsptr[2];
z3 = (INT32) wsptr[4];
@@ -2254,7 +2257,7 @@ jpeg_idct_15x15 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
tmp12 = z1 - tmp10;
tmp13 = z1 + tmp11;
z1 -= (tmp11 - tmp10) << 1; /* c0 = (c6-c12)*2 */
z1 -= LEFT_SHIFT(tmp11 - tmp10, 1); /* c0 = (c6-c12)*2 */
z4 = z2 - z3;
z3 += z2;
@@ -2392,7 +2395,7 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
/* Add fudge factor here for final descale. */
tmp0 += 1 << (CONST_BITS-PASS1_BITS-1);
@@ -2494,7 +2497,7 @@ jpeg_idct_16x16 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Add fudge factor here for final descale. */
tmp0 = (INT32) wsptr[0] + (ONE << (PASS1_BITS+2));
tmp0 <<= CONST_BITS;
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS);
z1 = (INT32) wsptr[4];
tmp1 = MULTIPLY(z1, FIX(1.306562965)); /* c4[16] = c2[8] */

View File

@@ -1,8 +1,10 @@
/*
* jidctred.c
*
* This file was part of the Independent JPEG Group's software.
* Copyright (C) 1994-1998, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, D. R. Commander
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains inverse-DCT routines that produce reduced-size output:
@@ -143,7 +145,8 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
/* AC terms all zero; we need not examine term 4 for 4x4 output */
int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
int dcval = LEFT_SHIFT(DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]),
PASS1_BITS);
wsptr[DCTSIZE*0] = dcval;
wsptr[DCTSIZE*1] = dcval;
@@ -156,7 +159,7 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp0 <<= (CONST_BITS+1);
tmp0 = LEFT_SHIFT(tmp0, CONST_BITS+1);
z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
@@ -217,7 +220,7 @@ jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
tmp0 = LEFT_SHIFT((INT32) wsptr[0], CONST_BITS+1);
tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
+ MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
@@ -294,7 +297,8 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
/* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
int dcval = LEFT_SHIFT(DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]),
PASS1_BITS);
wsptr[DCTSIZE*0] = dcval;
wsptr[DCTSIZE*1] = dcval;
@@ -305,7 +309,7 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
tmp10 = z1 << (CONST_BITS+2);
tmp10 = LEFT_SHIFT(z1, CONST_BITS+2);
/* Odd part */
@@ -347,7 +351,7 @@ jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* Even part */
tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
tmp10 = LEFT_SHIFT((INT32) wsptr[0], CONST_BITS+2);
/* Odd part */

View File

@@ -5,7 +5,7 @@
* Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 1997-2009 by Guido Vollbeding.
* libjpeg-turbo Modifications:
* Copyright (C) 2009, 2011, 2014, D. R. Commander.
* Copyright (C) 2009, 2011, 2014-2015, D. R. Commander.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains additional configuration options that customize the
@@ -152,6 +152,7 @@ typedef short INT16;
#ifndef _BASETSD_H_ /* Microsoft defines it in basetsd.h */
#ifndef _BASETSD_H /* MinGW is slightly different */
#ifndef QGLOBAL_H /* Qt defines it in qglobal.h */
#define __INT32_IS_ACTUALLY_LONG
typedef long INT32;
#endif
#endif

View File

@@ -4,8 +4,8 @@
* This file was part of the Independent JPEG Group's software:
* Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 1997-2009 by Guido Vollbeding.
* It was modified by The libjpeg-turbo Project to include only code relevant
* to libjpeg-turbo.
* libjpeg-turbo Modifications:
* Copyright (C) 2015, D. R. Commander
* For conditions of distribution and use, see the accompanying README file.
*
* This file provides common declarations for the various JPEG modules.
@@ -42,6 +42,18 @@ typedef enum { /* Operating modes for buffer controllers */
#define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
/*
* Left shift macro that handles a negative operand without causing any
* sanitizer warnings
*/
#ifdef __INT32_IS_ACTUALLY_LONG
#define LEFT_SHIFT(a, b) ((INT32)((unsigned long)(a) << (b)))
#else
#define LEFT_SHIFT(a, b) ((INT32)((unsigned int)(a) << (b)))
#endif
/* Declarations for compression modules */
/* Master control module */