move more wavetable loading logic

now it's safer
This commit is contained in:
tildearrow 2022-01-21 18:17:05 -05:00
parent 186e491c59
commit 724b1cd1a8
6 changed files with 41 additions and 19 deletions

8
src/engine/dataErrors.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef _DATA_ERRORS_H
#define _DATA_ERRORS_H
enum DivDataErrors {
DIV_DATA_SUCCESS=0,
DIV_DATA_INVALID_HEADER=1,
DIV_DATA_INVALID_DATA=2
};
#endif

View file

@ -1,3 +1,4 @@
#include "dataErrors.h"
#include <cstdint> #include <cstdint>
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include "engine.h" #include "engine.h"
@ -1424,18 +1425,15 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
// read wavetables // read wavetables
for (int i=0; i<ds.waveLen; i++) { for (int i=0; i<ds.waveLen; i++) {
reader.seek(wavePtr[i],SEEK_SET);
reader.read(magic,4);
if (strcmp(magic,"WAVE")!=0) {
logE("%d: invalid wavetable header!\n",i);
lastError="invalid wavetable header!";
delete[] file;
return false;
}
DivWavetable* wave=new DivWavetable; DivWavetable* wave=new DivWavetable;
reader.seek(wavePtr[i],SEEK_SET); reader.seek(wavePtr[i],SEEK_SET);
wave->readWaveData(reader,ds.version); if (wave->readWaveData(reader,ds.version)!=DIV_DATA_SUCCESS) {
lastError="invalid wavetable header/data!";
delete wave;
delete[] file;
return false;
}
ds.wave.push_back(wave); ds.wave.push_back(wave);
} }
@ -3126,15 +3124,13 @@ bool DivEngine::addWaveFromFile(const char* path) {
reader.seek(16,SEEK_SET); reader.seek(16,SEEK_SET);
short version=reader.readS(); short version=reader.readS();
reader.readS(); // reserved reader.readS(); // reserved
reader.read(magic,4); reader.seek(20,SEEK_SET);
if (memcmp(magic,"WAVE",4)!=0) { if (wave->readWaveData(reader,version)!=DIV_DATA_SUCCESS) {
logE("invalid wavetable header!\n"); lastError="invalid wavetable header/data!";
lastError="invalid wavetable header!"; delete wave;
delete[] buf; delete[] buf;
return false; return false;
} }
reader.seek(20,SEEK_SET);
wave->readWaveData(reader,version);
} else { } else {
try { try {
// read as .dmw // read as .dmw

View file

@ -2,6 +2,7 @@
#define _ENGINE_H #define _ENGINE_H
#include "song.h" #include "song.h"
#include "dispatch.h" #include "dispatch.h"
#include "dataErrors.h"
#include "safeWriter.h" #include "safeWriter.h"
#include "../audio/taAudio.h" #include "../audio/taAudio.h"
#include "blip_buf.h" #include "blip_buf.h"

View file

@ -1,3 +1,4 @@
#include "dataErrors.h"
#include "engine.h" #include "engine.h"
#include "wavetable.h" #include "wavetable.h"
#include "../ta-log.h" #include "../ta-log.h"
@ -16,15 +17,24 @@ void DivWavetable::putWaveData(SafeWriter* w) {
} }
} }
void DivWavetable::readWaveData(SafeReader& reader, short version) { DivDataErrors DivWavetable::readWaveData(SafeReader& reader, short version) {
reader.readI(); char magic[4];
reader.readI(); reader.read(magic,4);
if (memcmp(magic,"WAVE",4)!=0) {
return DIV_DATA_INVALID_HEADER;
}
reader.readI(); // reserved
reader.readString(); // ignored for now reader.readString(); // ignored for now
len=reader.readI(); len=reader.readI();
min=reader.readI(); min=reader.readI();
max=reader.readI(); max=reader.readI();
if (len>256 || min!=0 || max>255) return DIV_DATA_INVALID_DATA;
reader.read(data,4*len); reader.read(data,4*len);
return DIV_DATA_SUCCESS;
} }
bool DivWavetable::save(const char* path) { bool DivWavetable::save(const char* path) {

View file

@ -1,13 +1,14 @@
#ifndef _WAVETABLE_H #ifndef _WAVETABLE_H
#define _WAVETABLE_H #define _WAVETABLE_H
#include "safeWriter.h" #include "safeWriter.h"
#include "dataErrors.h"
struct DivWavetable { struct DivWavetable {
int len, min, max; int len, min, max;
int data[256]; int data[256];
void putWaveData(SafeWriter* w); void putWaveData(SafeWriter* w);
void readWaveData(SafeReader& reader, short version); DivDataErrors readWaveData(SafeReader& reader, short version);
bool save(const char* path); bool save(const char* path);
DivWavetable(): DivWavetable():
len(32), len(32),

View file

@ -3922,6 +3922,12 @@ bool FurnaceGUI::loop() {
curFileDialog==GUI_FILE_EXPORT_AUDIO_PER_CHANNEL) { curFileDialog==GUI_FILE_EXPORT_AUDIO_PER_CHANNEL) {
checkExtension(".wav"); checkExtension(".wav");
} }
if (curFileDialog==GUI_FILE_INS_SAVE) {
checkExtension(".fui");
}
if (curFileDialog==GUI_FILE_WAVE_SAVE) {
checkExtension(".fuw");
}
String copyOfName=fileName; String copyOfName=fileName;
switch (curFileDialog) { switch (curFileDialog) {
case GUI_FILE_OPEN: case GUI_FILE_OPEN: