Skip to content

Commit

Permalink
Repo cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed Apr 19, 2024
1 parent dd8ec85 commit a243513
Show file tree
Hide file tree
Showing 39 changed files with 1,075 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mtlGen
*.exe
*.ow
*.txt
!license.txt
*Check*
tests/benchmark
mtlFiles
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cNBT"]
path = cNBT
url = git@github.com:chmod222/cNBT.git
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"files.associations": {
"*.embeddedhtml": "html",
"*.check": "plaintext",
"*.m": "objective-c",
"cmath": "c",
"stdio.h": "c",
"array": "c",
Expand All @@ -15,7 +17,9 @@
"compare": "c",
"unistd.h": "c",
"types.h": "c",
"semaphore.h": "c"
"semaphore.h": "c",
"wait.h": "c",
"generator.h": "c"
},
"cSpell.words": [
"blockstates",
Expand Down
80 changes: 44 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ cNBT.o:
gcc cNBT/nbt_util.c -o cNBT/nbt_util.o -c $(CFLAGS)
ld -relocatable cNBT/buffer.o cNBT/nbt_parsing.o cNBT/nbt_treeops.o cNBT/nbt_util.o -o cNBT.o

regionParser.o: regionParser.c
gcc regionParser.c -o regionParser.o -c -lm $(CFLAGS)
regionParser.o: src/lib/regionParser.c
gcc src/lib/regionParser.c -o regionParser.o -c -lm $(CFLAGS)

chunkParser.o: chunkParser.c
gcc chunkParser.c -o chunkParser.o -c -lm $(CFLAGS)
chunkParser.o: src/lib/chunkParser.c
gcc src/lib/chunkParser.c -o chunkParser.o -c -lm $(CFLAGS)

hTable.o: hTable.c
gcc hTable.c -o hTable.o -c $(CFLAGS)
hTable.o: src/lib/hTable.c
gcc src/lib/hTable.c -o hTable.o -c $(CFLAGS)

model.o: model.c
gcc model.c -o model.o -c $(CFLAGS)
model.o: src/lib/model.c
gcc src/lib/model.c -o model.o -c $(CFLAGS)

regionFileReader: regionParser.o regionFileReader.c
gcc regionFileReader.c regionParser.o -o regionFileReader $(ZLIB) -lm $(CFLAGS)
regionFileReader: regionParser.o src/regionFileReader.c
gcc src/regionFileReader.c regionParser.o -o regionFileReader $(ZLIB) -lm $(CFLAGS)

chunkExtractor: regionParser.o chunkExtractor.c
gcc chunkExtractor.c regionParser.o -o chunkExtractor $(ZLIB) -lm $(CFLAGS)
chunkExtractor: regionParser.o src/chunkExtractor.c
gcc src/chunkExtractor.c regionParser.o -o chunkExtractor $(ZLIB) -lm $(CFLAGS)

generator.o: generator.c
gcc generator.c -o generator.o -c $(CFLAGS)
generator.o: src/lib/generator.c
gcc src/lib/generator.c -o generator.o -c $(CFLAGS)

modelGenerator: model.o generator.o hTable.o chunkParser.o cNBT.o modelGenerator.c
gcc modelGenerator.c generator.o model.o hTable.o chunkParser.o cNBT.o -o modelGenerator -lm $(CFLAGS)
modelGenerator: model.o generator.o hTable.o chunkParser.o cNBT.o src/modelGenerator.c
gcc src/modelGenerator.c generator.o model.o hTable.o chunkParser.o cNBT.o -o modelGenerator -lm $(CFLAGS)

radiusGenerator: model.o generator.o hTable.o chunkParser.o regionParser.o cNBT.o radiusGenerator.c
gcc radiusGenerator.c generator.o model.o regionParser.o hTable.o chunkParser.o cNBT.o $(ZLIB) -lm -o radiusGenerator $(CFLAGS)
radiusGenerator: model.o generator.o hTable.o chunkParser.o regionParser.o cNBT.o src/radiusGenerator.c
gcc src/radiusGenerator.c generator.o model.o regionParser.o hTable.o chunkParser.o cNBT.o $(ZLIB) -lm -o radiusGenerator $(CFLAGS)

cNBT.ow:
x86_64-w64-mingw32-gcc-win32 cNBT/buffer.c -o cNBT/buffer.ow -c $(CFLAGS)
Expand All @@ -60,32 +60,32 @@ cNBT.ow:
x86_64-w64-mingw32-gcc-win32 cNBT/nbt_util.c -o cNBT/nbt_util.ow -c $(CFLAGS)
x86_64-w64-mingw32-ld -relocatable cNBT/buffer.ow cNBT/nbt_parsing.ow cNBT/nbt_treeops.ow cNBT/nbt_util.ow -o cNBT.ow

regionParser.ow: regionParser.c
x86_64-w64-mingw32-gcc-win32 regionParser.c -o regionParser.ow -c $(CFLAGS)
regionParser.ow: src/lib/regionParser.c
x86_64-w64-mingw32-gcc-win32 src/lib/regionParser.c -o regionParser.ow -c $(CFLAGS)

chunkParser.ow: chunkParser.c
x86_64-w64-mingw32-gcc-win32 chunkParser.c -o chunkParser.ow -c -lm $(CFLAGS)
chunkParser.ow: src/lib/chunkParser.c
x86_64-w64-mingw32-gcc-win32 src/lib/chunkParser.c -o chunkParser.ow -c -lm $(CFLAGS)

hTable.ow: hTable.c
x86_64-w64-mingw32-gcc-win32 hTable.c -o hTable.ow -c $(CFLAGS)
hTable.ow: src/lib/hTable.c
x86_64-w64-mingw32-gcc-win32 src/lib/hTable.c -o hTable.ow -c $(CFLAGS)

model.ow: model.c
x86_64-w64-mingw32-gcc-win32 model.c -o model.ow -c $(CFLAGS)
model.ow: src/lib/model.c
x86_64-w64-mingw32-gcc-win32 src/lib/model.c -o model.ow -c $(CFLAGS)

regionFileReader.exe: regionParser.ow regionFileReader.c
x86_64-w64-mingw32-gcc-win32 regionFileReader.c regionParser.ow -o regionFileReader.exe -lz -static $(CFLAGS)
regionFileReader.exe: regionParser.ow src/regionFileReader.c
x86_64-w64-mingw32-gcc-win32 src/regionFileReader.c regionParser.ow -o regionFileReader.exe -lz -static $(CFLAGS)

chunkExtractor.exe: regionParser.ow chunkExtractor.c
x86_64-w64-mingw32-gcc-win32 chunkExtractor.c regionParser.ow -o chunkExtractor.exe -lz -static $(CFLAGS)
chunkExtractor.exe: regionParser.ow src/chunkExtractor.c
x86_64-w64-mingw32-gcc-win32 src/chunkExtractor.c regionParser.ow -o chunkExtractor.exe -lz -static $(CFLAGS)

generator.ow: generator.c
x86_64-w64-mingw32-gcc-win32 generator.c -o generator.ow -c $(CFLAGS)
generator.ow: src/lib/generator.c
x86_64-w64-mingw32-gcc-win32 src/lib/generator.c -o generator.ow -c $(CFLAGS)

modelGenerator.exe: generator.ow model.ow chunkParser.ow hTable.ow cNBT.ow modelGenerator.c
x86_64-w64-mingw32-gcc-win32 modelGenerator.c generator.ow model.ow chunkParser.ow hTable.ow cNBT.ow -o modelGenerator.exe -lm $(CFLAGS)
modelGenerator.exe: generator.ow model.ow chunkParser.ow hTable.ow cNBT.ow src/modelGenerator.c
x86_64-w64-mingw32-gcc-win32 src/modelGenerator.c generator.ow model.ow chunkParser.ow hTable.ow cNBT.ow -o modelGenerator.exe -lm $(CFLAGS)

radiusGenerator.exe: model.ow generator.ow hTable.ow chunkParser.ow regionParser.ow cNBT.ow radiusGenerator.c
x86_64-w64-mingw32-gcc-win32 radiusGenerator.c generator.ow model.ow regionParser.ow hTable.ow chunkParser.ow cNBT.ow -lm -static -lz -o radiusGenerator.exe $(CFLAGS)
radiusGenerator.exe: model.ow generator.ow hTable.ow chunkParser.ow regionParser.ow cNBT.ow src/radiusGenerator.c
x86_64-w64-mingw32-gcc-win32 src/radiusGenerator.c generator.ow model.ow regionParser.ow hTable.ow chunkParser.ow cNBT.ow -lm -static -lz -o radiusGenerator.exe $(CFLAGS)

windows: modelGenerator.exe chunkExtractor.exe regionFileReader.exe radiusGenerator.exe

Expand All @@ -94,6 +94,14 @@ clean:
rm -f cNBT/*.o
rm -f *.ow
rm -f cNBT/*.ow
rm -f radiusGenerator
rm -f radiusGenerator.exe
rm -f modelGenerator
rm -f modelGenerator.exe
rm -f chunkExtractor
rm -f chunkExtractor.exe
rm -f regionFileReader
rm -f regionFileReader.exe

check: hTable.o regionParser.o chunkParser.o cNBT.o model.o
#hTable tests
Expand Down
11 changes: 11 additions & 0 deletions cNBT/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.o
*.a
*.plist
tags
check
nbtreader
regioninfo
testdata/dump.bin
testdata/dump2.bin
build/
.vscode/
34 changes: 34 additions & 0 deletions cNBT/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# Lukas Niederbremer <webmaster@flippeh.de> and Clark Gaebel <cg.wowus.cg@gmail.com>
# wrote this file. As long as you retain this notice you can do whatever you
# want with this stuff. If we meet some day, and you think this stuff is worth
# it, you can buy us a beer in return.
# -----------------------------------------------------------------------------

CFLAGS=-g -Wall -Wextra -std=c99 -pedantic -fPIC

all: nbtreader check

nbtreader: main.o libnbt.a
$(CC) $(CFLAGS) main.o -L. -lnbt -lz -o nbtreader

check: check.c libnbt.a
$(CC) $(CFLAGS) check.c -L. -lnbt -lz -o check

regioninfo: regioninfo.c libnbt.a
$(CC) $(CFLAGS) regioninfo.c -L. -lnbt -lz -o regioninfo

test: check
cd testdata && ls -1 *.nbt | xargs -n1 valgrind ../check && cd ..

main.o: main.c

libnbt.a: buffer.o nbt_loading.o nbt_parsing.o nbt_treeops.o nbt_util.o
ar -rcs libnbt.a buffer.o nbt_loading.o nbt_parsing.o nbt_treeops.o nbt_util.o

buffer.o: buffer.c
nbt_loading.o: nbt_loading.c
nbt_parsing.o: nbt_parsing.c
nbt_treeops.o: nbt_treeops.c
nbt_util.o: nbt_util.c
14 changes: 14 additions & 0 deletions cNBT/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cNBT is an NBT File (see NBT.txt) parser that doesn't
depend on the horrible slowdown that is the JRE and sticks to the C principle of
being as simple (and, consequently, as fast) as possible.

Currently implemented features:

* Complete parsing of NBT files
* Basic tree-manipulation
* Pretty printing with indentation
* Writing modified NBT structures back to a compressed file
* Full error reporting and graceful recovery from corrupt files and trees

This project depends on zlib for gzip decompressing and compressing, and a compiler
with C99 support.
5 changes: 0 additions & 5 deletions cNBT/README.md

This file was deleted.

29 changes: 29 additions & 0 deletions cNBT/afl_check.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
#include "nbt.h"

int main(int argc, const char *argv[]) {
FILE *in = stdin;
if (argc > 1) {
in = fopen(argv[1], "rb");
if (!in) {
perror("fopen");
return -1;
}
}

#ifdef __AFL_HAVE_MANUAL_CONTROL
__AFL_INIT();
#endif


#ifdef __AFL_HAVE_MANUAL_CONTROL
while (__AFL_LOOP(10000)) {
#endif
char buf[65536];
size_t len = fread(buf, 1, sizeof(buf), in);
nbt_free(nbt_parse(buf, len));
#ifdef __AFL_HAVE_MANUAL_CONTROL
}
#endif
}
9 changes: 9 additions & 0 deletions cNBT/afl_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

afl_check="$1"

echo -ne '\x09\x00\x00' | $afl_check
echo -ne '\x64' | $afl_check
echo -ne '\xff\xfe\xff\xfe\x00\x03\xe8\xff\xfe\xff\xfe\x00\x03\xe8\xfe\x96\xf3\xf8' | $afl_check
echo -ne '\x40' | $afl_check
echo -ne '\x00\x64\x2e' | $afl_check
128 changes: 128 additions & 0 deletions cNBT/check.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include "nbt.h"

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void die(const char* message)
{
fprintf(stderr, "%s\n", message);
exit(1);
}

static void die_with_err(int err)
{
fprintf(stderr, "Error %i: %s\n", err, nbt_error_to_string(err));
exit(1);
}

static nbt_node* get_tree(const char* filename)
{
FILE* fp = fopen(filename, "rb");
if(fp == NULL) die("Could not open the file for reading.");

nbt_node* ret = nbt_parse_file(fp);
if(ret == NULL) die_with_err(errno);
fclose(fp);

return ret;
}

static bool check_size(nbt_node* n, void* aux)
{
(void)n;
int* size = aux;
*size += 1;

return true;
}

int main(int argc, char** argv)
{
if(argc == 1 || strcmp(argv[1], "--help") == 0)
{
printf("Usage: %s [nbt file]\n", argv[0]);
return 0;
}

printf("Getting tree from %s... ", argv[1]);
nbt_node* tree = get_tree(argv[1]);
printf("OK.\n");

/* Use this to refer to the tree in gdb. */
char* the_tree = nbt_dump_ascii(tree);

if(the_tree == NULL)
die_with_err(errno);

{
printf("Checking nbt_map and nbt_size...");
size_t mapped_size = 0;
bool ret = nbt_map(tree, check_size, &mapped_size);
size_t actual_size = nbt_size(tree);
if(!ret)
die("FAILED. nbt_map was terminated by a visitor, even though the visitor wants to do no such thing.");
if(mapped_size != actual_size)
die("FAILED. nbt_map and nbt_size are not playing nice.");
printf("OK.\n");
}

{
printf("Checking nbt_clone... ");
nbt_node* clone = nbt_clone(tree);
if(!nbt_eq(tree, clone))
die("FAILED. Clones not equal.");
nbt_free(tree); /* swap the tree out for its clone */
tree = clone;
printf("OK.\n");
}

FILE* temp = fopen("delete_me.nbt", "wb");
if(temp == NULL) die("Could not open a temporary file.");

nbt_status err;

printf("Dumping binary... ");
if((err = nbt_dump_file(tree, temp, STRAT_GZIP)) != NBT_OK)
die_with_err(err);
printf("OK.\n");

fclose(temp);
temp = fopen("delete_me.nbt", "rb");
if(temp == NULL) die("Could not re-open a temporary file.");

printf("Reparsing... ");
nbt_node* tree_copy = nbt_parse_file(temp);
if(tree_copy == NULL) die_with_err(errno);
printf("OK.\n");

printf("Checking trees... ");
if(!nbt_eq(tree, tree_copy))
{
printf("Original tree:\n%s\n", the_tree);

char* copy = nbt_dump_ascii(tree_copy);
if(copy == NULL) die_with_err(err);

printf("Reparsed tree:\n%s\n", copy);
die("Trees not equal.");
}
printf("OK.\n");

printf("Freeing resources... ");

fclose(temp);

if(remove("delete_me.nbt") == -1)
die("Could not delete delete_me.nbt. Race condition?");

nbt_free(tree);
nbt_free(tree_copy);

printf("OK.\n");

free(the_tree);
return 0;
}
Loading

0 comments on commit a243513

Please sign in to comment.