summaryrefslogtreecommitdiff
path: root/src/usr.bin/nc/data/xor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr.bin/nc/data/xor.c')
-rw-r--r--src/usr.bin/nc/data/xor.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/usr.bin/nc/data/xor.c b/src/usr.bin/nc/data/xor.c
new file mode 100644
index 0000000000..9feead0cba
--- /dev/null
+++ b/src/usr.bin/nc/data/xor.c
@@ -0,0 +1,92 @@
1/* Generic xor handler.
2
3 With no args, xors stdin against 0xFF to stdout. A single argument is a
4 file to read xor-bytes out of. Any zero in the xor-bytes array is treated
5 as the end; if you need to xor against a string that *includes* zeros,
6 you're on your own.
7
8 The indirect file can be generated easily with data.c.
9
10 Written because there are so many lame schemes for "masking" plaintext
11 passwords and the like floating around, and it's handy to just run an
12 obscure binary-format configuration file through this and look for strings.
13
14 *Hobbit*, 960208 */
15
16#include <stdio.h>
17#include <fcntl.h>
18
19char buf[8192];
20char bytes[256];
21char * py;
22
23/* do the xor, in place. Uses global ptr "py" to maintain "bytes" state */
24xorb (buf, len)
25 char * buf;
26 int len;
27{
28 register int x;
29 register char * pb;
30
31 pb = buf;
32 x = len;
33 while (x > 0) {
34 *pb = (*pb ^ *py);
35 pb++;
36 py++;
37 if (! *py)
38 py = bytes;
39 x--;
40 }
41} /* xorb */
42
43/* blah */
44main (argc, argv)
45 int argc;
46 char ** argv;
47{
48 register int x = 0;
49 register int y;
50
51/* manually preload; xor-with-0xFF is all too common */
52 memset (bytes, 0, sizeof (bytes));
53 bytes[0] = 0xff;
54
55/* if file named in any arg, reload from that */
56#ifdef O_BINARY /* DOS shit... */
57 x = setmode (0, O_BINARY); /* make stdin raw */
58 if (x < 0) {
59 fprintf (stderr, "stdin binary setmode oops: %d\n", x);
60 exit (1);
61 }
62 x = setmode (1, O_BINARY); /* make stdout raw */
63 if (x < 0) {
64 fprintf (stderr, "stdout binary setmode oops: %d\n", x);
65 exit (1);
66 }
67#endif /* O_BINARY */
68
69 if (argv[1])
70#ifdef O_BINARY
71 x = open (argv[1], O_RDONLY | O_BINARY);
72#else
73 x = open (argv[1], O_RDONLY);
74#endif
75 if (x > 0) {
76 read (x, bytes, 250); /* nothin' fancy here */
77 close (x);
78 }
79 py = bytes;
80 x = 1;
81 while (x > 0) {
82 x = read (0, buf, sizeof (buf));
83 if (x <= 0)
84 break;
85 xorb (buf, x);
86 y = write (1, buf, x);
87 if (y <= 0)
88 exit (1);
89 }
90 exit (0);
91}
92