diff --git a/GenTosNoAvb.h b/GenTosNoAvb.h index 9619efb..6020f92 100644 --- a/GenTosNoAvb.h +++ b/GenTosNoAvb.h @@ -6,22 +6,25 @@ #include #include +//额外FILE库 +#include "core/file_io.h" + class TosPatcher { private: // 加载文件到内存(只读) static uint8_t* loadfile(const char* fn, size_t* num) { size_t n, j = 0; uint8_t* buf = nullptr; - FILE* fi = fopen(fn, "rb"); + EnhancedFile fi = oxfopen_enhanced(fn, "rb"); if (fi) { - fseek(fi, 0, SEEK_END); - n = ftell(fi); + fi.seek(0, SEEK_END); + n = fi.tell(); if (n) { - fseek(fi, 0, SEEK_SET); + fi.seek(0, SEEK_SET); buf = (uint8_t*)malloc(n); - if (buf) j = fread(buf, 1, n, fi); + if (buf) j = fi.read(buf, 1, n); } - fclose(fi); + fi.close(); } if (num) *num = j; return buf; @@ -223,7 +226,7 @@ class TosPatcher { memcpy(out_buf + sizeof(sys_img_header) + patched_payload_size, orig_remain, orig_remain_size); // 8. 写入最终文件 - FILE* fp = fopen(output_file, "wb"); + EnhancedFile fp = oxfopen_enhanced(output_file, "wb"); if (!fp) { printf("[TosPatcher] [ERROR] Cannot create output file %s\n", output_file); free(out_buf); @@ -231,8 +234,8 @@ class TosPatcher { free(patched_buf); return 1; } - fwrite(out_buf, 1, out_size, fp); - fclose(fp); + fp.write(out_buf, 1, out_size); + fp.close(); printf("[TosPatcher] [INFO] Successfully generated %s (size: %zu bytes)\n", output_file, out_size); printf("[TosPatcher] [INFO] Dual-image layout: [header][patched payload][original payload+signature]\n"); diff --git a/common.cpp b/common.cpp index fa62e66..202a29c 100644 --- a/common.cpp +++ b/common.cpp @@ -56,18 +56,18 @@ int check_confirm(const char *name) { uint8_t *loadfile(const char *fn, size_t *num, size_t extra) { size_t n = 0, j = 0; uint8_t *buf = nullptr; - UniqueFile fi = oxfopen_unique(fn, "rb"); + EnhancedFile fi = oxfopen_enhanced(fn, "rb"); if (fi) { - if (fseek(fi.get(), 0, SEEK_END) == 0) { - long n_long = ftell(fi.get()); + if (fi.seek(0, SEEK_END) == 0) { + long n_long = fi.tell(); if (n_long > 0) { n = static_cast(n_long); if (n <= SIZE_MAX - extra) { - rewind(fi.get()); + fi.rewind(); buf = NEWN uint8_t[n + extra]; if (buf) { - j = fread(buf, 1, n, fi.get()); + j = fi.read(buf, 1, n); } } } @@ -233,7 +233,7 @@ unsigned dump_flash(spdio_t *io, uint32_t addr, uint32_t start, uint32_t len, const char *fn, unsigned step, int mode) { uint32_t nread = 0; - UniqueFile fo = my_oxfopen_unique(fn, "wb"); + EnhancedFile fo = my_oxfopen_enhanced(fn, "wb"); if (!fo) ERR_EXIT("fopen(dump) failed\n"); if (mode == 1) { @@ -259,7 +259,7 @@ unsigned dump_flash(spdio_t *io, if (!READ32_LE(buf + 0x10)) break; // all zeros if (!~READ32_LE(buf + 0x10)) break; // all ones - if (fwrite(buf, 1, nread2, fo.get()) != nread2) + if (fo.write(buf, 1, nread2) != nread2) ERR_EXIT("fwrite(dump) failed\n"); nread1 = nread; len = nread += nread2; @@ -280,7 +280,7 @@ unsigned dump_mem(spdio_t *io, uint32_t start, uint32_t len, const char *fn, unsigned step) { uint32_t n, offset, nread; int ret; - UniqueFile fo = my_oxfopen_unique(fn, "wb"); + EnhancedFile fo = my_oxfopen_enhanced(fn, "wb"); if (!fo) ERR_EXIT("fopen(dump) failed\n"); for (offset = start; offset < start + len; ) { @@ -304,7 +304,7 @@ unsigned dump_mem(spdio_t *io, nread = READ16_BE(io->raw_buf + 2); if (n < nread) ERR_EXIT("excepted length\n"); - if (fwrite(io->raw_buf + 4, 1, nread, fo.get()) != nread) + if (fo.write(io->raw_buf + 4, 1, nread) != nread) ERR_EXIT("fwrite(dump) failed\n"); offset += nread; if (n != nread) break; @@ -482,7 +482,7 @@ uint64_t dump_partition(spdio_t *io, return 0; } if (isCancel) { return 0; } - UniqueFile fo = my_oxfopen_unique(fn, "wb"); + EnhancedFile fo = my_oxfopen_enhanced(fn, "wb"); if (!fo) ERR_EXIT("fopen(dump) failed\n"); unsigned long long time_start = GetTickCount64(); @@ -507,7 +507,7 @@ uint64_t dump_partition(spdio_t *io, nread = READ16_BE(io->raw_buf + 2); if (n < nread) ERR_EXIT("excepted length\n"); - if (fwrite(io->raw_buf + 4, 1, nread, fo.get()) != nread) + if (fo.write(io->raw_buf + 4, 1, nread) != nread) ERR_EXIT("fwrite(dump) failed\n"); print_progress_bar(io,offset + nread - start, len, time_start); offset += nread; @@ -691,7 +691,7 @@ int scan_xml_partitions(spdio_t *io, const char *fn, uint8_t *buf, size_t buf_si static int& selected_ab = g_app_state.flash.selected_ab; int gpt_info(partition_t *ptable, const char *fn_xml, int *part_count_ptr) { - UniqueFile fp = my_oxfopen_unique("pgpt.bin", "rb"); + EnhancedFile fp = my_oxfopen_enhanced("pgpt.bin", "rb"); if (!fp) { return -1; } @@ -702,7 +702,7 @@ int gpt_info(partition_t *ptable, const char *fn_xml, int *part_count_ptr) { int found = 0; while (sector_index < MAX_SECTORS) { - bytes_read = fread(buffer, 1, SECTOR_SIZE, fp.get()); + bytes_read = fp.read(buffer, 1, SECTOR_SIZE); if (bytes_read != SECTOR_SIZE) { return -1; } @@ -726,8 +726,8 @@ int gpt_info(partition_t *ptable, const char *fn_xml, int *part_count_ptr) { if (entries == nullptr) { return -1; } - fseek(fp.get(), (long)header.partition_entry_lba * real_SECTOR_SIZE, SEEK_SET); - bytes_read = fread(entries, 1, header.number_of_partition_entries * sizeof(efi_entry), fp.get()); + fp.seek((long)header.partition_entry_lba * real_SECTOR_SIZE, SEEK_SET); + bytes_read = fp.read(entries, 1, header.number_of_partition_entries * sizeof(efi_entry)); if (bytes_read != (int)(header.number_of_partition_entries * sizeof(efi_entry))) DEG_LOG(I,"read %d/%d only.", bytes_read, (int)(header.number_of_partition_entries * sizeof(efi_entry))); std::shared_ptr root = nullptr; @@ -832,9 +832,9 @@ partition_t *partition_list(spdio_t *io, const char *fn, int *part_count_ptr) { std::shared_ptr root = nullptr; bool needSave = (strcmp(fn, "-") != 0); - UniqueFile fpkt = my_oxfopen_unique("sprdpart.bin", "wb"); + EnhancedFile fpkt = my_oxfopen_enhanced("sprdpart.bin", "wb"); if (!fpkt) ERR_EXIT("fopen failed\n"); - fwrite(io->raw_buf + 4, 1, size, fpkt.get()); + fpkt.write(io->raw_buf + 4, 1, size); n = size / 0x4c; if (needSave) { @@ -1317,23 +1317,23 @@ void load_partition(spdio_t *io, const char *name, const char *fn, unsigned step, int CMethod) { uint64_t offset, len, n64; unsigned mode64, n, step0 = step; int ret; - UniqueFile fi; + EnhancedFile fi; double rtime = get_time(); if (strstr(name, "runtimenv")) { erase_partition(io, name, CMethod); return; } if (!strcmp(name, "calinv")) { return; } //skip calinv DEG_LOG(OP, "Start to write partition %s", name); DEG_LOG(I, "Type CTRL + C to cancel..."); start_signal(); - fi = oxfopen_unique(fn, "rb"); + fi = oxfopen_enhanced(fn, "rb"); if (!fi) ERR_EXIT("fopen(load) failed\n"); uint8_t header[4], is_simg = 0; - if (fread(header, 1, 4, fi.get()) != 4) + if (fi.read(header, 1, 4) != 4) ERR_EXIT("fread(load) failed\n"); if (0xED26FF3A == *(uint32_t *)header) is_simg = 1; - fseeko(fi.get(), 0, SEEK_END); - len = ftello(fi.get()); - fseek(fi.get(), 0, SEEK_SET); + fi.seeko(0, SEEK_END); + len = fi.tello(); + fi.seek(0, SEEK_SET); DEG_LOG(I,"File size : 0x%llx\n", (long long)len); mode64 = len >> 32; @@ -1371,7 +1371,7 @@ void load_partition(spdio_t *io, const char *name, // ���� n �����ֵ����ֹ��� n = step + 1; } - if (fread(rawbuf, 1, n, fi.get()) != n) ERR_EXIT("fread(load) failed\n"); + if (fi.read(rawbuf, 1, n) != n) ERR_EXIT("fread(load) failed\n"); #if USE_LIBUSB int err = libusb_bulk_transfer(io->dev_handle, io->endp_out, rawbuf, n, &ret, io->timeout); //libusb will fail with rawbuf if (err < 0) ERR_EXIT("usb_send failed : %s\n", libusb_error_name(err)); @@ -1402,7 +1402,7 @@ void load_partition(spdio_t *io, const char *name, for (offset = 0; (n64 = len - offset); offset += n) { if (isCancel) { return; } n = (unsigned)(n64 > step ? step : n64); - if (fread(io->temp_buf, 1, n, fi.get()) != n) + if (fi.read(io->temp_buf, 1, n) != n) ERR_EXIT("fread(load) failed\n"); encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, n); send_msg(io); @@ -2034,9 +2034,9 @@ void dump_partitions(spdio_t *io, const char *fn, int *nand_info, unsigned step) size_t size = 0; char* src = (char*)loadfile(fn, &size, 1); if (src) { - UniqueFile fo = my_oxfopen_unique(savepath, "wb"); + EnhancedFile fo = my_oxfopen_enhanced(savepath, "wb"); if (fo) { - fwrite(src, 1, size, fo.get()); + fo.write(src, 1, size); } else { DEG_LOG(W,"Create dump list failed, skipped."); } @@ -2110,13 +2110,13 @@ int get_nvlist_cfg(spdio_t *io, char *fn) { char line[512]; unsigned int id = 0; - UniqueFile cfg_fd = my_oxfopen_unique(fn, "rb"); + EnhancedFile cfg_fd = my_oxfopen_enhanced(fn, "rb"); if (!cfg_fd) return 0; io->nvid_list = NEWN int[0x10000]; if (!io->nvid_list) ERR_EXIT("malloc failed\n"); memset(io->nvid_list, 0, 0x10000 * sizeof(int)); - while (fgets(line, sizeof(line), cfg_fd.get())) { + while (cfg_fd.gets(line, sizeof(line))) { if (line[0] == '#' || line[0] == '\0') continue; if (-1 == sscanf(line, "%*s %x", &id)) continue; io->nvid_list[id] = 1; @@ -2418,11 +2418,11 @@ void load_partitions(spdio_t *io, const char *path, unsigned step, int force_ab, uint8_t *b = loadfile(partitions[i].file_path, &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = my_oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = my_oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); free(a); free(b); free(c); } free(io->nvid_list); @@ -2441,11 +2441,11 @@ void load_partitions(spdio_t *io, const char *path, unsigned step, int force_ab, uint8_t *b = loadfile(partitions[i].file_path, &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = my_oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = my_oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); free(a); free(b); free(c); } free(io->nvid_list); @@ -2464,11 +2464,11 @@ void load_partitions(spdio_t *io, const char *path, unsigned step, int force_ab, uint8_t *b = loadfile(partitions[i].file_path, &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = my_oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = my_oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); free(a); free(b); free(c); } free(io->nvid_list); @@ -2691,19 +2691,19 @@ void w_mem_to_part_offset(spdio_t *io, const char *name, size_t offset, uint8_t if (savepath[0]) snprintf(fix_fn, sizeof(fix_fn), "%s/%s", savepath, dfile); else strcpy(fix_fn, dfile); - UniqueFile fi; - if (offset == 0) fi = oxfopen_unique(fix_fn, "wb"); + EnhancedFile fi; + if (offset == 0) fi = oxfopen_enhanced(fix_fn, "wb"); else { if (gPartInfo.size != (long long)dump_partition(io, gPartInfo.name, 0, gPartInfo.size, fix_fn, step)) { remove(fix_fn); return; } - fi = oxfopen_unique(fix_fn, "rb+"); + fi = oxfopen_enhanced(fix_fn, "rb+"); } if (!fi) ERR_EXIT("fopen %s failed\n", fix_fn); - if (fseek(fi.get(), offset, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(mem, 1, length, fi.get()) != length) ERR_EXIT("fwrite failed\n"); - fi.reset(); + if (fi.seek(offset, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(mem, 1, length) != length) ERR_EXIT("fwrite failed\n"); + fi.close(); load_partition_unify(io, gPartInfo.name, fix_fn, step, CMethod); } @@ -2750,10 +2750,10 @@ int load_partition_unify(spdio_t *io, const char *name, const char *fn, unsigned if (size0 == size1) { if (!strcmp(name0, "vbmeta")) { char ch = '\0'; - UniqueFile fi = my_oxfopen_unique(fn, "rb+"); + EnhancedFile fi = my_oxfopen_enhanced(fn, "rb+"); if (!fi) { DEG_LOG(E,"fopen %s failed\n", fn); return 1; } - if (fseek(fi.get(), 0x7B, SEEK_SET) != 0) { DEG_LOG(E,"fseek failed"); return 1; } - if (fwrite(&ch, 1, 1, fi.get()) != 1) { DEG_LOG(E,"fwrite failed\n"); return 1; } + if (fi.seek(0x7B, SEEK_SET) != 0) { DEG_LOG(E,"fseek failed"); return 1; } + if (fi.write(&ch, 1, 1) != 1) { DEG_LOG(E,"fwrite failed\n"); return 1; } } load_partition(io, name1, fn, step, CMethod); return 2; diff --git a/core/config_service.cpp b/core/config_service.cpp index 5fd77e1..57811db 100644 --- a/core/config_service.cpp +++ b/core/config_service.cpp @@ -235,22 +235,22 @@ class DefaultConfigService : public ConfigService { return make_error(ConfigErrorCode::NotFound, "config file not found"); } - UniqueFile f = oxfopen_unique(path.c_str(), "rb"); + EnhancedFile f = oxfopen_enhanced(path.c_str(), "rb"); if (!f) { return make_error(ConfigErrorCode::IoError, "failed to open config file"); } - fseek(f.get(), 0, SEEK_END); - long len = ftell(f.get()); - fseek(f.get(), 0, SEEK_SET); + f.seek(0, SEEK_END); + long len = f.tell(); + f.seek(0, SEEK_SET); std::string buf; buf.resize(static_cast(len)); if (len > 0) { - if (fread(&buf[0], 1, static_cast(len), f.get()) != static_cast(len)) { - f.reset(); + if (f.read(&buf[0], 1, static_cast(len)) != static_cast(len)) { + f.close(); return make_error(ConfigErrorCode::IoError, "failed to read config file"); } } - f.reset(); + f.close(); try { json j = json::parse(buf); @@ -290,15 +290,15 @@ class DefaultConfigService : public ConfigService { to_json(j, config); std::string data = j.dump(2); - UniqueFile f = oxfopen_unique(path.c_str(), "wb"); + EnhancedFile f = oxfopen_enhanced(path.c_str(), "wb"); if (!f) { return make_error(ConfigErrorCode::IoError, "failed to open config file for write"); } - if (fwrite(data.data(), 1, data.size(), f.get()) != data.size()) { - f.reset(); + if (f.write(data.data(), 1, data.size()) != data.size()) { + f.close(); return make_error(ConfigErrorCode::IoError, "failed to write config file"); } - f.reset(); + f.close(); return make_ok(); } diff --git a/core/file_io.cpp b/core/file_io.cpp index d640bf0..6dde573 100644 --- a/core/file_io.cpp +++ b/core/file_io.cpp @@ -185,11 +185,21 @@ long EnhancedFile::tell() const noexcept { return -1L; } +long EnhancedFile::tello() const noexcept { + if (file) return ftello(file.get()); + return -1L; +} + bool EnhancedFile::seek(long offset, int origin) noexcept { if (file) return fseek(file.get(), offset, origin) == 0; return false; } +bool EnhancedFile::seeko(long offset, int origin) noexcept { + if (file) return fseeko(file.get(), offset, origin) == 0; + return false; +} + bool EnhancedFile::eof() const noexcept { if (file) return feof(file.get()) != 0; return true; diff --git a/core/file_io.h b/core/file_io.h index d67b75a..23aa891 100644 --- a/core/file_io.h +++ b/core/file_io.h @@ -71,7 +71,10 @@ class EnhancedFile { void close() noexcept; bool flush() noexcept; long tell() const noexcept; + long tello() const noexcept; bool seek(long offset, int origin) noexcept; + bool seeko(long offset, int origin) noexcept; + void rewind() noexcept { seek(0, SEEK_SET); } bool eof() const noexcept; bool error() const noexcept; void clearerr() noexcept; @@ -115,12 +118,16 @@ class EnhancedFile { // 工厂函数 EnhancedFile oxfopen_enhanced(const char* fn, const char* mode); +// 工厂函数 EnhancedFile my_oxfopen_enhanced(const char* fn, const char* mode); // 原有的函数声明 FILE* oxfopen(const char* fn, const char* mode); FILE* my_oxfopen(const char* fn, const char* mode); + +// 此接口已废弃 UniqueFile oxfopen_unique(const char* fn, const char* mode); +// 此接口已废弃 UniqueFile my_oxfopen_unique(const char* fn, const char* mode); // 自定义操纵符 diff --git a/core/pac_extract.cpp b/core/pac_extract.cpp index 76b26d1..fbd0456 100644 --- a/core/pac_extract.cpp +++ b/core/pac_extract.cpp @@ -130,7 +130,7 @@ static int check_path(char *path) { class Unpac { private: - FILE* fi; + EnhancedFile fi; sprd_head_t head; char str_buf[257]; unsigned chunk; @@ -148,7 +148,7 @@ class Unpac { ~Unpac() { if (buf) free(buf); - if (fi) fclose(fi); + if (fi) fi.close(); } void setDirectory(const char* directory) { @@ -184,7 +184,7 @@ class Unpac { } bool openPacFile(const char* filename) { - fi = fopen(filename, "rb"); + fi = my_oxfopen_enhanced(filename, "rb"); if (!fi) { printf("fopen(input) failed\n"); return false; @@ -334,12 +334,12 @@ bool Unpac::extractFiles() { if (argc && j == argc) continue; - FILE* fo; uint64_t l; uint32_t n; + EnhancedFile fo; uint64_t l; uint32_t n; CONV_STR(file.name); printf("%s\n", str_buf); - if (fseeko(fi, pac_offset, SEEK_SET)) { + if (fi.seeko(pac_offset, SEEK_SET)) { printf("fseek failed\n"); return false; } @@ -357,7 +357,7 @@ bool Unpac::extractFiles() { } } - fo = fopen(str_buf, "wb"); + fo = oxfopen_enhanced(str_buf, "wb"); if (!fo) { printf("fopen(output) failed\n"); return false; @@ -367,11 +367,11 @@ bool Unpac::extractFiles() { for (; l; l -= n) { n = (uint32_t)(l > chunk ? chunk : l); READ(buf, n, "chunk"); - fwrite(buf, n, 1, fo); + fo.write(buf, n, 1); } - fclose(fo); + fo.close(); - if (fseek(fi, sizeof(head) + (i + 1) * sizeof(sprd_file_t), SEEK_SET)) { + if (fi.seek(sizeof(head) + (i + 1) * sizeof(sprd_file_t), SEEK_SET)) { printf("fseek failed\n"); return false; } @@ -422,7 +422,7 @@ sfd::Result Unpac::checkCrc_result() { return sfd::Result::error(sfd::ErrorCode::ParseError, "unexpected pac size"); } - if (fseeko(fi, sizeof(head), SEEK_SET)) { + if (fi.seeko(sizeof(head), SEEK_SET)) { printf("fseeko failed in checkCrc\n"); return sfd::Result::error(sfd::ErrorCode::IoError, "fseeko failed in checkCrc"); } @@ -438,7 +438,7 @@ sfd::Result Unpac::checkCrc_result() { l -= sizeof(head); while (l) { uint32_t n = l > chunk_size ? chunk_size : l; - size_t read_count = fread(local_buf, 1, n, fi); + size_t read_count = fi.read(local_buf, 1, n); if (read_count != n) { printf("fread failed in checkCrc\n"); free(local_buf); @@ -480,8 +480,7 @@ void Unpac::close() { buf = NULL; } if (fi) { - fclose(fi); - fi = NULL; + fi.close(); } } static sfd::Result parse_partitions_xml_result(const char* temp_xml_path, @@ -885,7 +884,7 @@ std::string findBaseForID(const std::string& filename, const std::string& target } std::stringstream buffer; buffer << file.rdbuf(); - (file.close)(); // _close宏定义阻止 + file.close(); XmlParser parser; auto root = parser.parseString(buffer.str()); diff --git a/main_console.cpp b/main_console.cpp index f7f8a1c..3a3d313 100644 --- a/main_console.cpp +++ b/main_console.cpp @@ -939,8 +939,8 @@ int main_console(int argc, char** argv) { //FDL1, MAY NEED TO SEND CVE FILE else { if (fdl1_loaded != -1) { - UniqueFile fi = my_oxfopen_unique(fn, "r"); - if (fi == nullptr) { + EnhancedFile fi = my_oxfopen_enhanced(fn, "r"); + if (!fi) { DEG_LOG(W, "File does not exist.\n"); argc -= argchange; argv += argchange; @@ -955,12 +955,12 @@ int main_console(int argc, char** argv) { encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, n); if (send_and_check(io)) ERR_EXIT("CVE v2 failed");; } - fi = my_oxfopen_unique(execfile.c_str(), "rb"); + fi = my_oxfopen_enhanced(execfile.c_str(), "rb"); if (fi) { - fseek(fi.get(), 0, SEEK_END); - n = ftell(fi.get()); - fseek(fi.get(), 0, SEEK_SET); - execsize = fread(io->temp_buf, 1, n, fi.get()); + fi.seek(0, SEEK_END); + n = fi.tell(); + fi.seek(0, SEEK_SET); + execsize = fi.read(io->temp_buf, 1, n); } encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, execsize); if (send_and_check(io)) ERR_EXIT("CVE v2 failed"); @@ -1560,14 +1560,14 @@ int main_console(int argc, char** argv) { continue; } size_t length = 0; - UniqueFile fi; + EnhancedFile fi; if (argcount > 3) { - fi = my_oxfopen_unique(str2[3], "rb"); - fseek(fi.get(), 0, SEEK_END); - length = ftell(fi.get()); + fi = my_oxfopen_enhanced(str2[3], "rb"); + fi.seek(0, SEEK_END); + length = fi.tell(); if (length) { - fseek(fi.get(), 0, SEEK_SET); - size_t temp_fread_res = fread(io->temp_buf, 1, length, fi.get()); + fi.seek(0, SEEK_SET); + size_t temp_fread_res = fi.read(io->temp_buf, 1, length); (void)temp_fread_res; } } @@ -2412,11 +2412,11 @@ int main_console(int argc, char** argv) { uint8_t *b = loadfile(str2[3], &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); load_nv_partition(io, gPartInfo.name, "nvmerged", 4096); free(a); free(b); free(c); } @@ -2433,14 +2433,14 @@ int main_console(int argc, char** argv) { uint8_t *b = loadfile(str2[4], &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); - fi = oxfopen_unique("nvmerged", "rb"); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); + fi = oxfopen_enhanced("nvmerged", "rb"); if (!fi) DEG_LOG(E, "Failed to create merged nv file"); - if (fi) fi.reset(); + if (fi) fi.close(); free(a); free(b); free(c); } free(io->nvid_list); @@ -2473,11 +2473,11 @@ int main_console(int argc, char** argv) { uint8_t *b = loadfile(str2[3], &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); load_nv_partition(io, gPartInfo.name, "nvmerged", 4096); free(a); free(b); free(c); } @@ -2493,14 +2493,14 @@ int main_console(int argc, char** argv) { uint8_t *b = loadfile(str2[4], &b_size, 0); uint8_t *c = (uint8_t*)malloc(a_size + b_size); merge_nv(io, a, a_size, b, b_size, c, &c_size); - UniqueFile fi = oxfopen_unique("nvmerged", "wb"); + EnhancedFile fi = oxfopen_enhanced("nvmerged", "wb"); if (!fi) ERR_EXIT("fopen failed\n"); - if (fseek(fi.get(), 0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); - if (fwrite(c, 1, c_size, fi.get()) != c_size) ERR_EXIT("fwrite failed\n"); - fi.reset(); - fi = oxfopen_unique("nvmerged", "rb"); + if (fi.seek(0, SEEK_SET) != 0) ERR_EXIT("fseek failed\n"); + if (fi.write(c, 1, c_size) != c_size) ERR_EXIT("fwrite failed\n"); + fi.close(); + fi = oxfopen_enhanced("nvmerged", "rb"); if (!fi) DEG_LOG(E, "Failed to create merged nv file"); - if (fi) fi.reset(); + if (fi) fi.close(); free(a); free(b); free(c); } free(io->nvid_list); diff --git a/pages/page_connect.cpp b/pages/page_connect.cpp index 1cd49e1..a3ab6a0 100644 --- a/pages/page_connect.cpp +++ b/pages/page_connect.cpp @@ -369,7 +369,7 @@ void on_button_clicked_fdl_exec(GtkWidgetHelper helper) { std::string dtxt = helper.getLabelText(helper.getWidget("con")); bottom_bar_set_status(dtxt + " -> FDL Executing"); std::thread([helper, fdl_path, fdl_addr]() mutable { - UniqueFile fi = oxfopen_unique(fdl_path, "r"); + EnhancedFile fi = oxfopen_enhanced(fdl_path, "r"); GtkWidget* cveSwitch = helper.getWidget("exec_addr"); GtkWidget* cveAddr = helper.getWidget("cve_addr"); GtkWidget* cveAddrC = helper.getWidget("cve_addr_c"); @@ -381,7 +381,7 @@ void on_button_clicked_fdl_exec(GtkWidgetHelper helper) { if (!fi) { DEG_LOG(W, "File does not exist.\n"); return; - } + } else fi.close(); send_file(io, fdl_path, fdl_addr, end_data, 528, 0, 0); if (cve_addr && strlen(cve_addr) > 0 && isCve) { bool isCVEv2 = helper.getSwitchState(helper.getWidget("cve_v2")); @@ -401,13 +401,13 @@ void on_button_clicked_fdl_exec(GtkWidgetHelper helper) { encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, n); if (send_and_check(io)) ERR_EXIT("CVE v2 failed");; } - UniqueFile fi = oxfopen_unique(execfile, "rb"); + EnhancedFile fi = oxfopen_enhanced(execfile, "rb"); if (fi) { - fseek(fi.get(), 0, SEEK_END); - n = ftell(fi.get()); - fseek(fi.get(), 0, SEEK_SET); - execsize = fread(io->temp_buf, 1, n, fi.get()); - fi.reset(); + fi.seek(0, SEEK_END); + n = fi.tell(); + fi.seek(0, SEEK_SET); + execsize = fi.read(io->temp_buf, 1, n); + fi.close(); } encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, execsize); if (send_and_check(io)) ERR_EXIT("CVE v2 failed");; @@ -450,12 +450,12 @@ void on_button_clicked_fdl_exec(GtkWidgetHelper helper) { encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, n); if (send_and_check(io)) ERR_EXIT("CVE V2 failed"); } - UniqueFile fi = oxfopen_unique(execfile, "rb"); + EnhancedFile fi = oxfopen_enhanced(execfile, "rb"); if (fi) { - fseek(fi.get(), 0, SEEK_END); - n = ftell(fi.get()); - fseek(fi.get(), 0, SEEK_SET); - execsize = fread(io->temp_buf, 1, n, fi.get()); + fi.seek(0, SEEK_END); + n = fi.tell(); + fi.seek(0, SEEK_SET); + execsize = fi.read(io->temp_buf, 1, n); } encode_msg_nocpy(io, BSL_CMD_MIDST_DATA, execsize); if (send_and_check(io)) ERR_EXIT("CVE V2 failed");; diff --git a/pages/page_partition.cpp b/pages/page_partition.cpp index e786561..e5f6233 100644 --- a/pages/page_partition.cpp +++ b/pages/page_partition.cpp @@ -428,7 +428,7 @@ static bool inspect_file_is_all_zero(const std::string& path, out_all_zero = true; out_error.clear(); - UniqueFile fi = oxfopen_unique(path.c_str(), "rb"); + EnhancedFile fi = oxfopen_enhanced(path.c_str(), "rb"); if (!fi) { out_error = _("Failed to open image file."); return false; @@ -436,9 +436,9 @@ static bool inspect_file_is_all_zero(const std::string& path, std::vector buffer(1024 * 1024); while (true) { - const std::size_t nread = std::fread(buffer.data(), 1, buffer.size(), fi.get()); + const std::size_t nread = fi.read(buffer.data(), 1, buffer.size()); if (nread == 0) { - if (std::ferror(fi.get())) { + if (fi.error()) { out_error = _("Failed to read image file."); return false; }