bsnes/nall/beat/archive.hpp
Tim Allen 29be18ce0c Update to v097r17 release.
byuu says:

Changelog:
- ruby: if DirectSoundCreate fails (no sound device present), return
  false from init instead of crashing
- nall: improved edge case return values for
  (basename,pathname,dirname,...)
- nall: renamed file_system_object class to inode
- nall: varuint_t replaced with VariadicNatural; which contains
  .bit,.bits,.byte ala Natural/Integer
- nall: fixed boolean compilation error on Windows
- WS: popa should not restore SP
- GBA: rewrote the CPU/APU cores to use the .bit,.bits functions;
  removed registers.cpp from each

Note that the GBA changes are extremely major. This is about five hours
worth of extremely delicate work. Any slight errors could break
emulation in extremely bad ways. Let's hold off on extensive testing
until the next WIP, after I do the same to the PPU.

So far ... endrift's SOUNDCNT_X I/O test is failing, although that code
didn't change, so clearly I messed up SOUNDCNT_H somehow ...

To compile on Windows:

1. change nall/string/platform.hpp line 47 to

    return slice(result, 0, 3);

2. change ruby/video.wgl.cpp line 72 to

    auto lock(uint32_t*& data, uint& pitch, uint width, uint height) -> bool {

3. add this line to the very top of hiro/windows/header.cpp:

    #define boolean FuckYouMicrosoft
2016-03-13 11:22:14 +11:00

131 lines
3.9 KiB
C++

#pragma once
#include <nall/beat/file.hpp>
namespace nall { namespace Beat {
struct Archive {
static auto create(const string& beatname, const string& pathname, const string& metadata = "") -> bool;
static auto unpack(const string& beatname, const string& pathname) -> bool;
static auto extract(const string& beatname, const string& pathname) -> vector<uint8_t>;
private:
static auto scan(lstring& result, const string& basename, const string& pathname) -> void;
};
auto Archive::create(const string& beatname, const string& pathname, const string& metadata) -> bool {
if(!beatname.endsWith(".bpa") || !pathname.endsWith("/")) return false; //protect against reversed arguments
File beat{beatname, file::mode::write};
if(!beat) return false; //file not writable?
beat.writes("BPA1");
beat.writevu(metadata.size());
beat.writes(metadata);
lstring contents;
scan(contents, pathname, pathname);
for(auto& name : contents) {
string location{pathname, name};
bool directory = name.endsWith("/");
bool writable = inode::writable(location);
bool executable = inode::executable(location);
uint info = directory << 0 | writable << 1 | executable << 2 | (name.rtrim("/").size() - 1) << 3;
beat.writevu(info);
beat.writes(name);
if(directory) continue;
File input{location, file::mode::read};
if(input) {
auto size = input.size();
beat.writevu(size);
while(size--) beat.write(input.read());
beat.writel(input.checksum.value(), 4);
} else {
beat.writevu(0);
beat.writel(0x00000000, 4);
}
}
beat.writel(beat.checksum.value(), 4);
return true;
}
auto Archive::unpack(const string& beatname, const string& pathname) -> bool {
if(!beatname.endsWith(".bpa") || !pathname.endsWith("/")) return false; //protect against reversed arguments
File beat{beatname, file::mode::read};
if(!beat) return false; //file not readable?
if(beat.reads(4) != "BPA1") return false;
auto size = beat.readvu();
while(size--) beat.read();
directory::create(pathname);
while(beat.offset() < beat.size() - 4) {
auto info = beat.readvu();
auto name = beat.reads((info >> 3) + 1);
if(name.find("\\") || name.find("../")) return false; //block path exploits
string location{pathname, name};
bool directory = info & 1;
bool writable = info & 2;
bool executable = info & 4;
if(directory) {
if(!nall::directory::create(location)) return false;
} else {
File output{location, file::mode::write};
if(!output) return false;
auto size = beat.readvu();
while(size--) output.write(beat.read());
if(beat.readl(4) != output.checksum.value()) return false;
}
}
uint32_t checksum = beat.checksum.value();
return beat.readl(4) == checksum;
}
auto Archive::extract(const string& beatname, const string& filename) -> vector<uint8_t> {
File beat{beatname, file::mode::read};
if(!beat) return {}; //file not readable?
if(beat.reads(4) != "BPA1") return {};
auto size = beat.readvu();
beat.seek(beat.offset() + size);
while(beat.offset() < beat.size() - 4) {
auto info = beat.readvu();
auto name = beat.reads((info >> 3) + 1);
if(info & 1) continue; //ignore directories
auto size = beat.readvu();
if(name != filename) {
beat.seek(beat.offset() + size + 4);
continue;
}
vector<uint8_t> result;
result.resize(size);
beat.checksum.reset();
for(auto n : range(size)) result[n] = beat.read();
uint32_t checksum = beat.checksum.value();
if(beat.readl(4) != checksum) return {};
return result;
}
return {};
}
auto Archive::scan(lstring& result, const string& basename, const string& pathname) -> void {
for(auto& name : directory::contents(pathname)) {
result.append(string{pathname, name}.ltrim(basename, 1L));
if(name.endsWith("/")) scan(result, basename, {pathname, name});
}
}
}}