summaryrefslogtreecommitdiff
path: root/apps/plugins/puzzles/obfusc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/puzzles/obfusc.c')
-rw-r--r--apps/plugins/puzzles/obfusc.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/apps/plugins/puzzles/obfusc.c b/apps/plugins/puzzles/obfusc.c
new file mode 100644
index 0000000000..e95fa3f397
--- /dev/null
+++ b/apps/plugins/puzzles/obfusc.c
@@ -0,0 +1,126 @@
1/*
2 * Stand-alone tool to access the Puzzles obfuscation algorithm.
3 *
4 * To deobfuscate, use "obfusc -d":
5 *
6 * obfusc -d reads binary data from stdin, writes to stdout
7 * obfusc -d <hex string> works on the given hex string instead of stdin
8 * obfusc -d -h writes a hex string instead of binary to stdout
9 *
10 * To obfuscate, "obfusc -e":
11 *
12 * obfusc -e reads binary from stdin, writes hex to stdout
13 * obfusc -e <hex string> works on the given hex string instead of stdin
14 * obfusc -e -b writes binary instead of text to stdout
15 *
16 * The default output format is hex for -e and binary for -d
17 * because that's the way obfuscation is generally used in
18 * Puzzles. Either of -b and -h can always be specified to set it
19 * explicitly.
20 *
21 * Data read from standard input is assumed always to be binary;
22 * data provided on the command line is taken to be hex.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
29#include <errno.h>
30
31#include "puzzles.h"
32
33int main(int argc, char **argv)
34{
35 enum { BINARY, DEFAULT, HEX } outputmode = DEFAULT;
36 char *inhex = NULL;
37 unsigned char *data;
38 int datalen;
39 int decode = -1;
40 int doing_opts = TRUE;
41
42 while (--argc > 0) {
43 char *p = *++argv;
44
45 if (doing_opts && *p == '-') {
46 if (!strcmp(p, "--")) {
47 doing_opts = 0;
48 continue;
49 }
50 p++;
51 while (*p) {
52 switch (*p) {
53 case 'e':
54 decode = 0;
55 break;
56 case 'd':
57 decode = 1;
58 break;
59 case 'b':
60 outputmode = BINARY;
61 break;
62 case 'h':
63 outputmode = HEX;
64 break;
65 default:
66 return 1;
67 }
68 p++;
69 }
70 } else {
71 if (!inhex) {
72 inhex = p;
73 } else {
74 return 1;
75 }
76 }
77 }
78
79 if (decode < 0) {
80 return 0;
81 }
82
83 if (outputmode == DEFAULT)
84 outputmode = (decode ? BINARY : HEX);
85
86 if (inhex) {
87 datalen = strlen(inhex) / 2;
88 data = hex2bin(inhex, datalen);
89 } else {
90 int datasize = 4096;
91 datalen = 0;
92 data = snewn(datasize, unsigned char);
93 while (1) {
94 int ret = fread(data + datalen, 1, datasize - datalen, stdin);
95 if (ret < 0) {
96 fprintf(stderr, "obfusc: read: %s\n", strerror(errno));
97 return 1;
98 } else if (ret == 0) {
99 break;
100 } else {
101 datalen += ret;
102 if (datasize - datalen < 4096) {
103 datasize = datalen * 5 / 4 + 4096;
104 data = sresize(data, datasize, unsigned char);
105 }
106 }
107 }
108 }
109
110 obfuscate_bitmap(data, datalen * 8, decode);
111
112 if (outputmode == BINARY) {
113 int ret = fwrite(data, 1, datalen, stdout);
114 if (ret < 0) {
115 fprintf(stderr, "obfusc: write: %s\n", strerror(errno));
116 return 1;
117 }
118 } else {
119 int i;
120 for (i = 0; i < datalen; i++)
121 printf("%02x", data[i]);
122 printf("\n");
123 }
124
125 return 0;
126}