Compare commits
No commits in common. "ef0a2707dd448b2584175c0b1c94a3091fcdfb2e" and "6ef1d401bb84be7385873e61bc52598418cd9772" have entirely different histories.
ef0a2707dd
...
6ef1d401bb
|
@ -22,8 +22,10 @@ each file.
|
|||
Don't use excessive comments, use descriptive names instead.
|
||||
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.
|
||||
Format comments to improve readability, don't have long single-line comments.
|
||||
|
||||
Indent by four spaces.
|
||||
|
||||
|
|
|
@ -59,9 +59,3 @@ Usage: `check_endianness > header_file`
|
|||
Note that, while this tool should in theory be able to detect
|
||||
PDP and Honeywell-316-style endianness, the FOSS-VG project itself
|
||||
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.
|
||||
|
|
Binary file not shown.
|
@ -5,18 +5,9 @@ else
|
|||
VERBOSE=false
|
||||
fi
|
||||
|
||||
if [ "$1" = "-n" ]; then
|
||||
DECOMPRESS=false
|
||||
shift
|
||||
else
|
||||
DECOMPRESS=true
|
||||
fi
|
||||
|
||||
|
||||
if [ ! -f "$1" ]; then
|
||||
echo "Usage: $0 [-v] [-n] FILE"
|
||||
echo "Usage: $0 [-v] FILE"
|
||||
echo " -v display raw content and NBT dump"
|
||||
echo " -n don't decompress"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@ -71,20 +62,13 @@ sys.stdout.buffer.flush()
|
|||
esac
|
||||
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
|
||||
echo "Raw chunk data:"
|
||||
echo "$DATA"
|
||||
echo "NBT dump:"
|
||||
|
||||
#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
|
||||
rm /tmp/chunk_uncompressed_nbt
|
||||
fi
|
||||
|
|
|
@ -35,19 +35,10 @@ fi
|
|||
# dynamically linked libraries.
|
||||
echo ">>> Building libs..."
|
||||
create_directory bin/lib
|
||||
COMPILE_COMMANDS=(
|
||||
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/net/connection.so ./src/lib/net/connection.cpp"
|
||||
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/cli.so ./src/lib/cli.cpp"
|
||||
"$CXX_WITH_FLAGS -I ./include -fPIC -shared -o ./bin/lib/file.so ./src/lib/file.cpp"
|
||||
"$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]} &
|
||||
for lib in $(find ./src/lib -name "*.cpp"); do
|
||||
COMPILE_COMMAND="$CXX_WITH_FLAGS -I ./include -fPIC -shared -o $(sed -e 's/^.\/src/.\/bin/;s/cpp$/so/' <<< $lib) $lib"
|
||||
echo $COMPILE_COMMAND
|
||||
$COMPILE_COMMAND &
|
||||
$WAIT_ANYWAY
|
||||
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/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/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 -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"
|
||||
|
|
|
@ -39,7 +39,6 @@ fi
|
|||
|
||||
ln -vs ../dependencies/sockpp-0.8.1/include/sockpp/ ./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 ./include/.placeholder
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
source scripts/lib.sh
|
||||
|
||||
#TODO: move this to download.sh
|
||||
echo ">>> Checking download cache for unneeded or corrupted files..."
|
||||
for file in $(ls .download_cache); do
|
||||
#TODO: remove if unknown shasum
|
||||
|
|
|
@ -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
|
|
@ -59,16 +59,16 @@ fi
|
|||
function remove {
|
||||
local USE_FORCE=""
|
||||
if [ "$1" = "-f" ]; then
|
||||
USE_FORCE="f"
|
||||
shift
|
||||
USE_FORCE="-f"
|
||||
echo "Forcefully removing $1..."
|
||||
else
|
||||
echo "Removing $1..."
|
||||
fi;
|
||||
shift
|
||||
if [ -d "$1" ]; then
|
||||
rm -"$USE_FORCE"rv "$1"
|
||||
rm "$USE_FORCE" -rv "$1"
|
||||
else
|
||||
rm -"$USE_FORCE"v "$1"
|
||||
rm "$USE_FORCE" -v "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
|
@ -17,19 +17,74 @@
|
|||
|
||||
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_dependencies.sh
|
||||
|
||||
set -e # failures are not acceptable here
|
||||
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
|
||||
scripts/download.sh 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://github.com/DuffsDevice/tiny-utf8/archive/refs/tags/v4.4.3.tar.gz dependencies/tmp/tiny-utf8.tar.gz 8e3f61651909c9f3105d3501932a96aa65733127fb6e7cf94cb1b0a2dff42c8f
|
||||
download https://github.com/fpagliughi/sockpp/archive/refs/tags/v0.8.1.tar.gz dependencies/tmp/sockpp.tar.gz a8aedff8bd8c1da530b91be650352008fddabc9f1df0d19701d76cbc359c8651
|
||||
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
|
||||
#also TODO: target a specific commit
|
||||
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... "
|
||||
gzip -d dependencies/tmp/tiny-utf8.tar.gz
|
||||
|
@ -42,8 +97,11 @@ tar -xf dependencies/tmp/sockpp.tar -C dependencies
|
|||
echo "done"
|
||||
|
||||
echo -n ">>> Extracting zlib... "
|
||||
xz -d dependencies/tmp/zlib.tar.xz
|
||||
tar -xf dependencies/tmp/zlib.tar -C dependencies
|
||||
gzip -d dependencies/tmp/zlib-1.3.tar.gz
|
||||
tar -xf dependencies/tmp/zlib-1.3.tar -C dependencies
|
||||
cd include/
|
||||
ln -vs ../dependencies/zlib-1.3/ zlib
|
||||
cd ..
|
||||
echo "done"
|
||||
|
||||
echo ">>> Building sockpp..."
|
||||
|
@ -90,10 +148,9 @@ cp -v "dependencies/tmp/shaderc/$SHADERC_BUILD/glslc/glslc" dependencies/shaderc
|
|||
echo ">>> done"
|
||||
|
||||
echo ">>> Building zlib..."
|
||||
pushd dependencies/zlib-1.3.1/
|
||||
cd dependencies/zlib-1.3/
|
||||
./configure
|
||||
make
|
||||
popd
|
||||
echo "done"
|
||||
|
||||
echo ">>> Cleaning up..."
|
||||
|
|
|
@ -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
|
|
@ -1,46 +1,26 @@
|
|||
#!/usr/bin/env bash
|
||||
echo "================================================================================"
|
||||
echo "Testing \`zlibutil\`:"
|
||||
echo "--------------------------------------------------------------------------------"
|
||||
echo -n "Testing \`zlibutil\`... "
|
||||
|
||||
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"
|
||||
zlibutil "$TMPDIR/compress"
|
||||
bin/tools/zlibutil -c testfile
|
||||
|
||||
python3 <<< "
|
||||
import zlib, sys
|
||||
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"
|
||||
if [ "$(bin/tools/arraydump --binary testfile.compressed)" = "{0b01111000, 0b10011100, 0b01001011, 0b01001100, 0b01001010, 0b11100110, 0b00000010, 0b00000000, 0b00000011, 0b01111110, 0b00000001, 0b00110001}" ]; then
|
||||
echo -n "Compression Test: PASS... "
|
||||
else
|
||||
echo "FAIL"
|
||||
echo -n "Compression Test: FAILED... "
|
||||
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 "================================================================================"
|
||||
|
|
|
@ -17,11 +17,6 @@
|
|||
|
||||
#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;
|
||||
|
@ -105,9 +100,6 @@ 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;
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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
|
|
@ -16,101 +16,92 @@
|
|||
// version 3 along with this program.
|
||||
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
// includes vector and error.hpp
|
||||
#include "zlibutil.hpp"
|
||||
#include "../lib/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));
|
||||
|
||||
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];
|
||||
std::vector<char> compressedData;
|
||||
|
||||
int ret;
|
||||
char outbuffer[CHUNK_SIZE];
|
||||
std::vector<char> compressedData;
|
||||
do {
|
||||
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
|
||||
zs.avail_out = CHUNK_SIZE;
|
||||
|
||||
do {
|
||||
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
|
||||
zs.avail_out = CHUNK_SIZE;
|
||||
ret = deflate(&zs, Z_FINISH);
|
||||
|
||||
ret = deflate(&zs, Z_FINISH);
|
||||
if (compressedData.size() < zs.total_out)
|
||||
compressedData.insert(compressedData.end(), outbuffer, outbuffer + CHUNK_SIZE - zs.avail_out);
|
||||
} while (ret == Z_OK);
|
||||
|
||||
if (compressedData.size() < zs.total_out)
|
||||
compressedData.insert(compressedData.end(), outbuffer, outbuffer + CHUNK_SIZE - zs.avail_out);
|
||||
} while (ret == Z_OK);
|
||||
deflateEnd(&zs);
|
||||
|
||||
deflateEnd(&zs);
|
||||
|
||||
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()));
|
||||
}
|
||||
if (ret != Z_STREAM_END)
|
||||
throw(std::runtime_error("Error while compressing: " + std::to_string(ret)));
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
||||
namespace zlib {
|
||||
ErrorOr<std::vector<uint8_t>> compressData(std::vector<uint8_t> data);
|
||||
ErrorOr<std::vector<uint8_t>> decompressData(std::vector<uint8_t> data);
|
||||
}
|
||||
#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
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include <tinyutf8/tinyutf8.h>
|
||||
#include <zlib/zlib.h>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
#include "../lib/cli.hpp"
|
||||
#include "../lib/file.hpp"
|
||||
|
@ -33,10 +32,10 @@
|
|||
#define EXIT_RUNTIME 1
|
||||
#define EXIT_USAGE 2
|
||||
|
||||
#define CHUNK_SIZE 16384 // Chunk size
|
||||
|
||||
|
||||
/*
|
||||
Finally, the main file
|
||||
Finnaly, the main file
|
||||
⠀⠀⠀⠀⠀⠀⠀⢠⣤⣄⣀⣠⣤⣶⣿⣯⣿⣽⣾⣷⣶⣶⡦⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||
⠀⠀⠀⠀⠀⠀⠀⢀⣿⣟⣯⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣟⣦⣄⠀⠀⠀⠀⠀⠀⠀
|
||||
⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣾⡷⣄⠀⠀⠀⠀⠀
|
||||
|
@ -66,7 +65,8 @@ Finally, the main file
|
|||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::vector<CLI::Flag> flags;
|
||||
flags.push_back(CLI::Flag('d', "decompress", "descompress a file"));
|
||||
flags.push_back(CLI::Flag('c', "compress", "compress a file"));
|
||||
flags.push_back(CLI::Flag('d', "decompress", "descompress a file"));
|
||||
std::vector<CLI::Option> options;
|
||||
std::vector<CLI::Argument> arguments;
|
||||
arguments.push_back(CLI::Argument("FILE", "path of the file to compress/decompress"));
|
||||
|
@ -104,39 +104,32 @@ int main(int argc, char* argv[]) {
|
|||
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) {
|
||||
std::cout << "Failed to open file: " << filename << std::endl;
|
||||
std::cout << "Failed to open file: " << cliParser.getArgument(0).value << std::endl;
|
||||
return EXIT_RUNTIME;
|
||||
}
|
||||
File* file = filePointer.value;
|
||||
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<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end());
|
||||
|
||||
std::vector<char> compressed = decompressData(differentBytes.data(), file->size.value);
|
||||
std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end());
|
||||
|
||||
std::string outFilename;
|
||||
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 = File::open(cliParser.getArgument(0).value + ".uncompressed", 'w').value;
|
||||
writeFile->write(unsigneddata);
|
||||
writeFile->close();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue