lib/zlibutil, tools/zlibutil: moving things around

This includes the following changes:
- Move the data type wrangling into the lib to make it easier to use
- Put the functions into their own `zlib` namespace
- Use ErrorOr instead of exceptions
- Error codes for compression/decompression
jocadbz
BodgeMaster 2024-06-09 02:51:32 +02:00
parent 8482194b01
commit ef0a2707dd
3 changed files with 92 additions and 75 deletions

View File

@ -17,6 +17,11 @@
#include <cstdint> #include <cstdint>
//TODO: needed macros:
// TRY: takes a variable, a function call, and the ErrorOr return type of the calling function - intended to automatically unwrap the ErrorOr data type or propagate the error upwards
// RAISE: takes an error code and optionally a message to produce something like `return ErrorOr<T>(true, errorCode, file, lineNumber, message)`
template <typename T> template <typename T>
struct ErrorOr { struct ErrorOr {
bool isError; bool isError;
@ -100,6 +105,9 @@ namespace ErrorCodes {
// when too much data is available // when too much data is available
const uint8_t OVERFLOW = 13; const uint8_t OVERFLOW = 13;
const uint8_t COMPRESSION = 14;
const uint8_t DECOMPRESSION = 15;
const uint8_t UNIMPLEMENTED = 254; const uint8_t UNIMPLEMENTED = 254;
const uint8_t UNKNOWN = 255; const uint8_t UNKNOWN = 255;

View File

@ -16,92 +16,101 @@
// version 3 along with this program. // version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html // If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#include "../lib/zlibutil.hpp" // includes vector and error.hpp
#include "zlibutil.hpp"
#include <bitset>
#include <iomanip>
#include <iostream>
#include <vector>
#include <tinyutf8/tinyutf8.h>
#include <zlib/zlib.h> #include <zlib/zlib.h>
#include <cstring> #include <cstring>
#include "../lib/cli.hpp"
#include "../lib/file.hpp"
#define EXIT_SUCCESS 0 #define EXIT_SUCCESS 0
#define EXIT_RUNTIME 1 #define EXIT_RUNTIME 1
#define EXIT_USAGE 2 #define EXIT_USAGE 2
#include <iostream>
#include <fstream>
#include <vector>
#include <zlib.h>
#define CHUNK_SIZE 16384 // Chunk size #define CHUNK_SIZE 16384 // Chunk size
namespace zlib {
std::vector<char> compressData(const char* data, int size) { ErrorOr<std::vector<uint8_t>> compressData(std::vector<uint8_t> data) {
z_stream zs; // I, too, love the fact that raw bytes are signed and therefore can have negative values. -_-
memset(&zs, 0, sizeof(zs)); std::vector<int8_t> signedData = std::vector<int8_t>(data.begin(), data.end());
if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) z_stream zs;
throw(std::runtime_error("deflateInit failed while compressing.")); memset(&zs, 0, sizeof(zs));
zs.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) {
zs.avail_in = size; return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::COMPRESSION);
//TODO: include error message once implemented
//throw(std::runtime_error("deflateInit failed while compressing."));
}
int ret; zs.next_in = reinterpret_cast<Bytef*>(reinterpret_cast<char*>(signedData.data()));
char outbuffer[CHUNK_SIZE]; zs.avail_in = signedData.size();
std::vector<char> compressedData;
do { int ret;
zs.next_out = reinterpret_cast<Bytef*>(outbuffer); char outbuffer[CHUNK_SIZE];
zs.avail_out = CHUNK_SIZE; std::vector<char> compressedData;
ret = deflate(&zs, Z_FINISH); do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = CHUNK_SIZE;
if (compressedData.size() < zs.total_out) ret = deflate(&zs, Z_FINISH);
compressedData.insert(compressedData.end(), outbuffer, outbuffer + CHUNK_SIZE - zs.avail_out);
} while (ret == Z_OK);
deflateEnd(&zs); if (compressedData.size() < zs.total_out)
compressedData.insert(compressedData.end(), outbuffer, outbuffer + CHUNK_SIZE - zs.avail_out);
} while (ret == Z_OK);
if (ret != Z_STREAM_END) deflateEnd(&zs);
throw(std::runtime_error("Error while compressing: " + std::to_string(ret)));
if (ret != Z_STREAM_END) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::COMPRESSION);
//TODO: include error message once implemented
//throw(std::runtime_error("Error while compressing: " + std::to_string(ret)));
}
return ErrorOr<std::vector<uint8_t>>(std::vector<uint8_t>(compressedData.begin(), compressedData.end()));
}
ErrorOr<std::vector<uint8_t>> decompressData(std::vector<uint8_t> data) {
// I, too, love the fact that raw bytes are signed and therefore can have negative values. -_-
std::vector<int8_t> signedData = std::vector<int8_t>(data.begin(), data.end());
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::DECOMPRESSION);
//TODO: include error message once implemented
//throw(std::runtime_error("inflateInit failed while decompressing."));
}
zs.next_in = reinterpret_cast<Bytef*>(reinterpret_cast<char*>(signedData.data()));
zs.avail_in = signedData.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) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::DECOMPRESSION);
//TODO: include error message once implemented
//throw(std::runtime_error("Error while decompressing: " + std::to_string(ret)));
}
return ErrorOr<std::vector<uint8_t>>(std::vector<uint8_t>(decompressedData.begin(), decompressedData.end()));
}
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;
} }

View File

@ -16,12 +16,12 @@
// version 3 along with this program. // version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html // If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#pragma once
#include <vector> #include <vector>
#include "error.hpp"
#ifndef ZLIBUTIL_H namespace zlib {
#define ZLIBUTIL_H ErrorOr<std::vector<uint8_t>> compressData(std::vector<uint8_t> data);
ErrorOr<std::vector<uint8_t>> decompressData(std::vector<uint8_t> data);
std::vector<char> compressData(const char* data, int size); }
std::vector<char> decompressData(const char* data, int size);
#endif // ZLIBUTIL_H