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/decompressionmaster
parent
8482194b01
commit
53173dd7c7
|
@ -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
|
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include "../lib/cli.hpp"
|
#include "../lib/cli.hpp"
|
||||||
#include "../lib/file.hpp"
|
#include "../lib/file.hpp"
|
||||||
#include "../lib/zlibutil.hpp"
|
#include "../lib/zlibutil.hpp"
|
||||||
|
#include "../lib/error.hpp"
|
||||||
|
|
||||||
#define EXIT_SUCCESS 0
|
#define EXIT_SUCCESS 0
|
||||||
#define EXIT_RUNTIME 1
|
#define EXIT_RUNTIME 1
|
||||||
|
@ -115,11 +116,15 @@ int main(int argc, char* argv[]) {
|
||||||
File* writeFile;
|
File* writeFile;
|
||||||
|
|
||||||
if (cliParser.getFlag("decompress").value) {
|
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<uint8_t> bytes = file->read(file->size.value).value;
|
||||||
std::vector<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end());
|
|
||||||
|
|
||||||
std::vector<char> compressed = decompressData(differentBytes.data(), file->size.value);
|
ErrorOr<std::vector<uint8_t>> decompressed = zlib::decompressData(bytes);
|
||||||
std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end());
|
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;
|
std::string outFilename;
|
||||||
if (filename.length() > 3 && filename.rfind(".zz") == filename.length()-3) {
|
if (filename.length() > 3 && filename.rfind(".zz") == filename.length()-3) {
|
||||||
|
@ -128,16 +133,23 @@ int main(int argc, char* argv[]) {
|
||||||
outFilename = filename + ".uncompressed";
|
outFilename = filename + ".uncompressed";
|
||||||
}
|
}
|
||||||
writeFile = File::open(outFilename, 'w').value;
|
writeFile = File::open(outFilename, 'w').value;
|
||||||
writeFile->write(unsigneddata);
|
writeFile->write(decompressed.value);
|
||||||
writeFile->close();
|
writeFile->close();
|
||||||
} else {
|
} else {
|
||||||
std::vector<uint8_t> bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC
|
std::vector<uint8_t> bytes = file->read(file->size.value).value;
|
||||||
std::vector<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end());
|
|
||||||
|
|
||||||
std::vector<char> compressed = compressData(differentBytes.data(), file->size.value);
|
ErrorOr<std::vector<uint8_t>> compressed = zlib::compressData(bytes);
|
||||||
std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end());
|
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 = File::open(filename + ".zz", 'w').value;
|
||||||
writeFile->write(unsigneddata);
|
writeFile->write(compressed.value);
|
||||||
writeFile->close();
|
writeFile->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete file;
|
||||||
|
delete writeFile;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue