patch-libaudiofile_modules_MSADPCM_cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --- audiofile-0.3.6.orig/libaudiofile/modules/MSADPCM.cpp 2013-03-06 06:30:03.000000000 +0100
  2. +++ audiofile-0.3.6/libaudiofile/modules/MSADPCM.cpp 2017-04-26 02:15:29.573388050 +0200
  3. @@ -101,24 +101,60 @@ static const int16_t adaptationTable[] =
  4. 768, 614, 512, 409, 307, 230, 230, 230
  5. };
  6. +int firstBitSet(int x)
  7. +{
  8. + int position=0;
  9. + while (x!=0)
  10. + {
  11. + x>>=1;
  12. + ++position;
  13. + }
  14. + return position;
  15. +}
  16. +
  17. +#ifndef __has_builtin
  18. +#define __has_builtin(x) 0
  19. +#endif
  20. +
  21. +int multiplyCheckOverflow(int a, int b, int *result)
  22. +{
  23. +#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
  24. + return __builtin_mul_overflow(a, b, result);
  25. +#else
  26. + if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
  27. + return true;
  28. + *result = a * b;
  29. + return false;
  30. +#endif
  31. +}
  32. +
  33. +
  34. // Compute a linear PCM value from the given differential coded value.
  35. static int16_t decodeSample(ms_adpcm_state &state,
  36. - uint8_t code, const int16_t *coefficient)
  37. + uint8_t code, const int16_t *coefficient, bool *ok=NULL)
  38. {
  39. int linearSample = (state.sample1 * coefficient[0] +
  40. state.sample2 * coefficient[1]) >> 8;
  41. + int delta;
  42. linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta;
  43. linearSample = clamp(linearSample, MIN_INT16, MAX_INT16);
  44. - int delta = (state.delta * adaptationTable[code]) >> 8;
  45. + if (multiplyCheckOverflow(state.delta, adaptationTable[code], &delta))
  46. + {
  47. + if (ok) *ok=false;
  48. + _af_error(AF_BAD_COMPRESSION, "Error decoding sample");
  49. + return 0;
  50. + }
  51. + delta >>= 8;
  52. if (delta < 16)
  53. delta = 16;
  54. state.delta = delta;
  55. state.sample2 = state.sample1;
  56. state.sample1 = linearSample;
  57. + if (ok) *ok=true;
  58. return static_cast<int16_t>(linearSample);
  59. }
  60. @@ -212,13 +248,16 @@ int MSADPCM::decodeBlock(const uint8_t *
  61. {
  62. uint8_t code;
  63. int16_t newSample;
  64. + bool ok;
  65. code = *encoded >> 4;
  66. - newSample = decodeSample(*state[0], code, coefficient[0]);
  67. + newSample = decodeSample(*state[0], code, coefficient[0], &ok);
  68. + if (!ok) return 0;
  69. *decoded++ = newSample;
  70. code = *encoded & 0x0f;
  71. - newSample = decodeSample(*state[1], code, coefficient[1]);
  72. + newSample = decodeSample(*state[1], code, coefficient[1], &ok);
  73. + if (!ok) return 0;
  74. *decoded++ = newSample;
  75. encoded++;