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/decompressionjocadbz
parent
8482194b01
commit
ef0a2707dd
|
@ -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;
|
||||||
|
|
|
@ -16,40 +16,35 @@
|
||||||
// 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 {
|
||||||
|
|
||||||
|
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;
|
z_stream zs;
|
||||||
memset(&zs, 0, sizeof(zs));
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
|
||||||
if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
|
if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) {
|
||||||
throw(std::runtime_error("deflateInit failed while compressing."));
|
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.next_in = reinterpret_cast<Bytef*>(reinterpret_cast<char*>(signedData.data()));
|
||||||
zs.avail_in = size;
|
zs.avail_in = signedData.size();
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
char outbuffer[CHUNK_SIZE];
|
char outbuffer[CHUNK_SIZE];
|
||||||
|
@ -67,22 +62,31 @@ std::vector<char> compressData(const char* data, int size) {
|
||||||
|
|
||||||
deflateEnd(&zs);
|
deflateEnd(&zs);
|
||||||
|
|
||||||
if (ret != Z_STREAM_END)
|
if (ret != Z_STREAM_END) {
|
||||||
throw(std::runtime_error("Error while compressing: " + std::to_string(ret)));
|
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;
|
z_stream zs;
|
||||||
memset(&zs, 0, sizeof(zs));
|
memset(&zs, 0, sizeof(zs));
|
||||||
|
|
||||||
if (inflateInit(&zs) != Z_OK)
|
if (inflateInit(&zs) != Z_OK) {
|
||||||
throw(std::runtime_error("inflateInit failed while decompressing."));
|
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.next_in = reinterpret_cast<Bytef*>(reinterpret_cast<char*>(signedData.data()));
|
||||||
zs.avail_in = size;
|
zs.avail_in = signedData.size();
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
char outbuffer[CHUNK_SIZE];
|
char outbuffer[CHUNK_SIZE];
|
||||||
|
@ -100,8 +104,13 @@ std::vector<char> decompressData(const char* data, int size) {
|
||||||
|
|
||||||
inflateEnd(&zs);
|
inflateEnd(&zs);
|
||||||
|
|
||||||
if (ret != Z_STREAM_END)
|
if (ret != Z_STREAM_END) {
|
||||||
throw(std::runtime_error("Error while decompressing: " + std::to_string(ret)));
|
return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::DECOMPRESSION);
|
||||||
|
//TODO: include error message once implemented
|
||||||
return decompressedData;
|
//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()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
||||||
|
|
Loading…
Reference in New Issue