From fb539d1bfad3fc62cdec976e6676848903d637cc Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 29 Nov 2008 13:51:36 +0000 Subject: [PATCH] Stand-alone command-line interface to the obfuscate_bitmap() function. Useful in conjunction with the new --save option to generate lots of games, extract the aux strings from the game generator, and de-obfuscate them in order to measure statistical properties of their solutions. git-svn-id: svn://svn.tartarus.org/sgt/puzzles@8352 cda61777-01e9-0310-a592-d414129be87e --- Recipe | 3 ++ obfusc.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 obfusc.c diff --git a/Recipe b/Recipe index 363cc32..e377acd 100644 --- a/Recipe +++ b/Recipe @@ -53,6 +53,9 @@ const game *gamelist[] = { GAMELIST(REF) }; const int gamecount = lenof(gamelist); !end +# Unix standalone application for special-purpose obfuscation. +obfusc : [U] obfusc STANDALONE + # Mac OS X unified application containing all the puzzles. Puzzles : [MX] osx osx.icns osx-info.plist COMMON ALL # For OS X, we must create the online help and include it in the diff --git a/obfusc.c b/obfusc.c new file mode 100644 index 0000000..0db8aee --- /dev/null +++ b/obfusc.c @@ -0,0 +1,126 @@ +/* + * Stand-alone tool to access the Puzzles obfuscation algorithm. + * + * To deobfuscate, use "obfusc -d": + * + * obfusc -d reads binary data from stdin, writes to stdout + * obfusc -d works on the given hex string instead of stdin + * obfusc -d -h writes a hex string instead of binary to stdout + * + * To obfuscate, "obfusc -e": + * + * obfusc -e reads binary from stdin, writes hex to stdout + * obfusc -e works on the given hex string instead of stdin + * obfusc -e -b writes binary instead of text to stdout + * + * The default output format is hex for -e and binary for -d + * because that's the way obfuscation is generally used in + * Puzzles. Either of -b and -h can always be specified to set it + * explicitly. + * + * Data read from standard input is assumed always to be binary; + * data provided on the command line is taken to be hex. + */ + +#include +#include +#include +#include +#include + +#include "puzzles.h" + +int main(int argc, char **argv) +{ + enum { BINARY, DEFAULT, HEX } outputmode = DEFAULT; + char *inhex = NULL; + unsigned char *data; + int datalen; + int decode = -1; + int doing_opts = TRUE; + + while (--argc > 0) { + char *p = *++argv; + + if (doing_opts && *p == '-') { + if (!strcmp(p, "--")) { + doing_opts = 0; + continue; + } + p++; + while (*p) { + switch (*p) { + case 'e': + decode = 0; + break; + case 'd': + decode = 1; + break; + case 'b': + outputmode = BINARY; + break; + case 'h': + outputmode = HEX; + break; + default: + fprintf(stderr, "obfusc: unrecognised option '-%c'\n", + *p); + return 1; + } + p++; + } + } else { + if (!inhex) { + inhex = p; + } else { + fprintf(stderr, "obfusc: expected at most one argument\n"); + return 1; + } + } + } + + if (decode < 0) { + fprintf(stderr, "usage: obfusc < -e | -d > [ -b | -h ] [hex data]\n"); + return 0; + } + + if (outputmode == DEFAULT) + outputmode = (decode ? BINARY : HEX); + + if (inhex) { + datalen = strlen(inhex) / 2; + data = hex2bin(inhex, datalen); + } else { + int datasize = 4096; + datalen = 0; + data = snewn(datasize, unsigned char); + while (1) { + int ret = fread(data + datalen, 1, datasize - datalen, stdin); + if (ret < 0) { + fprintf(stderr, "obfusc: read: %s\n", strerror(errno)); + return 1; + } else if (ret == 0) { + break; + } else { + datalen += ret; + if (datasize - datalen < 4096) { + datasize = datalen * 5 / 4 + 4096; + data = sresize(data, datasize, unsigned char); + } + } + } + } + + obfuscate_bitmap(data, datalen * 8, decode); + + if (outputmode == BINARY) { + fwrite(data, 1, datalen, stdout); + } else { + int i; + for (i = 0; i < datalen; i++) + printf("%02x", data[i]); + printf("\n"); + } + + return 0; +} -- 2.11.0