@@ -, +, @@ --- libaudiofile/modules/IMA.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/libaudiofile/modules/IMA.cpp +++ a/libaudiofile/modules/IMA.cpp @@ -169,7 +169,7 @@ int IMA::decodeBlockWAVE(const uint8_t *encoded, int16_t *decoded) if (encoded[1] & 0x80) m_adpcmState[c].previousValue -= 0x10000; - m_adpcmState[c].index = encoded[2]; + m_adpcmState[c].index = clamp(encoded[2], 0, 88); *decoded++ = m_adpcmState[c].previousValue; @@ -210,7 +210,7 @@ int IMA::decodeBlockQT(const uint8_t *encoded, int16_t *decoded) predictor -= 0x10000; state.previousValue = clamp(predictor, MIN_INT16, MAX_INT16); - state.index = encoded[1] & 0x7f; + state.index = clamp(encoded[1] & 0x7f, 0, 88); encoded += 2; for (int n=0; n>=1; + ++position; + } + return position; +} + +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +int multiplyCheckOverflow(int a, int b, int *result) +{ +#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow)) + return __builtin_mul_overflow(a, b, result); +#else + if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits + return true; + *result = a * b; + return false; +#endif +} + int main (int argc, char **argv) { if (argc == 2) @@ -323,8 +350,11 @@ bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid) { int frameSize = afGetVirtualFrameSize(infile, trackid, 1); - const int kBufferFrameCount = 65536; - void *buffer = malloc(kBufferFrameCount * frameSize); + int kBufferFrameCount = 65536; + int bufferSize; + while (multiplyCheckOverflow(kBufferFrameCount, frameSize, &bufferSize)) + kBufferFrameCount /= 2; + void *buffer = malloc(bufferSize); AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK); AFframecount totalFramesWritten = 0; --- libaudiofile/WAVE.cpp | 2 ++ 1 file changed, 2 insertions(+) --- a/libaudiofile/WAVE.cpp +++ a/libaudiofile/WAVE.cpp @@ -326,6 +326,7 @@ status WAVEFile::parseFormat(const Tag &id, uint32_t size) { _af_error(AF_BAD_NOT_IMPLEMENTED, "IMA ADPCM compression supports only 4 bits per sample"); + return AF_FAIL; } int bytesPerBlock = (samplesPerBlock + 14) / 8 * 4 * channelCount; @@ -333,6 +334,7 @@ status WAVEFile::parseFormat(const Tag &id, uint32_t size) { _af_error(AF_BAD_CODEC_CONFIG, "Invalid samples per block for IMA ADPCM compression"); + return AF_FAIL; } track->f.sampleWidth = 16; --- libaudiofile/modules/BlockCodec.cpp | 5 +-- libaudiofile/modules/MSADPCM.cpp | 47 ++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) --- a/libaudiofile/modules/BlockCodec.cpp +++ a/libaudiofile/modules/BlockCodec.cpp @@ -52,8 +52,9 @@ void BlockCodec::runPull() // Decompress into m_outChunk. for (int i=0; i(m_inChunk->buffer) + i * m_bytesPerPacket, - static_cast(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount); + if (decodeBlock(static_cast(m_inChunk->buffer) + i * m_bytesPerPacket, + static_cast(m_outChunk->buffer) + i * m_framesPerPacket * m_track->f.channelCount)==0) + break; framesRead += m_framesPerPacket; } --- a/libaudiofile/modules/MSADPCM.cpp +++ a/libaudiofile/modules/MSADPCM.cpp @@ -101,24 +101,60 @@ static const int16_t adaptationTable[] = 768, 614, 512, 409, 307, 230, 230, 230 }; +int firstBitSet(int x) +{ + int position=0; + while (x!=0) + { + x>>=1; + ++position; + } + return position; +} + +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +int multiplyCheckOverflow(int a, int b, int *result) +{ +#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow)) + return __builtin_mul_overflow(a, b, result); +#else + if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits + return true; + *result = a * b; + return false; +#endif +} + + // Compute a linear PCM value from the given differential coded value. static int16_t decodeSample(ms_adpcm_state &state, - uint8_t code, const int16_t *coefficient) + uint8_t code, const int16_t *coefficient, bool *ok=NULL) { int linearSample = (state.sample1 * coefficient[0] + state.sample2 * coefficient[1]) >> 8; + int delta; linearSample += ((code & 0x08) ? (code - 0x10) : code) * state.delta; linearSample = clamp(linearSample, MIN_INT16, MAX_INT16); - int delta = (state.delta * adaptationTable[code]) >> 8; + if (multiplyCheckOverflow(state.delta, adaptationTable[code], &delta)) + { + if (ok) *ok=false; + _af_error(AF_BAD_COMPRESSION, "Error decoding sample"); + return 0; + } + delta >>= 8; if (delta < 16) delta = 16; state.delta = delta; state.sample2 = state.sample1; state.sample1 = linearSample; + if (ok) *ok=true; return static_cast(linearSample); } @@ -212,13 +248,16 @@ int MSADPCM::decodeBlock(const uint8_t *encoded, int16_t *decoded) { uint8_t code; int16_t newSample; + bool ok; code = *encoded >> 4; - newSample = decodeSample(*state[0], code, coefficient[0]); + newSample = decodeSample(*state[0], code, coefficient[0], &ok); + if (!ok) return 0; *decoded++ = newSample; code = *encoded & 0x0f; - newSample = decodeSample(*state[1], code, coefficient[1]); + newSample = decodeSample(*state[1], code, coefficient[1], &ok); + if (!ok) return 0; *decoded++ = newSample; encoded++; --- libaudiofile/WAVE.cpp | 6 ++++++ 1 file changed, 6 insertions(+) --- a/libaudiofile/WAVE.cpp +++ a/libaudiofile/WAVE.cpp @@ -281,6 +281,12 @@ status WAVEFile::parseFormat(const Tag &id, uint32_t size) /* numCoefficients should be at least 7. */ assert(numCoefficients >= 7 && numCoefficients <= 255); + if (numCoefficients < 7 || numCoefficients > 255) + { + _af_error(AF_BAD_HEADER, + "Bad number of coefficients"); + return AF_FAIL; + } m_msadpcmNumCoefficients = numCoefficients;