1
0
mirror of https://github.com/ScrelliCopter/VGM-Tools synced 2025-02-21 04:09:25 +11:00

add some basic error checking in DecodeSample

This commit is contained in:
2019-10-02 10:59:07 +10:00
parent 6510096f90
commit 47df3e2177

View File

@@ -25,21 +25,23 @@
typedef struct { uint8_t* data; size_t size; } Buffer; typedef struct { uint8_t* data; size_t size; } Buffer;
void DecodeSample(FILE* fin, const char* name, Buffer* buf) int DecodeSample(FILE* fin, const char* name, Buffer* buf)
{ {
// Get sample data length. // Get sample data length.
uint32_t sampLen = 0; uint32_t sampLen = 0;
fread(&sampLen, sizeof(uint32_t), 1, fin); fread(&sampLen, sizeof(uint32_t), 1, fin);
if (sampLen < sizeof(uint64_t)) if (sampLen < sizeof(uint64_t))
return; return 1;
sampLen -= sizeof(uint64_t); sampLen -= sizeof(uint64_t);
// Resize buffer if needed. // Resize buffer if needed.
if (!buf->data || buf->size < sampLen) if (!buf->data || buf->size < sampLen)
{ {
free(buf->data); free(buf->data);
buf->data = malloc(sampLen);
buf->size = sampLen; buf->size = sampLen;
buf->data = malloc(sampLen);
if (!buf->data)
return 1;
} }
// Ignore 8 bytes. // Ignore 8 bytes.
@@ -49,12 +51,14 @@ void DecodeSample(FILE* fin, const char* name, Buffer* buf)
// Read adpcm data. // Read adpcm data.
fread(buf->data, sizeof(uint8_t), sampLen, fin); fread(buf->data, sizeof(uint8_t), sampLen, fin);
// Write adpcm sample.
FILE* fout = fopen(name, "wb"); FILE* fout = fopen(name, "wb");
if (!fout) if (!fout)
return; return 1;
fwrite(buf->data, sizeof(uint8_t), sampLen, fout); fwrite(buf->data, sizeof(uint8_t), sampLen, fout);
fclose(fout); fclose(fout);
return 0;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@@ -81,13 +85,15 @@ int main(int argc, char** argv)
{ {
printf("ADPCM-A data found at 0x%08lX\n", ftell(file)); printf("ADPCM-A data found at 0x%08lX\n", ftell(file));
snprintf(namebuf, sizeof(namebuf), "smpa_%x.pcm", smpaCount++); snprintf(namebuf, sizeof(namebuf), "smpa_%x.pcm", smpaCount++);
DecodeSample(file, namebuf, &smpBytes); if (DecodeSample(file, namebuf, &smpBytes))
break;
} }
else if (byte == 0x83) else if (byte == 0x83)
{ {
printf("ADPCM-B data found at 0x%08lX\n", ftell(file)); printf("ADPCM-B data found at 0x%08lX\n", ftell(file));
snprintf(namebuf, sizeof(namebuf), "smpb_%x.pcm", smpbCount++); snprintf(namebuf, sizeof(namebuf), "smpb_%x.pcm", smpbCount++);
DecodeSample(file, namebuf, &smpBytes); if (DecodeSample(file, namebuf, &smpBytes))
break;
} }
} }