From 49ff09dc9ebe066f3a25e5a98789bf3dfb7a0040 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 10 Feb 2012 11:29:56 -0700 Subject: [PATCH] add pyesc, unhex, xor --- hd.c | 2 -- pyesc.c | 26 ++++++++++++++++++++++++++ unhex.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ xor.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 pyesc.c create mode 100644 unhex.c create mode 100644 xor.c diff --git a/hd.c b/hd.c index e9471ef..a49643e 100644 --- a/hd.c +++ b/hd.c @@ -94,5 +94,3 @@ main(int argc, char *argv[]) return 0; } - - diff --git a/pyesc.c b/pyesc.c new file mode 100644 index 0000000..929951c --- /dev/null +++ b/pyesc.c @@ -0,0 +1,26 @@ +#include + +int +main(int argc, char *argv[]) +{ + while (1) { + int c = getchar(); + + switch (c) { + case EOF: + return 0; + case 134: + printf("\\\\"); + break; + case 32 ... 91: + case 93 ... 126: + putchar(c); + break; + default: + printf("\\x%02x", c); + break; + } + } + + return 0; +} diff --git a/unhex.c b/unhex.c new file mode 100644 index 0000000..113c35a --- /dev/null +++ b/unhex.c @@ -0,0 +1,51 @@ +/* Hex Decoder -- 2012 Zephyr + * + * This file is in the public domain. I make no promises + * about the functionality of this program. + */ + +#include + +int +main(int argc, char *argv[]) +{ + unsigned char acc = 0; + unsigned char nybble = 0; + unsigned long int count = 0; + + while (1) { + int c = getchar(); + + count += 1; + + switch (c) { + case EOF: + return 0; + case '0' ... '9': + acc = (acc << 4) + c - '0'; + nybble += 1; + break; + case 'a' ... 'f': + acc = (acc << 4) + c - 'a' + 10; + nybble += 1; + break; + case 'A' ... 'F': + acc = (acc << 4) + c - 'A' + 10; + nybble += 1; + break; + default: + if (nybble != 0) { + fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count); + } + break; + } + + if (nybble == 2) { + putchar(acc); + acc = 0; + nybble = 0; + } + } + + return 0; +} diff --git a/xor.c b/xor.c new file mode 100644 index 0000000..b3e5a78 --- /dev/null +++ b/xor.c @@ -0,0 +1,48 @@ +/* xor filter -- 2012 Zephyr + * + * This file is in the public domain. I make no promises + * about the functionality of this program. + */ + +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + int start = 1; + int base = 0; + int arg; + + if (argc < 2) { + fprintf(stderr, "Usage: %s [-x] m1 [m2 ...]\n", argv[0]); + return 1; + } + + if (0 == strcmp(argv[1], "-x")) { + base = 16; + start += 1; + } + + arg = start; + + while (1) { + int c = getchar(); + unsigned char mask; + + if (! argv[arg]) { + arg = start; + } + mask = strtol(argv[arg++], NULL, base); + + if (EOF == c) { + break; + } + + c ^= mask; + putchar(c); + } + + return 0; +}