o add missing files
This commit is contained in:
49
tools/bsnes/lib/libreader/filereader.cpp
Executable file
49
tools/bsnes/lib/libreader/filereader.cpp
Executable file
@@ -0,0 +1,49 @@
|
||||
#ifdef READER_CPP
|
||||
#include "filereader.hpp"
|
||||
|
||||
unsigned FileReader::size() {
|
||||
return fp.size();
|
||||
}
|
||||
|
||||
//This function will allocate memory even if open() fails.
|
||||
//This is needed so that when SRAM files do not exist, the
|
||||
//memory for the SRAM data will be allocated still.
|
||||
//The memory is flushed to 0x00 when no file is opened.
|
||||
uint8_t* FileReader::read(unsigned length) {
|
||||
uint8_t *data = 0;
|
||||
|
||||
if(length == 0) {
|
||||
//read the entire file into RAM
|
||||
data = new(zeromemory) uint8_t[fp.size()];
|
||||
if(fp.open()) fp.read(data, fp.size());
|
||||
} else if(length > fp.size()) {
|
||||
//read the entire file into RAM, pad the rest with 0x00s
|
||||
data = new(zeromemory) uint8_t[length];
|
||||
if(fp.open()) fp.read(data, fp.size());
|
||||
} else { //filesize >= length
|
||||
//read only what was requested
|
||||
data = new(zeromemory) uint8_t[length];
|
||||
if(fp.open()) fp.read(data, length);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool FileReader::ready() {
|
||||
return fp.open();
|
||||
}
|
||||
|
||||
FileReader::FileReader(const char *fn) {
|
||||
if(!fp.open(fn, file::mode_read)) return;
|
||||
|
||||
if(fp.size() == 0) {
|
||||
//empty file
|
||||
fp.close();
|
||||
}
|
||||
}
|
||||
|
||||
FileReader::~FileReader() {
|
||||
if(fp.open()) fp.close();
|
||||
}
|
||||
|
||||
#endif //ifdef READER_CPP
|
||||
12
tools/bsnes/lib/libreader/filereader.hpp
Executable file
12
tools/bsnes/lib/libreader/filereader.hpp
Executable file
@@ -0,0 +1,12 @@
|
||||
class FileReader : public Reader {
|
||||
public:
|
||||
unsigned size();
|
||||
uint8_t* read(unsigned length = 0);
|
||||
bool ready();
|
||||
|
||||
FileReader(const char *fn);
|
||||
~FileReader();
|
||||
|
||||
private:
|
||||
file fp;
|
||||
};
|
||||
84
tools/bsnes/lib/libreader/gzreader.cpp
Executable file
84
tools/bsnes/lib/libreader/gzreader.cpp
Executable file
@@ -0,0 +1,84 @@
|
||||
#ifdef READER_CPP
|
||||
#include "gzreader.hpp"
|
||||
|
||||
unsigned GZReader::size() {
|
||||
return filesize;
|
||||
}
|
||||
|
||||
//This function will allocate memory even if open() fails.
|
||||
//This is needed so that when SRAM files do not exist, the
|
||||
//memory for the SRAM data will be allocated still.
|
||||
//The memory is flushed to 0x00 when no file is opened.
|
||||
uint8_t* GZReader::read(unsigned length) {
|
||||
uint8_t *data = 0;
|
||||
|
||||
if(length == 0) {
|
||||
//read the entire file into RAM
|
||||
data = new(zeromemory) uint8_t[filesize];
|
||||
if(gp) gzread(gp, data, filesize);
|
||||
} else if(length > filesize) {
|
||||
//read the entire file into RAM, pad the rest with 0x00s
|
||||
data = new(zeromemory) uint8_t[length];
|
||||
if(gp) gzread(gp, data, filesize);
|
||||
} else { //filesize >= length
|
||||
//read only what was requested
|
||||
data = new(zeromemory) uint8_t[length];
|
||||
if(gp) gzread(gp, data, length);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool GZReader::ready() {
|
||||
return (gp != 0);
|
||||
}
|
||||
|
||||
GZReader::GZReader(const char *fn) : gp(0) {
|
||||
#if !defined(_WIN32)
|
||||
fp = fopen(fn, "rb");
|
||||
#else
|
||||
fp = _wfopen(utf16_t(fn), L"rb");
|
||||
#endif
|
||||
if(!fp) return;
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
filesize = ftell(fp);
|
||||
|
||||
if(filesize < 4) {
|
||||
//too small to be a valid GZ archive
|
||||
fclose(fp);
|
||||
fp = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(fp, -4, SEEK_END);
|
||||
unsigned gzsize;
|
||||
gzsize = fgetc(fp);
|
||||
gzsize |= fgetc(fp) << 8;
|
||||
gzsize |= fgetc(fp) << 16;
|
||||
gzsize |= fgetc(fp) << 24;
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
//zlib does not support UTF-8 filenames on Windows,
|
||||
//thus _wfopen() wrapper above + fileno() wrapper here.
|
||||
gp = gzdopen(fileno(fp), "rb");
|
||||
if(!gp) return;
|
||||
|
||||
if(gzdirect(gp) == false) filesize = gzsize;
|
||||
|
||||
if(filesize == 0) {
|
||||
//archive is empty
|
||||
gzclose(gp);
|
||||
gp = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
GZReader::~GZReader() {
|
||||
if(gp) {
|
||||
gzclose(gp);
|
||||
gp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //ifdef READER_CPP
|
||||
16
tools/bsnes/lib/libreader/gzreader.hpp
Executable file
16
tools/bsnes/lib/libreader/gzreader.hpp
Executable file
@@ -0,0 +1,16 @@
|
||||
#include <zlib/zlib.h>
|
||||
|
||||
class GZReader : public Reader {
|
||||
private:
|
||||
FILE *fp;
|
||||
gzFile gp;
|
||||
unsigned filesize;
|
||||
|
||||
public:
|
||||
unsigned size();
|
||||
uint8_t* read(unsigned length = 0);
|
||||
bool ready();
|
||||
|
||||
GZReader(const char *fn);
|
||||
~GZReader();
|
||||
};
|
||||
36
tools/bsnes/lib/libreader/jmareader.cpp
Executable file
36
tools/bsnes/lib/libreader/jmareader.cpp
Executable file
@@ -0,0 +1,36 @@
|
||||
#ifdef READER_CPP
|
||||
#include "jmareader.hpp"
|
||||
|
||||
unsigned JMAReader::size() {
|
||||
return filesize;
|
||||
}
|
||||
|
||||
uint8_t* JMAReader::read(unsigned length) {
|
||||
uint8_t *data = 0;
|
||||
if(!filesize) return 0;
|
||||
|
||||
if(length <= filesize) {
|
||||
//read the entire file into RAM
|
||||
data = new(zeromemory) uint8_t[filesize];
|
||||
JMAFile.extract_file(cname, data);
|
||||
} else if(length > filesize) {
|
||||
//read the entire file into RAM, pad the rest with 0x00s
|
||||
data = new(zeromemory) uint8_t[length];
|
||||
JMAFile.extract_file(cname, data);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
JMAReader::JMAReader(const char *fn) : JMAFile(fn), filesize(0) {
|
||||
std::vector<JMA::jma_public_file_info> file_info = JMAFile.get_files_info();
|
||||
for(std::vector<JMA::jma_public_file_info>::iterator i = file_info.begin(); i != file_info.end(); i++) {
|
||||
//Check for valid ROM based on size
|
||||
if((i->size <= 0x1000000 + 512) && (i->size > filesize)) {
|
||||
cname = i->name;
|
||||
filesize = i->size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif //ifdef READER_CPP
|
||||
14
tools/bsnes/lib/libreader/jmareader.hpp
Executable file
14
tools/bsnes/lib/libreader/jmareader.hpp
Executable file
@@ -0,0 +1,14 @@
|
||||
#include <libjma/jma.h>
|
||||
|
||||
class JMAReader : public Reader {
|
||||
public:
|
||||
unsigned size();
|
||||
uint8_t* read(unsigned length = 0);
|
||||
|
||||
JMAReader(const char *fn);
|
||||
|
||||
private:
|
||||
JMA::jma_open JMAFile;
|
||||
unsigned filesize;
|
||||
std::string cname;
|
||||
};
|
||||
36
tools/bsnes/lib/libreader/libreader.cpp
Executable file
36
tools/bsnes/lib/libreader/libreader.cpp
Executable file
@@ -0,0 +1,36 @@
|
||||
#include "libreader.hpp"
|
||||
#define READER_CPP
|
||||
|
||||
#include "filereader.cpp"
|
||||
|
||||
#if defined(GZIP_SUPPORT)
|
||||
#include "gzreader.cpp"
|
||||
#include "zipreader.cpp"
|
||||
#endif
|
||||
|
||||
#if defined(JMA_SUPPORT)
|
||||
#include "jmareader.cpp"
|
||||
#endif
|
||||
|
||||
Reader::Type Reader::detect(const char *fn, bool inspectheader) {
|
||||
file fp;
|
||||
if(!fp.open(fn, file::mode_read)) return Unknown;
|
||||
|
||||
uint8_t p[8];
|
||||
memset(p, 0, sizeof p);
|
||||
fp.read(p, 8);
|
||||
fp.close();
|
||||
|
||||
if(inspectheader == true) {
|
||||
//inspect file header to determine type
|
||||
if(p[0] == 0x1f && p[1] == 0x8b && p[2] == 0x08 && p[3] <= 0x1f) return GZIP;
|
||||
if(p[0] == 0x50 && p[1] == 0x4b && p[2] == 0x03 && p[3] == 0x04) return ZIP;
|
||||
if(p[0] == 0x4a && p[1] == 0x4d && p[2] == 0x41 && p[3] == 0x00 && p[4] == 0x4e) return JMA;
|
||||
} else {
|
||||
//check file extension to determine type
|
||||
if(striend(fn, ".gz")) return GZIP;
|
||||
if(striend(fn, ".zip") || striend(fn, ".z")) return ZIP;
|
||||
if(striend(fn, ".jma")) return JMA;
|
||||
}
|
||||
return Normal;
|
||||
}
|
||||
21
tools/bsnes/lib/libreader/libreader.hpp
Executable file
21
tools/bsnes/lib/libreader/libreader.hpp
Executable file
@@ -0,0 +1,21 @@
|
||||
#include <nall/file.hpp>
|
||||
#include <nall/platform.hpp>
|
||||
#include <nall/stdint.hpp>
|
||||
#include <nall/string.hpp>
|
||||
using namespace nall;
|
||||
|
||||
class Reader {
|
||||
public:
|
||||
enum Type {
|
||||
Unknown,
|
||||
Normal,
|
||||
GZIP,
|
||||
ZIP,
|
||||
JMA,
|
||||
};
|
||||
|
||||
static Type detect(const char *fn, bool inspectheader);
|
||||
virtual unsigned size() = 0;
|
||||
virtual uint8_t* read(unsigned length = 0) = 0;
|
||||
virtual bool ready() { return true; } //can only call read() when ready() returns true
|
||||
};
|
||||
60
tools/bsnes/lib/libreader/zipreader.cpp
Executable file
60
tools/bsnes/lib/libreader/zipreader.cpp
Executable file
@@ -0,0 +1,60 @@
|
||||
#ifdef READER_CPP
|
||||
#include "zipreader.hpp"
|
||||
|
||||
unsigned ZipReader::size() {
|
||||
return filesize;
|
||||
}
|
||||
|
||||
uint8_t* ZipReader::read(unsigned length) {
|
||||
if(!filesize) return 0;
|
||||
|
||||
uint8_t *data = 0;
|
||||
if(length <= filesize) {
|
||||
//read the entire file into RAM
|
||||
data = new(zeromemory) uint8_t[filesize];
|
||||
unzReadCurrentFile(zipfile, data, filesize);
|
||||
} else { /* length > filesize */
|
||||
//read the entire file into RAM, pad the rest with 0x00s
|
||||
data = new(zeromemory) uint8_t[length];
|
||||
unzReadCurrentFile(zipfile, data, filesize);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool ZipReader::ready() {
|
||||
return zipready;
|
||||
}
|
||||
|
||||
ZipReader::ZipReader(const char *fn) : filesize(0), zipready(false) {
|
||||
unz_file_info cFileInfo; //Create variable to hold info for a compressed file
|
||||
char cFileName[sizeof(cname)];
|
||||
|
||||
if(zipfile = unzOpen(fn)) { //Open zip file
|
||||
for(int cFile = unzGoToFirstFile(zipfile); cFile == UNZ_OK; cFile = unzGoToNextFile(zipfile)) {
|
||||
//Gets info on current file, and places it in cFileInfo
|
||||
unzGetCurrentFileInfo(zipfile, &cFileInfo, cFileName, sizeof(cname), 0, 0, 0, 0);
|
||||
|
||||
//verify uncompressed file is not bigger than max ROM size
|
||||
if((cFileInfo.uncompressed_size <= 0x1000000 + 512) && (cFileInfo.uncompressed_size > filesize)) {
|
||||
strcpy(cname, cFileName);
|
||||
filesize = cFileInfo.uncompressed_size;
|
||||
}
|
||||
}
|
||||
|
||||
if(filesize) {
|
||||
unzLocateFile(zipfile, cname, 1);
|
||||
unzOpenCurrentFile(zipfile);
|
||||
zipready = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ZipReader::~ZipReader() {
|
||||
if(zipfile) {
|
||||
unzCloseCurrentFile(zipfile);
|
||||
unzClose(zipfile);
|
||||
}
|
||||
}
|
||||
|
||||
#endif //ifdef READER_CPP
|
||||
19
tools/bsnes/lib/libreader/zipreader.hpp
Executable file
19
tools/bsnes/lib/libreader/zipreader.hpp
Executable file
@@ -0,0 +1,19 @@
|
||||
#include <zlib/unzip.h>
|
||||
|
||||
#define ZIP_MAX_FILE_NAME PATH_MAX
|
||||
|
||||
class ZipReader : public Reader {
|
||||
public:
|
||||
unsigned size();
|
||||
uint8_t* read(unsigned length = 0);
|
||||
bool ready();
|
||||
|
||||
ZipReader(const char *fn);
|
||||
~ZipReader();
|
||||
|
||||
private:
|
||||
unzFile zipfile;
|
||||
unsigned filesize;
|
||||
bool zipready;
|
||||
char cname[PATH_MAX];
|
||||
};
|
||||
Reference in New Issue
Block a user