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

View File

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

View File

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

View File

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

View File

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