Compare commits

..

No commits in common. "ef0a2707dd448b2584175c0b1c94a3091fcdfb2e" and "6ef1d401bb84be7385873e61bc52598418cd9772" have entirely different histories.

22 changed files with 209 additions and 323 deletions

View File

@ -22,8 +22,10 @@ each file.
Don't use excessive comments, use descriptive names instead. Don't use excessive comments, use descriptive names instead.
There is no such thing as too long names (within reason, of course). There is no such thing as too long names (within reason, of course).
Don't have comments that go beyond 80 characters per line, preferably
slightly less.
Put comments on their own lines. Put comments on their own lines.
Format comments to improve readability, don't have long single-line comments.
Indent by four spaces. Indent by four spaces.

View File

@ -59,9 +59,3 @@ Usage: `check_endianness > header_file`
Note that, while this tool should in theory be able to detect Note that, while this tool should in theory be able to detect
PDP and Honeywell-316-style endianness, the FOSS-VG project itself PDP and Honeywell-316-style endianness, the FOSS-VG project itself
does not support these. does not support these.
## region_files
Exactly what the name says. At this point just a random region file I grabbed
from Minecraft, will probably contain synthetic files for test cases in the
future as well.

View File

@ -5,18 +5,9 @@ else
VERBOSE=false VERBOSE=false
fi fi
if [ "$1" = "-n" ]; then
DECOMPRESS=false
shift
else
DECOMPRESS=true
fi
if [ ! -f "$1" ]; then if [ ! -f "$1" ]; then
echo "Usage: $0 [-v] [-n] FILE" echo "Usage: $0 [-v] FILE"
echo " -v display raw content and NBT dump" echo " -v display raw content and NBT dump"
echo " -n don't decompress"
exit 1 exit 1
fi fi
@ -71,20 +62,13 @@ sys.stdout.buffer.flush()
esac esac
echo " format: $FORMAT ($FORMAT_HEX)" echo " format: $FORMAT ($FORMAT_HEX)"
if $DECOMPRESS; then
dd if="$1" bs=4096 count=$LENGTH skip=$OFFSET 2>/dev/null | dd bs=1 skip=5 count=$(($COMPRESSED_LENGTH-1)) 2>/dev/null | UNCOMPRESS > /tmp/chunk_uncompressed_nbt
else
dd if="$1" bs=4096 count=$LENGTH skip=$OFFSET 2>/dev/null | dd bs=1 skip=5 count=$(($COMPRESSED_LENGTH-1)) 2>/dev/null > /tmp/chunk_compressed_nbt
fi
if $VERBOSE; then if $VERBOSE; then
echo "Raw chunk data:" echo "Raw chunk data:"
echo "$DATA" echo "$DATA"
echo "NBT dump:" echo "NBT dump:"
#TODO: use pipes instead of a file #TODO: use pipes instead of a file
#TODO: fix this up to work with both compressed and uncompressed NBT dd if="$1" bs=4096 count=$LENGTH skip=$OFFSET 2>/dev/null | dd bs=1 skip=5 count=$(($COMPRESSED_LENGTH-1)) 2>/dev/null | UNCOMPRESS > /tmp/chunk_uncompressed_nbt
dumpnbt /tmp/chunk_uncompressed_nbt dumpnbt /tmp/chunk_uncompressed_nbt
rm /tmp/chunk_uncompressed_nbt rm /tmp/chunk_uncompressed_nbt
fi fi

View File

@ -35,19 +35,10 @@ fi
# dynamically linked libraries. # dynamically linked libraries.
echo ">>> Building libs..." echo ">>> Building libs..."
create_directory bin/lib create_directory bin/lib
COMPILE_COMMANDS=( for lib in $(find ./src/lib -name "*.cpp"); do
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/net/connection.so ./src/lib/net/connection.cpp" COMPILE_COMMAND="$CXX_WITH_FLAGS -I ./include -fPIC -shared -o $(sed -e 's/^.\/src/.\/bin/;s/cpp$/so/' <<< $lib) $lib"
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/cli.so ./src/lib/cli.cpp" echo $COMPILE_COMMAND
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/file.so ./src/lib/file.cpp" $COMPILE_COMMAND &
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/game/block.so ./src/lib/game/block.cpp"
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/game/entity.so ./src/lib/game/entity.cpp"
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/nbt.so ./src/lib/nbt.cpp"
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/region.so ./src/lib/region.cpp"
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -l:libz.so -o ./bin/lib/zlibutil.so ./src/lib/zlibutil.cpp"
)
for command in ${!COMPILE_COMMANDS[@]}; do
echo "${COMPILE_COMMANDS[command]}"
${COMPILE_COMMANDS[command]} &
$WAIT_ANYWAY $WAIT_ANYWAY
done done
@ -71,7 +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/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/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/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:file.so -l:zlibutil.so -l:libz.so -o bin/tools/zlibutil" "$CXX_WITH_FLAGS src/tools/zlibutil.cpp -I./include -Lbin/lib -l:cli.so -l:libz.so -l:file.so -l:zlibutil.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 -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 -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" "$CXX_WITH_FLAGS src/fossvgd.cpp -I./include -Lbin/lib -l:cli.so -o bin/fossvgd"

View File

@ -39,7 +39,6 @@ fi
ln -vs ../dependencies/sockpp-0.8.1/include/sockpp/ ./include/ ln -vs ../dependencies/sockpp-0.8.1/include/sockpp/ ./include/
ln -vs ../dependencies/tiny-utf8-4.4.3/include/tinyutf8/ ./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 ./bin/.placeholder
create_file ./include/.placeholder create_file ./include/.placeholder

View File

@ -17,7 +17,6 @@
source scripts/lib.sh source scripts/lib.sh
#TODO: move this to download.sh
echo ">>> Checking download cache for unneeded or corrupted files..." echo ">>> Checking download cache for unneeded or corrupted files..."
for file in $(ls .download_cache); do for file in $(ls .download_cache); do
#TODO: remove if unknown shasum #TODO: remove if unknown shasum

View File

@ -1,61 +0,0 @@
if [ "$#" -lt 3 -o "$#" -gt 3 ]; then
echo "Usage: $0 URL DESTINATION SHA256SUM"
exit 1
fi
source scripts/lib.sh
URL="$1"
DESTINATION="$2"
SHA256SUM="$3"
if command -v wget >/dev/null 2>&1; then
USE_WGET=yes
else
if command -v curl >/dev/null 2>&1; then
USE_WGET=no
else
echo "Found neither wget nor curl. Aborting."
exit 1
fi
fi
if [ ! -d .download_cache ]; then
echo "Cache directory missing."
create_directory .download_cache
fi
#TODO: keep track of which cached file is downloaded to which destination
#TODO: when downloading a file with a new hash to the same destination, mark the old one as no longer in use
#TODO: if there is an even older one for the same download path, delete it
#TODO: remove cache maintenance from clean_dependencies
if [ -f ".download_cache/$SHA256SUM" ]; then
if check_sha256 ".download_cache/$SHA256SUM" "$SHA256SUM"; then
echo "Using locally cached file for $DESTINATION"
cp ".download_cache/$SHA256SUM" "$DESTINATION"
exit 0
else
echo "Locally cached file for $DESTINATION is corrupted."
rm ".download_cache/$SHA256SUM"
$0 "$URL" "$DESTINATION" "$SHA256SUM"
exit $?
fi
else
echo -n "Downloading $URL to $DESTINATION... "
if [ $USE_WGET = yes ]; then
wget -O ".download_cache/$SHA256SUM" "$URL" >/dev/null 2>&1
else
curl -L "$URL" --output ".download_cache/$SHA256SUM" >/dev/null 2>&1
fi
if check_sha256 ".download_cache/$SHA256SUM" "$SHA256SUM"; then
cp ".download_cache/$SHA256SUM" "$DESTINATION"
echo "done."
exit 0
else
echo "error."
echo "Checksum verification failed. Your download is either corrupted or the file has been altered. Removing file."
rm ".download_cache/$SHA256SUM"
exit 1
fi
fi

View File

@ -59,16 +59,16 @@ fi
function remove { function remove {
local USE_FORCE="" local USE_FORCE=""
if [ "$1" = "-f" ]; then if [ "$1" = "-f" ]; then
USE_FORCE="f" USE_FORCE="-f"
shift
echo "Forcefully removing $1..." echo "Forcefully removing $1..."
else else
echo "Removing $1..." echo "Removing $1..."
fi; fi;
shift
if [ -d "$1" ]; then if [ -d "$1" ]; then
rm -"$USE_FORCE"rv "$1" rm "$USE_FORCE" -rv "$1"
else else
rm -"$USE_FORCE"v "$1" rm "$USE_FORCE" -v "$1"
fi fi
} }

View File

@ -17,19 +17,74 @@
source scripts/lib.sh source scripts/lib.sh
echo -n "Wget or curl? "
if command -v wget >/dev/null 2>&1; then
USE_WGET=yes
echo "wget"
else
if command -v curl >/dev/null 2>&1; then
USE_WGET=no
echo "curl"
else
echo "Found neither wget nor curl. Aborting."
exit 1
fi
fi
function download {
URL="$1"
DESTINATION="$2"
SHA256SUM="$3"
if [ ! -d .download_cache ]; then
echo "Cache directory missing."
create_directory .download_cache
fi
if [ -f ".download_cache/$SHA256SUM" ]; then
if check_sha256 ".download_cache/$SHA256SUM" "$SHA256SUM"; then
echo "Using locally cached file for $DESTINATION"
cp ".download_cache/$SHA256SUM" "$DESTINATION"
return 0
else
echo "Locally cached file for $DESTINATION is corrupted."
rm ".download_cache/$SHA256SUM"
download "$URL" "$DESTINATION" "$SHA256SUM"
return $?
fi
else
echo -n "Downloading $URL to $DESTINATION... "
if [ $USE_WGET = yes ]; then
wget -O ".download_cache/$SHA256SUM" "$URL" >/dev/null 2>&1
else
curl -L "$URL" --output ".download_cache/$SHA256SUM" >/dev/null 2>&1
fi
if check_sha256 ".download_cache/$SHA256SUM" "$SHA256SUM"; then
cp ".download_cache/$SHA256SUM" "$DESTINATION"
echo "done."
return 0
else
echo "error."
echo "Checksum verification failed. Your download is either corrupted or the file has been altered. Removing file."
rm ".download_cache/$SHA256SUM"
return 1
fi
fi
}
scripts/clean.sh scripts/clean.sh
scripts/clean_dependencies.sh scripts/clean_dependencies.sh
set -e # failures are not acceptable here set -e # failures are not acceptable here
create_directory dependencies/tmp create_directory dependencies/tmp
scripts/download.sh 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/DuffsDevice/tiny-utf8/archive/refs/tags/v4.4.3.tar.gz dependencies/tmp/tiny-utf8.tar.gz 8e3f61651909c9f3105d3501932a96aa65733127fb6e7cf94cb1b0a2dff42c8f
scripts/download.sh https://github.com/fpagliughi/sockpp/archive/refs/tags/v0.8.1.tar.gz dependencies/tmp/sockpp.tar.gz a8aedff8bd8c1da530b91be650352008fddabc9f1df0d19701d76cbc359c8651 download https://github.com/fpagliughi/sockpp/archive/refs/tags/v0.8.1.tar.gz dependencies/tmp/sockpp.tar.gz a8aedff8bd8c1da530b91be650352008fddabc9f1df0d19701d76cbc359c8651
scripts/download.sh https://www.zlib.net/zlib-1.3.1.tar.xz dependencies/tmp/zlib.tar.xz 38ef96b8dfe510d42707d9c781877914792541133e1870841463bfa73f883e32 download https://www.zlib.net/zlib-1.3.tar.gz dependencies/tmp/zlib-1.3.tar.gz ff0ba4c292013dbc27530b3a81e1f9a813cd39de01ca5e0f8bf355702efa593e
#TODO: figure out how to cache shaderc #TODO: figure out how to cache shaderc
#also TODO: target a specific commit #also TODO: target a specific commit
git clone --branch known-good https://github.com/google/shaderc.git dependencies/tmp/shaderc git clone --branch known-good https://github.com/google/shaderc.git dependencies/tmp/shaderc
#scripts/download.sh https://github.com/google/shaderc/archive/refs/tags/v2023.7.tar.gz dependencies/tmp/shaderc.tar.gz 681e1340726a0bf46bea7e31f10cbfe78e01e4446a35d90fedc2b78d400fcdeb #download https://github.com/google/shaderc/archive/refs/tags/v2023.7.tar.gz dependencies/tmp/shaderc.tar.gz 681e1340726a0bf46bea7e31f10cbfe78e01e4446a35d90fedc2b78d400fcdeb
echo -n ">>> Extracting tiny-utf8... " echo -n ">>> Extracting tiny-utf8... "
gzip -d dependencies/tmp/tiny-utf8.tar.gz gzip -d dependencies/tmp/tiny-utf8.tar.gz
@ -42,8 +97,11 @@ tar -xf dependencies/tmp/sockpp.tar -C dependencies
echo "done" echo "done"
echo -n ">>> Extracting zlib... " echo -n ">>> Extracting zlib... "
xz -d dependencies/tmp/zlib.tar.xz gzip -d dependencies/tmp/zlib-1.3.tar.gz
tar -xf dependencies/tmp/zlib.tar -C dependencies tar -xf dependencies/tmp/zlib-1.3.tar -C dependencies
cd include/
ln -vs ../dependencies/zlib-1.3/ zlib
cd ..
echo "done" echo "done"
echo ">>> Building sockpp..." echo ">>> Building sockpp..."
@ -90,10 +148,9 @@ cp -v "dependencies/tmp/shaderc/$SHADERC_BUILD/glslc/glslc" dependencies/shaderc
echo ">>> done" echo ">>> done"
echo ">>> Building zlib..." echo ">>> Building zlib..."
pushd dependencies/zlib-1.3.1/ cd dependencies/zlib-1.3/
./configure ./configure
make make
popd
echo "done" echo "done"
echo ">>> Cleaning up..." echo ">>> Cleaning up..."

23
scripts/test/hexnet.sh Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Copyright 2022, FOSS-VG Developers and Contributers
#
# 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
echo "################################################################################
Testing hexnet
################################################################################"
echo "Test not yet implemented."
#TODO: implement unit test after merging back with master

View File

@ -1,46 +1,26 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo "================================================================================" echo "================================================================================"
echo "Testing \`zlibutil\`:" echo -n "Testing \`zlibutil\`... "
echo "--------------------------------------------------------------------------------"
TMPDIR="$(mktemp -d -t fossvg-zlibutil-XXXXX)"
TMPDATA="$(dd if=/dev/urandom bs=33 count=1 2>/dev/null | base64)"
echo -n "Compression test... " echo "abc" >> testfile
echo -n "$TMPDATA" >> "$TMPDIR/compress" bin/tools/zlibutil -c testfile
zlibutil "$TMPDIR/compress"
python3 <<< " if [ "$(bin/tools/arraydump --binary testfile.compressed)" = "{0b01111000, 0b10011100, 0b01001011, 0b01001100, 0b01001010, 0b11100110, 0b00000010, 0b00000000, 0b00000011, 0b01111110, 0b00000001, 0b00110001}" ]; then
import zlib, sys echo -n "Compression Test: PASS... "
tmpfile = open('$TMPDIR/compress.zz', 'rb')
data = tmpfile.read()
tmpfile.close()
try:
if zlib.decompress(data)==b'$TMPDATA':
print('PASS')
else:
print('FAIL: Wrong data.')
except:
print('FAIL: Exception.')
"
echo -n "Decompression test... "
#TODO: create a compressed file using another implementation (Python)
python3 <<< "
import zlib
tmpfile = open('$TMPDIR/decompress.zz', 'wb')
tmpfile.write(zlib.compress(b'$TMPDATA'))
tmpfile.close()
"
zlibutil -d "$TMPDIR/decompress.zz"
if [ "$(cat "$TMPDIR/decompress")" = "$TMPDATA" ]; then
echo "PASS"
else else
echo "FAIL" echo -n "Compression Test: FAILED... "
fi fi
rm -r "$TMPDIR" bin/tools/zlibutil -d testfile.compressed
if [ "$(bin/tools/arraydump --binary testfile.compressed.uncompressed)" = "{0b01100001, 0b01100010, 0b01100011, 0b00001010}" ]; then
echo "Decompression Test: PASS"
else
echo "Decompression Test: FAILED"
fi
rm testfile testfile.compressed testfile.compressed.uncompressed
echo "================================================================================" echo "================================================================================"

View File

@ -17,11 +17,6 @@
#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;
@ -105,9 +100,6 @@ 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

@ -1,42 +0,0 @@
// Copyright 2022, FOSS-VG Developers and Contributers
//
// Author(s):
// BodgeMaster
//
// 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
#pragma once
#include <string>
#include "../error.hpp"
namespace network {
struct Connection {
}
// host could be a hostname or address
ErrorOr<Connection> connect(std::string host);
void disconnect(Connection);
// start listening for incoming connections
ErrorOrVoid startListening();
// stop listening for new incoming connections but keep existing
// connections open
void stopListening();
// stop listening for incoming connections and disconnect all
void stopCommunication();
ErrorOr<Connection> acceptNextIncoming();
}

View File

@ -1,16 +0,0 @@
// Copyright 2022, FOSS-VG Developers and Contributers
//
// Author(s):
//
// 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

View File

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

View File

@ -23,7 +23,6 @@
#include <tinyutf8/tinyutf8.h> #include <tinyutf8/tinyutf8.h>
#include <zlib/zlib.h> #include <zlib/zlib.h>
#include <cstring> #include <cstring>
#include <fstream>
#include "../lib/cli.hpp" #include "../lib/cli.hpp"
#include "../lib/file.hpp" #include "../lib/file.hpp"
@ -33,10 +32,10 @@
#define EXIT_RUNTIME 1 #define EXIT_RUNTIME 1
#define EXIT_USAGE 2 #define EXIT_USAGE 2
#define CHUNK_SIZE 16384 // Chunk size
/* /*
Finally, the main file Finnaly, the main file
@ -66,6 +65,7 @@ Finally, the main file
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::vector<CLI::Flag> flags; 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")); flags.push_back(CLI::Flag('d', "decompress", "descompress a file"));
std::vector<CLI::Option> options; std::vector<CLI::Option> options;
std::vector<CLI::Argument> arguments; std::vector<CLI::Argument> arguments;
@ -104,39 +104,32 @@ int main(int argc, char* argv[]) {
return EXIT_USAGE; return EXIT_USAGE;
} }
std::string filename = cliParser.getArgument(0).value;
ErrorOr<File*> filePointer = File::open(filename, 'r'); ErrorOr<File*> filePointer = File::open(cliParser.getArgument(0).value, 'r');
if (filePointer.isError) { if (filePointer.isError) {
std::cout << "Failed to open file: " << filename << std::endl; std::cout << "Failed to open file: " << cliParser.getArgument(0).value << std::endl;
return EXIT_RUNTIME; return EXIT_RUNTIME;
} }
File* file = filePointer.value; File* file = filePointer.value;
File *writeFile; File *writeFile;
if (cliParser.getFlag("decompress").value) { 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<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> differentBytes = std::vector<char>(bytes.begin(), bytes.end());
std::vector<char> compressed = decompressData(differentBytes.data(), file->size.value); std::vector<char> compressed = decompressData(differentBytes.data(), file->size.value);
std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end()); std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end());
std::string outFilename; writeFile = File::open(cliParser.getArgument(0).value + ".uncompressed", 'w').value;
if (filename.length() > 3 && filename.rfind(".zz") == filename.length()-3) {
outFilename = filename.substr(0, filename.length()-3);
} else {
outFilename = filename + ".uncompressed";
}
writeFile = File::open(outFilename, 'w').value;
writeFile->write(unsigneddata);
writeFile->close();
} else {
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(filename + ".zz", 'w').value;
writeFile->write(unsigneddata); writeFile->write(unsigneddata);
writeFile->close(); writeFile->close();
} }