From 8796ec38b486defa07b43b01316a867f93d1f0a2 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 15 Oct 2021 20:57:38 -0600 Subject: [PATCH] Add undec and unoct --- Makefile | 2 ++ README.md | 17 ++++++++++++++++- undec.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ unoct.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 undec.c create mode 100644 unoct.c diff --git a/Makefile b/Makefile index 1f17cf0..4d5ae2d 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ TARGETS += hd TARGETS += pyesc TARGETS += xor TARGETS += unhex +TARGETS += undec +TARGETS += unoct TARGETS += pcat TARGETS += slice TARGETS += hex diff --git a/README.md b/README.md index 244ec2d..b072361 100644 --- a/README.md +++ b/README.md @@ -105,12 +105,27 @@ You can disable this with `-v` ## unhex: unescape hex -Reads ASCII hex codes on stdin, +Reads octet hex codes on stdin, writes those octets to stdout. $ echo 68 65 6c 6c 6f 0a | unhex hello +## undec: unescape decimal + +Reads octet decimal codes on stdin, +writes those octets to stdout. + + $ echo 104 101 108 108 111 10 | undec + hello + +## unoct: unescape octal + +Reads octet octal codes on stdin, +writes those octets to stdout. + + $ echo 150 145 154 154 157 012 | unoct + hello ## xor: xor octets diff --git a/undec.c b/undec.c new file mode 100644 index 0000000..6f29776 --- /dev/null +++ b/undec.c @@ -0,0 +1,48 @@ +/* + * 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 digit = 0; + unsigned long int count = 0; + + while (1) { + int c = getchar(); + + count += 1; + + switch (c) { + case EOF: + return 0; + case ' ': + case '\r': + case '\n': + case '\t': + digit += 3; + break; + case '0' ... '9': + acc = (acc * 10) + c - '0'; + digit += 1; + break; + default: + if (digit != 0) { + fprintf(stderr, "Warning: non-numeric character mid-octet at offset %lu\n", count); + } + break; + } + + if (digit > 3) { + putchar(acc); + acc = 0; + digit = 0; + } + } + + return 0; +} diff --git a/unoct.c b/unoct.c new file mode 100644 index 0000000..2afa137 --- /dev/null +++ b/unoct.c @@ -0,0 +1,48 @@ +/* + * Octal 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 digit = 0; + unsigned long int count = 0; + + while (1) { + int c = getchar(); + + count += 1; + + switch (c) { + case EOF: + return 0; + case ' ': + case '\r': + case '\n': + case '\t': + digit += 3; + break; + case '0' ... '7': + acc = (acc * 8) + c - '0'; + digit += 1; + break; + default: + if (digit != 0) { + fprintf(stderr, "Warning: non-numeric character mid-octet at offset %lu\n", count); + } + break; + } + + if (digit > 3) { + putchar(acc); + acc = 0; + digit = 0; + } + } + + return 0; +}