Use C++ iostream in untar, enforce naming convention, remodel threading to use join, reduce lambda usage

This commit is contained in:
msqr1
2024-08-20 22:59:13 -07:00
parent 2122902190
commit 53262fe7f3
12 changed files with 125 additions and 102 deletions

View File

@@ -1,45 +1,46 @@
#include "Util.h"
void fireEv(int index, const char* content, const char* type) {
MAIN_THREAD_EM_ASM({
objs[$0].dispatchEvent(new CustomEvent($2 === 0 ? "0" : UTF8ToString($2), { "detail" : UTF8ToString($1) }));
}, index, content, type);
}
int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
if(memcmp(tar + 257, "ustar", 5)) return IncorrectFormat;
unsigned char* ptr = tar;
size_t size{0};
std::string path{};
if(std::memcmp(tar + 257, "ustar", 5)) return IncorrectFormat;
size_t size{};
std::string path;
path.reserve(100); // Max length
unsigned char* end = tar + tarSize;
while(ptr <= end) {
if(ptr[156] != '5' && ptr[156] != 0 &&
ptr[156] != '0') {
while(tar <= end) {
if(tar[156] != '5' && tar[156] != 0 &&
tar[156] != '0') {
return IncorrectFiletype;
}
path.clear();
path += reinterpret_cast<char*>(ptr + 345);
path += reinterpret_cast<char*>(ptr);
ptr += 124;
path += reinterpret_cast<char*>(tar + 345);
path += reinterpret_cast<char*>(tar);
tar += 124;
for(int i{0}; i < 11; i++) {
size *= 8;
size += *ptr - 48;
ptr++;
size += *tar - 48;
tar++;
}
ptr += 377;
tar += 377;
size_t firstSlash = path.find_first_of("/");
if(firstSlash == std::string::npos) {
if(size != 0) ptr += size + 512 - size % 512;
if(size != 0) tar += size + 512 - size % 512;
continue;
}
path = storepath + path.substr(firstSlash);
std::ofstream file;
if(size == 0) fs::create_directory(path);
else {
int fd {open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0777)};
if(fd == -1) return FailedOpen;
int res = write(fd, ptr, size);
if(res == -1) return FailedWrite;
if(close(fd) == -1) return FailedClose;
ptr += size + 512 - size % 512;
file.open(path, std::ios::trunc | std::ios::binary);
if(!file) return FailedOpen;
if(!file.write(reinterpret_cast<char*>(tar), size)) return FailedWrite;
file.close();
if(!file) return FailedClose;
tar += size + 512 - size % 512;
}
}
return Successful;