diff --git a/src/tools/zlibutil.cpp b/src/tools/zlibutil.cpp index f9e3bdb..184f05b 100644 --- a/src/tools/zlibutil.cpp +++ b/src/tools/zlibutil.cpp @@ -28,6 +28,7 @@ #include "../lib/cli.hpp" #include "../lib/file.hpp" #include "../lib/zlibutil.hpp" +#include "../lib/error.hpp" #define EXIT_SUCCESS 0 #define EXIT_RUNTIME 1 @@ -112,14 +113,18 @@ int main(int argc, char* argv[]) { return EXIT_RUNTIME; } File* file = filePointer.value; - File *writeFile; + File* writeFile; if (cliParser.getFlag("decompress").value) { - std::vector bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC - std::vector differentBytes = std::vector(bytes.begin(), bytes.end()); + std::vector bytes = file->read(file->size.value).value; - std::vector compressed = decompressData(differentBytes.data(), file->size.value); - std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); + ErrorOr> decompressed = zlib::decompressData(bytes); + if (decompressed.isError) { + std::cout << "Error: Failed to decompress: " << filename << std::endl; + delete file; + // not cleaning up writeFile here bc it hasn't been created + return EXIT_RUNTIME; + } std::string outFilename; if (filename.length() > 3 && filename.rfind(".zz") == filename.length()-3) { @@ -128,16 +133,23 @@ int main(int argc, char* argv[]) { outFilename = filename + ".uncompressed"; } writeFile = File::open(outFilename, 'w').value; - writeFile->write(unsigneddata); + writeFile->write(decompressed.value); writeFile->close(); } else { - std::vector bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC - std::vector differentBytes = std::vector(bytes.begin(), bytes.end()); + std::vector bytes = file->read(file->size.value).value; - std::vector compressed = compressData(differentBytes.data(), file->size.value); - std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); + ErrorOr> compressed = zlib::compressData(bytes); + if (compressed.isError) { + std::cout << "Error: Failed to compress: " << filename << std::endl; + delete file; + // not cleaning up writeFile here bc it hasn't been created + return EXIT_RUNTIME; + } writeFile = File::open(filename + ".zz", 'w').value; - writeFile->write(unsigneddata); + writeFile->write(compressed.value); writeFile->close(); } + + delete file; + delete writeFile; }