enowars5-service-stldoctor

STL-Analyzing A/D Service for ENOWARS5 in 2021
git clone https://git.sinitax.com/sinitax/enowars5-service-stldoctor
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 452885a387b3a1613defa378cee79eb97e7b4fc8
parent dafdcc3547ec786fac85486cbb77909fffe1a4de
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue, 18 May 2021 14:45:50 +0200

seperated modelname and solidname handling

Diffstat:
Mservice/src/.gitignore | 2+-
Mservice/src/main.c | 11++++++++---
Dservice/src/stldoctor | 0
Mservice/src/stlfile.c | 45+++++++++++++++++++++++++++++++++------------
Mservice/src/stlfile.h | 5+++--
Mservice/src/test.sh | 36+++++++++++++++---------------------
Mservice/src/tests/evil1.stl | 4++--
Aservice/test.stl | 0
8 files changed, 62 insertions(+), 41 deletions(-)

diff --git a/service/src/.gitignore b/service/src/.gitignore @@ -1,4 +1,4 @@ -printdoc +stldoctor scans *.o vgcore.* diff --git a/service/src/main.c b/service/src/main.c @@ -154,10 +154,15 @@ query_cmd(char *arg) FILE *f = NULL; size_t size; - if (cached.valid) + if (arg && !strcmp(arg, "last")) { + if (!cached.valid) { + fprintf(stderr, "No cached info report available\n"); + return; + } hash = cached.hash; - else - hash = mhash(ask("> What is the model name? "), -1); + } else { + hash = mhash(ask("> Model name: "), -1); + } if (!(d = opendir(resultdir))) return; diff --git a/service/src/stldoctor b/service/src/stldoctor Binary files differ. diff --git a/service/src/stlfile.c b/service/src/stlfile.c @@ -133,7 +133,7 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) PARSE_FAIL("Multiple nested solids!\n"); tmp = bp; if (!consume_keyword(&bp) && (arg = consume_arg(&bp, &end))) { - info->modelname = strndup(arg, end - arg); + info->solidname = strndup(arg, end - arg); } else { bp = tmp; } @@ -142,9 +142,9 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) if ((kw = stack_pop(&states)) != STATE_SOLID) PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str); tmp = bp; - if (info->modelname && !consume_keyword(&bp) + if (info->solidname && !consume_keyword(&bp) && (arg = consume_arg(&bp, &end))) { - if (strncmp(info->modelname, arg, end - arg)) + if (strncmp(info->solidname, arg, end - arg)) PARSE_FAIL("Solid end/begin names do not match!\n"); } else { bp = tmp; @@ -219,9 +219,18 @@ parse_file_bin(struct parseinfo *info, char *buf, size_t len) memcpy(info->header, buf, 80); + if (strlen(buf + 1)) + info->solidname = checkp(strdup(buf + 1)); + bp = buf + 80; info->loopcount = le32toh(*(uint32_t*)bp); + if (!info->loopcount) { + memset(info->bbmax, 0, sizeof(float) * 3); + memset(info->bbmin, 0, sizeof(float) * 3); + return OK; + } + for (i = 0; i < 3; i++) { info->bbmin[i] = INFINITY; info->bbmax[i] = -INFINITY; @@ -261,14 +270,17 @@ parse_file(struct parseinfo *info, char *buf, size_t len) return FAIL; } - for (bp = buf; strchr(wsset, *bp); bp++); + info->filesize = len; /* check bin vs ascii with first keyword */ + for (bp = buf; strchr(wsset, *bp); bp++); status = !strncmp("solid", bp, 5) && strchr(wsset, bp[5]) ? parse_file_ascii(info, buf, len) : parse_file_bin(info, buf, len); if (status == FAIL) return FAIL; + if (!info->solidname) info->solidname = checkp(strdup("")); + if (!info->modelname) { resp = ask("Please enter your model name:\n"); if (strlen(resp) < 4) { @@ -291,6 +303,7 @@ save_info(struct parseinfo *info, FILE *f) nwrote += fwrite(&info->type, sizeof(int), 1, f); nwrote += fwrite(&info->loopcount, sizeof(int), 1, f); + nwrote += fwrite(&info->filesize, sizeof(unsigned), 1, f); for (i = 0; i < 3; i++) { nwrote += fwrite(&info->bbmin[i], sizeof(float), 1, f); @@ -299,10 +312,11 @@ save_info(struct parseinfo *info, FILE *f) nwrote += fwrite(info->header, 80, 1, f); - if (nwrote != 9) return FAIL; + if (nwrote != 10) return FAIL; - fputstr(f, info->modelname); + fputstr(f, info->solidname); fputstr(f, info->hash); + fputstr(f, info->modelname); return OK; } @@ -315,6 +329,7 @@ load_info(struct parseinfo *info, FILE *f) nread += fread(&info->type, sizeof(int), 1, f); nread += fread(&info->loopcount, sizeof(int), 1, f); + nread += fread(&info->filesize, sizeof(unsigned), 1, f); for (i = 0; i < 3; i++) { nread += fread(&info->bbmin[i], sizeof(float), 1, f); @@ -323,10 +338,11 @@ load_info(struct parseinfo *info, FILE *f) nread += fread(info->header, 80, 1, f); - if (nread != 9) return FAIL; + if (nread != 10) return FAIL; - freadstr(f, &info->modelname); + freadstr(f, &info->solidname); freadstr(f, &info->hash); + freadstr(f, &info->modelname); info->valid = 1; @@ -342,7 +358,7 @@ print_info(struct parseinfo *info) printf(" === Model info === \n"); - printf(" Name: %s\n", info->modelname); + printf(" File Size: %u\n", info->filesize); if (info->type == TYPE_BIN) { printf(" Header:\n "); @@ -356,12 +372,16 @@ print_info(struct parseinfo *info) } } - printf(" Hash: %s\n", info->hash); - printf(" Triangles: %i\n", info->loopcount); - printf(" Bounding Box: %.2f x %.2f x %.2f\n", + printf(" Model ID: %s\n", info->hash); + printf(" Model Name: %s\n", info->modelname); + printf(" Solid Name: %s\n", info->solidname); + printf(" Triangle Count: %i\n", info->loopcount); + printf(" Bounding Box Size: %.2f x %.2f x %.2f\n", info->bbmax[0] - info->bbmin[0], info->bbmax[1] - info->bbmin[1], info->bbmax[2] - info->bbmin[2]); + printf(" Bounding Box Origin: %.2f x %.2f x %.2f\n", + info->bbmin[0], info->bbmin[1], info->bbmin[2]); printf(" ================== \n"); } @@ -371,5 +391,6 @@ free_info(struct parseinfo *info) { NULLFREE(info->hash); NULLFREE(info->modelname); + NULLFREE(info->solidname); info->valid = 0; } diff --git a/service/src/stlfile.h b/service/src/stlfile.h @@ -38,8 +38,9 @@ struct stack { }; struct parseinfo { - char header[80], *hash, *modelname; - unsigned int loopcount; + char header[80], *hash, *modelname, *solidname; + uint32_t loopcount; + unsigned filesize; float bbmin[3], bbmax[3]; int type, valid; }; diff --git a/service/src/test.sh b/service/src/test.sh @@ -2,7 +2,10 @@ set -e -# RUN_REMOTE=1 +# RUNTYPE=1 + +SCRIPTPATH="$(dirname $(readlink -f "$0"))" +cd "$SCRIPTPATH" export RESULTDIR="../data/scans" export ECHO_INPUT=1 @@ -30,8 +33,10 @@ checkleaks() { } connect() { - if [ $RUN_REMOTE ]; then + if [ "$RUNTYPE" == "remote" ]; then nc localhost 9000 + elif [ "$RUNTYPE" == "debug" ]; then + checkleaks else ./stldoctor fi @@ -58,7 +63,7 @@ elif [ "$1" == "poc" ]; then announce "Testing Proof-Of-Concept" - rm -rf "$RESULTDIR"/* + [ ! -z "$RESULTDIR" ] && rm -rf "$RESULTDIR"/* echo -e "\n--- Uploading target STL ---\n" 1>&2 ( @@ -66,6 +71,7 @@ elif [ "$1" == "poc" ]; then echo "submit" cat tests/flag1.stl | wc -c cat tests/flag1.stl + echo "N0TaFL4G" echo "exit" ) | connect @@ -75,7 +81,7 @@ elif [ "$1" == "poc" ]; then echo "submit" cat tests/evil1.stl | wc -c cat tests/evil1.stl - echo -e "AAAA\xff" + echo "EV1L" echo "exit" ) | connect @@ -85,38 +91,26 @@ elif [ "$1" == "poc" ]; then # try index 0 echo "query" - echo -e "AAAA\xff" + echo "EV1L" echo "0" echo "n" - echo "query" + echo "query last" echo "0" echo "n" - # reset cached result - echo "submit" - echo "2" - echo "aa" - # try index 1 echo "query" - echo -e "AAAA\xff" + echo -e "EV1L" echo "0" echo "n" - echo "query" + echo "query last" echo "1" echo "n" echo "exit" ) | connect else - ( - echo "submit" - echo "2" - echo "AA" - echo "AAAA" - echo "exit" - ) | connect - + connect fi diff --git a/service/src/tests/evil1.stl b/service/src/tests/evil1.stl @@ -1,5 +1,5 @@ -solid - facet normal 0 0 1.0 +solid test˙ +facet normal 0 0 1.0 outer loop vertex 1 0 0 vertex 1 1 0 diff --git a/service/test.stl b/service/test.stl Binary files differ.