all repos — mgba @ fabdfc86f3c944d3d61ee728d002f1c014694aaa

mGBA Game Boy Advance Emulator

Add magic numbers to movie files
Jeffrey Pfau jeffrey@endrift.com
Sun, 03 Aug 2014 20:15:34 -0700
commit

fabdfc86f3c944d3d61ee728d002f1c014694aaa

parent

a09bb6d51ad83085f1c4e04a30a19a744a83c749

1 files changed, 34 insertions(+), 6 deletions(-)

jump to
M src/gba/gba-rr.csrc/gba/gba-rr.c

@@ -3,9 +3,12 @@

#include "gba.h" #include "util/vfs.h" -#define BINEXT ".dat" -#define METADATA_FILENAME "metadata" BINEXT +#define BINARY_EXT ".dat" +#define BINARY_MAGIC "GBAb" +#define METADATA_FILENAME "metadata" BINARY_EXT +static bool _emitMagic(struct GBARRContext* rr, struct VFile* vf); +static bool _verifyMagic(struct GBARRContext* rr, struct VFile* vf); static enum GBARRTag _readTag(struct GBARRContext* rr, struct VFile* vf); static bool _seekTag(struct GBARRContext* rr, struct VFile* vf, enum GBARRTag tag); static bool _emitTag(struct GBARRContext* rr, struct VFile* vf, uint8_t tag);

@@ -68,7 +71,7 @@ }

rr->movieStream = 0; rr->streamId = streamId; char buffer[14]; - snprintf(buffer, sizeof(buffer), "%u" BINEXT, streamId); + snprintf(buffer, sizeof(buffer), "%u" BINARY_EXT, streamId); if (GBARRIsRecording(rr)) { int flags = O_CREAT | O_WRONLY; if (streamId > rr->maxStreamId) {

@@ -80,7 +83,7 @@ rr->movieStream = rr->streamDir->openFile(rr->streamDir, buffer, flags);

} else if (GBARRIsPlaying(rr)) { rr->movieStream = rr->streamDir->openFile(rr->streamDir, buffer, O_RDONLY); rr->peekedTag = TAG_INVALID; - if (!rr->movieStream || !_seekTag(rr, rr->movieStream, TAG_BEGIN)) { + if (!rr->movieStream || !_verifyMagic(rr, rr->movieStream) || !_seekTag(rr, rr->movieStream, TAG_BEGIN)) { GBARRStopPlaying(rr); } }

@@ -104,6 +107,7 @@ }

if (!GBARRLoadStream(rr, newStreamId)) { return false; } + _emitMagic(rr, rr->movieStream); rr->maxStreamId = newStreamId; _emitTag(rr, rr->movieStream, TAG_PREVIOUSLY); rr->movieStream->write(rr->movieStream, &oldStreamId, sizeof(oldStreamId));

@@ -120,13 +124,16 @@ return false;

} char buffer[14]; - snprintf(buffer, sizeof(buffer), "%u" BINEXT, rr->streamId); + snprintf(buffer, sizeof(buffer), "%u" BINARY_EXT, rr->streamId); rr->movieStream = rr->streamDir->openFile(rr->streamDir, buffer, O_RDONLY); if (!rr->movieStream) { return false; } rr->autorecord = autorecord; + if (!_verifyMagic(rr, rr->movieStream)) { + return false; + } rr->peekedTag = TAG_INVALID; _readTag(rr, rr->movieStream); // Discard the buffer enum GBARRTag tag = _readTag(rr, rr->movieStream);

@@ -157,8 +164,12 @@ return false;

} char buffer[14]; - snprintf(buffer, sizeof(buffer), "%u" BINEXT, rr->streamId); + snprintf(buffer, sizeof(buffer), "%u" BINARY_EXT, rr->streamId); rr->movieStream = rr->streamDir->openFile(rr->streamDir, buffer, O_TRUNC | O_CREAT | O_WRONLY); + if (!rr->movieStream) { + return false; + } + _emitMagic(rr, rr->movieStream); if (!_emitTag(rr, rr->movieStream, TAG_BEGIN)) { rr->movieStream->close(rr->movieStream); rr->movieStream = 0;

@@ -256,6 +267,23 @@ bool GBARRSkipSegment(struct GBARRContext* rr) {

rr->nextTime = 0; while (_readTag(rr, rr->movieStream) != TAG_EOF); if (!rr->nextTime || !GBARRLoadStream(rr, rr->nextTime)) { + return false; + } + return true; +} + +bool _emitMagic(struct GBARRContext* rr, struct VFile* vf) { + UNUSED(rr); + return vf->write(vf, BINARY_MAGIC, 4) == 4; +} + +bool _verifyMagic(struct GBARRContext* rr, struct VFile* vf) { + UNUSED(rr); + char buffer[4]; + if (vf->read(vf, buffer, sizeof(buffer)) != sizeof(buffer)) { + return false; + } + if (memcmp(buffer, BINARY_MAGIC, sizeof(buffer)) != 0) { return false; } return true;