Build fixes (gcc4).

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30136 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2009-04-12 10:08:22 +00:00
parent 1e855c376c
commit 0b8b543af8
7 changed files with 134 additions and 120 deletions
@@ -41,9 +41,9 @@ const char* lib_monkeys_audio_components()
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const char* lib_monkeys_audio_copyright() const char* lib_monkeys_audio_copyright()
{ {
static string saCright; static std::string saCright;
saCright = string(gCright)+"\n"+gOriginal; saCright = std::string(gCright)+"\n"+gOriginal;
return saCright.c_str(); return saCright.c_str();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -1,6 +1,8 @@
#include "All.h" #include "All.h"
#include "APEDecompress.h" #include "APEDecompress.h"
#include <algorithm>
#include "APEInfo.h" #include "APEInfo.h"
#include "Prepare.h" #include "Prepare.h"
#include "UnBitArray.h" #include "UnBitArray.h"
@@ -35,14 +37,17 @@ CAPEDecompress::CAPEDecompress(int * pErrorCode, CAPEInfo * pAPEInfo, int nStart
m_bErrorDecodingCurrentFrame = FALSE; m_bErrorDecodingCurrentFrame = FALSE;
// set the "real" start and finish blocks // set the "real" start and finish blocks
m_nStartBlock = (nStartBlock < 0) ? 0 : min(nStartBlock, GetInfo(APE_INFO_TOTAL_BLOCKS)); m_nStartBlock = (nStartBlock < 0)
m_nFinishBlock = (nFinishBlock < 0) ? GetInfo(APE_INFO_TOTAL_BLOCKS) : min(nFinishBlock, GetInfo(APE_INFO_TOTAL_BLOCKS)); ? 0 : std::min(nStartBlock, GetInfo(APE_INFO_TOTAL_BLOCKS));
m_nFinishBlock = (nFinishBlock < 0)
? GetInfo(APE_INFO_TOTAL_BLOCKS)
: std::min(nFinishBlock, GetInfo(APE_INFO_TOTAL_BLOCKS));
m_bIsRanged = (m_nStartBlock != 0) || (m_nFinishBlock != GetInfo(APE_INFO_TOTAL_BLOCKS)); m_bIsRanged = (m_nStartBlock != 0) || (m_nFinishBlock != GetInfo(APE_INFO_TOTAL_BLOCKS));
} }
CAPEDecompress::~CAPEDecompress() CAPEDecompress::~CAPEDecompress()
{ {
} }
int CAPEDecompress::InitializeDecompressor() int CAPEDecompress::InitializeDecompressor()
@@ -56,7 +61,7 @@ int CAPEDecompress::InitializeDecompressor()
// create a frame buffer // create a frame buffer
m_cbFrameBuffer.CreateBuffer((GetInfo(APE_INFO_BLOCKS_PER_FRAME) + DECODE_BLOCK_SIZE) * m_nBlockAlign, m_nBlockAlign * 64); m_cbFrameBuffer.CreateBuffer((GetInfo(APE_INFO_BLOCKS_PER_FRAME) + DECODE_BLOCK_SIZE) * m_nBlockAlign, m_nBlockAlign * 64);
// create decoding components // create decoding components
m_spUnBitArray.Assign((CUnBitArrayBase *) CreateUnBitArray(this, GetInfo(APE_INFO_FILE_VERSION))); m_spUnBitArray.Assign((CUnBitArrayBase *) CreateUnBitArray(this, GetInfo(APE_INFO_FILE_VERSION)));
@@ -70,7 +75,7 @@ int CAPEDecompress::InitializeDecompressor()
m_spNewPredictorX.Assign(new CPredictorDecompressNormal3930to3950(GetInfo(APE_INFO_COMPRESSION_LEVEL), GetInfo(APE_INFO_FILE_VERSION))); m_spNewPredictorX.Assign(new CPredictorDecompressNormal3930to3950(GetInfo(APE_INFO_COMPRESSION_LEVEL), GetInfo(APE_INFO_FILE_VERSION)));
m_spNewPredictorY.Assign(new CPredictorDecompressNormal3930to3950(GetInfo(APE_INFO_COMPRESSION_LEVEL), GetInfo(APE_INFO_FILE_VERSION))); m_spNewPredictorY.Assign(new CPredictorDecompressNormal3930to3950(GetInfo(APE_INFO_COMPRESSION_LEVEL), GetInfo(APE_INFO_FILE_VERSION)));
} }
// seek to the beginning // seek to the beginning
return Seek(0); return Seek(0);
} }
@@ -79,14 +84,14 @@ int CAPEDecompress::GetData(char * pBuffer, int nBlocks, int * pBlocksRetrieved)
{ {
int nRetVal = ERROR_SUCCESS; int nRetVal = ERROR_SUCCESS;
if (pBlocksRetrieved) *pBlocksRetrieved = 0; if (pBlocksRetrieved) *pBlocksRetrieved = 0;
// make sure we're initialized // make sure we're initialized
RETURN_ON_ERROR(InitializeDecompressor()) RETURN_ON_ERROR(InitializeDecompressor())
// cap // cap
int nBlocksUntilFinish = m_nFinishBlock - m_nCurrentBlock; int nBlocksUntilFinish = m_nFinishBlock - m_nCurrentBlock;
const int nBlocksToRetrieve = min(nBlocks, nBlocksUntilFinish); const int nBlocksToRetrieve = std::min(nBlocks, nBlocksUntilFinish);
// get the data // get the data
unsigned char * pOutputBuffer = (unsigned char *) pBuffer; unsigned char * pOutputBuffer = (unsigned char *) pBuffer;
int nBlocksLeft = nBlocksToRetrieve; int nBlocksThisPass = 1; int nBlocksLeft = nBlocksToRetrieve; int nBlocksThisPass = 1;
@@ -99,7 +104,7 @@ int CAPEDecompress::GetData(char * pBuffer, int nBlocks, int * pBlocksRetrieved)
// analyze how much to remove from the buffer // analyze how much to remove from the buffer
const int nFrameBufferBlocks = m_nFrameBufferFinishedBlocks; const int nFrameBufferBlocks = m_nFrameBufferFinishedBlocks;
nBlocksThisPass = min(nBlocksLeft, nFrameBufferBlocks); nBlocksThisPass = std::min(nBlocksLeft, nFrameBufferBlocks);
// remove as much as possible // remove as much as possible
if (nBlocksThisPass > 0) if (nBlocksThisPass > 0)
@@ -127,7 +132,7 @@ int CAPEDecompress::Seek(int nBlockOffset)
// use the offset // use the offset
nBlockOffset += m_nStartBlock; nBlockOffset += m_nStartBlock;
// cap (to prevent seeking too far) // cap (to prevent seeking too far)
if (nBlockOffset >= m_nFinishBlock) if (nBlockOffset >= m_nFinishBlock)
nBlockOffset = m_nFinishBlock - 1; nBlockOffset = m_nFinishBlock - 1;
@@ -138,7 +143,7 @@ int CAPEDecompress::Seek(int nBlockOffset)
int nBaseFrame = nBlockOffset / GetInfo(APE_INFO_BLOCKS_PER_FRAME); int nBaseFrame = nBlockOffset / GetInfo(APE_INFO_BLOCKS_PER_FRAME);
int nBlocksToSkip = nBlockOffset % GetInfo(APE_INFO_BLOCKS_PER_FRAME); int nBlocksToSkip = nBlockOffset % GetInfo(APE_INFO_BLOCKS_PER_FRAME);
int nBytesToSkip = nBlocksToSkip * m_nBlockAlign; int nBytesToSkip = nBlocksToSkip * m_nBlockAlign;
m_nCurrentBlock = nBaseFrame * GetInfo(APE_INFO_BLOCKS_PER_FRAME); m_nCurrentBlock = nBaseFrame * GetInfo(APE_INFO_BLOCKS_PER_FRAME);
m_nCurrentFrameBufferBlock = nBaseFrame * GetInfo(APE_INFO_BLOCKS_PER_FRAME); m_nCurrentFrameBufferBlock = nBaseFrame * GetInfo(APE_INFO_BLOCKS_PER_FRAME);
m_nCurrentFrame = nBaseFrame; m_nCurrentFrame = nBaseFrame;
@@ -149,7 +154,7 @@ int CAPEDecompress::Seek(int nBlockOffset)
// skip necessary blocks // skip necessary blocks
CSmartPtr<char> spTempBuffer(new char [nBytesToSkip], TRUE); CSmartPtr<char> spTempBuffer(new char [nBytesToSkip], TRUE);
if (spTempBuffer == NULL) return ERROR_INSUFFICIENT_MEMORY; if (spTempBuffer == NULL) return ERROR_INSUFFICIENT_MEMORY;
int nBlocksRetrieved = 0; int nBlocksRetrieved = 0;
GetData(spTempBuffer, nBlocksToSkip, &nBlocksRetrieved); GetData(spTempBuffer, nBlocksToSkip, &nBlocksRetrieved);
if (nBlocksRetrieved != nBlocksToSkip) if (nBlocksRetrieved != nBlocksToSkip)
@@ -182,7 +187,7 @@ int CAPEDecompress::FillFrameBuffer()
int nFrameOffsetBlocks = m_nCurrentFrameBufferBlock % GetInfo(APE_INFO_BLOCKS_PER_FRAME); int nFrameOffsetBlocks = m_nCurrentFrameBufferBlock % GetInfo(APE_INFO_BLOCKS_PER_FRAME);
int nFrameBlocksLeft = nFrameBlocks - nFrameOffsetBlocks; int nFrameBlocksLeft = nFrameBlocks - nFrameOffsetBlocks;
int nBlocksThisPass = min(nFrameBlocksLeft, nBlocksLeft); int nBlocksThisPass = std::min(nFrameBlocksLeft, nBlocksLeft);
// start the frame if we need to // start the frame if we need to
if (nFrameOffsetBlocks == 0) if (nFrameOffsetBlocks == 0)
@@ -193,7 +198,7 @@ int CAPEDecompress::FillFrameBuffer()
// decode data // decode data
DecodeBlocksToFrameBuffer(nBlocksThisPass); DecodeBlocksToFrameBuffer(nBlocksThisPass);
// end the frame if we need to // end the frame if we need to
if ((nFrameOffsetBlocks + nBlocksThisPass) >= nFrameBlocks) if ((nFrameOffsetBlocks + nBlocksThisPass) >= nFrameBlocks)
{ {
@@ -234,8 +239,8 @@ void CAPEDecompress::DecodeBlocksToFrameBuffer(int nBlocks)
{ {
if (m_wfeInput.nChannels == 2) if (m_wfeInput.nChannels == 2)
{ {
if ((m_nSpecialCodes & SPECIAL_FRAME_LEFT_SILENCE) && if ((m_nSpecialCodes & SPECIAL_FRAME_LEFT_SILENCE) &&
(m_nSpecialCodes & SPECIAL_FRAME_RIGHT_SILENCE)) (m_nSpecialCodes & SPECIAL_FRAME_RIGHT_SILENCE))
{ {
for (nBlocksProcessed = 0; nBlocksProcessed < nBlocks; nBlocksProcessed++) for (nBlocksProcessed = 0; nBlocksProcessed < nBlocks; nBlocksProcessed++)
{ {
@@ -251,7 +256,7 @@ void CAPEDecompress::DecodeBlocksToFrameBuffer(int nBlocks)
m_Prepare.Unprepare(X, 0, &m_wfeInput, m_cbFrameBuffer.GetDirectWritePointer(), &m_nCRC); m_Prepare.Unprepare(X, 0, &m_wfeInput, m_cbFrameBuffer.GetDirectWritePointer(), &m_nCRC);
m_cbFrameBuffer.UpdateAfterDirectWrite(m_nBlockAlign); m_cbFrameBuffer.UpdateAfterDirectWrite(m_nBlockAlign);
} }
} }
else else
{ {
if (m_spAPEInfo->GetInfo(APE_INFO_FILE_VERSION) >= 3950) if (m_spAPEInfo->GetInfo(APE_INFO_FILE_VERSION) >= 3950)
@@ -274,7 +279,7 @@ void CAPEDecompress::DecodeBlocksToFrameBuffer(int nBlocks)
{ {
int X = m_spNewPredictorX->DecompressValue(m_spUnBitArray->DecodeValueRange(m_BitArrayStateX)); int X = m_spNewPredictorX->DecompressValue(m_spUnBitArray->DecodeValueRange(m_BitArrayStateX));
int Y = m_spNewPredictorY->DecompressValue(m_spUnBitArray->DecodeValueRange(m_BitArrayStateY)); int Y = m_spNewPredictorY->DecompressValue(m_spUnBitArray->DecodeValueRange(m_BitArrayStateY));
m_Prepare.Unprepare(X, Y, &m_wfeInput, m_cbFrameBuffer.GetDirectWritePointer(), &m_nCRC); m_Prepare.Unprepare(X, Y, &m_wfeInput, m_cbFrameBuffer.GetDirectWritePointer(), &m_nCRC);
m_cbFrameBuffer.UpdateAfterDirectWrite(m_nBlockAlign); m_cbFrameBuffer.UpdateAfterDirectWrite(m_nBlockAlign);
} }
@@ -313,7 +318,7 @@ void CAPEDecompress::DecodeBlocksToFrameBuffer(int nBlocks)
void CAPEDecompress::StartFrame() void CAPEDecompress::StartFrame()
{ {
m_nCRC = 0xFFFFFFFF; m_nCRC = 0xFFFFFFFF;
// get the frame header // get the frame header
m_nStoredCRC = m_spUnBitArray->DecodeValue(DECODE_VALUE_METHOD_UNSIGNED_INT); m_nStoredCRC = m_spUnBitArray->DecodeValue(DECODE_VALUE_METHOD_UNSIGNED_INT);
m_bErrorDecodingCurrentFrame = FALSE; m_bErrorDecodingCurrentFrame = FALSE;
@@ -322,7 +327,7 @@ void CAPEDecompress::StartFrame()
m_nSpecialCodes = 0; m_nSpecialCodes = 0;
if (GET_USES_SPECIAL_FRAMES(m_spAPEInfo)) if (GET_USES_SPECIAL_FRAMES(m_spAPEInfo))
{ {
if (m_nStoredCRC & 0x80000000) if (m_nStoredCRC & 0x80000000)
{ {
m_nSpecialCodes = m_spUnBitArray->DecodeValue(DECODE_VALUE_METHOD_UNSIGNED_INT); m_nSpecialCodes = m_spUnBitArray->DecodeValue(DECODE_VALUE_METHOD_UNSIGNED_INT);
} }
@@ -445,7 +450,7 @@ int CAPEDecompress::GetInfo(APE_DECOMPRESS_FIELDS Field, int nParam1, int nParam
{ {
char * pBuffer = (char *) nParam1; char * pBuffer = (char *) nParam1;
int nMaxBytes = nParam2; int nMaxBytes = nParam2;
if (sizeof(WAVE_HEADER) > static_cast<uint32>(nMaxBytes)) if (sizeof(WAVE_HEADER) > static_cast<uint32>(nMaxBytes))
{ {
nRetVal = -1; nRetVal = -1;
@@ -453,8 +458,8 @@ int CAPEDecompress::GetInfo(APE_DECOMPRESS_FIELDS Field, int nParam1, int nParam
else else
{ {
WAVEFORMATEX wfeFormat; GetInfo(APE_INFO_WAVEFORMATEX, (int) &wfeFormat, 0); WAVEFORMATEX wfeFormat; GetInfo(APE_INFO_WAVEFORMATEX, (int) &wfeFormat, 0);
WAVE_HEADER WAVHeader; FillWaveHeader(&WAVHeader, WAVE_HEADER WAVHeader; FillWaveHeader(&WAVHeader,
(m_nFinishBlock - m_nStartBlock) * GetInfo(APE_INFO_BLOCK_ALIGN), (m_nFinishBlock - m_nStartBlock) * GetInfo(APE_INFO_BLOCK_ALIGN),
&wfeFormat, 0); &wfeFormat, 0);
memcpy(pBuffer, &WAVHeader, sizeof(WAVE_HEADER)); memcpy(pBuffer, &WAVHeader, sizeof(WAVE_HEADER));
nRetVal = 0; nRetVal = 0;
@@ -9,6 +9,9 @@
#include "MD5.h" #include "MD5.h"
#include "CharacterHelper.h" #include "CharacterHelper.h"
#include <algorithm>
#define UNMAC_DECODER_OUTPUT_NONE 0 #define UNMAC_DECODER_OUTPUT_NONE 0
#define UNMAC_DECODER_OUTPUT_WAV 1 #define UNMAC_DECODER_OUTPUT_WAV 1
#define UNMAC_DECODER_OUTPUT_APE 2 #define UNMAC_DECODER_OUTPUT_APE 2
@@ -58,7 +61,7 @@ int __stdcall CompressFileW(const str_utf16 * pInputFilename, const str_utf16 *
CSmartPtr<CMACProgressHelper> spMACProgressHelper; CSmartPtr<CMACProgressHelper> spMACProgressHelper;
CSmartPtr<unsigned char> spBuffer; CSmartPtr<unsigned char> spBuffer;
CSmartPtr<IAPECompress> spAPECompress; CSmartPtr<IAPECompress> spAPECompress;
try try
{ {
// create the input source // create the input source
@@ -73,7 +76,7 @@ int __stdcall CompressFileW(const str_utf16 * pInputFilename, const str_utf16 *
// create the compressor // create the compressor
spAPECompress.Assign(CreateIAPECompress()); spAPECompress.Assign(CreateIAPECompress());
if (spAPECompress == NULL) throw ERROR_UNDEFINED; if (spAPECompress == NULL) throw ERROR_UNDEFINED;
// figure the audio bytes // figure the audio bytes
int nAudioBytes = nAudioBlocks * WaveFormatEx.nBlockAlign; int nAudioBytes = nAudioBlocks * WaveFormatEx.nBlockAlign;
@@ -82,7 +85,7 @@ int __stdcall CompressFileW(const str_utf16 * pInputFilename, const str_utf16 *
THROW_ON_ERROR(spInputSource->GetHeaderData(spBuffer.GetPtr())) THROW_ON_ERROR(spInputSource->GetHeaderData(spBuffer.GetPtr()))
THROW_ON_ERROR(spAPECompress->Start(pOutputFilename, &WaveFormatEx, nAudioBytes, THROW_ON_ERROR(spAPECompress->Start(pOutputFilename, &WaveFormatEx, nAudioBytes,
nCompressionLevel, spBuffer.GetPtr(), nHeaderBytes)); nCompressionLevel, spBuffer.GetPtr(), nHeaderBytes));
spBuffer.Delete(); spBuffer.Delete();
// set-up the progress // set-up the progress
@@ -122,11 +125,11 @@ int __stdcall CompressFileW(const str_utf16 * pInputFilename, const str_utf16 *
{ {
nFunctionRetVal = ERROR_UNDEFINED; nFunctionRetVal = ERROR_UNDEFINED;
} }
// kill the compressor if we failed // kill the compressor if we failed
if ((nFunctionRetVal != 0) && (spAPECompress != NULL)) if ((nFunctionRetVal != 0) && (spAPECompress != NULL))
spAPECompress->Kill(); spAPECompress->Kill();
// return // return
return nFunctionRetVal; return nFunctionRetVal;
} }
@@ -146,7 +149,7 @@ int __stdcall VerifyFileW(const str_utf16 * pInputFilename, int * pPercentageDon
// return value // return value
int nRetVal = ERROR_UNDEFINED; int nRetVal = ERROR_UNDEFINED;
// see if we can quick verify // see if we can quick verify
if (bQuickVerifyIfPossible) if (bQuickVerifyIfPossible)
{ {
@@ -154,7 +157,7 @@ int __stdcall VerifyFileW(const str_utf16 * pInputFilename, int * pPercentageDon
try try
{ {
int nFunctionRetVal = ERROR_SUCCESS; int nFunctionRetVal = ERROR_SUCCESS;
spAPEDecompress.Assign(CreateIAPEDecompress(pInputFilename, &nFunctionRetVal)); spAPEDecompress.Assign(CreateIAPEDecompress(pInputFilename, &nFunctionRetVal));
if (spAPEDecompress == NULL || nFunctionRetVal != ERROR_SUCCESS) throw(nFunctionRetVal); if (spAPEDecompress == NULL || nFunctionRetVal != ERROR_SUCCESS) throw(nFunctionRetVal);
@@ -183,7 +186,7 @@ int __stdcall VerifyFileW(const str_utf16 * pInputFilename, int * pPercentageDon
if (spAPEDecompress == NULL || nFunctionRetVal != ERROR_SUCCESS) throw(nFunctionRetVal); if (spAPEDecompress == NULL || nFunctionRetVal != ERROR_SUCCESS) throw(nFunctionRetVal);
CMD5Helper MD5Helper; CMD5Helper MD5Helper;
CIO * pIO = GET_IO(spAPEDecompress); CIO * pIO = GET_IO(spAPEDecompress);
APE_FILE_INFO * pInfo = (APE_FILE_INFO *) spAPEDecompress->GetInfo(APE_INTERNAL_INFO); APE_FILE_INFO * pInfo = (APE_FILE_INFO *) spAPEDecompress->GetInfo(APE_INTERNAL_INFO);
@@ -198,13 +201,13 @@ int __stdcall VerifyFileW(const str_utf16 * pInputFilename, int * pPercentageDon
CSmartPtr<unsigned char> spHeadBuffer(new unsigned char [nHeadBytes], TRUE); CSmartPtr<unsigned char> spHeadBuffer(new unsigned char [nHeadBytes], TRUE);
if ((pIO->Read(spHeadBuffer, nHeadBytes, &nBytesRead) != ERROR_SUCCESS) || (nHeadBytes != int(nBytesRead))) if ((pIO->Read(spHeadBuffer, nHeadBytes, &nBytesRead) != ERROR_SUCCESS) || (nHeadBytes != int(nBytesRead)))
throw(ERROR_IO_READ); throw(ERROR_IO_READ);
int nBytesLeft = pInfo->spAPEDescriptor->nHeaderDataBytes + pInfo->spAPEDescriptor->nAPEFrameDataBytes + pInfo->spAPEDescriptor->nTerminatingDataBytes; int nBytesLeft = pInfo->spAPEDescriptor->nHeaderDataBytes + pInfo->spAPEDescriptor->nAPEFrameDataBytes + pInfo->spAPEDescriptor->nTerminatingDataBytes;
CSmartPtr<unsigned char> spBuffer(new unsigned char [16384], TRUE); CSmartPtr<unsigned char> spBuffer(new unsigned char [16384], TRUE);
nBytesRead = 1; nBytesRead = 1;
while ((nBytesLeft > 0) && (nBytesRead > 0)) while ((nBytesLeft > 0) && (nBytesRead > 0))
{ {
int nBytesToRead = min(16384, nBytesLeft); int nBytesToRead = std::min(16384, nBytesLeft);
if (pIO->Read(spBuffer, nBytesToRead, &nBytesRead) != ERROR_SUCCESS) if (pIO->Read(spBuffer, nBytesToRead, &nBytesRead) != ERROR_SUCCESS)
throw(ERROR_IO_READ); throw(ERROR_IO_READ);
@@ -232,7 +235,7 @@ int __stdcall VerifyFileW(const str_utf16 * pInputFilename, int * pPercentageDon
{ {
nFunctionRetVal = ERROR_UNDEFINED; nFunctionRetVal = ERROR_UNDEFINED;
} }
// return value // return value
nRetVal = nFunctionRetVal; nRetVal = nFunctionRetVal;
} }
@@ -259,7 +262,7 @@ int __stdcall DecompressFileW(const str_utf16 * pInputFilename, const str_utf16
/***************************************************************************************** /*****************************************************************************************
Convert file Convert file
*****************************************************************************************/ *****************************************************************************************/
int __stdcall ConvertFileW(const str_utf16 * pInputFilename, const str_utf16 * pOutputFilename, int nCompressionLevel, int * pPercentageDone, APE_PROGRESS_CALLBACK ProgressCallback, int * pKillFlag) int __stdcall ConvertFileW(const str_utf16 * pInputFilename, const str_utf16 * pOutputFilename, int nCompressionLevel, int * pPercentageDone, APE_PROGRESS_CALLBACK ProgressCallback, int * pKillFlag)
{ {
return DecompressCore(pInputFilename, pOutputFilename, UNMAC_DECODER_OUTPUT_APE, nCompressionLevel, pPercentageDone, ProgressCallback, pKillFlag); return DecompressCore(pInputFilename, pOutputFilename, UNMAC_DECODER_OUTPUT_APE, nCompressionLevel, pPercentageDone, ProgressCallback, pKillFlag);
} }
@@ -267,10 +270,10 @@ int __stdcall ConvertFileW(const str_utf16 * pInputFilename, const str_utf16 * p
/***************************************************************************************** /*****************************************************************************************
Decompress a file using the specified output method Decompress a file using the specified output method
*****************************************************************************************/ *****************************************************************************************/
int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFilename, int nOutputMode, int nCompressionLevel, int * pPercentageDone, APE_PROGRESS_CALLBACK ProgressCallback, int * pKillFlag) int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFilename, int nOutputMode, int nCompressionLevel, int * pPercentageDone, APE_PROGRESS_CALLBACK ProgressCallback, int * pKillFlag)
{ {
// error check the function parameters // error check the function parameters
if (pInputFilename == NULL) if (pInputFilename == NULL)
{ {
return ERROR_INVALID_FUNCTION_PARAMETER; return ERROR_INVALID_FUNCTION_PARAMETER;
} }
@@ -305,7 +308,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
{ {
// create the file // create the file
spioOutput.Assign(new IO_CLASS_NAME); THROW_ON_ERROR(spioOutput->Create(pOutputFilename)) spioOutput.Assign(new IO_CLASS_NAME); THROW_ON_ERROR(spioOutput->Create(pOutputFilename))
// output the header // output the header
THROW_ON_ERROR(WriteSafe(spioOutput, spTempBuffer, spAPEDecompress->GetInfo(APE_INFO_WAV_HEADER_BYTES))); THROW_ON_ERROR(WriteSafe(spioOutput, spTempBuffer, spAPEDecompress->GetInfo(APE_INFO_WAV_HEADER_BYTES)));
} }
@@ -326,7 +329,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
if (spTempBuffer == NULL) throw(ERROR_INSUFFICIENT_MEMORY); if (spTempBuffer == NULL) throw(ERROR_INSUFFICIENT_MEMORY);
int nBlocksLeft = spAPEDecompress->GetInfo(APE_DECOMPRESS_TOTAL_BLOCKS); int nBlocksLeft = spAPEDecompress->GetInfo(APE_DECOMPRESS_TOTAL_BLOCKS);
// create the progress helper // create the progress helper
spMACProgressHelper.Assign(new CMACProgressHelper(nBlocksLeft / BLOCKS_PER_DECODE, pPercentageDone, ProgressCallback, pKillFlag)); spMACProgressHelper.Assign(new CMACProgressHelper(nBlocksLeft / BLOCKS_PER_DECODE, pPercentageDone, ProgressCallback, pKillFlag));
@@ -336,7 +339,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
// decode data // decode data
int nBlocksDecoded = -1; int nBlocksDecoded = -1;
int nRetVal = spAPEDecompress->GetData((char *) spTempBuffer.GetPtr(), BLOCKS_PER_DECODE, &nBlocksDecoded); int nRetVal = spAPEDecompress->GetData((char *) spTempBuffer.GetPtr(), BLOCKS_PER_DECODE, &nBlocksDecoded);
if (nRetVal != ERROR_SUCCESS) if (nRetVal != ERROR_SUCCESS)
throw(ERROR_INVALID_CHECKSUM); throw(ERROR_INVALID_CHECKSUM);
// handle the output // handle the output
@@ -345,7 +348,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
unsigned int nBytesToWrite = (nBlocksDecoded * spAPEDecompress->GetInfo(APE_INFO_BLOCK_ALIGN)); unsigned int nBytesToWrite = (nBlocksDecoded * spAPEDecompress->GetInfo(APE_INFO_BLOCK_ALIGN));
unsigned int nBytesWritten = 0; unsigned int nBytesWritten = 0;
int nRetVal = spioOutput->Write(spTempBuffer, nBytesToWrite, &nBytesWritten); int nRetVal = spioOutput->Write(spTempBuffer, nBytesToWrite, &nBytesWritten);
if ((nRetVal != 0) || (nBytesToWrite != nBytesWritten)) if ((nRetVal != 0) || (nBytesToWrite != nBytesWritten))
throw(ERROR_IO_WRITE); throw(ERROR_IO_WRITE);
} }
else if (nOutputMode == UNMAC_DECODER_OUTPUT_APE) else if (nOutputMode == UNMAC_DECODER_OUTPUT_APE)
@@ -355,7 +358,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
// update amount remaining // update amount remaining
nBlocksLeft -= nBlocksDecoded; nBlocksLeft -= nBlocksDecoded;
// update progress and kill flag // update progress and kill flag
spMACProgressHelper->UpdateProgress(); spMACProgressHelper->UpdateProgress();
if (spMACProgressHelper->ProcessKillFlag(TRUE) != 0) if (spMACProgressHelper->ProcessKillFlag(TRUE) != 0)
@@ -366,16 +369,16 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
if (nOutputMode == UNMAC_DECODER_OUTPUT_WAV) if (nOutputMode == UNMAC_DECODER_OUTPUT_WAV)
{ {
// write any terminating WAV data // write any terminating WAV data
if (spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES) > 0) if (spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES) > 0)
{ {
spTempBuffer.Assign(new unsigned char[spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES)], TRUE); spTempBuffer.Assign(new unsigned char[spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES)], TRUE);
if (spTempBuffer == NULL) throw(ERROR_INSUFFICIENT_MEMORY); if (spTempBuffer == NULL) throw(ERROR_INSUFFICIENT_MEMORY);
THROW_ON_ERROR(spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_DATA, (int) spTempBuffer.GetPtr(), spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES))) THROW_ON_ERROR(spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_DATA, (int) spTempBuffer.GetPtr(), spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES)))
unsigned int nBytesToWrite = spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES); unsigned int nBytesToWrite = spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES);
unsigned int nBytesWritten = 0; unsigned int nBytesWritten = 0;
int nRetVal = spioOutput->Write(spTempBuffer, nBytesToWrite, &nBytesWritten); int nRetVal = spioOutput->Write(spTempBuffer, nBytesToWrite, &nBytesWritten);
if ((nRetVal != 0) || (nBytesToWrite != nBytesWritten)) if ((nRetVal != 0) || (nBytesToWrite != nBytesWritten))
throw(ERROR_IO_WRITE); throw(ERROR_IO_WRITE);
} }
} }
@@ -387,11 +390,11 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
int nTerminatingBytes = nTagBytes; int nTerminatingBytes = nTagBytes;
nTerminatingBytes += spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES); nTerminatingBytes += spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES);
if (nTerminatingBytes > 0) if (nTerminatingBytes > 0)
{ {
spTempBuffer.Assign(new unsigned char[nTerminatingBytes], TRUE); spTempBuffer.Assign(new unsigned char[nTerminatingBytes], TRUE);
if (spTempBuffer == NULL) throw(ERROR_INSUFFICIENT_MEMORY); if (spTempBuffer == NULL) throw(ERROR_INSUFFICIENT_MEMORY);
THROW_ON_ERROR(spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_DATA, (int) spTempBuffer.GetPtr(), nTerminatingBytes)) THROW_ON_ERROR(spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_DATA, (int) spTempBuffer.GetPtr(), nTerminatingBytes))
if (bHasTag) if (bHasTag)
@@ -403,7 +406,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
THROW_ON_ERROR(spAPECompress->Finish(spTempBuffer, nTerminatingBytes, spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES))); THROW_ON_ERROR(spAPECompress->Finish(spTempBuffer, nTerminatingBytes, spAPEDecompress->GetInfo(APE_INFO_WAV_TERMINATING_BYTES)));
} }
else else
{ {
THROW_ON_ERROR(spAPECompress->Finish(NULL, 0, 0)); THROW_ON_ERROR(spAPECompress->Finish(NULL, 0, 0));
} }
@@ -420,7 +423,7 @@ int DecompressCore(const str_utf16 * pInputFilename, const str_utf16 * pOutputFi
{ {
nFunctionRetVal = ERROR_UNDEFINED; nFunctionRetVal = ERROR_UNDEFINED;
} }
// return // return
return nFunctionRetVal; return nFunctionRetVal;
} }
@@ -1,6 +1,9 @@
#include "All.h" #include "All.h"
#include "ID3Genres.h"
#include "APETag.h" #include "APETag.h"
#include <algorithm>
#include "ID3Genres.h"
#include "CharacterHelper.h" #include "CharacterHelper.h"
#include "IO.h" #include "IO.h"
#include IO_HEADER_FILE #include IO_HEADER_FILE
@@ -14,9 +17,9 @@ CAPETagField::CAPETagField(const str_utf16 * pFieldName, const void * pFieldValu
// field name // field name
m_spFieldNameUTF16.Assign(new str_utf16 [wcslen(pFieldName) + 1], TRUE); m_spFieldNameUTF16.Assign(new str_utf16 [wcslen(pFieldName) + 1], TRUE);
memcpy(m_spFieldNameUTF16, pFieldName, (wcslen(pFieldName) + 1) * sizeof(str_utf16)); memcpy(m_spFieldNameUTF16, pFieldName, (wcslen(pFieldName) + 1) * sizeof(str_utf16));
// data (we'll always allocate two extra bytes and memset to 0 so we're safely NULL terminated) // data (we'll always allocate two extra bytes and memset to 0 so we're safely NULL terminated)
m_nFieldValueBytes = max(nFieldBytes, 0); m_nFieldValueBytes = std::max(nFieldBytes, 0);
m_spFieldValue.Assign(new char [m_nFieldValueBytes + 2], TRUE); m_spFieldValue.Assign(new char [m_nFieldValueBytes + 2], TRUE);
memset(m_spFieldValue, 0, m_nFieldValueBytes + 2); memset(m_spFieldValue, 0, m_nFieldValueBytes + 2);
if (m_nFieldValueBytes > 0) if (m_nFieldValueBytes > 0)
@@ -29,10 +32,10 @@ CAPETagField::CAPETagField(const str_utf16 * pFieldName, const void * pFieldValu
CAPETagField::~CAPETagField() CAPETagField::~CAPETagField()
{ {
} }
int CAPETagField::GetFieldSize() int CAPETagField::GetFieldSize()
{ {
CSmartPtr<char> spFieldNameANSI(GetANSIFromUTF16(m_spFieldNameUTF16), TRUE); CSmartPtr<char> spFieldNameANSI(GetANSIFromUTF16(m_spFieldNameUTF16), TRUE);
return (strlen(spFieldNameANSI) + 1) + m_nFieldValueBytes + 4 + 4; return (strlen(spFieldNameANSI) + 1) + m_nFieldValueBytes + 4 + 4;
} }
@@ -62,8 +65,8 @@ int CAPETagField::SaveField(char * pBuffer)
pBuffer += 4; pBuffer += 4;
*((int *) pBuffer) = m_nFieldFlags; *((int *) pBuffer) = m_nFieldFlags;
pBuffer += 4; pBuffer += 4;
CSmartPtr<char> spFieldNameANSI((char *) GetANSIFromUTF16(m_spFieldNameUTF16), TRUE); CSmartPtr<char> spFieldNameANSI((char *) GetANSIFromUTF16(m_spFieldNameUTF16), TRUE);
strcpy(pBuffer, spFieldNameANSI); strcpy(pBuffer, spFieldNameANSI);
pBuffer += strlen(spFieldNameANSI) + 1; pBuffer += strlen(spFieldNameANSI) + 1;
@@ -86,7 +89,7 @@ CAPETag::CAPETag(const str_utf16 * pFilename, BOOL bAnalyze)
m_nFields = 0; m_nFields = 0;
m_nTagBytes = 0; m_nTagBytes = 0;
m_bIgnoreReadOnly = FALSE; m_bIgnoreReadOnly = FALSE;
if (bAnalyze) if (bAnalyze)
{ {
Analyze(); Analyze();
@@ -99,7 +102,7 @@ CAPETag::CAPETag(CIO * pIO, BOOL bAnalyze)
m_bAnalyzed = FALSE; m_bAnalyzed = FALSE;
m_nFields = 0; m_nFields = 0;
m_nTagBytes = 0; m_nTagBytes = 0;
if (bAnalyze) if (bAnalyze)
{ {
Analyze(); Analyze();
@@ -134,7 +137,7 @@ int CAPETag::Save(BOOL bUseOldID3)
{ {
if (Remove(FALSE) != ERROR_SUCCESS) if (Remove(FALSE) != ERROR_SUCCESS)
return -1; return -1;
if (m_nFields == 0) { return ERROR_SUCCESS; } if (m_nFields == 0) { return ERROR_SUCCESS; }
int nRetVal = -1; int nRetVal = -1;
@@ -184,12 +187,12 @@ int CAPETag::Save(BOOL bUseOldID3)
int CAPETag::WriteBufferToEndOfIO(void * pBuffer, int nBytes) int CAPETag::WriteBufferToEndOfIO(void * pBuffer, int nBytes)
{ {
int nOriginalPosition = m_spIO->GetPosition(); int nOriginalPosition = m_spIO->GetPosition();
unsigned int nBytesWritten = 0; unsigned int nBytesWritten = 0;
m_spIO->Seek(0, FILE_END); m_spIO->Seek(0, FILE_END);
int nRetVal = m_spIO->Write(pBuffer, nBytes, &nBytesWritten); int nRetVal = m_spIO->Write(pBuffer, nBytes, &nBytesWritten);
m_spIO->Seek(nOriginalPosition, FILE_BEGIN); m_spIO->Seek(nOriginalPosition, FILE_BEGIN);
return nRetVal; return nRetVal;
@@ -206,7 +209,7 @@ int CAPETag::Analyze()
// store the original location // store the original location
int nOriginalPosition = m_spIO->GetPosition(); int nOriginalPosition = m_spIO->GetPosition();
// check for a tag // check for a tag
unsigned int nBytesRead; unsigned int nBytesRead;
int nRetVal; int nRetVal;
@@ -215,16 +218,16 @@ int CAPETag::Analyze()
m_nAPETagVersion = -1; m_nAPETagVersion = -1;
m_spIO->Seek(-ID3_TAG_BYTES, FILE_END); m_spIO->Seek(-ID3_TAG_BYTES, FILE_END);
nRetVal = m_spIO->Read((unsigned char *) &ID3Tag, sizeof(ID3_TAG), &nBytesRead); nRetVal = m_spIO->Read((unsigned char *) &ID3Tag, sizeof(ID3_TAG), &nBytesRead);
if ((nBytesRead == sizeof(ID3_TAG)) && (nRetVal == 0)) if ((nBytesRead == sizeof(ID3_TAG)) && (nRetVal == 0))
{ {
if (ID3Tag.Header[0] == 'T' && ID3Tag.Header[1] == 'A' && ID3Tag.Header[2] == 'G') if (ID3Tag.Header[0] == 'T' && ID3Tag.Header[1] == 'A' && ID3Tag.Header[2] == 'G')
{ {
m_bHasID3Tag = TRUE; m_bHasID3Tag = TRUE;
m_nTagBytes += ID3_TAG_BYTES; m_nTagBytes += ID3_TAG_BYTES;
} }
} }
// set the fields // set the fields
if (m_bHasID3Tag) if (m_bHasID3Tag)
{ {
@@ -233,13 +236,13 @@ int CAPETag::Analyze()
SetFieldID3String(APE_TAG_FIELD_TITLE, ID3Tag.Title, 30); SetFieldID3String(APE_TAG_FIELD_TITLE, ID3Tag.Title, 30);
SetFieldID3String(APE_TAG_FIELD_COMMENT, ID3Tag.Comment, 28); SetFieldID3String(APE_TAG_FIELD_COMMENT, ID3Tag.Comment, 28);
SetFieldID3String(APE_TAG_FIELD_YEAR, ID3Tag.Year, 4); SetFieldID3String(APE_TAG_FIELD_YEAR, ID3Tag.Year, 4);
char cTemp[16]; sprintf(cTemp, "%d", ID3Tag.Track); char cTemp[16]; sprintf(cTemp, "%d", ID3Tag.Track);
SetFieldString(APE_TAG_FIELD_TRACK, cTemp, FALSE); SetFieldString(APE_TAG_FIELD_TRACK, cTemp, FALSE);
if ((ID3Tag.Genre == GENRE_UNDEFINED) || (ID3Tag.Genre >= GENRE_COUNT)) if ((ID3Tag.Genre == GENRE_UNDEFINED) || (ID3Tag.Genre >= GENRE_COUNT))
SetFieldString(APE_TAG_FIELD_GENRE, APE_TAG_GENRE_UNDEFINED); SetFieldString(APE_TAG_FIELD_GENRE, APE_TAG_GENRE_UNDEFINED);
else else
SetFieldString(APE_TAG_FIELD_GENRE, g_ID3Genre[ID3Tag.Genre]); SetFieldString(APE_TAG_FIELD_GENRE, g_ID3Genre[ID3Tag.Genre]);
} }
@@ -258,7 +261,7 @@ int CAPETag::Analyze()
int nRawFieldBytes = APETagFooter.GetFieldBytes(); int nRawFieldBytes = APETagFooter.GetFieldBytes();
m_nTagBytes += APETagFooter.GetTotalTagBytes(); m_nTagBytes += APETagFooter.GetTotalTagBytes();
CSmartPtr<char> spRawTag(new char [nRawFieldBytes], TRUE); CSmartPtr<char> spRawTag(new char [nRawFieldBytes], TRUE);
m_spIO->Seek(-(APETagFooter.GetTotalTagBytes() - APETagFooter.GetFieldsOffset()), FILE_END); m_spIO->Seek(-(APETagFooter.GetTotalTagBytes() - APETagFooter.GetFieldsOffset()), FILE_END);
nRetVal = m_spIO->Read((unsigned char *) spRawTag.GetPtr(), nRawFieldBytes, &nBytesRead); nRetVal = m_spIO->Read((unsigned char *) spRawTag.GetPtr(), nRawFieldBytes, &nBytesRead);
@@ -270,7 +273,7 @@ int CAPETag::Analyze()
for (int z = 0; z < APETagFooter.GetNumberFields(); z++) for (int z = 0; z < APETagFooter.GetNumberFields(); z++)
{ {
int nMaximumFieldBytes = nRawFieldBytes - nLocation; int nMaximumFieldBytes = nRawFieldBytes - nLocation;
int nBytes = 0; int nBytes = 0;
if (LoadField(&spRawTag[nLocation], nMaximumFieldBytes, &nBytes) != ERROR_SUCCESS) if (LoadField(&spRawTag[nLocation], nMaximumFieldBytes, &nBytes) != ERROR_SUCCESS)
{ {
@@ -287,7 +290,7 @@ int CAPETag::Analyze()
// restore the file pointer // restore the file pointer
m_spIO->Seek(nOriginalPosition, FILE_BEGIN); m_spIO->Seek(nOriginalPosition, FILE_BEGIN);
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@@ -297,7 +300,7 @@ int CAPETag::ClearFields()
{ {
SAFE_DELETE(m_aryFields[z]) SAFE_DELETE(m_aryFields[z])
} }
m_nFields = 0; m_nFields = 0;
return ERROR_SUCCESS; return ERROR_SUCCESS;
@@ -347,7 +350,7 @@ int CAPETag::GetFieldString(const str_utf16 * pFieldName, str_ansi * pBuffer, in
*pBufferCharacters = strlen(spANSI); *pBufferCharacters = strlen(spANSI);
} }
} }
delete [] pUTF16; delete [] pUTF16;
return nRetVal; return nRetVal;
@@ -413,7 +416,7 @@ int CAPETag::GetFieldString(const str_utf16 * pFieldName, str_utf16 * pBuffer, i
int CAPETag::GetFieldBinary(const str_utf16 * pFieldName, void * pBuffer, int * pBufferBytes) int CAPETag::GetFieldBinary(const str_utf16 * pFieldName, void * pBuffer, int * pBufferBytes)
{ {
if (m_bAnalyzed == FALSE) { Analyze(); } if (m_bAnalyzed == FALSE) { Analyze(); }
int nRetVal = ERROR_UNDEFINED; int nRetVal = ERROR_UNDEFINED;
if (*pBufferBytes > 0) if (*pBufferBytes > 0)
@@ -447,7 +450,7 @@ int CAPETag::GetFieldBinary(const str_utf16 * pFieldName, void * pBuffer, int *
int CAPETag::CreateID3Tag(ID3_TAG * pID3Tag) int CAPETag::CreateID3Tag(ID3_TAG * pID3Tag)
{ {
// error check // error check
if (pID3Tag == NULL) { return -1; } if (pID3Tag == NULL) { return -1; }
if (m_bAnalyzed == FALSE) { Analyze(); } if (m_bAnalyzed == FALSE) { Analyze(); }
if (m_nFields == 0) { return -1; } if (m_nFields == 0) { return -1; }
@@ -457,7 +460,7 @@ int CAPETag::CreateID3Tag(ID3_TAG * pID3Tag)
// header // header
pID3Tag->Header[0] = 'T'; pID3Tag->Header[1] = 'A'; pID3Tag->Header[2] = 'G'; pID3Tag->Header[0] = 'T'; pID3Tag->Header[1] = 'A'; pID3Tag->Header[2] = 'G';
// standard fields // standard fields
GetFieldID3String(APE_TAG_FIELD_ARTIST, pID3Tag->Artist, 30); GetFieldID3String(APE_TAG_FIELD_ARTIST, pID3Tag->Artist, 30);
GetFieldID3String(APE_TAG_FIELD_ALBUM, pID3Tag->Album, 30); GetFieldID3String(APE_TAG_FIELD_ALBUM, pID3Tag->Album, 30);
@@ -469,11 +472,11 @@ int CAPETag::CreateID3Tag(ID3_TAG * pID3Tag)
str_utf16 cBuffer[256] = { 0 }; int nBufferCharacters = 255; str_utf16 cBuffer[256] = { 0 }; int nBufferCharacters = 255;
GetFieldString(APE_TAG_FIELD_TRACK, cBuffer, &nBufferCharacters); GetFieldString(APE_TAG_FIELD_TRACK, cBuffer, &nBufferCharacters);
pID3Tag->Track = (unsigned char) _wtoi(cBuffer); pID3Tag->Track = (unsigned char) _wtoi(cBuffer);
// genre // genre
cBuffer[0] = 0; nBufferCharacters = 255; cBuffer[0] = 0; nBufferCharacters = 255;
GetFieldString(APE_TAG_FIELD_GENRE, cBuffer, &nBufferCharacters); GetFieldString(APE_TAG_FIELD_GENRE, cBuffer, &nBufferCharacters);
// convert the genre string to an index // convert the genre string to an index
pID3Tag->Genre = 255; pID3Tag->Genre = 255;
int nGenreIndex = 0; int nGenreIndex = 0;
@@ -485,7 +488,7 @@ int CAPETag::CreateID3Tag(ID3_TAG * pID3Tag)
pID3Tag->Genre = nGenreIndex; pID3Tag->Genre = nGenreIndex;
bFound = TRUE; bFound = TRUE;
} }
nGenreIndex++; nGenreIndex++;
} }
@@ -503,7 +506,7 @@ int CAPETag::LoadField(const char * pBuffer, int nMaximumBytes, int * pBytes)
nLocation += 4; nLocation += 4;
int nFieldFlags = *((int *) &pBuffer[nLocation]); int nFieldFlags = *((int *) &pBuffer[nLocation]);
nLocation += 4; nLocation += 4;
// safety check (so we can't get buffer overflow attacked) // safety check (so we can't get buffer overflow attacked)
int nMaximumRead = nMaximumBytes - 8 - nFieldValueSize; int nMaximumRead = nMaximumBytes - 8 - nFieldValueSize;
BOOL bSafe = TRUE; BOOL bSafe = TRUE;
@@ -585,7 +588,7 @@ int CAPETag::SetFieldBinary(const str_utf16 * pFieldName, const void * pFieldVal
// fail if we're read-only (and not ignoring the read-only flag) // fail if we're read-only (and not ignoring the read-only flag)
if ((m_bIgnoreReadOnly == FALSE) && (m_aryFields[nFieldIndex]->GetIsReadOnly())) if ((m_bIgnoreReadOnly == FALSE) && (m_aryFields[nFieldIndex]->GetIsReadOnly()))
return -1; return -1;
// erase the existing field // erase the existing field
SAFE_DELETE(m_aryFields[nFieldIndex]) SAFE_DELETE(m_aryFields[nFieldIndex])
@@ -602,7 +605,7 @@ int CAPETag::SetFieldBinary(const str_utf16 * pFieldName, const void * pFieldVal
nFieldIndex = m_nFields; nFieldIndex = m_nFields;
m_nFields++; m_nFields++;
} }
// create the field and add it to the field array // create the field and add it to the field array
m_aryFields[nFieldIndex] = new CAPETagField(pFieldName, pFieldValue, nFieldBytes, nFieldFlags); m_aryFields[nFieldIndex] = new CAPETagField(pFieldName, pFieldValue, nFieldBytes, nFieldFlags);
@@ -685,7 +688,7 @@ int CAPETag::Remove(BOOL bUpdate)
} }
} }
m_spIO->Seek(nOriginalPosition, FILE_BEGIN); m_spIO->Seek(nOriginalPosition, FILE_BEGIN);
if (bUpdate && bFailedToRemove == FALSE) if (bUpdate && bFailedToRemove == FALSE)
@@ -701,17 +704,17 @@ int CAPETag::SetFieldID3String(const str_utf16 * pFieldName, const char * pField
// allocate a buffer and terminate it // allocate a buffer and terminate it
CSmartPtr<str_ansi> spBuffer(new str_ansi [nBytes + 1], TRUE); CSmartPtr<str_ansi> spBuffer(new str_ansi [nBytes + 1], TRUE);
spBuffer[nBytes] = 0; spBuffer[nBytes] = 0;
// make a capped copy of the string // make a capped copy of the string
memcpy(spBuffer.GetPtr(), pFieldValue, nBytes); memcpy(spBuffer.GetPtr(), pFieldValue, nBytes);
// remove trailing white-space // remove trailing white-space
char * pEnd = &spBuffer[nBytes]; char * pEnd = &spBuffer[nBytes];
while (((*pEnd == ' ') || (*pEnd == 0)) && pEnd >= &spBuffer[0]) { *pEnd-- = 0; } while (((*pEnd == ' ') || (*pEnd == 0)) && pEnd >= &spBuffer[0]) { *pEnd-- = 0; }
// set the field // set the field
SetFieldString(pFieldName, spBuffer, FALSE); SetFieldString(pFieldName, spBuffer, FALSE);
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@@ -1,7 +1,7 @@
#ifndef APE_CIRCLEBUFFER_H #ifndef APE_CIRCLEBUFFER_H
#define APE_CIRCLEBUFFER_H #define APE_CIRCLEBUFFER_H
class CCircleBuffer class CCircleBuffer
{ {
public: public:
@@ -17,14 +17,14 @@ public:
int MaxGet(); int MaxGet();
// direct writing // direct writing
inline unsigned char * CCircleBuffer::GetDirectWritePointer() inline unsigned char * GetDirectWritePointer()
{ {
// return a pointer to the tail -- note that it will always be safe to write // return a pointer to the tail -- note that it will always be safe to write
// at least m_nMaxDirectWriteBytes since we use an end cap region // at least m_nMaxDirectWriteBytes since we use an end cap region
return &m_pBuffer[m_nTail]; return &m_pBuffer[m_nTail];
} }
inline void CCircleBuffer::UpdateAfterDirectWrite(int nBytes) inline void UpdateAfterDirectWrite(int nBytes)
{ {
// update the tail // update the tail
m_nTail += nBytes; m_nTail += nBytes;
@@ -86,6 +86,6 @@ public:
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(pop) #pragma warning(pop)
#endif _MSC_VER #endif // _MSC_VER
#endif // #ifndef APE_SMARTPTR_H #endif // #ifndef APE_SMARTPTR_H
@@ -3,6 +3,9 @@
#include "UnBitArray.h" #include "UnBitArray.h"
#include "BitArray.h" #include "BitArray.h"
#include <algorithm>
const uint32 POWERS_OF_TWO_MINUS_ONE_REVERSED[33] = {4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0}; const uint32 POWERS_OF_TWO_MINUS_ONE_REVERSED[33] = {4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0};
const uint32 K_SUM_MIN_BOUNDARY[32] = {0,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,0,0,0,0}; const uint32 K_SUM_MIN_BOUNDARY[32] = {0,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,0,0,0,0};
@@ -27,7 +30,7 @@ const uint32 RANGE_WIDTH_2[64] = {19578,16582,12257,7906,4576,2366,1170,536,261,
/*********************************************************************************** /***********************************************************************************
Construction Construction
***********************************************************************************/ ***********************************************************************************/
CUnBitArray::CUnBitArray(CIO * pIO, int nVersion) CUnBitArray::CUnBitArray(CIO * pIO, int nVersion)
{ {
CreateHelper(pIO, 16384, nVersion); CreateHelper(pIO, 16384, nVersion);
m_nFlushCounter = 0; m_nFlushCounter = 0;
@@ -46,11 +49,11 @@ unsigned int CUnBitArray::DecodeValue(DECODE_VALUE_METHOD DecodeMethod, int nPar
case DECODE_VALUE_METHOD_UNSIGNED_INT: case DECODE_VALUE_METHOD_UNSIGNED_INT:
return DecodeValueXBits(32); return DecodeValueXBits(32);
} }
return 0; return 0;
} }
void CUnBitArray::GenerateArray(int * pOutputArray, int nElements, int nBytesRequired) void CUnBitArray::GenerateArray(int * pOutputArray, int nElements, int nBytesRequired)
{ {
GenerateArrayRange(pOutputArray, nElements); GenerateArrayRange(pOutputArray, nElements);
} }
@@ -60,12 +63,12 @@ __inline unsigned char CUnBitArray::GetC()
unsigned char nValue = (unsigned char) (m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))); unsigned char nValue = (unsigned char) (m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31)));
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
return nValue; return nValue;
} }
__inline int CUnBitArray::RangeDecodeFast(int nShift) __inline int CUnBitArray::RangeDecodeFast(int nShift)
{ {
while (m_RangeCoderInfo.range <= BOTTOM_VALUE) while (m_RangeCoderInfo.range <= BOTTOM_VALUE)
{ {
m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF); m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF);
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF); m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF);
@@ -80,7 +83,7 @@ __inline int CUnBitArray::RangeDecodeFast(int nShift)
__inline int CUnBitArray::RangeDecodeFastWithUpdate(int nShift) __inline int CUnBitArray::RangeDecodeFastWithUpdate(int nShift)
{ {
while (m_RangeCoderInfo.range <= BOTTOM_VALUE) while (m_RangeCoderInfo.range <= BOTTOM_VALUE)
{ {
m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF); m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF);
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF); m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF);
@@ -108,21 +111,21 @@ int CUnBitArray::DecodeValueRange(UNBIT_ARRAY_STATE & BitArrayState)
if (m_nVersion >= 3990) if (m_nVersion >= 3990)
{ {
// figure the pivot value // figure the pivot value
int nPivotValue = max(BitArrayState.nKSum / 32, 1); int nPivotValue = std::max(BitArrayState.nKSum / 32, 1UL);
// get the overflow // get the overflow
int nOverflow = 0; int nOverflow = 0;
{ {
// decode // decode
int nRangeTotal = RangeDecodeFast(RANGE_OVERFLOW_SHIFT); int nRangeTotal = RangeDecodeFast(RANGE_OVERFLOW_SHIFT);
// lookup the symbol (must be a faster way than this) // lookup the symbol (must be a faster way than this)
while (nRangeTotal >= RANGE_TOTAL_2[nOverflow + 1]) { nOverflow++; } while (nRangeTotal >= RANGE_TOTAL_2[nOverflow + 1]) { nOverflow++; }
// update // update
m_RangeCoderInfo.low -= m_RangeCoderInfo.range * RANGE_TOTAL_2[nOverflow]; m_RangeCoderInfo.low -= m_RangeCoderInfo.range * RANGE_TOTAL_2[nOverflow];
m_RangeCoderInfo.range = m_RangeCoderInfo.range * RANGE_WIDTH_2[nOverflow]; m_RangeCoderInfo.range = m_RangeCoderInfo.range * RANGE_WIDTH_2[nOverflow];
// get the working k // get the working k
if (nOverflow == (MODEL_ELEMENTS - 1)) if (nOverflow == (MODEL_ELEMENTS - 1))
{ {
@@ -146,7 +149,7 @@ int CUnBitArray::DecodeValueRange(UNBIT_ARRAY_STATE & BitArrayState)
int nPivotValueB = nSplitFactor; int nPivotValueB = nSplitFactor;
while (m_RangeCoderInfo.range <= BOTTOM_VALUE) while (m_RangeCoderInfo.range <= BOTTOM_VALUE)
{ {
m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF); m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF);
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF); m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF);
@@ -157,7 +160,7 @@ int CUnBitArray::DecodeValueRange(UNBIT_ARRAY_STATE & BitArrayState)
m_RangeCoderInfo.low -= m_RangeCoderInfo.range * nBaseA; m_RangeCoderInfo.low -= m_RangeCoderInfo.range * nBaseA;
while (m_RangeCoderInfo.range <= BOTTOM_VALUE) while (m_RangeCoderInfo.range <= BOTTOM_VALUE)
{ {
m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF); m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF);
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF); m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF);
@@ -172,7 +175,7 @@ int CUnBitArray::DecodeValueRange(UNBIT_ARRAY_STATE & BitArrayState)
else else
{ {
while (m_RangeCoderInfo.range <= BOTTOM_VALUE) while (m_RangeCoderInfo.range <= BOTTOM_VALUE)
{ {
m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF); m_RangeCoderInfo.buffer = (m_RangeCoderInfo.buffer << 8) | ((m_pBitArray[m_nCurrentBitIndex >> 5] >> (24 - (m_nCurrentBitIndex & 31))) & 0xFF);
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF); m_RangeCoderInfo.low = (m_RangeCoderInfo.low << 8) | ((m_RangeCoderInfo.buffer >> 1) & 0xFF);
@@ -195,15 +198,15 @@ int CUnBitArray::DecodeValueRange(UNBIT_ARRAY_STATE & BitArrayState)
{ {
// decode // decode
int nRangeTotal = RangeDecodeFast(RANGE_OVERFLOW_SHIFT); int nRangeTotal = RangeDecodeFast(RANGE_OVERFLOW_SHIFT);
// lookup the symbol (must be a faster way than this) // lookup the symbol (must be a faster way than this)
int nOverflow = 0; int nOverflow = 0;
while (nRangeTotal >= RANGE_TOTAL_1[nOverflow + 1]) { nOverflow++; } while (nRangeTotal >= RANGE_TOTAL_1[nOverflow + 1]) { nOverflow++; }
// update // update
m_RangeCoderInfo.low -= m_RangeCoderInfo.range * RANGE_TOTAL_1[nOverflow]; m_RangeCoderInfo.low -= m_RangeCoderInfo.range * RANGE_TOTAL_1[nOverflow];
m_RangeCoderInfo.range = m_RangeCoderInfo.range * RANGE_WIDTH_1[nOverflow]; m_RangeCoderInfo.range = m_RangeCoderInfo.range * RANGE_WIDTH_1[nOverflow];
// get the working k // get the working k
int nTempK; int nTempK;
if (nOverflow == (MODEL_ELEMENTS - 1)) if (nOverflow == (MODEL_ELEMENTS - 1))
@@ -215,30 +218,30 @@ int CUnBitArray::DecodeValueRange(UNBIT_ARRAY_STATE & BitArrayState)
{ {
nTempK = (BitArrayState.k < 1) ? 0 : BitArrayState.k - 1; nTempK = (BitArrayState.k < 1) ? 0 : BitArrayState.k - 1;
} }
// figure the extra bits on the left and the left value // figure the extra bits on the left and the left value
if (nTempK <= 16 || m_nVersion < 3910) if (nTempK <= 16 || m_nVersion < 3910)
{ {
nValue = RangeDecodeFastWithUpdate(nTempK); nValue = RangeDecodeFastWithUpdate(nTempK);
} }
else else
{ {
int nX1 = RangeDecodeFastWithUpdate(16); int nX1 = RangeDecodeFastWithUpdate(16);
int nX2 = RangeDecodeFastWithUpdate(nTempK - 16); int nX2 = RangeDecodeFastWithUpdate(nTempK - 16);
nValue = nX1 | (nX2 << 16); nValue = nX1 | (nX2 << 16);
} }
// build the value and output it // build the value and output it
nValue += (nOverflow << nTempK); nValue += (nOverflow << nTempK);
} }
// update nKSum // update nKSum
BitArrayState.nKSum += ((nValue + 1) / 2) - ((BitArrayState.nKSum + 16) >> 5); BitArrayState.nKSum += ((nValue + 1) / 2) - ((BitArrayState.nKSum + 16) >> 5);
// update k // update k
if (BitArrayState.nKSum < K_SUM_MIN_BOUNDARY[BitArrayState.k]) if (BitArrayState.nKSum < K_SUM_MIN_BOUNDARY[BitArrayState.k])
BitArrayState.k--; BitArrayState.k--;
else if (BitArrayState.nKSum >= K_SUM_MIN_BOUNDARY[BitArrayState.k + 1]) else if (BitArrayState.nKSum >= K_SUM_MIN_BOUNDARY[BitArrayState.k + 1])
BitArrayState.k++; BitArrayState.k++;
// output the value (converted to signed) // output the value (converted to signed)
@@ -266,11 +269,11 @@ void CUnBitArray::Finalize()
{ {
// normalize // normalize
while (m_RangeCoderInfo.range <= BOTTOM_VALUE) while (m_RangeCoderInfo.range <= BOTTOM_VALUE)
{ {
m_nCurrentBitIndex += 8; m_nCurrentBitIndex += 8;
m_RangeCoderInfo.range <<= 8; m_RangeCoderInfo.range <<= 8;
} }
// used to back-pedal the last two bytes out // used to back-pedal the last two bytes out
// this should never have been a problem because we've outputted and normalized beforehand // this should never have been a problem because we've outputted and normalized beforehand
// but stopped doing it as of 3.96 in case it accounted for rare decompression failures // but stopped doing it as of 3.96 in case it accounted for rare decompression failures
@@ -283,11 +286,11 @@ void CUnBitArray::GenerateArrayRange(int * pOutputArray, int nElements)
UNBIT_ARRAY_STATE BitArrayState; UNBIT_ARRAY_STATE BitArrayState;
FlushState(BitArrayState); FlushState(BitArrayState);
FlushBitArray(); FlushBitArray();
for (int z = 0; z < nElements; z++) for (int z = 0; z < nElements; z++)
{ {
pOutputArray[z] = DecodeValueRange(BitArrayState); pOutputArray[z] = DecodeValueRange(BitArrayState);
} }
Finalize(); Finalize();
} }