Add undec and unoct

This commit is contained in:
Neale Pickett 2021-10-15 20:57:38 -06:00
parent c93b7604b9
commit 8796ec38b4
4 changed files with 114 additions and 1 deletions

View File

@ -8,6 +8,8 @@ TARGETS += hd
TARGETS += pyesc
TARGETS += xor
TARGETS += unhex
TARGETS += undec
TARGETS += unoct
TARGETS += pcat
TARGETS += slice
TARGETS += hex

View File

@ -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

48
undec.c Normal file
View File

@ -0,0 +1,48 @@
/*
* Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* of this program.
*/
#include <stdio.h>
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;
}

48
unoct.c Normal file
View File

@ -0,0 +1,48 @@
/*
* Octal Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* of this program.
*/
#include <stdio.h>
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;
}