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>
//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>
struct ErrorOr {
bool isError;
@ -100,6 +105,9 @@ namespace ErrorCodes {
// when too much data is available
const uint8_t OVERFLOW = 13;
const uint8_t COMPRESSION = 14;
const uint8_t DECOMPRESSION = 15;
const uint8_t UNIMPLEMENTED = 254;
const uint8_t UNKNOWN = 255;

View File

@ -16,40 +16,35 @@
// version 3 along with this program.
// 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 <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
namespace zlib {
ErrorOr<std::vector<uint8_t>> compressData(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());
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."));
if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) {
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::COMPRESSION);
//TODO: include error message once implemented
//throw(std::runtime_error("deflateInit failed while compressing."));
}
zs.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data));
zs.avail_in = size;
zs.next_in = reinterpret_cast<Bytef*>(reinterpret_cast<char*>(signedData.data()));
zs.avail_in = signedData.size();
int ret;
char outbuffer[CHUNK_SIZE];
@ -67,22 +62,31 @@ std::vector<char> compressData(const char* data, int size) {
deflateEnd(&zs);
if (ret != Z_STREAM_END)
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 compressedData;
}
return ErrorOr<std::vector<uint8_t>>(std::vector<uint8_t>(compressedData.begin(), compressedData.end()));
}
std::vector<char> decompressData(const char* data, int size) {
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)
throw(std::runtime_error("inflateInit failed while decompressing."));
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*>(const_cast<char*>(data));
zs.avail_in = size;
zs.next_in = reinterpret_cast<Bytef*>(reinterpret_cast<char*>(signedData.data()));
zs.avail_in = signedData.size();
int ret;
char outbuffer[CHUNK_SIZE];
@ -100,8 +104,13 @@ std::vector<char> decompressData(const char* data, int size) {
inflateEnd(&zs);
if (ret != Z_STREAM_END)
throw(std::runtime_error("Error while decompressing: " + std::to_string(ret)));
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 decompressedData;
}

View File

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