code:
Copied from Debian.

From: Kurt Roeckx <kurt@roeckx.be>
Date: Sun, 28 Jan 2018 19:26:36 +0100
Subject: Check the size before reading with mad_bit_read

There are various cases where it attemps to read past the end of the buffer
using mad_bit_read(). Most functions didn't even know the size of the buffer
they were reading from.

Index: libmad-0.15.1b/bit.c
===================================================================
--- libmad-0.15.1b.orig/bit.c
+++ libmad-0.15.1b/bit.c
@@ -138,6 +138,9 @@ unsigned long mad_bit_read(struct mad_bi
 {
   register unsigned long value;

+  if (len == 0)
+    return 0;
+
   if (bitptr->left == CHAR_BIT)
     bitptr->cache = *bitptr->byte;

Index: libmad-0.15.1b/frame.c
===================================================================
--- libmad-0.15.1b.orig/frame.c
+++ libmad-0.15.1b/frame.c
@@ -120,11 +120,18 @@ static
 int decode_header(struct mad_header *header, struct mad_stream *stream)
 {
   unsigned int index;
+  struct mad_bitptr bufend_ptr;

   header->flags        = 0;
   header->private_bits = 0;

+  mad_bit_init(&bufend_ptr, stream->bufend);
+
   /* header() */
+  if (mad_bit_length(&stream->ptr, &bufend_ptr) < 32) {
+    stream->error = MAD_ERROR_BUFLEN;
+    return -1;
+  }

   /* syncword */
   mad_bit_skip(&stream->ptr, 11);
@@ -225,8 +232,13 @@ int decode_header(struct mad_header *hea
   /* error_check() */

   /* crc_check */
-  if (header->flags & MAD_FLAG_PROTECTION)
+  if (header->flags & MAD_FLAG_PROTECTION) {
+    if (mad_bit_length(&stream->ptr, &bufend_ptr) < 16) {
+      stream->error = MAD_ERROR_BUFLEN;
+      return -1;
+    }
     header->crc_target = mad_bit_read(&stream->ptr, 16);
+  }

   return 0;
 }
@@ -338,7 +350,7 @@ int mad_header_decode(struct mad_header
       stream->error = MAD_ERROR_BUFLEN;
       goto fail;
     }
-    else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
+    else if ((end - ptr >= 2) && !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
       /* mark point where frame sync word was expected */
       stream->this_frame = ptr;
       stream->next_frame = ptr + 1;
@@ -361,6 +373,8 @@ int mad_header_decode(struct mad_header
     ptr = mad_bit_nextbyte(&stream->ptr);
   }

+  stream->error = MAD_ERROR_NONE;
+
   /* begin processing */
   stream->this_frame = ptr;
   stream->next_frame = ptr + 1;  /* possibly bogus sync word */
@@ -413,7 +427,7 @@ int mad_header_decode(struct mad_header
     /* check that a valid frame header follows this frame */

     ptr = stream->next_frame;
-    if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
+    if ((end - ptr >= 2) && !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
       ptr = stream->next_frame = stream->this_frame + 1;
       goto sync;
     }
Index: libmad-0.15.1b/layer12.c
===================================================================
--- libmad-0.15.1b.orig/layer12.c
+++ libmad-0.15.1b/layer12.c
@@ -72,10 +72,18 @@ mad_fixed_t const linear_table[14] = {
  * DESCRIPTION:	decode one requantized Layer I sample from a bitstream
  */
 static
-mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb)
+mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb, struct mad_stream *stream)
 {
   mad_fixed_t sample;
+  struct mad_bitptr frameend_ptr;

+  mad_bit_init(&frameend_ptr, stream->next_frame);
+
+  if (mad_bit_length(ptr, &frameend_ptr) < nb) {
+    stream->error = MAD_ERROR_LOSTSYNC;
+    stream->sync = 0;
+    return 0;
+  }
   sample = mad_bit_read(ptr, nb);

   /* invert most significant bit, extend sign, then scale to fixed format */
@@ -106,6 +114,10 @@ int mad_layer_I(struct mad_stream *strea
   struct mad_header *header = &frame->header;
   unsigned int nch, bound, ch, s, sb, nb;
   unsigned char allocation[2][32], scalefactor[2][32];
+  struct mad_bitptr bufend_ptr, frameend_ptr;
+
+  mad_bit_init(&bufend_ptr, stream->bufend);
+  mad_bit_init(&frameend_ptr, stream->next_frame);

   nch = MAD_NCHANNELS(header);

@@ -118,6 +130,11 @@ int mad_layer_I(struct mad_stream *strea
   /* check CRC word */

   if (header->flags & MAD_FLAG_PROTECTION) {
+    if (mad_bit_length(&stream->ptr, &bufend_ptr)
+		< 4 * (bound * nch + (32 - bound))) {
+      stream->error = MAD_ERROR_BADCRC;
+      return -1;
+    }
     header->crc_check =
       mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
 		  header->crc_check);
@@ -133,6 +150,11 @@ int mad_layer_I(struct mad_stream *strea

   for (sb = 0; sb < bound; ++sb) {
     for (ch = 0; ch < nch; ++ch) {
+      if (mad_bit_length(&stream->ptr, &frameend_ptr) < 4) {
+	stream->error = MAD_ERROR_LOSTSYNC;
+	stream->sync = 0;
+	return -1;
+      }
       nb = mad_bit_read(&stream->ptr, 4);

       if (nb == 15) {
@@ -145,6 +167,11 @@ int mad_layer_I(struct mad_stream *strea
   }

   for (sb = bound; sb < 32; ++sb) {
+    if (mad_bit_length(&stream->ptr, &frameend_ptr) < 4) {
+      stream->error = MAD_ERROR_LOSTSYNC;
+      stream->sync = 0;
+      return -1;
+    }
     nb = mad_bit_read(&stream->ptr, 4);

     if (nb == 15) {
@@ -161,6 +188,11 @@ int mad_layer_I(struct mad_stream *strea
   for (sb = 0; sb < 32; ++sb) {
     for (ch = 0; ch < nch; ++ch) {
       if (allocation[ch][sb]) {
+        if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
+	  stream->error = MAD_ERROR_LOSTSYNC;
+	  stream->sync = 0;
+	  return -1;
+	}
 	scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);

 # if defined(OPT_STRICT)
@@ -185,8 +217,10 @@ int mad_layer_I(struct mad_stream *strea
       for (ch = 0; ch < nch; ++ch) {
 	nb = allocation[ch][sb];
 	frame->sbsample[ch][s][sb] = nb ?
-	  mad_f_mul(I_sample(&stream->ptr, nb),
+	  mad_f_mul(I_sample(&stream->ptr, nb, stream),
 		    sf_table[scalefactor[ch][sb]]) : 0;
+	if (stream->error != 0)
+	  return -1;
       }
     }

@@ -194,7 +228,14 @@ int mad_layer_I(struct mad_stream *strea
       if ((nb = allocation[0][sb])) {
 	mad_fixed_t sample;

-	sample = I_sample(&stream->ptr, nb);
+	if (mad_bit_length(&stream->ptr, &frameend_ptr) < nb) {
+	  stream->error = MAD_ERROR_LOSTSYNC;
+	  stream->sync = 0;
+          return -1;
+	}
+	sample = I_sample(&stream->ptr, nb, stream);
+        if (stream->error != 0)
+	  return -1;

 	for (ch = 0; ch < nch; ++ch) {
 	  frame->sbsample[ch][s][sb] =
@@ -280,13 +321,21 @@ struct quantclass {
 static
 void II_samples(struct mad_bitptr *ptr,
 		struct quantclass const *quantclass,
-		mad_fixed_t output[3])
+		mad_fixed_t output[3], struct mad_stream *stream)
 {
   unsigned int nb, s, sample[3];
+  struct mad_bitptr frameend_ptr;
+
+  mad_bit_init(&frameend_ptr, stream->next_frame);

   if ((nb = quantclass->group)) {
     unsigned int c, nlevels;

+    if (mad_bit_length(ptr, &frameend_ptr) < quantclass->bits) {
+      stream->error = MAD_ERROR_LOSTSYNC;
+      stream->sync = 0;
+      return;
+    }
     /* degrouping */
     c = mad_bit_read(ptr, quantclass->bits);
     nlevels = quantclass->nlevels;
@@ -299,8 +348,14 @@ void II_samples(struct mad_bitptr *ptr,
   else {
     nb = quantclass->bits;

-    for (s = 0; s < 3; ++s)
+    for (s = 0; s < 3; ++s) {
+      if (mad_bit_length(ptr, &frameend_ptr) < nb) {
+	stream->error = MAD_ERROR_LOSTSYNC;
+	stream->sync = 0;
+	return;
+      }
       sample[s] = mad_bit_read(ptr, nb);
+    }
   }

   for (s = 0; s < 3; ++s) {
@@ -336,6 +391,9 @@ int mad_layer_II(struct mad_stream *stre
   unsigned char const *offsets;
   unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
   mad_fixed_t samples[3];
+  struct mad_bitptr frameend_ptr;
+
+  mad_bit_init(&frameend_ptr, stream->next_frame);

   nch = MAD_NCHANNELS(header);

@@ -402,13 +460,24 @@ int mad_layer_II(struct mad_stream *stre
   for (sb = 0; sb < bound; ++sb) {
     nbal = bitalloc_table[offsets[sb]].nbal;

-    for (ch = 0; ch < nch; ++ch)
+    for (ch = 0; ch < nch; ++ch) {
+      if (mad_bit_length(&stream->ptr, &frameend_ptr) < nbal) {
+	stream->error = MAD_ERROR_LOSTSYNC;
+	stream->sync = 0;
+	return -1;
+      }
       allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
+    }
   }

   for (sb = bound; sb < sblimit; ++sb) {
     nbal = bitalloc_table[offsets[sb]].nbal;

+    if (mad_bit_length(&stream->ptr, &frameend_ptr) < nbal) {
+      stream->error = MAD_ERROR_LOSTSYNC;
+      stream->sync = 0;
+      return -1;
+    }
     allocation[0][sb] =
     allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
   }
@@ -417,8 +486,14 @@ int mad_layer_II(struct mad_stream *stre

   for (sb = 0; sb < sblimit; ++sb) {
     for (ch = 0; ch < nch; ++ch) {
-      if (allocation[ch][sb])
+      if (allocation[ch][sb]) {
+	if (mad_bit_length(&stream->ptr, &frameend_ptr) < 2) {
+	  stream->error = MAD_ERROR_LOSTSYNC;
+	  stream->sync = 0;
+	  return -1;
+	}
 	scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
+      }
     }
   }

@@ -441,6 +516,11 @@ int mad_layer_II(struct mad_stream *stre
   for (sb = 0; sb < sblimit; ++sb) {
     for (ch = 0; ch < nch; ++ch) {
       if (allocation[ch][sb]) {
+	if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
+	  stream->error = MAD_ERROR_LOSTSYNC;
+	  stream->sync = 0;
+	  return -1;
+	}
 	scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);

 	switch (scfsi[ch][sb]) {
@@ -451,11 +531,21 @@ int mad_layer_II(struct mad_stream *stre
 	  break;

 	case 0:
+	  if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
+	    stream->error = MAD_ERROR_LOSTSYNC;
+	    stream->sync = 0;
+	    return -1;
+	  }
 	  scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
 	  /* fall through */

 	case 1:
 	case 3:
+	  if (mad_bit_length(&stream->ptr, &frameend_ptr) < 6) {
+	    stream->error = MAD_ERROR_LOSTSYNC;
+	    stream->sync = 0;
+	    return -1;
+	  }
 	  scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
 	}

@@ -487,7 +577,9 @@ int mad_layer_II(struct mad_stream *stre
 	if ((index = allocation[ch][sb])) {
 	  index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];

-	  II_samples(&stream->ptr, &qc_table[index], samples);
+	  II_samples(&stream->ptr, &qc_table[index], samples, stream);
+	  if (stream->error != 0)
+            return -1;

 	  for (s = 0; s < 3; ++s) {
 	    frame->sbsample[ch][3 * gr + s][sb] =
@@ -505,7 +597,9 @@ int mad_layer_II(struct mad_stream *stre
       if ((index = allocation[0][sb])) {
 	index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];

-	II_samples(&stream->ptr, &qc_table[index], samples);
+	II_samples(&stream->ptr, &qc_table[index], samples, stream);
+	if (stream->error != 0)
+          return -1;

 	for (ch = 0; ch < nch; ++ch) {
 	  for (s = 0; s < 3; ++s) {
Index: libmad-0.15.1b/layer3.c
===================================================================
--- libmad-0.15.1b.orig/layer3.c
+++ libmad-0.15.1b/layer3.c
@@ -598,7 +598,8 @@ enum mad_error III_sideinfo(struct mad_b
 static
 unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
 				  struct channel *channel,
-				  struct channel *gr1ch, int mode_extension)
+				  struct channel *gr1ch, int mode_extension,
+				  unsigned int bits_left, unsigned int *part2_length)
 {
   struct mad_bitptr start;
   unsigned int scalefac_compress, index, slen[4], part, n, i;
@@ -644,8 +645,12 @@ unsigned int III_scalefactors_lsf(struct

     n = 0;
     for (part = 0; part < 4; ++part) {
-      for (i = 0; i < nsfb[part]; ++i)
+      for (i = 0; i < nsfb[part]; ++i) {
+	if (bits_left < slen[part])
+	  return MAD_ERROR_BADSCFSI;
 	channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
+	bits_left -= slen[part];
+      }
     }

     while (n < 39)
@@ -690,7 +695,10 @@ unsigned int III_scalefactors_lsf(struct
       max = (1 << slen[part]) - 1;

       for (i = 0; i < nsfb[part]; ++i) {
+	if (bits_left < slen[part])
+	  return MAD_ERROR_BADSCFSI;
 	is_pos = mad_bit_read(ptr, slen[part]);
+	bits_left -= slen[part];

 	channel->scalefac[n] = is_pos;
 	gr1ch->scalefac[n++] = (is_pos == max);
@@ -703,7 +711,8 @@ unsigned int III_scalefactors_lsf(struct
     }
   }

-  return mad_bit_length(&start, ptr);
+  *part2_length = mad_bit_length(&start, ptr);
+  return MAD_ERROR_NONE;
 }

 /*
@@ -712,7 +721,8 @@ unsigned int III_scalefactors_lsf(struct
  */
 static
 unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
-			      struct channel const *gr0ch, unsigned int scfsi)
+			      struct channel const *gr0ch, unsigned int scfsi,
+			      unsigned int bits_left, unsigned int *part2_length)
 {
   struct mad_bitptr start;
   unsigned int slen1, slen2, sfbi;
@@ -728,12 +738,20 @@ unsigned int III_scalefactors(struct mad
     sfbi = 0;

     nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
-    while (nsfb--)
+    while (nsfb--) {
+      if (bits_left < slen1)
+	return MAD_ERROR_BADSCFSI;
       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
+      bits_left -= slen1;
+    }

     nsfb = 6 * 3;
-    while (nsfb--)
+    while (nsfb--) {
+      if (bits_left < slen2)
+	return MAD_ERROR_BADSCFSI;
       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
+      bits_left -= slen2;
+    }

     nsfb = 1 * 3;
     while (nsfb--)
@@ -745,8 +763,12 @@ unsigned int III_scalefactors(struct mad
 	channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
     }
     else {
-      for (sfbi = 0; sfbi < 6; ++sfbi)
+      for (sfbi = 0; sfbi < 6; ++sfbi) {
+	if (bits_left < slen1)
+	  return MAD_ERROR_BADSCFSI;
 	channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
+	bits_left -= slen1;
+      }
     }

     if (scfsi & 0x4) {
@@ -754,8 +776,12 @@ unsigned int III_scalefactors(struct mad
 	channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
     }
     else {
-      for (sfbi = 6; sfbi < 11; ++sfbi)
+      for (sfbi = 6; sfbi < 11; ++sfbi) {
+	if (bits_left < slen1)
+	  return MAD_ERROR_BADSCFSI;
 	channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
+	bits_left -= slen1;
+      }
     }

     if (scfsi & 0x2) {
@@ -763,8 +789,12 @@ unsigned int III_scalefactors(struct mad
 	channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
     }
     else {
-      for (sfbi = 11; sfbi < 16; ++sfbi)
+      for (sfbi = 11; sfbi < 16; ++sfbi) {
+	if (bits_left < slen2)
+	  return MAD_ERROR_BADSCFSI;
 	channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
+	bits_left -= slen2;
+      }
     }

     if (scfsi & 0x1) {
@@ -772,14 +802,19 @@ unsigned int III_scalefactors(struct mad
 	channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
     }
     else {
-      for (sfbi = 16; sfbi < 21; ++sfbi)
+      for (sfbi = 16; sfbi < 21; ++sfbi) {
+	if (bits_left < slen2)
+	  return MAD_ERROR_BADSCFSI;
 	channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
+	bits_left -= slen2;
+      }
     }

     channel->scalefac[21] = 0;
   }

-  return mad_bit_length(&start, ptr);
+  *part2_length = mad_bit_length(&start, ptr);
+  return MAD_ERROR_NONE;
 }

 /*
@@ -933,19 +968,17 @@ static
 enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
 			      struct channel *channel,
 			      unsigned char const *sfbwidth,
-			      unsigned int part2_length)
+			      signed int part3_length)
 {
   signed int exponents[39], exp;
   signed int const *expptr;
   struct mad_bitptr peek;
-  signed int bits_left, cachesz;
+  signed int bits_left, cachesz, fakebits;
   register mad_fixed_t *xrptr;
   mad_fixed_t const *sfbound;
   register unsigned long bitcache;

-  bits_left = (signed) channel->part2_3_length - (signed) part2_length;
-  if (bits_left < 0)
-    return MAD_ERROR_BADPART3LEN;
+  bits_left = part3_length;

   III_exponents(channel, sfbwidth, exponents);

@@ -956,8 +989,12 @@ enum mad_error III_huffdecode(struct mad
   cachesz  = mad_bit_bitsleft(&peek);
   cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;

+  if (bits_left < cachesz) {
+    cachesz = bits_left;
+  }
   bitcache   = mad_bit_read(&peek, cachesz);
   bits_left -= cachesz;
+  fakebits = 0;

   xrptr = &xr[0];

@@ -986,7 +1023,7 @@ enum mad_error III_huffdecode(struct mad

     big_values = channel->big_values;

-    while (big_values-- && cachesz + bits_left > 0) {
+    while (big_values-- && cachesz + bits_left - fakebits > 0) {
       union huffpair const *pair;
       unsigned int clumpsz, value;
       register mad_fixed_t requantized;
@@ -1023,10 +1060,19 @@ enum mad_error III_huffdecode(struct mad
 	unsigned int bits;

 	bits       = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
+	if (bits_left < bits) {
+	  bits = bits_left;
+	}
 	bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
 	cachesz   += bits;
 	bits_left -= bits;
       }
+      if (cachesz < 21) {
+	unsigned int bits = 21 - cachesz;
+	bitcache <<= bits;
+	cachesz += bits;
+	fakebits += bits;
+      }

       /* hcod (0..19) */

@@ -1041,6 +1087,8 @@ enum mad_error III_huffdecode(struct mad
       }

       cachesz -= pair->value.hlen;
+      if (cachesz < fakebits)
+	return MAD_ERROR_BADHUFFDATA;

       if (linbits) {
 	/* x (0..14) */
@@ -1054,10 +1102,15 @@ enum mad_error III_huffdecode(struct mad

 	case 15:
 	  if (cachesz < linbits + 2) {
-	    bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
-	    cachesz   += 16;
-	    bits_left -= 16;
+	    unsigned int bits = 16;
+	    if (bits_left < 16)
+	      bits = bits_left;
+	    bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
+	    cachesz   += bits;
+	    bits_left -= bits;
 	  }
+	  if (cachesz - fakebits < linbits)
+	    return MAD_ERROR_BADHUFFDATA;

 	  value += MASK(bitcache, cachesz, linbits);
 	  cachesz -= linbits;
@@ -1074,6 +1127,8 @@ enum mad_error III_huffdecode(struct mad
 	  }

 	x_final:
+	  if (cachesz - fakebits < 1)
+	    return MAD_ERROR_BADHUFFDATA;
 	  xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
 	    -requantized : requantized;
 	}
@@ -1089,10 +1144,15 @@ enum mad_error III_huffdecode(struct mad

 	case 15:
 	  if (cachesz < linbits + 1) {
-	    bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
-	    cachesz   += 16;
-	    bits_left -= 16;
+	    unsigned int bits = 16;
+	    if (bits_left < 16)
+	      bits = bits_left;
+	    bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
+	    cachesz   += bits;
+	    bits_left -= bits;
 	  }
+	  if (cachesz - fakebits < linbits)
+	    return MAD_ERROR_BADHUFFDATA;

 	  value += MASK(bitcache, cachesz, linbits);
 	  cachesz -= linbits;
@@ -1109,6 +1169,8 @@ enum mad_error III_huffdecode(struct mad
 	  }

 	y_final:
+	  if (cachesz - fakebits < 1)
+	    return MAD_ERROR_BADHUFFDATA;
 	  xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
 	    -requantized : requantized;
 	}
@@ -1128,6 +1190,8 @@ enum mad_error III_huffdecode(struct mad
 	    requantized = reqcache[value] = III_requantize(value, exp);
 	  }

+	  if (cachesz - fakebits < 1)
+	    return MAD_ERROR_BADHUFFDATA;
 	  xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
 	    -requantized : requantized;
 	}
@@ -1146,6 +1210,8 @@ enum mad_error III_huffdecode(struct mad
 	    requantized = reqcache[value] = III_requantize(value, exp);
 	  }

+	  if (cachesz - fakebits < 1)
+	    return MAD_ERROR_BADHUFFDATA;
 	  xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
 	    -requantized : requantized;
 	}
@@ -1155,9 +1221,6 @@ enum mad_error III_huffdecode(struct mad
     }
   }

-  if (cachesz + bits_left < 0)
-    return MAD_ERROR_BADHUFFDATA;  /* big_values overrun */
-
   /* count1 */
   {
     union huffquad const *table;
@@ -1167,15 +1230,24 @@ enum mad_error III_huffdecode(struct mad

     requantized = III_requantize(1, exp);

-    while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
+    while (cachesz + bits_left - fakebits > 0 && xrptr <= &xr[572]) {
       union huffquad const *quad;

       /* hcod (1..6) */

       if (cachesz < 10) {
-	bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
-	cachesz   += 16;
-	bits_left -= 16;
+	unsigned int bits = 16;
+	if (bits_left < 16)
+	  bits = bits_left;
+	bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
+	cachesz   += bits;
+	bits_left -= bits;
+      }
+      if (cachesz < 10) {
+	unsigned int bits = 10 - cachesz;
+	bitcache <<= bits;
+	cachesz += bits;
+	fakebits += bits;
       }

       quad = &table[MASK(bitcache, cachesz, 4)];
@@ -1188,6 +1260,11 @@ enum mad_error III_huffdecode(struct mad
 		      MASK(bitcache, cachesz, quad->ptr.bits)];
       }

+      if (cachesz - fakebits < quad->value.hlen + quad->value.v
+        + quad->value.w + quad->value.x + quad->value.y)
+	/* We don't have enough bits to read one more entry, consider them
+	 * stuffing bits. */
+	break;
       cachesz -= quad->value.hlen;

       if (xrptr == sfbound) {
@@ -1236,22 +1313,8 @@ enum mad_error III_huffdecode(struct mad

       xrptr += 2;
     }
-
-    if (cachesz + bits_left < 0) {
-# if 0 && defined(DEBUG)
-      fprintf(stderr, "huffman count1 overrun (%d bits)\n",
-	      -(cachesz + bits_left));
-# endif
-
-      /* technically the bitstream is misformatted, but apparently
-	 some encoders are just a bit sloppy with stuffing bits */
-
-      xrptr -= 4;
-    }
   }

-  assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
-
 # if 0 && defined(DEBUG)
   if (bits_left < 0)
     fprintf(stderr, "read %d bits too many\n", -bits_left);
@@ -2348,10 +2411,11 @@ void III_freqinver(mad_fixed_t sample[18
  */
 static
 enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
-			  struct sideinfo *si, unsigned int nch)
+			  struct sideinfo *si, unsigned int nch, unsigned int md_len)
 {
   struct mad_header *header = &frame->header;
   unsigned int sfreqi, ngr, gr;
+  int bits_left = md_len * CHAR_BIT;

   {
     unsigned int sfreq;
@@ -2383,6 +2447,7 @@ enum mad_error III_decode(struct mad_bit
     for (ch = 0; ch < nch; ++ch) {
       struct channel *channel = &granule->ch[ch];
       unsigned int part2_length;
+      unsigned int part3_length;

       sfbwidth[ch] = sfbwidth_table[sfreqi].l;
       if (channel->block_type == 2) {
@@ -2391,18 +2456,30 @@ enum mad_error III_decode(struct mad_bit
       }

       if (header->flags & MAD_FLAG_LSF_EXT) {
-	part2_length = III_scalefactors_lsf(ptr, channel,
+	error = III_scalefactors_lsf(ptr, channel,
 					    ch == 0 ? 0 : &si->gr[1].ch[1],
-					    header->mode_extension);
+					    header->mode_extension, bits_left, &part2_length);
       }
       else {
-	part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
-					gr == 0 ? 0 : si->scfsi[ch]);
+	error = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
+					gr == 0 ? 0 : si->scfsi[ch], bits_left, &part2_length);
       }
+      if (error)
+        return error;
+
+      bits_left -= part2_length;

-      error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
+      if (part2_length > channel->part2_3_length)
+        return MAD_ERROR_BADPART3LEN;
+
+      part3_length = channel->part2_3_length - part2_length;
+      if (part3_length > bits_left)
+        return MAD_ERROR_BADPART3LEN;
+
+      error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part3_length);
       if (error)
 	return error;
+      bits_left -= part3_length;
     }

     /* joint stereo processing */
@@ -2519,11 +2596,13 @@ int mad_layer_III(struct mad_stream *str
   unsigned int nch, priv_bitlen, next_md_begin = 0;
   unsigned int si_len, data_bitlen, md_len;
   unsigned int frame_space, frame_used, frame_free;
-  struct mad_bitptr ptr;
+  struct mad_bitptr ptr, bufend_ptr;
   struct sideinfo si;
   enum mad_error error;
   int result = 0;

+  mad_bit_init(&bufend_ptr, stream->bufend);
+
   /* allocate Layer III dynamic structures */

   if (stream->main_data == 0) {
@@ -2587,14 +2666,15 @@ int mad_layer_III(struct mad_stream *str
     unsigned long header;

     mad_bit_init(&peek, stream->next_frame);
+    if (mad_bit_length(&peek, &bufend_ptr) >= 57) {
+      header = mad_bit_read(&peek, 32);
+      if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
+        if (!(header & 0x00010000L))  /* protection_bit */
+	  mad_bit_skip(&peek, 16);  /* crc_check */

-    header = mad_bit_read(&peek, 32);
-    if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
-      if (!(header & 0x00010000L))  /* protection_bit */
-	mad_bit_skip(&peek, 16);  /* crc_check */
-
-      next_md_begin =
-	mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
+        next_md_begin =
+	  mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
+      }
     }

     mad_bit_finish(&peek);
@@ -2653,7 +2733,7 @@ int mad_layer_III(struct mad_stream *str
   /* decode main_data */

   if (result == 0) {
-    error = III_decode(&ptr, frame, &si, nch);
+    error = III_decode(&ptr, frame, &si, nch, md_len);
     if (error) {
       stream->error = error;
       result = -1;


code:
Copied from Debian.

From: Kurt Roeckx <kurt@roeckx.be>
Date: Sun, 28 Jan 2018 15:44:08 +0100
Subject: Check the size of the main data

The main data to decode a frame can come from the current frame and part of the
previous frame, the so called bit reservoir. si.main_data_begin is the part of
the previous frame we need for this frame. frame_space is the amount of main
data that can be in this frame, and next_md_begin is the part of this frame that
is going to be used for the next frame.

The maximum amount of data from a previous frame that the format allows is 511
bytes. The maximum frame size for the defined bitrates is at MPEG 2.5 layer 2
at 320 kbit/s and 8 kHz sample rate which gives 72 * (320000 / 8000) + 1 = 2881.
So those defines are not large enough:
 # define MAD_BUFFER_GUARD      8
 # define MAD_BUFFER_MDLEN      (511 + 2048 + MAD_BUFFER_GUARD)

There is also support for a "free" bitrate which allows you to create any frame
size, which can be larger than the buffer.

Changing the defines is not an option since it's part of the ABI, so we check
that the main data fits in the bufer.

The previous frame data is stored in *stream->main_data and contains
stream->md_len bytes. If stream->md_len is larger than the data we
need from the previous frame (si.main_data_begin) it still wouldn't fit
in the buffer, so just keep the data that we need.

Index: libmad-0.15.1b/layer3.c
===================================================================
--- libmad-0.15.1b.orig/layer3.c
+++ libmad-0.15.1b/layer3.c
@@ -2608,6 +2608,11 @@ int mad_layer_III(struct mad_stream *str
     next_md_begin = 0;

   md_len = si.main_data_begin + frame_space - next_md_begin;
+  if (md_len + MAD_BUFFER_GUARD > MAD_BUFFER_MDLEN) {
+    stream->error = MAD_ERROR_LOSTSYNC;
+    stream->sync = 0;
+    return -1;
+  }

   frame_used = 0;

@@ -2625,8 +2630,11 @@ int mad_layer_III(struct mad_stream *str
       }
     }
     else {
-      mad_bit_init(&ptr,
-		   *stream->main_data + stream->md_len - si.main_data_begin);
+      memmove(stream->main_data,
+	*stream->main_data + stream->md_len - si.main_data_begin,
+	si.main_data_begin);
+      stream->md_len = si.main_data_begin;
+      mad_bit_init(&ptr, *stream->main_data);

       if (md_len > si.main_data_begin) {
 	assert(stream->md_len + md_len -

repo_name: maximed/guix

license: mit


code:
Fix MIPS assembly code to work with newer GCC, where the "=h"
constraint is no longer supported.

--- libmad/fixed.h.orig	2004-02-16 21:02:03.000000000 -0500
+++ libmad/fixed.h	2011-10-29 18:47:21.000000000 -0400
@@ -304,8 +304,8 @@
  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  */
 #  define MAD_F_MLX(hi, lo, x, y)  \
-    asm ("mult	%2,%3"  \
-	 : "=l" (lo), "=h" (hi)  \
+    asm ("mult	%2,%3\n\tmfhi  %1"  \
+	 : "=l" (lo), "=r" (hi)  \
 	 : "%r" (x), "r" (y))

 # if defined(HAVE_MADD_ASM)


code:
Author: Tobias Geerinckx-Rice <me@tobias.gr>
Date: Wed 22 Jan 22:58:13 CET 2020
Subject: gnu: memcached: Fix build with GCC 7.

Taken verbating from this bug report:
<https://bugs.launchpad.net/libmemcached/+bug/1663985>.

diff -up ./clients/memflush.cc.old ./clients/memflush.cc
--- ./clients/memflush.cc.old	2017-02-12 10:12:59.615209225 +0100
+++ ./clients/memflush.cc	2017-02-12 10:13:39.998382783 +0100
@@ -39,7 +39,7 @@ int main(int argc, char *argv[])
 {
   options_parse(argc, argv);

-  if (opt_servers == false)
+  if (!opt_servers)
   {
     char *temp;

@@ -48,7 +48,7 @@ int main(int argc, char *argv[])
       opt_servers= strdup(temp);
     }

-    if (opt_servers == false)
+    if (!opt_servers)
     {
       std::cerr << "No Servers provided" << std::endl;
       exit(EXIT_FAILURE);


code:
Avoid a type mismatch that shows up in C++11.
Taken from <https://bugs.debian.org/853560>.

--- a/src/rtphint.cpp	2012-05-20 16:11:53.000000000 -0600
+++ b/src/rtphint.cpp	2017-05-16 10:25:26.930705191 -0600
@@ -339,7 +339,7 @@
                 pSlash = strchr(pSlash, '/');
                 if (pSlash != NULL) {
                     pSlash++;
-                    if (pSlash != '\0') {
+                    if (*pSlash != '\0') {
                         length = (uint32_t)strlen(pRtpMap) - (pSlash - pRtpMap);
                         *ppEncodingParams = (char *)MP4Calloc(length + 1);
                         strncpy(*ppEncodingParams, pSlash, length);


code:
Set visibility of global symbols used in ARM specific assembly file to
internal.

Taken from Debian:
https://salsa.debian.org/multimedia-team/mpeg2dec/blob/master/debian/patches/60_arm-private-symbols.patch

--- mpeg2dec.orig/libmpeg2/motion_comp_arm_s.S
+++ mpeg2dec/libmpeg2/motion_comp_arm_s.S
@@ -23,7 +23,8 @@

 @ ----------------------------------------------------------------
 	.align
-	.global MC_put_o_16_arm
+	.global   MC_put_o_16_arm
+	.internal MC_put_o_16_arm
 MC_put_o_16_arm:
 	@@ void func(uint8_t * dest, const uint8_t * ref, int stride, int height)
 	pld [r1]
@@ -83,7 +84,8 @@ MC_put_o_16_arm_align_jt:

 @ ----------------------------------------------------------------
 	.align
-	.global MC_put_o_8_arm
+	.global   MC_put_o_8_arm
+	.internal MC_put_o_8_arm
 MC_put_o_8_arm:
 	@@ void func(uint8_t * dest, const uint8_t * ref, int stride, int height)
 	pld [r1]
@@ -152,7 +154,8 @@ MC_put_o_8_arm_align_jt:
 .endm

 	.align
-	.global MC_put_x_16_arm
+	.global   MC_put_x_16_arm
+	.internal MC_put_x_16_arm
 MC_put_x_16_arm:
 	@@ void func(uint8_t * dest, const uint8_t * ref, int stride, int height)
 	pld [r1]
@@ -244,7 +247,8 @@ MC_put_x_16_arm_align_jt:

 @ ----------------------------------------------------------------
 	.align
-	.global MC_put_x_8_arm
+	.global   MC_put_x_8_arm
+	.internal MC_put_x_8_arm
 MC_put_x_8_arm:
 	@@ void func(uint8_t * dest, const uint8_t * ref, int stride, int height)
 	pld [r1]


code:
Rewrite the public symbol check to verify the shared libraries, to check for
more things, and to avoid duplication; fixes make check on ARM

Taken from Debian:
https://salsa.debian.org/multimedia-team/mpeg2dec/blob/master/debian/patches/61_global-symbol-test.patch

--- mpeg2dec.orig/test/globals
+++ mpeg2dec/test/globals
@@ -1,4 +1,8 @@
 #!/bin/sh
+# TODO
+# - fix checking of .a libs; problem is that "nm -g --defined-only" lists
+#   internal symbols; this can be solved by using objdump, but it's probably
+#   good enough to just run the tests on the shared lib

 if test x"$srcdir" != x""; then
     builddir="."	# running from make check, but it does not define that
@@ -14,22 +18,30 @@ builddir=`cd $builddir;pwd`

 error=0

-bad_globals=`nm -g --defined-only $builddir/../libmpeg2/*.o |\
-    awk '{if ($3) print $3}' | grep -v '^_\?mpeg2_'`
-
-if test x"$bad_globals" != x""; then
-    echo BAD GLOBAL SYMBOLS:
-    for s in $bad_globals; do echo $s; done
+# check_bad_public_symbols <symbol prefix> <lib file> [<lib file>...]
+#
+# checks public symbols in shared libs:
+# - allow prefix_anything
+# - reject _prefixanything
+# - allow _anything
+# - reject anything else
+#
+# NB: skips missing files
+check_bad_public_symbols() {
+    symbols_prefix="$1"
+    shift
+    lib_files=`ls "$@" 2>/dev/null`
+    [ -z "$lib_files" ] && return
+    bad_globals=`nm -g --defined-only $lib_files |
+        awk '{if ($3) print $3}' |
+        sed -n "/^${symbols_prefix}_/ d; /^_${symbols_prefix}/ { p; d }; /^_/ d; p"`
+    [ -z "$bad_globals" ] && return
     error=1
-fi
-
-bad_globals=`nm -g --defined-only $builddir/../libmpeg2/convert/*.o |\
-    awk '{if ($3) print $3}' | grep -v '^_\?mpeg2convert_'`
+    echo BAD GLOBAL SYMBOLS in $lib_files:
+    echo "$bad_globals"
+}

-if test x"$bad_globals" != x""; then
-    echo BAD GLOBAL SYMBOLS:
-    for s in $bad_globals; do echo $s; done
-    error=1
-fi
+check_bad_public_symbols mpeg2 $builddir/../libmpeg2/.libs/libmpeg2.so
+check_bad_public_symbols mpeg2convert $builddir/../libmpeg2/convert/.libs/libmpeg2convert.so

 exit $error


code:
From 1b53767b988b339aac8b353327a1542b2a0c0610 Mon Sep 17 00:00:00 2001
From: Andreas Sturmlechner <andreas.sturmlechner@gmail.com>
Date: Sun, 15 Apr 2018 22:29:33 +0200
Subject: [PATCH] Fix build with Qt 5.11_beta3 (dropping qt5_use_modules)

---
 CMakeLists.txt       |  3 ---
 src/CMakeLists.txt   |  8 ++++++--
 tests/CMakeLists.txt | 12 +++++++++---
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index eb006d9..fa4b0cb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,9 +34,6 @@ else()
     set( QT_DONT_USE_QTGUI TRUE )
     include( ${QT_USE_FILE} )

-    macro(qt5_use_modules)
-    endmacro()
-
     macro(qt_wrap_cpp)
         qt4_wrap_cpp(${ARGN})
     endmacro()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 802125b..253e81e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -79,9 +79,13 @@ QT_WRAP_CPP(LIBMYGPO_QT_MOC_SRC ${LIBMYGPO_QT_MOC_H} )

 add_library( ${MYGPO_QT_TARGET_NAME} SHARED ${LIBMYGPO_QT_SRC} ${LIBMYGPO_QT_MOC_SRC} )

-target_link_libraries( ${MYGPO_QT_TARGET_NAME} ${QJSON_LIBRARIES} ${QT_QTCORE_LIBRARY} ${QT_QTNETWORK_LIBRARY}  )
 set_target_properties( ${MYGPO_QT_TARGET_NAME} PROPERTIES VERSION ${MYGPO_QT_VERSION} SOVERSION ${MYGPO_QT_SONAME} DEFINE_SYMBOL MYGPO_MAKEDLL)
-qt5_use_modules( ${MYGPO_QT_TARGET_NAME} Core Network )
+
+if( NOT BUILD_WITH_QT4 )
+    target_link_libraries( ${MYGPO_QT_TARGET_NAME} Qt5::Core Qt5::Network)
+else()
+    target_link_libraries( ${MYGPO_QT_TARGET_NAME} ${QT_QTCORE_LIBRARY} ${QT_QTNETWORK_LIBRARY} ${QJSON_LIBRARIES})
+endif()

 install( TARGETS ${MYGPO_QT_TARGET_NAME} EXPORT ${MYGPO_QT_TARGET_NAME}Export DESTINATION ${LIB_INSTALL_DIR} )

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index a3e3223..4f77158 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -11,11 +11,15 @@ include_directories( ${QJSON_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CU

 # UrlBuilder library
 add_library( UrlBuilder ${TESTOBJECTS_SRCDIR}/UrlBuilder.cpp ${TESTOBJECTS_SRCDIR}/Config.cpp )
-qt5_use_modules( UrlBuilder Core )
+if( NOT BUILD_WITH_QT4 )
+    target_link_libraries( UrlBuilder Qt5::Core )
+endif()
 # JSONCreator library
 QT_WRAP_CPP(LIBMYGPO_QT_MOC_SRC ${TESTOBJECTS_SRCDIR}/EpisodeAction.h ${TESTOBJECTS_SRCDIR}/EpisodeAction_p.h )
 add_library( JsonCreator ${TESTOBJECTS_SRCDIR}/JsonCreator.cpp ${TESTOBJECTS_SRCDIR}/EpisodeAction.cpp ${TESTOBJECTS_SRCDIR}/qjsonwrapper/Json.cpp ${LIBMYGPO_QT_MOC_SRC})
-qt5_use_modules( JsonCreator Core Network )
+if( NOT BUILD_WITH_QT4 )
+    target_link_libraries( JsonCreator Qt5::Core Qt5::Network )
+endif()

 set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )

@@ -25,7 +29,9 @@ macro(add_libmygpo_test _source)
     add_executable(${_name} ${_source} ${${_name}_MOC_SRC})
     target_link_libraries(${_name} ${QT_QTCORE_LIBRARY} ${QT_QTTEST_LIBRARY} ${QT_QTNETWORK_LIBRARY})
     add_test(${_name}-test ${EXECUTABLE_OUTPUT_PATH}/${_name})
-    qt5_use_modules(${_name} Network Test)
+    if( NOT BUILD_WITH_QT4 )
+        target_link_libraries(${_name} Qt5::Network Qt5::Test)
+    endif()
 endmacro(add_libmygpo_test)

 add_libmygpo_test( UrlBuilderTest.cpp )


code:
From 0d76d960727018bddf04c6cc89552af69aaa7e55 Mon Sep 17 00:00:00 2001
From: Andreas Sturmlechner <andreas.sturmlechner@gmail.com>
Date: Sun, 15 Apr 2018 22:20:34 +0200
Subject: [PATCH] Add missing Qt5Network, Qt5Test, make BUILD_WITH_QT4 more
 strict

---
 CMakeLists.txt | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2a1f652..eb006d9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,25 +5,25 @@ project( libmygpo-qt )
 set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
 set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII" )

-option(BUILD_WITH_QT4 "Build libmygpo-qt with Qt4 no matter if Qt5 was found" OFF)
+option(BUILD_WITH_QT4 "Build libmygpo-qt with Qt4" OFF)

 if( NOT BUILD_WITH_QT4 )
-    find_package(Qt5Core QUIET)
-    if( Qt5Core_DIR )
-        set(MYGPO_QT_VERSION_SUFFIX 5)
-
-        macro(qt_wrap_cpp)
-            qt5_wrap_cpp(${ARGN})
-        endmacro()
+    if( MYGPO_BUILD_TESTS )
+        find_package(Qt5 REQUIRED COMPONENTS Core Network Test CONFIG)
+    else()
+        find_package(Qt5 REQUIRED COMPONENTS Core Network CONFIG)
     endif()
+    set(MYGPO_QT_VERSION_SUFFIX 5)
+
+    macro(qt_wrap_cpp)
+        qt5_wrap_cpp(${ARGN})
+    endmacro()

     # pkg-config names of QtCore and QtNetwork are Qt5Core and Qt5Network for
     # Qt5
     set(MYGPO_QT_MAJOR_VERSION "5")
     set(MYGPO_QT4_QJSON_DEP "")
-endif()
-
-if( NOT Qt5Core_DIR )
+else()
     message(STATUS "Could not find Qt5, searching for Qt4 instead...")
     message(STATUS "Qt4 Support is deprecated, building with Qt4 is no longer officially supported")
     if( MYGPO_BUILD_TESTS )


code:
Description: Fix FTBFS due to unexisting and useless include. (Closes: #637896)
Author: Regis Boudin <regis@debian.org>
Last-Update: 2011-10-03
Index: libofa-0.9.3/examples/protocol.cpp
===================================================================
--- libofa-0.9.3.orig/examples/protocol.cpp
+++ libofa-0.9.3/examples/protocol.cpp
@@ -13,7 +13,6 @@
 #include <map>
 #include <expat.h>
 #include <curl/curl.h>
-#include <curl/types.h>
 #include <curl/easy.h>

 using namespace std;


code:
Description: fix FTBFS with GCC 4.3. (Closes: #417355)
Author: Martin Michlmayr <tbm@cyrius.com>
Last-Update: 2007-04-02

Description: add a missing include. (Closes: #441557)
Author: Brian M. Carlson <sandals@crustytoothpaste.ath.cx>
Last-Update: 2007-12-29

Index: libofa-0.9.3/lib/signal_op.cpp
===================================================================
--- libofa-0.9.3.orig/lib/signal_op.cpp
+++ libofa-0.9.3/lib/signal_op.cpp
@@ -12,6 +12,7 @@
 // DATE CREATED: 1/12/06

+#include <cstdlib>
 #include <math.h>
 #include "signal_op.h"
 #include "AFLIB/aflibConverter.h"
Index: libofa-0.9.3/examples/example.cpp
===================================================================
--- libofa-0.9.3.orig/examples/example.cpp
+++ libofa-0.9.3/examples/example.cpp
@@ -8,6 +8,8 @@
 -------------------------------------------------------------------*/

 #include "protocol.h"
+#include <cctype>
+#include <string.h>

 AudioData* loadWaveFile(char *file);
 AudioData* loadDataUsingLAME(char *file);
Index: libofa-0.9.3/examples/protocol.cpp
===================================================================
--- libofa-0.9.3.orig/examples/protocol.cpp
+++ libofa-0.9.3/examples/protocol.cpp
@@ -8,6 +8,7 @@
 -------------------------------------------------------------------*/
 #include <stdio.h>
 #include <stdlib.h>
+#include <cstring>
 #include <string>
 #include <map>
 #include <expat.h>


code:
Description: ftbfs with GCC-4.7. (Closes: #667250)
Author: Matthias Klose <doko@debian.org>
Last-Update: 2012-04-17
Index: libofa-0.9.3/examples/uselame.cpp
===================================================================
--- libofa-0.9.3.orig/examples/uselame.cpp
+++ libofa-0.9.3/examples/uselame.cpp
@@ -11,6 +11,7 @@
 #include "windows.h"
 #else
 #include <sys/wait.h>
+#include <unistd.h>
 #endif

 AudioData *loadWaveFile(char *file);
Index: libofa-0.9.3/examples/wavefile.cpp
===================================================================
--- libofa-0.9.3.orig/examples/wavefile.cpp
+++ libofa-0.9.3/examples/wavefile.cpp
@@ -11,6 +11,7 @@
 #include "io.h"
 #endif
 #include <fcntl.h>
+#include <unistd.h>

 static bool readBytes(int fd, unsigned char *buf, int size) {
     int ct = 0;


code:
Initialize the context in 'test_auth_clear'.
Fixes the test suite on mips64el.

Patch by Mark H Weaver <mhw@netris.org>

--- libotr-4.1.1/tests/unit/test_auth.c.orig	2015-12-25 12:39:45.000000000 -0500
+++ libotr-4.1.1/tests/unit/test_auth.c	2016-03-17 18:53:10.169999706 -0400
@@ -67,6 +67,7 @@
 	OtrlAuthInfo *auth = &ctx.auth;

 	/* API call. */
+	otrl_auth_new(&ctx);
 	otrl_auth_clear(auth);

 	ok(auth->authstate == OTRL_AUTHSTATE_NONE &&


code:
Author: R Veera Kumar 2020 <vkor@vkten.in>
Desc:
 1) Fixes download of exchange rates by specifying SSL CA certificates bundle
    file while using libcurl (Since libcurl in guix is compiled without using
    a default CA cert bundle file)
 2) Like above fix for using https site in another case

diff -uNr libqalculate-3.8.0/libqalculate/Calculator-definitions.cc libqalculate-3.8.0.new/libqalculate/Calculator-definitions.cc
--- libqalculate-3.8.0/libqalculate/Calculator-definitions.cc	2020-02-16 15:08:29.000000000 +0530
+++ libqalculate-3.8.0.new/libqalculate/Calculator-definitions.cc	2020-04-17 21:27:36.386039369 +0530
@@ -3610,6 +3610,7 @@
 	curl = curl_easy_init();
 	if(!curl) {return false;}
 	curl_easy_setopt(curl, CURLOPT_URL, getExchangeRatesUrl(1).c_str());
+	curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("SSL_CERT_FILE"));
 	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sbuffer);
@@ -3663,6 +3664,7 @@

 		sbuffer = "";
 		curl_easy_setopt(curl, CURLOPT_URL, getExchangeRatesUrl(2).c_str());
+		curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("SSL_CERT_FILE"));
 		curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sbuffer);
@@ -3687,6 +3689,7 @@

 		sbuffer = "";
 		curl_easy_setopt(curl, CURLOPT_URL, getExchangeRatesUrl(3).c_str());
+		curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("SSL_CERT_FILE"));
 		curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sbuffer);
@@ -3710,6 +3713,7 @@

 		sbuffer = "";
 		curl_easy_setopt(curl, CURLOPT_URL, getExchangeRatesUrl(4).c_str());
+		curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("SSL_CERT_FILE"));
 		curl_easy_setopt(curl, CURLOPT_TIMEOUT, (timeout > 4 && n <= 0) ? 4 : timeout);
 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sbuffer);
diff -uNr libqalculate-3.8.0/libqalculate/util.cc libqalculate-3.8.0.new/libqalculate/util.cc
--- libqalculate-3.8.0/libqalculate/util.cc	2019-12-14 22:56:45.000000000 +0530
+++ libqalculate-3.8.0.new/libqalculate/util.cc	2020-04-17 21:12:17.259674572 +0530
@@ -769,6 +769,7 @@
 	curl = curl_easy_init();
 	if(!curl) {return -1;}
 	curl_easy_setopt(curl, CURLOPT_URL, "https://qalculate.github.io/CURRENT_VERSIONS");
+	curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("SSL_CERT_FILE"));
 	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sbuffer);


code:
Submitted By: Ken Moffat <ken at linuxfromscratch dot org>
Date: 2018-05-12
Initial Package Version: 1.2.4
Upstream Status: Defunct
Origin: Gentoo
Description: Accumulated fixes up to and including building with ffmpeg-4

From gentoo, their libav-9.patch, ffmpeg2.patch, CVE-2016-2399.patch,
ffmpeg29.patch, ffmpeg4.patch, and seds to files lqt_ffmpeg.c, video.c,
audio.c in plugins/ffmpeg/ to change CODEC_ID to AV_CODEC_ID.

Build-tested only.

diff -Naur a/plugins/ffmpeg/audio.c b/plugins/ffmpeg/audio.c
--- a/plugins/ffmpeg/audio.c	2012-03-29 20:44:28.000000000 +0100
+++ b/plugins/ffmpeg/audio.c	2018-05-11 23:15:21.057985300 +0100
@@ -45,6 +45,11 @@
 #define ENCODE_AUDIO 1
 #endif

+#ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
+/* from libavcodec/avcodec.h dated Dec 23 2012 */
+#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
+#endif
+
 /* The following code was ported from gmerlin_avdecoder (http://gmerlin.sourceforge.net) */

 /* MPEG Audio header parsing code */
@@ -540,7 +545,7 @@

 #if DECODE_AUDIO3 || DECODE_AUDIO4
     codec->pkt.data = codec->chunk_buffer;
-    codec->pkt.size = packet_size + FF_INPUT_BUFFER_PADDING_SIZE;
+    codec->pkt.size = packet_size + AV_INPUT_BUFFER_PADDING_SIZE;

 #if DECODE_AUDIO4
     frame_bytes = avcodec_decode_audio4(codec->avctx, &f,
@@ -578,7 +583,7 @@
                                          (codec->sample_buffer_end - codec->sample_buffer_start)],
                   &bytes_decoded,
                   codec->chunk_buffer,
-                  packet_size + FF_INPUT_BUFFER_PADDING_SIZE);
+                  packet_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if(frame_bytes < 0)
       {
       lqt_log(file, LQT_LOG_ERROR, LOG_DOMAIN, "avcodec_decode_audio2 error");
@@ -626,7 +631,7 @@
     {
     /* If the codec is mp3, make sure to decode the very last frame */

-    if((codec->avctx->codec_id == CODEC_ID_MP3) &&
+    if((codec->avctx->codec_id == AV_CODEC_ID_MP3) &&
        (codec->bytes_in_chunk_buffer >= 4))
       {
       if(!mpa_decode_header(&mph, codec->chunk_buffer, (const mpa_header*)0))
@@ -640,13 +645,13 @@
         return 0;
         }

-      if(codec->chunk_buffer_alloc < mph.frame_bytes + FF_INPUT_BUFFER_PADDING_SIZE)
+      if(codec->chunk_buffer_alloc < mph.frame_bytes + AV_INPUT_BUFFER_PADDING_SIZE)
         {
-        codec->chunk_buffer_alloc = mph.frame_bytes + FF_INPUT_BUFFER_PADDING_SIZE;
+        codec->chunk_buffer_alloc = mph.frame_bytes + AV_INPUT_BUFFER_PADDING_SIZE;
         codec->chunk_buffer = realloc(codec->chunk_buffer, codec->chunk_buffer_alloc);
         }
       memset(codec->chunk_buffer + codec->bytes_in_chunk_buffer, 0,
-             mph.frame_bytes - codec->bytes_in_chunk_buffer + FF_INPUT_BUFFER_PADDING_SIZE);
+             mph.frame_bytes - codec->bytes_in_chunk_buffer + AV_INPUT_BUFFER_PADDING_SIZE);
       num_samples = mph.samples_per_frame;
       codec->bytes_in_chunk_buffer = mph.frame_bytes;
       }
@@ -690,12 +695,12 @@
     {

-    /* BIG NOTE: We pass extra FF_INPUT_BUFFER_PADDING_SIZE for the buffer size
+    /* BIG NOTE: We pass extra AV_INPUT_BUFFER_PADDING_SIZE for the buffer size
        because we know, that lqt_read_audio_chunk allocates 16 extra bytes for us */

     /* Some really broken mp3 files have the header bytes split across 2 chunks */

-    if(codec->avctx->codec_id == CODEC_ID_MP3)
+    if(codec->avctx->codec_id == AV_CODEC_ID_MP3)
       {
       if(codec->bytes_in_chunk_buffer < 4)
         {
@@ -756,7 +761,7 @@

 #if DECODE_AUDIO3 || DECODE_AUDIO4
     codec->pkt.data = &codec->chunk_buffer[bytes_used];
-    codec->pkt.size = codec->bytes_in_chunk_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
+    codec->pkt.size = codec->bytes_in_chunk_buffer + AV_INPUT_BUFFER_PADDING_SIZE;

 #if DECODE_AUDIO4

@@ -793,7 +798,7 @@
                                                   (codec->sample_buffer_end - codec->sample_buffer_start)],
                             &bytes_decoded,
                             &codec->chunk_buffer[bytes_used],
-                            codec->bytes_in_chunk_buffer + FF_INPUT_BUFFER_PADDING_SIZE);
+                            codec->bytes_in_chunk_buffer + AV_INPUT_BUFFER_PADDING_SIZE);
 #endif
     if(frame_bytes < 0)
       {
@@ -806,7 +811,7 @@

     if(bytes_decoded < 0)
       {
-      if(codec->avctx->codec_id == CODEC_ID_MP3)
+      if(codec->avctx->codec_id == AV_CODEC_ID_MP3)
         {
         /* For mp3, bytes_decoded < 0 means, that the frame should be muted */
         memset(&codec->sample_buffer[track_map->channels * (codec->sample_buffer_end -
@@ -833,7 +838,7 @@
         }
       }

-    /* This happens because ffmpeg adds FF_INPUT_BUFFER_PADDING_SIZE to the bytes returned */
+    /* This happens because ffmpeg adds AV_INPUT_BUFFER_PADDING_SIZE to the bytes returned */

     if(codec->bytes_in_chunk_buffer < 0)
       codec->bytes_in_chunk_buffer = 0;
@@ -866,8 +871,8 @@
   quicktime_audio_map_t *track_map = &file->atracks[track];
   quicktime_ffmpeg_audio_codec_t *codec = track_map->codec->priv;

-  if((codec->decoder->id == CODEC_ID_MP2) ||
-     (codec->decoder->id == CODEC_ID_MP3))
+  if((codec->decoder->id == AV_CODEC_ID_MP2) ||
+     (codec->decoder->id == AV_CODEC_ID_MP3))
     {
     mpa_header h;
     uint32_t header;
@@ -909,7 +914,7 @@
     else
       track_map->ci.bitrate = h.bitrate;
     }
-  else if(codec->decoder->id == CODEC_ID_AC3)
+  else if(codec->decoder->id == AV_CODEC_ID_AC3)
     {
     a52_header h;
     uint8_t * ptr;
@@ -986,7 +991,7 @@
 #endif
     /* Some codecs need extra stuff */

-    if(codec->decoder->id == CODEC_ID_ALAC)
+    if(codec->decoder->id == AV_CODEC_ID_ALAC)
       {
       header = quicktime_wave_get_user_atom(track_map->track, "alac", &header_len);
       if(header)
@@ -995,7 +1000,7 @@
         codec->avctx->extradata_size = header_len;
         }
       }
-    if(codec->decoder->id == CODEC_ID_QDM2)
+    if(codec->decoder->id == AV_CODEC_ID_QDM2)
       {
       header = quicktime_wave_get_user_atom(track_map->track, "QDCA", &header_len);
       if(header)
@@ -1261,7 +1266,7 @@
     pkt.data = codec->chunk_buffer;
     pkt.size = codec->chunk_buffer_alloc;

-    avcodec_get_frame_defaults(&f);
+    av_frame_unref(&f);
     f.nb_samples = codec->avctx->frame_size;

     avcodec_fill_audio_frame(&f, channels, codec->avctx->sample_fmt,
@@ -1495,9 +1500,9 @@
     codec_base->decode_audio = lqt_ffmpeg_decode_audio;
   codec_base->set_parameter = set_parameter;

-  if((decoder->id == CODEC_ID_MP3) || (decoder->id == CODEC_ID_MP2))
+  if((decoder->id == AV_CODEC_ID_MP3) || (decoder->id == AV_CODEC_ID_MP2))
     codec_base->read_packet = read_packet_mpa;
-  else if(decoder->id == CODEC_ID_AC3)
+  else if(decoder->id == AV_CODEC_ID_AC3)
     {
     codec_base->write_packet = write_packet_ac3;
     codec_base->read_packet = read_packet_ac3;
diff -Naur a/plugins/ffmpeg/ffmpeg.h b/plugins/ffmpeg/ffmpeg.h
--- a/plugins/ffmpeg/ffmpeg.h	2012-02-15 19:48:30.000000000 +0000
+++ b/plugins/ffmpeg/ffmpeg.h	2018-05-11 23:10:24.204992468 +0100
@@ -27,6 +27,7 @@

 #include <quicktime/qtprivate.h>
 #include AVCODEC_HEADER
+#include <libavutil/mem.h>

 void quicktime_init_video_codec_ffmpeg(quicktime_codec_t * codec,
                                        quicktime_video_map_t *vtrack,
diff -Naur a/plugins/ffmpeg/lqt_ffmpeg.c b/plugins/ffmpeg/lqt_ffmpeg.c
--- a/plugins/ffmpeg/lqt_ffmpeg.c	2012-02-15 19:48:30.000000000 +0000
+++ b/plugins/ffmpeg/lqt_ffmpeg.c	2018-05-11 23:15:07.583985628 +0100
@@ -386,7 +386,7 @@
 struct CODECIDMAP codecidmap_v[] =
   {
     {
-      .id = CODEC_ID_MPEG1VIDEO,
+      .id = AV_CODEC_ID_MPEG1VIDEO,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -397,7 +397,7 @@
       .wav_ids = { LQT_WAV_ID_NONE }
     },
     {
-      .id = CODEC_ID_MPEG4,
+      .id = AV_CODEC_ID_MPEG4,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -415,7 +415,7 @@
       .compression_id = LQT_COMPRESSION_MPEG4_ASP,
     },
     {
-      .id = CODEC_ID_MSMPEG4V1,
+      .id = AV_CODEC_ID_MSMPEG4V1,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -426,7 +426,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_MSMPEG4V2,
+      .id = AV_CODEC_ID_MSMPEG4V2,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -437,7 +437,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_MSMPEG4V3,
+      .id = AV_CODEC_ID_MSMPEG4V3,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -453,7 +453,7 @@
       .do_encode = 1,
     },
     {
-      .id = CODEC_ID_MSMPEG4V3,
+      .id = AV_CODEC_ID_MSMPEG4V3,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -468,7 +468,7 @@
     },
 #if 0
     {
-      .id = CODEC_ID_WMV1,
+      .id = AV_CODEC_ID_WMV1,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -481,7 +481,7 @@
     },
 #endif
     {
-      .id = CODEC_ID_H263,
+      .id = AV_CODEC_ID_H263,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -493,7 +493,7 @@
       .compatibility_flags = LQT_FILE_QT_OLD | LQT_FILE_QT | LQT_FILE_MP4 | LQT_FILE_3GP,
     },
     {
-      .id = CODEC_ID_H263,
+      .id = AV_CODEC_ID_H263,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -508,7 +508,7 @@
       .do_encode = 1,
     },
     {
-      .id = CODEC_ID_H264,
+      .id = AV_CODEC_ID_H264,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -519,7 +519,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_H263P,
+      .id = AV_CODEC_ID_H263P,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -533,7 +533,7 @@
       .do_encode = 1,
     },
     {
-      .id = CODEC_ID_H263I,
+      .id = AV_CODEC_ID_H263I,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -544,7 +544,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_SVQ1,
+      .id = AV_CODEC_ID_SVQ1,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -555,7 +555,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_SVQ3,
+      .id = AV_CODEC_ID_SVQ3,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -566,7 +566,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_MJPEG,
+      .id = AV_CODEC_ID_MJPEG,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -580,7 +580,7 @@
       .do_encode = 1,
     },
     {
-      .id = CODEC_ID_MJPEGB,
+      .id = AV_CODEC_ID_MJPEGB,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -594,7 +594,7 @@
     },
 #if LIBAVCODEC_BUILD >= 3346688
     {
-      .id = CODEC_ID_TARGA,
+      .id = AV_CODEC_ID_TARGA,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -606,7 +606,7 @@
 #endif
 #if LIBAVCODEC_BUILD >= 3347456
     {
-      .id = CODEC_ID_TIFF,
+      .id = AV_CODEC_ID_TIFF,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -617,7 +617,7 @@
     },
 #endif
     {
-      .id = CODEC_ID_8BPS,
+      .id = AV_CODEC_ID_8BPS,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -627,7 +627,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_INDEO3,
+      .id = AV_CODEC_ID_INDEO3,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -638,7 +638,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_RPZA,
+      .id = AV_CODEC_ID_RPZA,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -648,7 +648,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_SMC,
+      .id = AV_CODEC_ID_SMC,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -658,7 +658,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_CINEPAK,
+      .id = AV_CODEC_ID_CINEPAK,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -669,7 +669,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_CYUV,
+      .id = AV_CODEC_ID_CYUV,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -680,7 +680,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_QTRLE,
+      .id = AV_CODEC_ID_QTRLE,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -693,7 +693,7 @@
       .encoding_colormodels = (int[]){ BC_RGB888, BC_RGBA8888, LQT_COLORMODEL_NONE },
     },
     {
-      .id = CODEC_ID_MSRLE,
+      .id = AV_CODEC_ID_MSRLE,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -703,7 +703,7 @@
       .wav_ids = { LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_DVVIDEO,
+      .id = AV_CODEC_ID_DVVIDEO,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -719,7 +719,7 @@
       .image_sizes = image_sizes_dv,
     },
     {
-      .id = CODEC_ID_DVVIDEO,
+      .id = AV_CODEC_ID_DVVIDEO,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -735,7 +735,7 @@
       .image_sizes = image_sizes_dv,
     },
     {
-      .id = CODEC_ID_DVVIDEO,
+      .id = AV_CODEC_ID_DVVIDEO,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -751,7 +751,7 @@
     },
     /* DVCPRO HD (decoding only for now) */
     {
-      .id = CODEC_ID_DVVIDEO,
+      .id = AV_CODEC_ID_DVVIDEO,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -772,7 +772,7 @@
       // .do_encode = 1
     },
     {
-      .id = CODEC_ID_FFVHUFF,
+      .id = AV_CODEC_ID_FFVHUFF,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -785,7 +785,7 @@
       .do_encode = 1
     },
     {
-      .id = CODEC_ID_FFV1,
+      .id = AV_CODEC_ID_FFV1,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -801,7 +801,7 @@
     },
 #if LIBAVCODEC_BUILD >= 3352576
     {
-      .id = CODEC_ID_DNXHD,
+      .id = AV_CODEC_ID_DNXHD,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -817,7 +817,7 @@
     },
 #endif
     {
-      .id = CODEC_ID_MPEG2VIDEO,
+      .id = AV_CODEC_ID_MPEG2VIDEO,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -839,7 +839,7 @@
 struct CODECIDMAP codecidmap_a[] =
   {
     {
-      .id = CODEC_ID_MP3,
+      .id = AV_CODEC_ID_MP3,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -851,7 +851,7 @@
       .wav_ids = { 0x50, 0x55, LQT_WAV_ID_NONE },
     },
     {
-      .id = CODEC_ID_MP2,
+      .id = AV_CODEC_ID_MP2,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -866,7 +866,7 @@
       .compression_id = LQT_COMPRESSION_MP2,
     },
     {
-      .id = CODEC_ID_AC3,
+      .id = AV_CODEC_ID_AC3,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -881,7 +881,7 @@
       .compression_id = LQT_COMPRESSION_AC3,
     },
     {
-      .id = CODEC_ID_QDM2,
+      .id = AV_CODEC_ID_QDM2,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -896,7 +896,7 @@
 #if 1
     /* Doesn't work as long as audio chunks are not split into VBR "Samples" */
     {
-      .id = CODEC_ID_ALAC,
+      .id = AV_CODEC_ID_ALAC,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -909,7 +909,7 @@
 #if 1
     /* Sounds ugly */
     {
-      .id = CODEC_ID_ADPCM_MS,
+      .id = AV_CODEC_ID_ADPCM_MS,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
@@ -922,7 +922,7 @@
 #if 1
     /* Sounds ugly */
     {
-      .id = CODEC_ID_ADPCM_IMA_WAV,
+      .id = AV_CODEC_ID_ADPCM_IMA_WAV,
       .index = -1,
       .encoder = NULL,
       .decoder = NULL,
diff -Naur a/plugins/ffmpeg/params.c b/plugins/ffmpeg/params.c
--- a/plugins/ffmpeg/params.c	2012-03-07 14:10:41.000000000 +0000
+++ b/plugins/ffmpeg/params.c	2018-05-11 23:11:59.803990160 +0100
@@ -101,6 +101,17 @@
     }                                           \
   }

+#define PARAM_DICT_INT(name, dict_name)             \
+  {                                                 \
+  if(!strcasecmp(name, key))                        \
+    {                                               \
+    char buf[128];                                  \
+    snprintf(buf, sizeof(buf), "%d", *(int*)value); \
+    av_dict_set(options, dict_name, buf, 0);        \
+    found = 1;                                      \
+    }                                               \
+  }
+
 #define PARAM_DICT_FLAG(name, dict_name)        \
   {                                             \
   if(!strcasecmp(name, key))                    \
@@ -113,16 +124,6 @@
   }

-enum_t me_method[] =
-  {
-    { "Zero",  ME_ZERO },
-    { "Phods", ME_PHODS },
-    { "Log",   ME_LOG },
-    { "X1",    ME_X1 },
-    { "Epzs",  ME_EPZS },
-    { "Full",  ME_FULL }
-  };
-
 enum_t prediction_method[] =
   {
     { "Left",   FF_PRED_LEFT },
@@ -152,15 +153,6 @@
     { "Rate distoration",     FF_MB_DECISION_RD }
   };

-enum_t coder_type[] =
-  {
-    { "VLC",                  FF_CODER_TYPE_VLC },
-    { "Arithmetic",           FF_CODER_TYPE_AC },
-    { "Raw",                  FF_CODER_TYPE_RAW },
-    { "RLE",                  FF_CODER_TYPE_RLE },
-    { "Deflate",              FF_CODER_TYPE_DEFLATE },
-  };
-
 #define PARAM_ENUM(name, var, arr) \
   if(!strcasecmp(key, name)) \
     { \
@@ -192,7 +184,7 @@
   PARAM_INT_SCALE("ff_bit_rate_audio",bit_rate,1000);
   PARAM_INT_SCALE("ff_bit_rate_video",bit_rate,1000);
   PARAM_INT_SCALE("ff_bit_rate_tolerance",bit_rate_tolerance,1000);
-  PARAM_ENUM("ff_me_method",me_method,me_method);
+  PARAM_DICT_INT("ff_me_method","motion-est");
   PARAM_INT("ff_gop_size",gop_size);
   PARAM_FLOAT("ff_qcompress",qcompress);
   PARAM_FLOAT("ff_qblur",qblur);
@@ -202,17 +194,23 @@
   PARAM_INT("ff_max_b_frames",max_b_frames);
   PARAM_FLOAT("ff_b_quant_factor",b_quant_factor);
   PARAM_INT("ff_b_frame_strategy",b_frame_strategy);
+
+#if LIBAVCODEC_VERSION_MAJOR >= 55
+  PARAM_DICT_INT("ff_luma_elim_threshold","luma_elim_threshold");
+  PARAM_DICT_INT("ff_chroma_elim_threshold","chroma_elim_threshold");
+#else
   PARAM_INT("ff_luma_elim_threshold",luma_elim_threshold);
   PARAM_INT("ff_chroma_elim_threshold",chroma_elim_threshold);
+#endif
+
   PARAM_INT("ff_strict_std_compliance",strict_std_compliance);
   PARAM_QP2LAMBDA("ff_b_quant_offset",b_quant_offset);
   PARAM_INT("ff_rc_min_rate",rc_min_rate);
   PARAM_INT("ff_rc_max_rate",rc_max_rate);
   PARAM_INT_SCALE("ff_rc_buffer_size",rc_buffer_size,1000);
-  PARAM_FLOAT("ff_rc_buffer_aggressivity",rc_buffer_aggressivity);
   PARAM_FLOAT("ff_i_quant_factor",i_quant_factor);
   PARAM_QP2LAMBDA("ff_i_quant_offset",i_quant_offset);
-  PARAM_FLOAT("ff_rc_initial_cplx",rc_initial_cplx);
+  PARAM_DICT_INT("ff_rc_initial_cplx","rc_init_cplx");
   PARAM_FLOAT("ff_lumi_masking",lumi_masking);
   PARAM_FLOAT("ff_temporal_cplx_masking",temporal_cplx_masking);
   PARAM_FLOAT("ff_spatial_cplx_masking",spatial_cplx_masking);
@@ -237,58 +235,68 @@
   PARAM_INT("ff_me_range",me_range);
   PARAM_ENUM("ff_mb_decision",mb_decision,mb_decision);
   PARAM_INT("ff_scenechange_threshold",scenechange_threshold);
-  PARAM_QP2LAMBDA("ff_lmin", lmin);
-  PARAM_QP2LAMBDA("ff_lmax", lmax);
+  PARAM_DICT_INT("ff_lmin", "lmin");
+  PARAM_DICT_INT("ff_lmax", "lmax");
   PARAM_INT("ff_noise_reduction",noise_reduction);
   PARAM_INT_SCALE("ff_rc_initial_buffer_occupancy",rc_initial_buffer_occupancy,1000);
+
+#if LIBAVCODEC_VERSION_MAJOR >= 55
+  PARAM_DICT_INT("ff_inter_threshold","inter_threshold");
+  PARAM_DICT_INT("ff_quantizer_noise_shaping","quantizer_noise_shaping");
+#else
   PARAM_INT("ff_inter_threshold",inter_threshold);
   PARAM_INT("ff_quantizer_noise_shaping",quantizer_noise_shaping);
+#endif
+
   PARAM_INT("ff_thread_count",thread_count);
-  PARAM_INT("ff_me_threshold",me_threshold);
-  PARAM_INT("ff_mb_threshold",mb_threshold);
   PARAM_INT("ff_nsse_weight",nsse_weight);
-  PARAM_FLOAT("ff_border_masking",border_masking);
+  PARAM_DICT_INT("ff_border_masking","border_mask");
   PARAM_QP2LAMBDA("ff_mb_lmin", mb_lmin);
   PARAM_QP2LAMBDA("ff_mb_lmax", mb_lmax);
   PARAM_INT("ff_me_penalty_compensation",me_penalty_compensation);
   PARAM_INT("ff_bidir_refine",bidir_refine);
   PARAM_INT("ff_brd_scale",brd_scale);
-  PARAM_INT("ff_scenechange_factor",scenechange_factor);
-  PARAM_FLAG("ff_flag_qscale",CODEC_FLAG_QSCALE);
-  PARAM_FLAG("ff_flag_4mv",CODEC_FLAG_4MV);
-  PARAM_FLAG("ff_flag_qpel",CODEC_FLAG_QPEL);
-  PARAM_FLAG("ff_flag_gmc",CODEC_FLAG_GMC);
-  PARAM_FLAG("ff_flag_mv0",CODEC_FLAG_MV0);
+  PARAM_FLAG("ff_flag_qscale",AV_CODEC_FLAG_QSCALE);
+  PARAM_FLAG("ff_flag_4mv",AV_CODEC_FLAG_4MV);
+  PARAM_FLAG("ff_flag_qpel",AV_CODEC_FLAG_QPEL);
+  PARAM_DICT_FLAG("ff_flag_gmc","gmc");
+  PARAM_DICT_FLAG("ff_flag_mv0","mpv_flags");
   //  PARAM_FLAG("ff_flag_part",CODEC_FLAG_PART); // Unused
-  PARAM_FLAG("ff_flag_gray",CODEC_FLAG_GRAY);
-  PARAM_FLAG("ff_flag_emu_edge",CODEC_FLAG_EMU_EDGE);
-  PARAM_FLAG("ff_flag_normalize_aqp",CODEC_FLAG_NORMALIZE_AQP);
+  PARAM_FLAG("ff_flag_gray",AV_CODEC_FLAG_GRAY);
+  PARAM_DICT_FLAG("ff_flag_normalize_aqp","naq");
   //  PARAM_FLAG("ff_flag_alt_scan",CODEC_FLAG_ALT_SCAN); // Unused
 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
   PARAM_FLAG("ff_flag_trellis_quant",CODEC_FLAG_TRELLIS_QUANT);
 #else
   PARAM_INT("ff_trellis",trellis);
 #endif
-  PARAM_FLAG("ff_flag_bitexact",CODEC_FLAG_BITEXACT);
-  PARAM_FLAG("ff_flag_ac_pred",CODEC_FLAG_AC_PRED);
+  PARAM_FLAG("ff_flag_bitexact",AV_CODEC_FLAG_BITEXACT);
+  PARAM_FLAG("ff_flag_ac_pred",AV_CODEC_FLAG_AC_PRED);
   //  PARAM_FLAG("ff_flag_h263p_umv",CODEC_FLAG_H263P_UMV); // Unused
-  PARAM_FLAG("ff_flag_cbp_rd",CODEC_FLAG_CBP_RD);
-  PARAM_FLAG("ff_flag_qp_rd",CODEC_FLAG_QP_RD);
+
+#if LIBAVCODEC_VERSION_MAJOR >= 55
+  PARAM_DICT_FLAG("ff_flag_cbp_rd","cbp_rd");
+  PARAM_DICT_FLAG("ff_flag_qp_rd","qp_rd");
+  PARAM_DICT_FLAG("ff_flag2_strict_gop","strict_gop");
+#else
+  PARAM_FLAG("ff_flag_cbp_rd",AV_CODEC_FLAG_CBP_RD);
+  PARAM_FLAG("ff_flag_qp_rd",AV_CODEC_FLAG_QP_RD);
+  PARAM_FLAG2("ff_flag2_strict_gop",AV_CODEC_FLAG2_STRICT_GOP);
+#endif

 #if LIBAVCODEC_VERSION_MAJOR >= 54
   PARAM_DICT_FLAG("ff_flag_h263p_aiv", "aiv");
   PARAM_DICT_FLAG("ff_flag_obmc","obmc");
   PARAM_DICT_FLAG("ff_flag_h263p_slice_struct","structured_slices");
 #else
-  PARAM_FLAG("ff_flag_h263p_aiv",CODEC_FLAG_H263P_AIV);
-  PARAM_FLAG("ff_flag_obmc",CODEC_FLAG_OBMC);
-  PARAM_FLAG("ff_flag_h263p_slice_struct",CODEC_FLAG_H263P_SLICE_STRUCT);
+  PARAM_FLAG("ff_flag_h263p_aiv",AV_CODEC_FLAG_H263P_AIV);
+  PARAM_FLAG("ff_flag_obmc",AV_CODEC_FLAG_OBMC);
+  PARAM_FLAG("ff_flag_h263p_slice_struct",AV_CODEC_FLAG_H263P_SLICE_STRUCT);
 #endif

-  PARAM_FLAG("ff_flag_loop_filter",CODEC_FLAG_LOOP_FILTER);
-  PARAM_FLAG("ff_flag_closed_gop",CODEC_FLAG_CLOSED_GOP);
-  PARAM_FLAG2("ff_flag2_fast",CODEC_FLAG2_FAST);
-  PARAM_FLAG2("ff_flag2_strict_gop",CODEC_FLAG2_STRICT_GOP);
-  PARAM_ENUM("ff_coder_type",coder_type,coder_type);
+  PARAM_FLAG("ff_flag_loop_filter",AV_CODEC_FLAG_LOOP_FILTER);
+  PARAM_FLAG("ff_flag_closed_gop",AV_CODEC_FLAG_CLOSED_GOP);
+  PARAM_FLAG2("ff_flag2_fast",AV_CODEC_FLAG2_FAST);
+  PARAM_DICT_INT("ff_coder_type","coder");

   }
diff -Naur a/plugins/ffmpeg/params.h b/plugins/ffmpeg/params.h
--- a/plugins/ffmpeg/params.h	2011-05-11 16:13:39.000000000 +0100
+++ b/plugins/ffmpeg/params.h	2018-05-11 23:11:59.803990160 +0100
@@ -149,7 +149,7 @@
     .type =        LQT_PARAMETER_INT, \
     .val_default = { .val_int = 0 }, \
     .val_min =     { .val_int = 0 }, \
-    .val_max =     { .val_int = FF_MAX_B_FRAMES }, \
+    .val_max =     { .val_int = 16 }, \
     .help_string = TRS("Maximum number of B-frames between non B-frames") \
   }

diff -Naur a/plugins/ffmpeg/video.c b/plugins/ffmpeg/video.c
--- a/plugins/ffmpeg/video.c	2012-02-25 19:46:56.000000000 +0000
+++ b/plugins/ffmpeg/video.c	2018-05-11 23:15:15.697985432 +0100
@@ -37,10 +37,10 @@
 #endif

-#ifdef  PIX_FMT_YUV422P10
-#define PIX_FMT_YUV422P10_OR_DUMMY PIX_FMT_YUV422P10
+#ifdef  AV_PIX_FMT_YUV422P10
+#define AV_PIX_FMT_YUV422P10_OR_DUMMY AV_PIX_FMT_YUV422P10
 #else
-#define PIX_FMT_YUV422P10_OR_DUMMY -1234
+#define AV_PIX_FMT_YUV422P10_OR_DUMMY -1234
 #endif

 #if LIBAVCODEC_VERSION_INT >= ((54<<16)|(1<<8)|0)
@@ -90,9 +90,9 @@
   int imx_bitrate;
   int imx_strip_vbi;

-  /* In some cases FFMpeg would report something like PIX_FMT_YUV422P, while
-     we would like to treat it as PIX_FMT_YUVJ422P. It's only used for decoding */
-  enum PixelFormat reinterpret_pix_fmt;
+  /* In some cases FFMpeg would report something like AV_PIX_FMT_YUV422P, while
+     we would like to treat it as AV_PIX_FMT_YUVJ422P. It's only used for decoding */
+  enum AVPixelFormat reinterpret_pix_fmt;

   int is_imx;
   int y_offset;
@@ -137,42 +137,42 @@

 static const struct
   {
-  enum PixelFormat ffmpeg_id;
+  enum AVPixelFormat ffmpeg_id;
   int              lqt_id;
   int              exact;
   }
 colormodels[] =
   {
-    { PIX_FMT_YUV420P,   BC_YUV420P,   1 }, ///< Planar YUV 4:2:0 (1 Cr & Cb sample per 2x2 Y samples)
+    { AV_PIX_FMT_YUV420P,   BC_YUV420P,   1 }, ///< Planar YUV 4:2:0 (1 Cr & Cb sample per 2x2 Y samples)
 #if LIBAVUTIL_VERSION_INT < (50<<16)
-    { PIX_FMT_YUV422,    BC_YUV422,    1 },
+    { AV_PIX_FMT_YUV422,    BC_YUV422,    1 },
 #else
-    { PIX_FMT_YUYV422,   BC_YUV422,    1 },
+    { AV_PIX_FMT_YUYV422,   BC_YUV422,    1 },
 #endif
-    { PIX_FMT_RGB24,     BC_RGB888,    1 }, ///< Packed pixel, 3 bytes per pixel, RGBRGB...
-    { PIX_FMT_BGR24,     BC_BGR888,    1 }, ///< Packed pixel, 3 bytes per pixel, BGRBGR...
-    { PIX_FMT_YUV422P,   BC_YUV422P,   1 }, ///< Planar YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples)
-    { PIX_FMT_YUV444P,   BC_YUV444P,   1 }, ///< Planar YUV 4:4:4 (1 Cr & Cb sample per 1x1 Y samples)
-    { PIX_FMT_YUV411P,   BC_YUV411P,   1 }, ///< Planar YUV 4:1:1 (1 Cr & Cb sample per 4x1 Y samples)
-    { PIX_FMT_YUV422P16, BC_YUV422P16, 1 }, ///< Planar 16 bit YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples)
-#ifdef PIX_FMT_YUV422P10
-    { PIX_FMT_YUV422P10, BC_YUV422P10, 1 }, ///< 10 bit samples in uint16_t containers, planar 4:2:2
-#endif
-    { PIX_FMT_RGB565,    BC_RGB565,    1 }, ///< always stored in cpu endianness
-    { PIX_FMT_YUVJ420P,  BC_YUVJ420P,  1 }, ///< Planar YUV 4:2:0 full scale (jpeg)
-    { PIX_FMT_YUVJ422P,  BC_YUVJ422P,  1 }, ///< Planar YUV 4:2:2 full scale (jpeg)
-    { PIX_FMT_YUVJ444P,  BC_YUVJ444P,  1 }, ///< Planar YUV 4:4:4 full scale (jpeg)
+    { AV_PIX_FMT_RGB24,     BC_RGB888,    1 }, ///< Packed pixel, 3 bytes per pixel, RGBRGB...
+    { AV_PIX_FMT_BGR24,     BC_BGR888,    1 }, ///< Packed pixel, 3 bytes per pixel, BGRBGR...
+    { AV_PIX_FMT_YUV422P,   BC_YUV422P,   1 }, ///< Planar YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples)
+    { AV_PIX_FMT_YUV444P,   BC_YUV444P,   1 }, ///< Planar YUV 4:4:4 (1 Cr & Cb sample per 1x1 Y samples)
+    { AV_PIX_FMT_YUV411P,   BC_YUV411P,   1 }, ///< Planar YUV 4:1:1 (1 Cr & Cb sample per 4x1 Y samples)
+    { AV_PIX_FMT_YUV422P16, BC_YUV422P16, 1 }, ///< Planar 16 bit YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples)
+#ifdef AV_PIX_FMT_YUV422P10
+    { AV_PIX_FMT_YUV422P10, BC_YUV422P10, 1 }, ///< 10 bit samples in uint16_t containers, planar 4:2:2
+#endif
+    { AV_PIX_FMT_RGB565,    BC_RGB565,    1 }, ///< always stored in cpu endianness
+    { AV_PIX_FMT_YUVJ420P,  BC_YUVJ420P,  1 }, ///< Planar YUV 4:2:0 full scale (jpeg)
+    { AV_PIX_FMT_YUVJ422P,  BC_YUVJ422P,  1 }, ///< Planar YUV 4:2:2 full scale (jpeg)
+    { AV_PIX_FMT_YUVJ444P,  BC_YUVJ444P,  1 }, ///< Planar YUV 4:4:4 full scale (jpeg)
 #if LIBAVUTIL_VERSION_INT < (50<<16)
-    { PIX_FMT_RGBA32,    BC_RGBA8888,  0 }, ///< Packed pixel, 4 bytes per pixel, BGRABGRA...
+    { AV_PIX_FMT_RGBA32,    BC_RGBA8888,  0 }, ///< Packed pixel, 4 bytes per pixel, BGRABGRA...
 #else
-    { PIX_FMT_RGB32,     BC_RGBA8888,  0 }, ///< Packed pixel, 4 bytes per pixel, BGRABGRA...
+    { AV_PIX_FMT_RGB32,     BC_RGBA8888,  0 }, ///< Packed pixel, 4 bytes per pixel, BGRABGRA...
 #endif
-    { PIX_FMT_RGB555,    BC_RGB888,    0 }, ///< always stored in cpu endianness, most significant bit to 1
-    { PIX_FMT_GRAY8,     BC_RGB888,    0 },
-    { PIX_FMT_MONOWHITE, BC_RGB888,    0 }, ///< 0 is white
-    { PIX_FMT_MONOBLACK, BC_RGB888,    0 }, ///< 0 is black
-    { PIX_FMT_PAL8,      BC_RGB888,    0 }, ///< 8 bit with RGBA palette
-    { PIX_FMT_YUV410P,   BC_YUV420P,   0 }, ///< Planar YUV 4:1:0 (1 Cr & Cb sample per 4x4 Y samples)
+    { AV_PIX_FMT_RGB555,    BC_RGB888,    0 }, ///< always stored in cpu endianness, most significant bit to 1
+    { AV_PIX_FMT_GRAY8,     BC_RGB888,    0 },
+    { AV_PIX_FMT_MONOWHITE, BC_RGB888,    0 }, ///< 0 is white
+    { AV_PIX_FMT_MONOBLACK, BC_RGB888,    0 }, ///< 0 is black
+    { AV_PIX_FMT_PAL8,      BC_RGB888,    0 }, ///< 8 bit with RGBA palette
+    { AV_PIX_FMT_YUV410P,   BC_YUV420P,   0 }, ///< Planar YUV 4:1:0 (1 Cr & Cb sample per 4x4 Y samples)
   };

 static const struct
@@ -343,16 +343,16 @@
   if (!codec->pix_fmts)
     return 0;

-  for (i = 0; codec->pix_fmts[i] != PIX_FMT_NONE; ++i)
+  for (i = 0; codec->pix_fmts[i] != AV_PIX_FMT_NONE; ++i)
     {
-    if (codec->pix_fmts[i] == PIX_FMT_YUV422P10_OR_DUMMY)
+    if (codec->pix_fmts[i] == AV_PIX_FMT_YUV422P10_OR_DUMMY)
       return 1;
     }

   return 0;
   }

-static enum PixelFormat lqt_ffmpeg_get_ffmpeg_colormodel(int id)
+static enum AVPixelFormat lqt_ffmpeg_get_ffmpeg_colormodel(int id)
   {
   int i;

@@ -361,10 +361,10 @@
     if(colormodels[i].lqt_id == id)
       return colormodels[i].ffmpeg_id;
     }
-  return PIX_FMT_NB;
+  return AV_PIX_FMT_NB;
   }

-static int lqt_ffmpeg_get_lqt_colormodel(enum PixelFormat id, int * exact)
+static int lqt_ffmpeg_get_lqt_colormodel(enum AVPixelFormat id, int * exact)
   {
   int i;

@@ -400,26 +400,26 @@
   codec->reinterpret_pix_fmt = codec->avctx->pix_fmt;

   /* First we try codec-specific colormodel matching. */
-  if(codec->decoder->id == CODEC_ID_DNXHD)
+  if(codec->decoder->id == AV_CODEC_ID_DNXHD)
     {
-    /* FFMpeg supports PIX_FMT_YUV422P and PIX_FMT_YUV422P10 for DNxHD, which
-       we sometimes interpret as PIX_FMT_YUVJ422P and PIX_FMT_YUVJ422P10. */
-    if (codec->avctx->pix_fmt == PIX_FMT_YUV422P || codec->avctx->pix_fmt == PIX_FMT_YUV422P10_OR_DUMMY)
+    /* FFMpeg supports AV_PIX_FMT_YUV422P and AV_PIX_FMT_YUV422P10 for DNxHD, which
+       we sometimes interpret as AV_PIX_FMT_YUVJ422P and AV_PIX_FMT_YUVJ422P10. */
+    if (codec->avctx->pix_fmt == AV_PIX_FMT_YUV422P || codec->avctx->pix_fmt == AV_PIX_FMT_YUV422P10_OR_DUMMY)
       {
-      int p10 = (codec->avctx->pix_fmt == PIX_FMT_YUV422P10_OR_DUMMY);
+      int p10 = (codec->avctx->pix_fmt == AV_PIX_FMT_YUV422P10_OR_DUMMY);
       *exact = 1;
       if (lqt_ffmpeg_get_avid_yuv_range(vtrack->track) == AVID_FULL_YUV_RANGE)
         {
         vtrack->stream_cmodel = p10 ? BC_YUVJ422P10 : BC_YUVJ422P;
-        codec->reinterpret_pix_fmt = p10 ? PIX_FMT_YUV422P10_OR_DUMMY : PIX_FMT_YUVJ422P;
-        // Note: reinterpret_pix_fmt should really be PIX_FMT_YUVJ422P10, except
+        codec->reinterpret_pix_fmt = p10 ? AV_PIX_FMT_YUV422P10_OR_DUMMY : AV_PIX_FMT_YUVJ422P;
+        // Note: reinterpret_pix_fmt should really be AV_PIX_FMT_YUVJ422P10, except
         // there is no such colormodel in FFMpeg. Fortunately, it's not a problem
         // in this case, as reinterpret_pix_fmt is only used when *exact == 0.
         }
       else
         {
         vtrack->stream_cmodel = p10 ? BC_YUV422P10 : BC_YUV422P;
-        codec->reinterpret_pix_fmt = p10 ? PIX_FMT_YUV422P10_OR_DUMMY : PIX_FMT_YUV422P;
+        codec->reinterpret_pix_fmt = p10 ? AV_PIX_FMT_YUV422P10_OR_DUMMY : AV_PIX_FMT_YUV422P;
         }
       return;
       }
@@ -438,16 +438,16 @@
   quicktime_ffmpeg_video_codec_t *codec = vtrack->codec->priv;
   codec->avctx->pix_fmt = lqt_ffmpeg_get_ffmpeg_colormodel(vtrack->stream_cmodel);

-  if (codec->encoder->id == CODEC_ID_DNXHD)
+  if (codec->encoder->id == AV_CODEC_ID_DNXHD)
     {
-    /* FFMpeg's DNxHD encoder only supports PIX_FMT_YUV422P and PIX_FMT_YUV422P10
-       and doesn't know anything about PIX_FMT_YUVJ422P and PIX_FMT_YUVJ422P10
+    /* FFMpeg's DNxHD encoder only supports AV_PIX_FMT_YUV422P and AV_PIX_FMT_YUV422P10
+       and doesn't know anything about AV_PIX_FMT_YUVJ422P and AV_PIX_FMT_YUVJ422P10
        (in fact, the latter doesn't even exist) */
-    codec->avctx->pix_fmt = PIX_FMT_YUV422P;
+    codec->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
     if (vtrack->stream_cmodel == BC_YUV422P10 || vtrack->stream_cmodel == BC_YUVJ422P10)
       {
       if (lqt_tenbit_dnxhd_supported(codec->encoder))
-        codec->avctx->pix_fmt = PIX_FMT_YUV422P10_OR_DUMMY;
+        codec->avctx->pix_fmt = AV_PIX_FMT_YUV422P10_OR_DUMMY;
       }
     }
   }
@@ -458,7 +458,7 @@
 /* From avcodec.h: */

 /*
- * PIX_FMT_RGBA32 is handled in an endian-specific manner. A RGBA
+ * AV_PIX_FMT_RGBA32 is handled in an endian-specific manner. A RGBA
  * color is put together as:
  *  (A << 24) | (R << 16) | (G << 8) | B
  * This is stored as BGRA on little endian CPU architectures and ARGB on
@@ -530,7 +530,7 @@
  */

 static void convert_image_decode(quicktime_ffmpeg_video_codec_t *codec,
-                                 AVFrame * in_frame, enum PixelFormat in_format,
+                                 AVFrame * in_frame, enum AVPixelFormat in_format,
                                  unsigned char ** out_frame, int out_format,
                                  int width, int height, int row_span, int row_span_uv)
   {
@@ -547,9 +547,9 @@
    *  RGBA format like in ffmpeg??
    */
 #if LIBAVUTIL_VERSION_INT < (50<<16)
-  if((in_format == PIX_FMT_RGBA32) && (out_format == BC_RGBA8888))
+  if((in_format == AV_PIX_FMT_RGBA32) && (out_format == BC_RGBA8888))
 #else
-    if((in_format == PIX_FMT_RGB32) && (out_format == BC_RGBA8888))
+    if((in_format == AV_PIX_FMT_RGB32) && (out_format == BC_RGBA8888))
 #endif
       {
       convert_image_decode_rgba(in_frame, out_frame, width, height, codec->y_offset);
@@ -728,13 +728,13 @@

     /* Set extradata: It's done differently for each codec */

-    if(codec->decoder->id == CODEC_ID_SVQ3)
+    if(codec->decoder->id == AV_CODEC_ID_SVQ3)
       {
       extradata       = trak->mdia.minf.stbl.stsd.table[0].table_raw + 4;
       extradata_size  = trak->mdia.minf.stbl.stsd.table[0].table_raw_size - 4;

       }
-    else if(codec->decoder->id == CODEC_ID_H264)
+    else if(codec->decoder->id == AV_CODEC_ID_H264)
       {
       user_atom = quicktime_stsd_get_user_atom(trak, "avcC", &user_atom_len);

@@ -753,7 +753,7 @@
         }

       }
-    else if(codec->decoder->id == CODEC_ID_MPEG4)
+    else if(codec->decoder->id == AV_CODEC_ID_MPEG4)
       {
       if(trak->mdia.minf.stbl.stsd.table[0].has_esds)
         {
@@ -781,7 +781,7 @@
     if(extradata)
       {
       codec->extradata =
-        calloc(1, extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+        calloc(1, extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
       memcpy(codec->extradata, extradata, extradata_size);
       codec->avctx->extradata_size = extradata_size;
       codec->avctx->extradata = codec->extradata;
@@ -829,7 +829,7 @@
     if(avcodec_open2(codec->avctx, codec->decoder, NULL) != 0)
       return -1;
 #endif
-    codec->frame = avcodec_alloc_frame();
+    codec->frame = av_frame_alloc();
     vtrack->stream_cmodel = LQT_COLORMODEL_NONE;
     codec->initialized = 1;
     }
@@ -929,10 +929,10 @@
 #ifdef HAVE_LIBSWSCALE

 #if LIBAVUTIL_VERSION_INT < (50<<16)
-      if(!((codec->avctx->pix_fmt == PIX_FMT_RGBA32) &&
+      if(!((codec->avctx->pix_fmt == AV_PIX_FMT_RGBA32) &&
            (vtrack->stream_cmodel == BC_RGBA8888)))
 #else
-        if(!((codec->avctx->pix_fmt == PIX_FMT_RGB32) &&
+        if(!((codec->avctx->pix_fmt == AV_PIX_FMT_RGB32) &&
              (vtrack->stream_cmodel == BC_RGBA8888)))
 #endif
           {
@@ -947,15 +947,15 @@
           }
 #endif
       }
-    if(codec->decoder->id == CODEC_ID_DVVIDEO)
+    if(codec->decoder->id == AV_CODEC_ID_DVVIDEO)
       {
       if(vtrack->stream_cmodel == BC_YUV420P)
         vtrack->chroma_placement = LQT_CHROMA_PLACEMENT_DVPAL;
       vtrack->interlace_mode = LQT_INTERLACE_BOTTOM_FIRST;
       vtrack->ci.id = LQT_COMPRESSION_DV;
       }
-    else if((codec->decoder->id == CODEC_ID_MPEG4) ||
-            (codec->decoder->id == CODEC_ID_H264))
+    else if((codec->decoder->id == AV_CODEC_ID_MPEG4) ||
+            (codec->decoder->id == AV_CODEC_ID_H264))
       {
       if(vtrack->stream_cmodel == BC_YUV420P)
         vtrack->chroma_placement = LQT_CHROMA_PLACEMENT_MPEG2;
@@ -1140,8 +1140,8 @@
   codec->avctx->qmin = 1;
   codec->avctx->qmax = 3;
   codec->avctx->rtp_payload_size = 1; // ??
-  codec->avctx->rc_buffer_aggressivity = 0.25;
-  codec->avctx->flags |= CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_LOW_DELAY;
+  av_dict_set(&codec->options, "rc_buf_aggressivity", "0.25", 0);
+  codec->avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT|AV_CODEC_FLAG_LOW_DELAY;

 #if (LIBAVCODEC_VERSION_MAJOR < 54)
   codec->avctx->flags2 |= CODEC_FLAG2_INTRA_VLC|CODEC_FLAG2_NON_LINEAR_QUANT;
@@ -1299,13 +1299,13 @@
     {
     if(vtrack->stream_cmodel == BC_YUV420P)
       {
-      if(codec->encoder->id == CODEC_ID_MPEG4)
+      if(codec->encoder->id == AV_CODEC_ID_MPEG4)
         {
         vtrack->chroma_placement = LQT_CHROMA_PLACEMENT_MPEG2;
         /* enable interlaced encoding */
         vtrack->interlace_mode = LQT_INTERLACE_NONE;
         }
-      else if(codec->encoder->id == CODEC_ID_DVVIDEO)
+      else if(codec->encoder->id == AV_CODEC_ID_DVVIDEO)
         {
         vtrack->chroma_placement = LQT_CHROMA_PLACEMENT_DVPAL;
         }
@@ -1318,7 +1318,7 @@

   if(!codec->initialized)
     {
-    codec->frame = avcodec_alloc_frame();
+    codec->frame = av_frame_alloc();

     /* time_base is 1/framerate for constant framerate */

@@ -1328,7 +1328,7 @@
     //          codec->avctx->time_base.den = 1;
     //          codec->avctx->time_base.num = lqt_video_time_scale(file, track);

-    if(codec->avctx->flags & CODEC_FLAG_QSCALE)
+    if(codec->avctx->flags & AV_CODEC_FLAG_QSCALE)
       codec->avctx->global_quality = codec->qscale;

     codec->avctx->width = width;
@@ -1340,11 +1340,11 @@
     codec->avctx->sample_aspect_ratio.num = pixel_width;
     codec->avctx->sample_aspect_ratio.den = pixel_height;
     /* Use global headers for mp4v */
-    if(codec->encoder->id == CODEC_ID_MPEG4)
+    if(codec->encoder->id == AV_CODEC_ID_MPEG4)
       {
       if(!(file->file_type & (LQT_FILE_AVI|LQT_FILE_AVI_ODML)))
         {
-        codec->avctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
+        codec->avctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
         codec->write_global_header = 1;
         }

@@ -1360,16 +1360,16 @@
         {
         lqt_log(file, LQT_LOG_INFO, LOG_DOMAIN, "Enabling interlaced encoding");
         codec->avctx->flags |=
-          (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN);
+          (AV_CODEC_FLAG_INTERLACED_DCT|AV_CODEC_FLAG_INTERLACED_ME|AV_CODEC_FLAG_ALT_SCAN);
         }
 #endif
       }
-    else if((codec->encoder->id == CODEC_ID_MSMPEG4V3) && (trak->strl) &&
+    else if((codec->encoder->id == AV_CODEC_ID_MSMPEG4V3) && (trak->strl) &&
             !strncmp(trak->strl->strf.bh.biCompression, "DIV3", 4))
       {
       strncpy(trak->strl->strh.fccHandler, "div3", 4);
       }
-    else if((codec->encoder->id == CODEC_ID_H263) &&
+    else if((codec->encoder->id == AV_CODEC_ID_H263) &&
             (file->file_type & (LQT_FILE_MP4|LQT_FILE_3GP)))
       {
       uint8_t d263_data[] =
@@ -1383,34 +1383,34 @@
       strncpy(trak->mdia.minf.stbl.stsd.table[0].format,
               "s263", 4);
       }
-    else if(codec->encoder->id == CODEC_ID_FFVHUFF)
+    else if(codec->encoder->id == AV_CODEC_ID_FFVHUFF)
       {
       if(!(file->file_type & (LQT_FILE_AVI|LQT_FILE_AVI_ODML)))
         {
-        codec->avctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
+        codec->avctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
         codec->write_global_header = 1;
         }
       }
-    else if(codec->encoder->id == CODEC_ID_QTRLE)
+    else if(codec->encoder->id == AV_CODEC_ID_QTRLE)
       {
       if(vtrack->stream_cmodel == BC_RGBA8888)
         {
         /* Libquicktime doesn't natively support a color model equivalent
-           to PIX_FMT_ARGB, which is required for QTRLE with alpha channel.
+           to AV_PIX_FMT_ARGB, which is required for QTRLE with alpha channel.
            So, we use BC_RGBA8888 and do ad hoc conversion below. */
-        codec->avctx->pix_fmt = PIX_FMT_ARGB;
+        codec->avctx->pix_fmt = AV_PIX_FMT_ARGB;
         vtrack->track->mdia.minf.stbl.stsd.table[0].depth = 32;
         }
       }
-    else if(codec->encoder->id == CODEC_ID_DVVIDEO)
+    else if(codec->encoder->id == AV_CODEC_ID_DVVIDEO)
       {
       set_dv_fourcc(width, height, vtrack->stream_cmodel, trak);
       }
-    else if(codec->encoder->id == CODEC_ID_DNXHD)
+    else if(codec->encoder->id == AV_CODEC_ID_DNXHD)
       {
       if(vtrack->interlace_mode != LQT_INTERLACE_NONE)
         {
-        codec->avctx->flags |= CODEC_FLAG_INTERLACED_DCT;
+        codec->avctx->flags |= AV_CODEC_FLAG_INTERLACED_DCT;
         }
       }
     else if(codec->is_imx)
@@ -1422,7 +1422,7 @@
       if(codec->pass == 1)
         {
         codec->stats_file = fopen(codec->stats_filename, "w");
-        codec->avctx->flags |= CODEC_FLAG_PASS1;
+        codec->avctx->flags |= AV_CODEC_FLAG_PASS1;
         }
       else if(codec->pass == codec->total_passes)
         {
@@ -1438,7 +1438,7 @@
         fclose(codec->stats_file);
         codec->stats_file = (FILE*)0;

-        codec->avctx->flags |= CODEC_FLAG_PASS2;
+        codec->avctx->flags |= AV_CODEC_FLAG_PASS2;
         }
       }
     /* Open codec */
@@ -1467,7 +1467,7 @@
     }
   //        codec->lqt_colormodel = ffmepg_2_lqt(codec->com.ffcodec_enc);

-  if(codec->y_offset != 0 || codec->avctx->pix_fmt == PIX_FMT_ARGB)
+  if(codec->y_offset != 0 || codec->avctx->pix_fmt == AV_PIX_FMT_ARGB)
     {
     if(!codec->tmp_rows)
       {
@@ -1492,7 +1492,7 @@
                         vtrack->stream_cmodel,
                         0, 0, 0, codec->y_offset);
       }
-    else if(codec->avctx->pix_fmt == PIX_FMT_ARGB)
+    else if(codec->avctx->pix_fmt == AV_PIX_FMT_ARGB)
       {
       convert_rgba_to_argb(row_pointers[0], vtrack->stream_row_span,
                            codec->tmp_rows[0], codec->tmp_row_span,
@@ -1516,7 +1516,7 @@
     }

   codec->frame->pts = vtrack->timestamp;
-  if(codec->avctx->flags & CODEC_FLAG_QSCALE)
+  if(codec->avctx->flags & AV_CODEC_FLAG_QSCALE)
     codec->frame->quality = codec->qscale;
 #ifdef DO_INTERLACE
   if(vtrack->interlace_mode != LQT_INTERLACE_NONE)
@@ -1558,12 +1558,12 @@

 #endif

-  if(!was_initialized && codec->encoder->id == CODEC_ID_DNXHD)
+  if(!was_initialized && codec->encoder->id == AV_CODEC_ID_DNXHD)
     setup_avid_atoms(file, vtrack, codec->buffer, bytes_encoded);

   if(bytes_encoded)
     {
-    if (pts == AV_NOPTS_VALUE || (codec->encoder->id == CODEC_ID_DNXHD && pts == 0))
+    if (pts == AV_NOPTS_VALUE || (codec->encoder->id == AV_CODEC_ID_DNXHD && pts == 0))
       {
       /* Some codecs don't bother generating presentation timestamps.
          FFMpeg's DNxHD encoder doesn't even bother to set it to AV_NOPTS_VALUE. */
@@ -1590,17 +1590,16 @@

   if(codec->write_global_header && !codec->global_header_written)
     {
-    if(codec->encoder->id == CODEC_ID_FFVHUFF)
+    if(codec->encoder->id == AV_CODEC_ID_FFVHUFF)
       {
       quicktime_user_atoms_add_atom(&trak->mdia.minf.stbl.stsd.table[0].user_atoms,
                                     "glbl",
                                     codec->avctx->extradata, codec->avctx->extradata_size );
       }
-    else if(codec->encoder->id == CODEC_ID_MPEG4)
+    else if(codec->encoder->id == AV_CODEC_ID_MPEG4)
       {
       int advanced = 0;
-      if(codec->avctx->max_b_frames ||
-         (codec->avctx->flags & (CODEC_FLAG_QPEL|CODEC_FLAG_GMC)))
+      if(codec->avctx->max_b_frames)
         advanced = 1;

       setup_header_mpeg4(file, track, codec->avctx->extradata,
@@ -1903,18 +1902,18 @@
     codec_base->encode_video = lqt_ffmpeg_encode_video;
     codec_base->set_pass = set_pass_ffmpeg;

-    if(encoder->id == CODEC_ID_MPEG4)
+    if(encoder->id == AV_CODEC_ID_MPEG4)
       {
       codec_base->writes_compressed = writes_compressed_mpeg4;
       codec_base->init_compressed   = init_compressed_mpeg4;
       codec_base->write_packet = write_packet_mpeg4;
       }
-    else if(encoder->id == CODEC_ID_MPEG2VIDEO)
+    else if(encoder->id == AV_CODEC_ID_MPEG2VIDEO)
       {
       codec_base->writes_compressed = writes_compressed_imx;
       codec_base->init_compressed   = init_compressed_imx;
       }
-    else if(encoder->id == CODEC_ID_DVVIDEO)
+    else if(encoder->id == AV_CODEC_ID_DVVIDEO)
       {
       codec_base->init_compressed = init_compressed_dv;
       }
@@ -1922,7 +1921,7 @@
     }
   if(decoder)
     {
-    if(decoder->id == CODEC_ID_H264)
+    if(decoder->id == AV_CODEC_ID_H264)
       codec_base->read_packet = read_packet_h264;
     codec_base->decode_video = lqt_ffmpeg_decode_video;
     }
diff -Naur a/src/util.c b/src/util.c
--- a/src/util.c	2011-01-07 14:40:47.000000000 +0000
+++ b/src/util.c	2018-05-11 23:11:04.367991499 +0100
@@ -340,9 +340,14 @@

 void quicktime_read_pascal(quicktime_t *file, char *data)
 {
-	char len = quicktime_read_char(file);
-	quicktime_read_data(file, (uint8_t*)data, len);
-	data[(int)len] = 0;
+	int len = quicktime_read_char(file);
+	if ((len > 0) && (len < 256)) {
+          /* data[] is expected to be 256 bytes long */
+          quicktime_read_data(file, (uint8_t*)data, len);
+          data[len] = 0;
+        } else {
+          data[0] = 0;
+        }
 }

 void quicktime_write_pascal(quicktime_t *file, char *data)


code:
https://github.com/libssh2/libssh2/commit/dedcbd106f8e52d5586b0205bc7677e4c9868f9c.patch

From dedcbd106f8e52d5586b0205bc7677e4c9868f9c Mon Sep 17 00:00:00 2001
From: Will Cosgrove <will@panic.com>
Date: Fri, 30 Aug 2019 09:57:38 -0700
Subject: [PATCH] packet.c: improve message parsing (#402)

* packet.c: improve parsing of packets

file: packet.c

notes:
Use _libssh2_get_string API in SSH_MSG_DEBUG/SSH_MSG_DISCONNECT. Additional uint32 bounds check in SSH_MSG_GLOBAL_REQUEST.
---
 src/packet.c | 68 ++++++++++++++++++++++------------------------------
 1 file changed, 29 insertions(+), 39 deletions(-)

diff --git a/src/packet.c b/src/packet.c
index 38ab62944..2e01bfc5d 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -419,8 +419,8 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
                     size_t datalen, int macstate)
 {
     int rc = 0;
-    char *message = NULL;
-    char *language = NULL;
+    unsigned char *message = NULL;
+    unsigned char *language = NULL;
     size_t message_len = 0;
     size_t language_len = 0;
     LIBSSH2_CHANNEL *channelp = NULL;
@@ -472,33 +472,23 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,

         case SSH_MSG_DISCONNECT:
             if(datalen >= 5) {
-                size_t reason = _libssh2_ntohu32(data + 1);
+                uint32_t reason = 0;
+                struct string_buf buf;
+                buf.data = (unsigned char *)data;
+                buf.dataptr = buf.data;
+                buf.len = datalen;
+                buf.dataptr++; /* advance past type */

-                if(datalen >= 9) {
-                    message_len = _libssh2_ntohu32(data + 5);
+                _libssh2_get_u32(&buf, &reason);
+                _libssh2_get_string(&buf, &message, &message_len);
+                _libssh2_get_string(&buf, &language, &language_len);

-                    if(message_len < datalen-13) {
-                        /* 9 = packet_type(1) + reason(4) + message_len(4) */
-                        message = (char *) data + 9;
-
-                        language_len =
-                            _libssh2_ntohu32(data + 9 + message_len);
-                        language = (char *) data + 9 + message_len + 4;
-
-                        if(language_len > (datalen-13-message_len)) {
-                            /* bad input, clear info */
-                            language = message = NULL;
-                            language_len = message_len = 0;
-                        }
-                    }
-                    else
-                        /* bad size, clear it */
-                        message_len = 0;
-                }
                 if(session->ssh_msg_disconnect) {
-                    LIBSSH2_DISCONNECT(session, reason, message,
-                                       message_len, language, language_len);
+                    LIBSSH2_DISCONNECT(session, reason, (const char *)message,
+                                       message_len, (const char *)language,
+                                       language_len);
                 }
+
                 _libssh2_debug(session, LIBSSH2_TRACE_TRANS,
                                "Disconnect(%d): %s(%s)", reason,
                                message, language);
@@ -539,24 +529,24 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
                 int always_display = data[1];

                 if(datalen >= 6) {
-                    message_len = _libssh2_ntohu32(data + 2);
-
-                    if(message_len <= (datalen - 10)) {
-                        /* 6 = packet_type(1) + display(1) + message_len(4) */
-                        message = (char *) data + 6;
-                        language_len = _libssh2_ntohu32(data + 6 +
-                                                        message_len);
-
-                        if(language_len <= (datalen - 10 - message_len))
-                            language = (char *) data + 10 + message_len;
-                    }
+                    struct string_buf buf;
+                    buf.data = (unsigned char *)data;
+                    buf.dataptr = buf.data;
+                    buf.len = datalen;
+                    buf.dataptr += 2; /* advance past type & always display */
+
+                    _libssh2_get_string(&buf, &message, &message_len);
+                    _libssh2_get_string(&buf, &language, &language_len);
                 }

                 if(session->ssh_msg_debug) {
-                    LIBSSH2_DEBUG(session, always_display, message,
-                                  message_len, language, language_len);
+                    LIBSSH2_DEBUG(session, always_display,
+                                  (const char *)message,
+                                  message_len, (const char *)language,
+                                  language_len);
                 }
             }
+
             /*
              * _libssh2_debug will actually truncate this for us so
              * that it's not an inordinate about of data
@@ -579,7 +569,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
                 uint32_t len = 0;
                 unsigned char want_reply = 0;
                 len = _libssh2_ntohu32(data + 1);
-                if(datalen >= (6 + len)) {
+                if((len <= (UINT_MAX - 6)) && (datalen >= (6 + len))) {
                     want_reply = data[5 + len];
                     _libssh2_debug(session,
                                    LIBSSH2_TRACE_CONN,


code:
Author: Raphael Geissert <geissert@debian.org>
Bug-Debian: https://bugs.debian.org/731860
Description: Avoid directory traversal when extracting archives
 by skipping over leading slashes and any prefix containing ".." components.
Forwarded: yes

--- a/lib/decode.c
+++ b/lib/decode.c
@@ -22,6 +22,36 @@
 #endif

+char *
+safer_name_suffix (char const *file_name)
+{
+	char const *p, *t;
+	p = t = file_name;
+	while (*p == '/') t = ++p;
+	while (*p)
+	{
+		while (p[0] == '.' && p[0] == p[1] && p[2] == '/')
+		{
+			p += 3;
+			t = p;
+		}
+		/* advance pointer past the next slash */
+		while (*p && (p++)[0] != '/');
+	}
+
+	if (!*t)
+	{
+		t = ".";
+	}
+
+	if (t != file_name)
+	{
+		/* TODO: warn somehow that the path was modified */
+	}
+	return (char*)t;
+}
+
+
 /* determine full path name */
 char *
 th_get_pathname(TAR *t)
@@ -29,17 +59,17 @@ th_get_pathname(TAR *t)
 	static char filename[MAXPATHLEN];

 	if (t->th_buf.gnu_longname)
-		return t->th_buf.gnu_longname;
+		return safer_name_suffix(t->th_buf.gnu_longname);

 	if (t->th_buf.prefix[0] != '\0')
 	{
 		snprintf(filename, sizeof(filename), "%.155s/%.100s",
 			 t->th_buf.prefix, t->th_buf.name);
-		return filename;
+		return safer_name_suffix(filename);
 	}

 	snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
-	return filename;
+	return safer_name_suffix(filename);
 }

--- a/lib/extract.c
+++ b/lib/extract.c
@@ -298,14 +298,14 @@ tar_extract_hardlink(TAR * t, char *real
 	if (mkdirhier(dirname(filename)) == -1)
 		return -1;
 	libtar_hashptr_reset(&hp);
-	if (libtar_hash_getkey(t->h, &hp, th_get_linkname(t),
+	if (libtar_hash_getkey(t->h, &hp, safer_name_suffix(th_get_linkname(t)),
 			       (libtar_matchfunc_t)libtar_str_match) != 0)
 	{
 		lnp = (char *)libtar_hashptr_data(&hp);
 		linktgt = &lnp[strlen(lnp) + 1];
 	}
 	else
-		linktgt = th_get_linkname(t);
+		linktgt = safer_name_suffix(th_get_linkname(t));

 #ifdef DEBUG
 	printf("  ==> extracting: %s (link to %s)\n", filename, linktgt);
@@ -343,9 +343,9 @@ tar_extract_symlink(TAR *t, char *realna

 #ifdef DEBUG
 	printf("  ==> extracting: %s (symlink to %s)\n",
-	       filename, th_get_linkname(t));
+	       filename, safer_name_suffix(th_get_linkname(t)));
 #endif
-	if (symlink(th_get_linkname(t), filename) == -1)
+	if (symlink(safer_name_suffix(th_get_linkname(t)), filename) == -1)
 	{
 #ifdef DEBUG
 		perror("symlink()");
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -15,3 +15,4 @@

 #include <libtar.h>

+char* safer_name_suffix(char const*);
--- a/lib/output.c
+++ b/lib/output.c
@@ -123,9 +123,9 @@ th_print_long_ls(TAR *t)
 		else
 			printf(" link to ");
 		if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL)
-			printf("%s", t->th_buf.gnu_longlink);
+			printf("%s", safer_name_suffix(t->th_buf.gnu_longlink));
 		else
-			printf("%.100s", t->th_buf.linkname);
+			printf("%.100s", safer_name_suffix(t->th_buf.linkname));
 	}

 	putchar('\n');


code:
Copied from Debian.

Description: Disable SSE2 code on i386
 This patch is not complete. A high-graded solution may use automatic switching
 between SSE2 and C++ implementations based on the results of runtime checks.
 The webrtc code already provides for one of them inside its GetCPUInfo function.
Bug-Debian: https://bugs.debian.org/892823
Author: Nicholas Guriev <guriev-ns@ya.ru>
Last-Update: Tue, 29 Jan 2019 23:26:38 +0300

--- a/libtgvoip.gyp
+++ b/libtgvoip.gyp
@@ -871,11 +871,7 @@
                 'WEBRTC_POSIX',
               ],
               'conditions': [
-                [ '"<!(uname -m)" == "i686"', {
-                  'cflags_cc': [
-                    '-msse2',
-                  ],
-                }], ['"<!(uname -s)" == "Linux"', {
+                [ '"<!(uname -s)" == "Linux"', {
                   'defines': [
                     'WEBRTC_LINUX',
                   ],
--- a/webrtc_dsp/rtc_base/system/arch.h
+++ b/webrtc_dsp/rtc_base/system/arch.h
@@ -28,7 +28,10 @@
 #define WEBRTC_ARCH_64_BITS
 #define WEBRTC_ARCH_LITTLE_ENDIAN
 #elif defined(_M_IX86) || defined(__i386__)
+#if defined(__SSE2__)
+// This macro is mostly used to detect SSE2 extension.
 #define WEBRTC_ARCH_X86_FAMILY
+#endif
 #define WEBRTC_ARCH_X86
 #define WEBRTC_ARCH_32_BITS
 #define WEBRTC_ARCH_LITTLE_ENDIAN
--- a/webrtc_dsp/typedefs.h
+++ b/webrtc_dsp/typedefs.h
@@ -28,7 +28,10 @@
 #define WEBRTC_ARCH_64_BITS
 #define WEBRTC_ARCH_LITTLE_ENDIAN
 #elif defined(_M_IX86) || defined(__i386__)
+#if defined(__SSE2__)
+// This macro is mostly used to detect SSE2 extension.
 #define WEBRTC_ARCH_X86_FAMILY
+#endif
 #define WEBRTC_ARCH_X86
 #define WEBRTC_ARCH_32_BITS
 #define WEBRTC_ARCH_LITTLE_ENDIAN