Skip to content

Commit

Permalink
Merge pull request #21 from YuzukiTsuru/dev
Browse files Browse the repository at this point in the history
delete files after get size
  • Loading branch information
YuzukiTsuru committed Jul 2, 2022
2 parents 32ab18a + 93f95da commit d0a8a7e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/OpenixCard/FEX2CFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void FEX2CFG::gen_cfg() {
std::cout << "Remaining space.";
}
} else if (opt.get_name() == "size") {
std::cout << opt.get<inicpp::unsigned_ini_t>() / 0x300 << "MB - " << opt.get<inicpp::unsigned_ini_t>() << "KB";
std::cout << static_cast<double>(opt.get<inicpp::unsigned_ini_t>()) / 2 / 0x300 << "MB - " << opt.get<inicpp::unsigned_ini_t>() / 2 << "KB";
}
}
std::cout << std::endl;
Expand Down
18 changes: 16 additions & 2 deletions src/OpenixCard/OpenixCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void OpenixCard::show_logo() {
"| |___ ___ ___|_|_ _| |___ ___ _| |\n"
"| | | . | -_| | |_'_| --| .'| _| . |\n"
"|_____| _|___|_|_|_|_,_|_____|__,|_| |___|\n"
" |_| Version: " << PROJECT_GIT_HASH
" |_| Version: " << PROJECT_GIT_HASH << " Commit: " << PROJECT_VER
<< cc::magenta <<
"\nCopyright (c) 2022, YuzukiTsuru <[email protected]>\n"
<< cc::reset << std::endl;
Expand Down Expand Up @@ -189,8 +189,21 @@ void OpenixCard::unpack_target_image() {
std::filesystem::create_directories(temp_file_path);
crypto_init();
std::cout << cc::cyan;
unpack_image(input_file.c_str(), temp_file_path.c_str(), is_absolute);
auto unpack_img_ret = unpack_image(input_file.c_str(), temp_file_path.c_str(), is_absolute);
std::cout << cc::reset;

switch (unpack_img_ret) {
case 2:
throw file_open_error(input_file);
case 3:
throw file_size_error(input_file);
case 4:
throw std::runtime_error("Unable to allocate memory for image: "+ input_file);
case 5:
throw file_format_error(input_file);
default:
break;
}
}

void OpenixCard::dump_and_clean() {
Expand Down Expand Up @@ -229,5 +242,6 @@ void OpenixCard::get_real_size() {
FEX2CFG fex2Cfg(temp_file_path);
auto real_size = fex2Cfg.get_image_real_size(true);
LOG::DATA("The accurate size of image: " + std::to_string(real_size / 1024) + "MB, " + std::to_string(real_size) + "KB");
std::filesystem::remove_all(temp_file_path);
}

10 changes: 10 additions & 0 deletions src/OpenixCard/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class file_open_error : public std::runtime_error {
explicit file_open_error(const std::string &what) : std::runtime_error("Fail to open file: " + what + ".") {};
};

class file_format_error : public std::runtime_error {
public:
explicit file_format_error(const std::string &what) : std::runtime_error("File: " + what + " is not Allwinner image.") {};
};

class file_size_error : public std::runtime_error {
public:
explicit file_size_error(const std::string &what) : std::runtime_error("Invalid file size: " + what + ".") {};
};

class no_file_provide_error : public std::runtime_error {
public:
no_file_provide_error() : std::runtime_error("No file Provide.") {};
Expand Down
8 changes: 3 additions & 5 deletions src/OpenixIMG/src/OpenixIMG.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ int flag_encryption_enabled;
} while (0)

#define O_LOG(fmt, arg...) OpenixIMG_LOG("[OpenixIMG INFO] " fmt, ##arg)
#define O_ERR(fmt, arg...) OpenixIMG_LOG("[OpenixIMG ERROR] " fmt, ##arg)

/* Crypto */
rc6_ctx_t header_ctx;
Expand Down Expand Up @@ -138,7 +137,6 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {

ifp = fopen(infn, "rb");
if (ifp == NULL) {
O_ERR("Error: unable to open %s!\n", infn);
return 2;
}

Expand All @@ -147,13 +145,11 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
fseek(ifp, 0, SEEK_SET);

if (imagesize <= 0) {
O_ERR("Error: Invalid file size %ld (%s)\n", imagesize, strerror(errno));
return 3;
}

image = malloc(imagesize);
if (!image) {
O_ERR("Error: Unable to allocate memory for image: %ld\n", imagesize);
return 4;
}

Expand All @@ -177,12 +173,14 @@ int unpack_image(const char *infn, const char *outdn, int is_absolute) {
firmware_id = header->v3.firmware_id;
pid = header->v3.pid;
vid = header->v3.vid;
} else /*if (header->header_version == 0x0100)*/ {
} else if (header->header_version == 0x0100) {
num_files = header->v1.num_files;
hardware_id = header->v1.hardware_id;
firmware_id = header->v1.firmware_id;
pid = header->v1.pid;
vid = header->v1.vid;
} else {
return 5;
}

/* Decrypt file headers */
Expand Down

0 comments on commit d0a8a7e

Please sign in to comment.