tools: Add zlibutil
Splitting one of Joca’s commits by topic - Bodge Add zlibutil.cpp and zlib to the project's dependencies. - Joca
							parent
							
								
									34e19486d6
								
							
						
					
					
						commit
						487d8f1636
					
				|  | @ -62,6 +62,7 @@ COMPILE_COMMANDS=( | |||
|     "$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -I./include -Lbin/lib -l:nbt.so -l:cli.so -o bin/tools/dumpnbt" | ||||
|     "$CXX_WITH_FLAGS src/tools/arraydump.cpp -I./include -Lbin/lib -l:file.so -l:cli.so -o bin/tools/arraydump" | ||||
|     "$CXX_WITH_FLAGS src/tools/baseconvert.cpp -I./include -Lbin/lib -l:cli.so -o bin/tools/baseconvert" | ||||
|     "$CXX_WITH_FLAGS src/tools/zlibutil.cpp -I./include -Lbin/lib -l:cli.so -l:libz.so -l:file.so -o bin/tools/zlibutil" | ||||
|     "$CXX_WITH_FLAGS -pthread src/tools/hexnet.cpp -I./include -Lbin/lib -l:cli.so -l:libsockpp.so -o bin/tools/hexnet" | ||||
|     "$CXX_WITH_FLAGS -DFOSSVG_DEBUG src/fossvg.cpp -I./include -Lbin/lib -l:file.so -l:cli.so -lglfw -lvulkan -o bin/fossvg" | ||||
|     "$CXX_WITH_FLAGS src/fossvgd.cpp -I./include -Lbin/lib -l:cli.so -o bin/fossvgd" | ||||
|  |  | |||
|  | @ -39,6 +39,7 @@ fi | |||
| 
 | ||||
| ln -vs ../dependencies/sockpp-0.8.1/include/sockpp/ ./include/ | ||||
| ln -vs ../dependencies/tiny-utf8-4.4.3/include/tinyutf8/ ./include/ | ||||
| ln -vs ../dependencies/zlib-1.3.1/ ./include/zlib | ||||
| 
 | ||||
| create_file ./bin/.placeholder | ||||
| create_file ./include/.placeholder | ||||
|  |  | |||
|  | @ -79,6 +79,7 @@ set -e # failures are not acceptable here | |||
| create_directory dependencies/tmp | ||||
| download https://github.com/DuffsDevice/tiny-utf8/archive/refs/tags/v4.4.3.tar.gz dependencies/tmp/tiny-utf8.tar.gz 8e3f61651909c9f3105d3501932a96aa65733127fb6e7cf94cb1b0a2dff42c8f | ||||
| download https://github.com/fpagliughi/sockpp/archive/refs/tags/v0.8.1.tar.gz dependencies/tmp/sockpp.tar.gz a8aedff8bd8c1da530b91be650352008fddabc9f1df0d19701d76cbc359c8651 | ||||
| download https://www.zlib.net/zlib-1.3.1.tar.xz dependencies/tmp/zlib.tar.xz 38ef96b8dfe510d42707d9c781877914792541133e1870841463bfa73f883e32 | ||||
| 
 | ||||
| #TODO: figure out how to cache shaderc | ||||
| #also TODO: target a specific commit | ||||
|  | @ -95,6 +96,11 @@ gzip -d dependencies/tmp/sockpp.tar.gz | |||
| tar -xf dependencies/tmp/sockpp.tar -C dependencies | ||||
| echo "done" | ||||
| 
 | ||||
| echo -n ">>> Extracting zlib... " | ||||
| xz -d dependencies/tmp/zlib.tar.xz | ||||
| tar -xf dependencies/tmp/zlib.tar -C dependencies | ||||
| echo "done" | ||||
| 
 | ||||
| echo ">>> Building sockpp..." | ||||
| pushd dependencies/sockpp-0.8.1/ >/dev/null 2>&1 | ||||
| if uname -s | tr [:upper:] [:lower:] | grep cygwin >/dev/null; then | ||||
|  | @ -138,5 +144,12 @@ mkdir -vp dependencies/shaderc/bin | |||
| cp -v "dependencies/tmp/shaderc/$SHADERC_BUILD/glslc/glslc" dependencies/shaderc/bin | ||||
| echo ">>> done" | ||||
| 
 | ||||
| echo ">>> Building zlib..." | ||||
| pushd dependencies/zlib-1.3.1/ | ||||
| ./configure | ||||
| make | ||||
| popd | ||||
| echo "done" | ||||
| 
 | ||||
| echo ">>> Cleaning up..." | ||||
| remove -f dependencies/tmp | ||||
|  |  | |||
|  | @ -0,0 +1,206 @@ | |||
| // Copyright 2022, FOSS-VG Developers and Contributers
 | ||||
| //
 | ||||
| // Author(s):
 | ||||
| //  Jocadbz
 | ||||
| //
 | ||||
| // This program is free software: you can redistribute it and/or modify it
 | ||||
| // under the terms of the GNU Affero General Public License as published
 | ||||
| // by the Free Software Foundation, version 3.
 | ||||
| //
 | ||||
| // This program is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied
 | ||||
| // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 | ||||
| // See the GNU Affero General Public License for more details.
 | ||||
| //
 | ||||
| // You should have received a copy of the GNU Affero General Public License
 | ||||
| // version 3 along with this program.
 | ||||
| // If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
 | ||||
| 
 | ||||
| #include <bitset> | ||||
| #include <iomanip> | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <tinyutf8/tinyutf8.h> | ||||
| #include <zlib/zlib.h> | ||||
| #include <cstring> | ||||
| 
 | ||||
| #include "../lib/cli.hpp" | ||||
| #include "../lib/file.hpp" | ||||
| 
 | ||||
| #define EXIT_SUCCESS 0 | ||||
| #define EXIT_RUNTIME 1 | ||||
| #define EXIT_USAGE   2 | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <fstream> | ||||
| #include <vector> | ||||
| #include <zlib.h> | ||||
| 
 | ||||
| #define CHUNK_SIZE 16384 // Chunk size
 | ||||
| 
 | ||||
| 
 | ||||
| std::vector<char> compressData(const char* data, int size) { | ||||
|     z_stream zs; | ||||
|     memset(&zs, 0, sizeof(zs)); | ||||
| 
 | ||||
|     if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) | ||||
|         throw(std::runtime_error("deflateInit failed while compressing.")); | ||||
| 
 | ||||
|     zs.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); | ||||
|     zs.avail_in = size; | ||||
| 
 | ||||
|     int ret; | ||||
|     char outbuffer[CHUNK_SIZE]; | ||||
|     std::vector<char> compressedData; | ||||
| 
 | ||||
|     do { | ||||
|         zs.next_out = reinterpret_cast<Bytef*>(outbuffer); | ||||
|         zs.avail_out = CHUNK_SIZE; | ||||
| 
 | ||||
|         ret = deflate(&zs, Z_FINISH); | ||||
| 
 | ||||
|         if (compressedData.size() < zs.total_out) | ||||
|             compressedData.insert(compressedData.end(), outbuffer, outbuffer + CHUNK_SIZE - zs.avail_out); | ||||
|     } while (ret == Z_OK); | ||||
| 
 | ||||
|     deflateEnd(&zs); | ||||
| 
 | ||||
|     if (ret != Z_STREAM_END) | ||||
|         throw(std::runtime_error("Error while compressing: " + std::to_string(ret))); | ||||
| 
 | ||||
|     return compressedData; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| std::vector<char> decompressData(const char* data, int size) { | ||||
|     z_stream zs; | ||||
|     memset(&zs, 0, sizeof(zs)); | ||||
| 
 | ||||
|     if (inflateInit(&zs) != Z_OK) | ||||
|         throw(std::runtime_error("inflateInit failed while decompressing.")); | ||||
| 
 | ||||
|     zs.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); | ||||
|     zs.avail_in = size; | ||||
| 
 | ||||
|     int ret; | ||||
|     char outbuffer[CHUNK_SIZE]; | ||||
|     std::vector<char> decompressedData; | ||||
| 
 | ||||
|     do { | ||||
|         zs.next_out = reinterpret_cast<Bytef*>(outbuffer); | ||||
|         zs.avail_out = CHUNK_SIZE; | ||||
| 
 | ||||
|         ret = inflate(&zs, 0); | ||||
| 
 | ||||
|         if (decompressedData.size() < zs.total_out) | ||||
|             decompressedData.insert(decompressedData.end(), outbuffer, outbuffer + CHUNK_SIZE - zs.avail_out); | ||||
|     } while (ret == Z_OK); | ||||
| 
 | ||||
|     inflateEnd(&zs); | ||||
| 
 | ||||
|     if (ret != Z_STREAM_END) | ||||
|         throw(std::runtime_error("Error while decompressing: " + std::to_string(ret))); | ||||
| 
 | ||||
|     return decompressedData; | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
| Finnaly, the main file | ||||
| ⠀⠀⠀⠀⠀⠀⠀⢠⣤⣄⣀⣠⣤⣶⣿⣯⣿⣽⣾⣷⣶⣶⡦⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ | ||||
| ⠀⠀⠀⠀⠀⠀⠀⢀⣿⣟⣯⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣟⣦⣄⠀⠀⠀⠀⠀⠀⠀ | ||||
| ⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣾⡷⣄⠀⠀⠀⠀⠀ | ||||
| ⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣾⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⣳⡀⠀⠀⠀ | ||||
| ⠀⠀⢠⣿⣿⣽⣿⢿⣿⣿⣿⣿⡿⣿⣿⣿⣿⢿⡿⣽⣿⣿⡿⣿⣿⣏⣿⣿⣿⣟⣿⣳⡄⠀⠀ | ||||
| ⠀⠀⣾⣿⣿⣽⣾⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⢿⣿⣾⢿⣷⡿⣿⣿⣷⢿⣿⣿⣷⡿⣷⠀⠀ | ||||
| ⠀⢸⣿⣿⣿⣿⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣳⣿⣟⣿⣿⣿⣻⣽⣿⣿⡽⡇⠀ | ||||
| ⠀⡿⣿⣿⣿⣿⣾⣿⣿⣿⣿⢿⣿⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣽⡀ | ||||
| ⢸⣿⣿⣿⣿⣿⣿⣿⣏⠀⠈⠀⣿⣿⣿⣿⣿⣿⣟⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⢿⡇ | ||||
| ⢨⣿⣿⣿⣿⣽⣿⣿⣇⣀⡤⠤⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⣿⣿⣿⣿⠎⠀ | ||||
| ⢰⣿⡿⣿⣿⣿⣿⣿⠃⠀⠀⠐⠃⠘⠙⢹⠿⡘⣿⠟⠉⢹⡂⠿⡞⣿⣿⣿⣿⣿⣿⣿⣿⠂⠀ | ||||
| ⠀⣿⣿⣿⣿⣿⣿⡞⠅⣀⣀⣀⣀⠉⠓⠚⠀⠁⡏⠀⢛⠋⢀⣀⣀⣋⠸⣿⣿⣿⣿⣿⣿⠃⠀ | ||||
| ⠀⣷⣟⣿⣿⣿⣿⣆⠠⡟⣿⣿⣿⡿⠦⠀⠀⠀⠀⠀⠠⢿⣿⣿⣿⠻⢇⣿⣿⣿⣿⣿⣿⠀⠀ | ||||
| ⠀⠈⢸⣻⣿⣿⣿⢯⡃⢀⠹⣿⡿⠗⠀⠀⠀⠀⠀⠀⠀⠘⢿⣿⠏⣈⠈⣿⣿⣿⣿⣿⣿⠀⠀ | ||||
| ⠀⠀⠈⣿⣿⣿⣿⣇⡗⠈⠉⠉⠉⠉⠀⠀⢐⡀⠀⠀⠀⠈⠉⠉⠉⠑⢸⣸⣿⡟⣿⣿⡟⠀⠀ | ||||
| ⠀⠀⠀⢻⣿⣽⣿⡏⢿⡆⠀⠀⠀⠀⠀⠀⠸⢆⡄⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⡿⠇⠀⠀ | ||||
| ⠀⠀⠀⠈⠿⣿⢿⡇⠀⠙⢦⡀⠀⠀⠀⠐⠲⠤⠔⠂⠀⠀⠀⢀⣴⣾⣿⣿⣿⣿⣿⡟⠀⠀⠀ | ||||
| ⠀⠀⠀⠀⠀⠘⣿⣇⠀⠀⠀⠙⠢⣄⡀⠀⠀⠀⠀⣀⣐⣺⣵⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀ | ||||
| ⠀⠀⠀⠀⠀⠀⠙⠋⠀⠀⠀⠀⠀⢹⢨⠑⠲⠤⠞⠋⠉⣿⣿⣿⣿⣿⣿⣿⣿⡿⠁⠀⠀⠀⠀ | ||||
| ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠈⠀⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀ | ||||
| ⠀⠀⠀⠀⠀⠀⢀⣀⣠⢤⡠⠒⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀ | ||||
| ⠀⠀⣠⠔⠒⠉⠁⠀⠀⠘⠶⣲⠄⠀⠀⠈⠀⠀⠀⢠⢀⠀⠀⣀⠤⢻⣿⣿⣿⡏⠐⠲⣄⠀⠀ | ||||
| ⠀⢰⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⠒⠚⢕⡀⠀⠀⣠⠖⠒⠚⠁⠀⢾⣿⣿⣿⡁⠀⠀⠨⡆⠀ | ||||
| ⠀⢸⠀⠀⠀⠒⠒⠒⠒⠒⠂⠀⠐⠚⠑⠆⠀⠀⠒⠓⠀⠀⠀⠀⠀⣿⣿⣿⣿⡆⠀⠀⠀⣇⠀ | ||||
| ⠀⠈⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠁⠀ | ||||
| */ | ||||
| 
 | ||||
| int main(int argc, char* argv[]) { | ||||
|     std::vector<CLI::Flag> flags; | ||||
|     flags.push_back(CLI::Flag('c', "compress",        "compress a file")); | ||||
|     flags.push_back(CLI::Flag('d', "decompress",         "descompress a file")); | ||||
|     std::vector<CLI::Option> options; | ||||
|     std::vector<CLI::Argument> arguments; | ||||
|     arguments.push_back(CLI::Argument("FILE", "path of the file to compress/decompress")); | ||||
|     CLI::ArgumentsParser cliParser = CLI::ArgumentsParser(argc, argv, flags, options, arguments, "Compress or decompress files using zlib"); | ||||
| 
 | ||||
|     if (cliParser.getFlag("help").value) { | ||||
|         std::cout << cliParser.getUsage() << std::endl; | ||||
|         return EXIT_SUCCESS; | ||||
|     } | ||||
| 
 | ||||
|     if (cliParser.getFlag("license").value){ | ||||
|         std::cout | ||||
|             << "Copyright 2022, FOSS-VG Developers and Contributers\n" | ||||
|             << "\n" | ||||
|             << "ZlibUtil is part of the FOSS-VG development tool suite.\n" | ||||
|             << "\n" | ||||
|             << "This program is free software: you can redistribute it and/or modify it\n" | ||||
|             << "under the terms of the GNU Affero General Public License as published\n" | ||||
|             << "by the Free Software Foundation, version 3.\n" | ||||
|             << "\n" | ||||
|             << "This program is distributed in the hope that it will be useful,\n" | ||||
|             << "but WITHOUT ANY WARRANTY; without even the implied\n" | ||||
|             << "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | ||||
|             << "See the GNU Affero General Public License for more details.\n" | ||||
|             << "\n" | ||||
|             << "You should have received a copy of the GNU Affero General Public License\n" | ||||
|             << "version 3 along with this program.\n" | ||||
|             << "If not, see https://www.gnu.org/licenses/agpl-3.0.en.html" | ||||
|             << std::endl; | ||||
|         return EXIT_SUCCESS; | ||||
|     } | ||||
| 
 | ||||
|     if (cliParser.wrongUsage) { | ||||
|         std::cout << cliParser.getUsage() << std::endl; | ||||
|         return EXIT_USAGE; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     ErrorOr<File*> filePointer = File::open(cliParser.getArgument(0).value, 'r'); | ||||
|     if (filePointer.isError) { | ||||
|         std::cout << "Failed to open file: " << cliParser.getArgument(0).value << std::endl; | ||||
|         return EXIT_RUNTIME; | ||||
|     } | ||||
|     File* file = filePointer.value; | ||||
|     File *writeFile; | ||||
| 
 | ||||
|     if (cliParser.getFlag("compress").value) { | ||||
|         std::vector<uint8_t> bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC
 | ||||
|         std::vector<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end()); | ||||
| 
 | ||||
|         std::vector<char> compressed = compressData(differentBytes.data(), file->size.value); | ||||
|         std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end()); | ||||
|         writeFile = File::open(cliParser.getArgument(0).value + ".compressed", 'w').value; | ||||
|         writeFile->write(unsigneddata); | ||||
|         writeFile->close(); | ||||
|    	} else if (cliParser.getFlag("decompress").value) { | ||||
|         std::vector<uint8_t> bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC
 | ||||
|         std::vector<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end()); | ||||
| 
 | ||||
|         std::vector<char> compressed = decompressData(differentBytes.data(), file->size.value); | ||||
|         std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end()); | ||||
| 
 | ||||
|         writeFile = File::open(cliParser.getArgument(0).value + ".uncompressed", 'w').value; | ||||
|         writeFile->write(unsigneddata); | ||||
|         writeFile->close(); | ||||
|    	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	 BodgeMaster
						BodgeMaster