- commit
- 8796ec3
- parent
- c93b760
- author
- Neale Pickett
- date
- 2021-10-15 20:57:38 -0600 MDT
Add undec and unoct
M
Makefile
+2,
-0
1@@ -8,6 +8,8 @@ TARGETS += hd
2 TARGETS += pyesc
3 TARGETS += xor
4 TARGETS += unhex
5+TARGETS += undec
6+TARGETS += unoct
7 TARGETS += pcat
8 TARGETS += slice
9 TARGETS += hex
+16,
-1
1@@ -105,12 +105,27 @@ You can disable this with `-v`
2
3 ## unhex: unescape hex
4
5-Reads ASCII hex codes on stdin,
6+Reads octet hex codes on stdin,
7 writes those octets to stdout.
8
9 $ echo 68 65 6c 6c 6f 0a | unhex
10 hello
11
12+## undec: unescape decimal
13+
14+Reads octet decimal codes on stdin,
15+writes those octets to stdout.
16+
17+ $ echo 104 101 108 108 111 10 | undec
18+ hello
19+
20+## unoct: unescape octal
21+
22+Reads octet octal codes on stdin,
23+writes those octets to stdout.
24+
25+ $ echo 150 145 154 154 157 012 | unoct
26+ hello
27
28 ## xor: xor octets
29
A
undec.c
+48,
-0
1@@ -0,0 +1,48 @@
2+/*
3+ * Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
4+ * of this program.
5+ */
6+
7+#include <stdio.h>
8+
9+int
10+main(int argc, char *argv[])
11+{
12+ unsigned char acc = 0;
13+ unsigned char digit = 0;
14+ unsigned long int count = 0;
15+
16+ while (1) {
17+ int c = getchar();
18+
19+ count += 1;
20+
21+ switch (c) {
22+ case EOF:
23+ return 0;
24+ case ' ':
25+ case '\r':
26+ case '\n':
27+ case '\t':
28+ digit += 3;
29+ break;
30+ case '0' ... '9':
31+ acc = (acc * 10) + c - '0';
32+ digit += 1;
33+ break;
34+ default:
35+ if (digit != 0) {
36+ fprintf(stderr, "Warning: non-numeric character mid-octet at offset %lu\n", count);
37+ }
38+ break;
39+ }
40+
41+ if (digit > 3) {
42+ putchar(acc);
43+ acc = 0;
44+ digit = 0;
45+ }
46+ }
47+
48+ return 0;
49+}
A
unoct.c
+48,
-0
1@@ -0,0 +1,48 @@
2+/*
3+ * Octal Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
4+ * of this program.
5+ */
6+
7+#include <stdio.h>
8+
9+int
10+main(int argc, char *argv[])
11+{
12+ unsigned char acc = 0;
13+ unsigned char digit = 0;
14+ unsigned long int count = 0;
15+
16+ while (1) {
17+ int c = getchar();
18+
19+ count += 1;
20+
21+ switch (c) {
22+ case EOF:
23+ return 0;
24+ case ' ':
25+ case '\r':
26+ case '\n':
27+ case '\t':
28+ digit += 3;
29+ break;
30+ case '0' ... '7':
31+ acc = (acc * 8) + c - '0';
32+ digit += 1;
33+ break;
34+ default:
35+ if (digit != 0) {
36+ fprintf(stderr, "Warning: non-numeric character mid-octet at offset %lu\n", count);
37+ }
38+ break;
39+ }
40+
41+ if (digit > 3) {
42+ putchar(acc);
43+ acc = 0;
44+ digit = 0;
45+ }
46+ }
47+
48+ return 0;
49+}