added an endianness check in VorbisDecoder.cpp

This commit is contained in:
bill@Ixion
2009-08-31 16:29:27 -05:00
parent d3d7d787e4
commit b8677cebd0
3 changed files with 145 additions and 128 deletions
+26 -18
View File
@@ -1,21 +1,21 @@
/** /**
* Copyright (c) 2006-2009 LOVE Development Team * Copyright (c) 2006-2009 LOVE Development Team
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
* arising from the use of this software. * arising from the use of this software.
* *
* Permission is granted to anyone to use this software for any purpose, * Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not * 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software * claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be * in a product, an acknowledgment in the product documentation would be
* appreciated but is not required. * appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be * 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software. * misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
#ifndef LOVE_CONFIG_H #ifndef LOVE_CONFIG_H
@@ -35,6 +35,14 @@
# define LOVE_MACOS 1 # define LOVE_MACOS 1
#endif #endif
// Endianness.
#if defined(__i386__) || defined(__i386)
# define LOVE_LITTLE_ENDIAN 1
#endif
#if defined(__ppc__) || defined(__ppc)
# define LOVE_BIG_ENDIAN 1
#endif
// Warnings. // Warnings.
#ifndef _CRT_SECURE_NO_WARNINGS #ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS # define _CRT_SECURE_NO_WARNINGS
+106 -98
View File
@@ -21,6 +21,7 @@
#include "VorbisDecoder.h" #include "VorbisDecoder.h"
#include <string.h> #include <string.h>
#include <common/config.h>
#include <common/Exception.h> #include <common/Exception.h>
namespace love namespace love
@@ -32,41 +33,41 @@ namespace lullaby
/** /**
* CALLBACK FUNCTIONS * CALLBACK FUNCTIONS
**/ **/
int vorbisClose(void * datasource /* ptr to the data that the vorbis files need*/) int vorbisClose(void * datasource /* ptr to the data that the vorbis files need*/)
{ {
// Does nothing (handled elsewhere) // Does nothing (handled elsewhere)
return 1; return 1;
} }
size_t vorbisRead(void * ptr /* ptr to the data that the vorbis files need*/, size_t vorbisRead(void * ptr /* ptr to the data that the vorbis files need*/,
size_t byteSize /* how big a byte is*/, size_t byteSize /* how big a byte is*/,
size_t sizeToRead /* How much we can read*/, size_t sizeToRead /* How much we can read*/,
void * datasource /* this is a pointer to the data we passed into ov_open_callbacks (our SOggFile struct*/) void * datasource /* this is a pointer to the data we passed into ov_open_callbacks (our SOggFile struct*/)
{ {
size_t spaceToEOF; // How much more we can read till we hit the EOF marker size_t spaceToEOF; // How much more we can read till we hit the EOF marker
size_t actualSizeToRead; // How much data we are actually going to read from memory size_t actualSizeToRead; // How much data we are actually going to read from memory
SOggFile* vorbisData; // Our vorbis data, for the typecast SOggFile* vorbisData; // Our vorbis data, for the typecast
// Get the data in the right format // Get the data in the right format
vorbisData = (SOggFile*)datasource; vorbisData = (SOggFile*)datasource;
// Calculate how much we need to read. This can be sizeToRead*byteSize or less depending on how near the EOF marker we are // Calculate how much we need to read. This can be sizeToRead*byteSize or less depending on how near the EOF marker we are
spaceToEOF = vorbisData->dataSize - vorbisData->dataRead; spaceToEOF = vorbisData->dataSize - vorbisData->dataRead;
if ((sizeToRead*byteSize) < spaceToEOF) if ((sizeToRead*byteSize) < spaceToEOF)
actualSizeToRead = (sizeToRead*byteSize); actualSizeToRead = (sizeToRead*byteSize);
else else
actualSizeToRead = spaceToEOF; actualSizeToRead = spaceToEOF;
// A simple copy of the data from memory to the datastruct that the vorbis libs will use // A simple copy of the data from memory to the datastruct that the vorbis libs will use
if (actualSizeToRead) if (actualSizeToRead)
{ {
// Copy the data from the start of the file PLUS how much we have already read in // Copy the data from the start of the file PLUS how much we have already read in
memcpy(ptr, (char*)vorbisData->dataPtr + vorbisData->dataRead, actualSizeToRead); memcpy(ptr, (char*)vorbisData->dataPtr + vorbisData->dataRead, actualSizeToRead);
// Increase by how much we have read by // Increase by how much we have read by
vorbisData->dataRead += (actualSizeToRead); vorbisData->dataRead += (actualSizeToRead);
} }
// Return how much we read (in the same way fread would) // Return how much we read (in the same way fread would)
return actualSizeToRead; return actualSizeToRead;
} }
@@ -74,43 +75,43 @@ namespace lullaby
ogg_int64_t offset /*offset from the point we wish to seek to*/, ogg_int64_t offset /*offset from the point we wish to seek to*/,
int whence /*where we want to seek to*/) int whence /*where we want to seek to*/)
{ {
size_t spaceToEOF; // How much more we can read till we hit the EOF marker size_t spaceToEOF; // How much more we can read till we hit the EOF marker
ogg_int64_t actualOffset; // How much we can actually offset it by ogg_int64_t actualOffset; // How much we can actually offset it by
SOggFile* vorbisData; // The data we passed in (for the typecast) SOggFile* vorbisData; // The data we passed in (for the typecast)
// Get the data in the right format // Get the data in the right format
vorbisData = (SOggFile*) datasource; vorbisData = (SOggFile*) datasource;
// Goto where we wish to seek to // Goto where we wish to seek to
switch (whence) switch (whence)
{ {
case SEEK_SET: // Seek to the start of the data file case SEEK_SET: // Seek to the start of the data file
// Make sure we are not going to the end of the file // Make sure we are not going to the end of the file
if (vorbisData->dataSize >= offset) if (vorbisData->dataSize >= offset)
actualOffset = offset; actualOffset = offset;
else else
actualOffset = vorbisData->dataSize; actualOffset = vorbisData->dataSize;
// Set where we now are // Set where we now are
vorbisData->dataRead = (int)actualOffset; vorbisData->dataRead = (int)actualOffset;
break; break;
case SEEK_CUR: // Seek from where we are case SEEK_CUR: // Seek from where we are
// Make sure we dont go past the end // Make sure we dont go past the end
spaceToEOF = vorbisData->dataSize - vorbisData->dataRead; spaceToEOF = vorbisData->dataSize - vorbisData->dataRead;
if (offset < spaceToEOF) if (offset < spaceToEOF)
actualOffset = (offset); actualOffset = (offset);
else else
actualOffset = spaceToEOF; actualOffset = spaceToEOF;
// Seek from our currrent location // Seek from our currrent location
vorbisData->dataRead += (int)actualOffset; vorbisData->dataRead += (int)actualOffset;
break; break;
case SEEK_END: // Seek from the end of the file case SEEK_END: // Seek from the end of the file
vorbisData->dataRead = vorbisData->dataSize+1; vorbisData->dataRead = vorbisData->dataSize+1;
break; break;
default: default:
throw love::Exception("Unknown seek command in vorbisSeek\n"); throw love::Exception("Unknown seek command in vorbisSeek\n");
break; break;
}; };
return 0; return 0;
} }
@@ -127,11 +128,18 @@ namespace lullaby
VorbisDecoder::VorbisDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate) VorbisDecoder::VorbisDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate)
: Decoder(data, ext, bufferSize, sampleRate) : Decoder(data, ext, bufferSize, sampleRate)
{ {
// Initialize callbacks // Initialize callbacks
vorbisCallbacks.close_func = vorbisClose; vorbisCallbacks.close_func = vorbisClose;
vorbisCallbacks.seek_func = vorbisSeek; vorbisCallbacks.seek_func = vorbisSeek;
vorbisCallbacks.read_func = vorbisRead; vorbisCallbacks.read_func = vorbisRead;
vorbisCallbacks.tell_func = vorbisTell; vorbisCallbacks.tell_func = vorbisTell;
// Check endianness
#ifdef LOVE_BIG_ENDIAN
endian = 1;
#else
endian = 0;
#endif
// Initialize OGG file // Initialize OGG file
oggFile.dataPtr = (char *) data->getData(); oggFile.dataPtr = (char *) data->getData();
@@ -143,7 +151,7 @@ namespace lullaby
throw love::Exception("Could not read Ogg bitstream"); throw love::Exception("Could not read Ogg bitstream");
// Get info and comments // Get info and comments
vorbisInfo = ov_info(&handle, -1); vorbisInfo = ov_info(&handle, -1);
vorbisComment = ov_comment(&handle, -1); vorbisComment = ov_comment(&handle, -1);
} }
@@ -176,7 +184,7 @@ namespace lullaby
{ {
int bits = this->getBits(); int bits = this->getBits();
int size = 0; int size = 0;
int section; int section;
int result; int result;
if(bits == 16) if(bits == 16)
@@ -186,7 +194,7 @@ namespace lullaby
while(size < bufferSize) while(size < bufferSize)
{ {
result = ov_read(&handle, (char*) buffer + size, bufferSize - size, 0, bits, 1, &section); result = ov_read(&handle, (char*) buffer + size, bufferSize - size, endian, bits, 1, &section);
if(result > 0) if(result > 0)
size += result; size += result;
@@ -194,17 +202,17 @@ namespace lullaby
{ {
switch(result) switch(result)
{ {
case OV_EREAD: case OV_EREAD:
throw love::Exception("Vorbis read: Read from media"); throw love::Exception("Vorbis read: Read from media");
case OV_ENOTVORBIS: case OV_ENOTVORBIS:
throw love::Exception("Vorbis read: Not Vorbis data"); throw love::Exception("Vorbis read: Not Vorbis data");
case OV_EVERSION: case OV_EVERSION:
throw love::Exception("Vorbis read: Vorbis version mismatch"); throw love::Exception("Vorbis read: Vorbis version mismatch");
case OV_EBADHEADER: case OV_EBADHEADER:
throw love::Exception("Vorbis read: Invalid Vorbis header"); throw love::Exception("Vorbis read: Invalid Vorbis header");
case OV_EFAULT: case OV_EFAULT:
throw love::Exception("Vorbis read: Internal logic fault (bug or heap/stack corruption)"); throw love::Exception("Vorbis read: Internal logic fault (bug or heap/stack corruption)");
default: default:
throw love::Exception("Vorbis read: Unknown error"); throw love::Exception("Vorbis read: Unknown error");
} }
} }
@@ -257,20 +265,20 @@ namespace lullaby
return (result != 0); return (result != 0);
} }
int VorbisDecoder::getChannels() const int VorbisDecoder::getChannels() const
{ {
return vorbisInfo->channels; return vorbisInfo->channels;
}
int VorbisDecoder::getBits() const
{
//return vorbisInfo->bitrate_nominal / 10000;
return 16;
} }
int VorbisDecoder::getSampleRate() const int VorbisDecoder::getBits() const
{ {
return vorbisInfo->rate; //return vorbisInfo->bitrate_nominal / 10000;
return 16;
}
int VorbisDecoder::getSampleRate() const
{
return vorbisInfo->rate;
} }
} // lullaby } // lullaby
+13 -12
View File
@@ -35,22 +35,23 @@ namespace sound
{ {
namespace lullaby namespace lullaby
{ {
// Struct for handling data // Struct for handling data
struct SOggFile struct SOggFile
{ {
char * dataPtr; // Pointer to the data in memory char * dataPtr; // Pointer to the data in memory
int dataSize; // Size of the data int dataSize; // Size of the data
int dataRead; // How much we've read so far int dataRead; // How much we've read so far
}; };
class VorbisDecoder : public Decoder class VorbisDecoder : public Decoder
{ {
private: private:
SOggFile oggFile; // (see struct) SOggFile oggFile; // (see struct)
ov_callbacks vorbisCallbacks; // Callbacks used to read the file from mem ov_callbacks vorbisCallbacks; // Callbacks used to read the file from mem
OggVorbis_File handle; // Handle to the file OggVorbis_File handle; // Handle to the file
vorbis_info * vorbisInfo; // Info vorbis_info * vorbisInfo; // Info
vorbis_comment * vorbisComment; // Comments vorbis_comment * vorbisComment; // Comments
int endian; // Endianness
public: public:
@@ -64,7 +65,7 @@ namespace lullaby
bool seek(float s); bool seek(float s);
bool rewind(); bool rewind();
bool isSeekable(); bool isSeekable();
int getChannels() const; int getChannels() const;
int getBits() const; int getBits() const;
int getSampleRate() const; int getSampleRate() const;