| 1 |
#ifndef NALL_BEAT_ARCHIVE_HPP |
| 2 |
#define NALL_BEAT_ARCHIVE_HPP |
| 3 |
|
| 4 |
#include <nall/beat/base.hpp> |
| 5 |
|
| 6 |
namespace nall { |
| 7 |
|
| 8 |
struct beatArchive : beatBase { |
| 9 |
bool create(const string& beatname, string pathname, const string& metadata = "") { |
| 10 |
if(fp.open(beatname, file::mode::write) == false) return false; |
| 11 |
if(pathname.endsWith("/") == false) pathname.append("/"); |
| 12 |
|
| 13 |
checksum = ~0; |
| 14 |
writeString("BPA1"); |
| 15 |
writeNumber(metadata.length()); |
| 16 |
writeString(metadata); |
| 17 |
|
| 18 |
lstring list; |
| 19 |
ls(list, pathname, pathname); |
| 20 |
for(auto &name : list) { |
| 21 |
if(name.endsWith("/")) { |
| 22 |
name.rtrim<1>("/"); |
| 23 |
writeNumber(0 | ((name.length() - 1) << 1)); |
| 24 |
writeString(name); |
| 25 |
} else { |
| 26 |
file stream; |
| 27 |
if(stream.open({pathname, name}, file::mode::read) == false) return false; |
| 28 |
writeNumber(1 | ((name.length() - 1) << 1)); |
| 29 |
writeString(name); |
| 30 |
unsigned size = stream.size(); |
| 31 |
writeNumber(size); |
| 32 |
uint32_t checksum = ~0; |
| 33 |
while(size--) { |
| 34 |
uint8_t data = stream.read(); |
| 35 |
write(data); |
| 36 |
checksum = crc32_adjust(checksum, data); |
| 37 |
} |
| 38 |
writeChecksum(~checksum); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
writeChecksum(~checksum); |
| 43 |
fp.close(); |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
bool unpack(const string& beatname, string pathname) { |
| 48 |
if(fp.open(beatname, file::mode::read) == false) return false; |
| 49 |
if(pathname.endsWith("/") == false) pathname.append("/"); |
| 50 |
|
| 51 |
checksum = ~0; |
| 52 |
if(readString(4) != "BPA1") return false; |
| 53 |
unsigned length = readNumber(); |
| 54 |
while(length--) read(); |
| 55 |
|
| 56 |
directory::create(pathname); |
| 57 |
while(fp.offset() < fp.size() - 4) { |
| 58 |
unsigned data = readNumber(); |
| 59 |
string name = readString((data >> 1) + 1); |
| 60 |
if(name.find("\\") || name.find("../")) return false; //block path exploits |
| 61 |
|
| 62 |
if((data & 1) == 0) { |
| 63 |
directory::create({pathname, name}); |
| 64 |
} else { |
| 65 |
file stream; |
| 66 |
if(stream.open({pathname, name}, file::mode::write) == false) return false; |
| 67 |
unsigned size = readNumber(); |
| 68 |
uint32_t checksum = ~0; |
| 69 |
while(size--) { |
| 70 |
uint8_t data = read(); |
| 71 |
stream.write(data); |
| 72 |
checksum = crc32_adjust(checksum, data); |
| 73 |
} |
| 74 |
if(readChecksum(~checksum) == false) return false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return readChecksum(~checksum); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
#endif |