From 9079cc81b75aaa0241bd55953ddc6493f995123a Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 9 Aug 2017 00:44:44 +0000 Subject: [PATCH] Provide examples for most commands, enhance pyesc, remove superfluous printfesc --- Makefile | 2 ++ README.md | 53 ++++++++++++++++++++++++++++------------------------- hex.c | 9 +++++++-- printfesc.c | 40 ---------------------------------------- pyesc.c | 10 ++++++++++ 5 files changed, 47 insertions(+), 67 deletions(-) delete mode 100644 printfesc.c diff --git a/Makefile b/Makefile index ad30d2c..7328ece 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ TARGETS += printfesc TARGETS += xor TARGETS += unhex TARGETS += pcat +TARGETS += skip +TARGETS += hex all: $(TARGETS) diff --git a/README.md b/README.md index 1230354..9ab5bf2 100644 --- a/README.md +++ b/README.md @@ -42,15 +42,18 @@ Like the normal hd, but with unicode characters to represent all 256 octets, instead of using "." for unprintable characters. + $ printf "\0\x01\x02\x03\x30\x52\x9a" | hd + 00000000 00 01 02 03 30 52 9a ┆·☺☻♥0RÜ┆ + 00000007 + ### unhex: unescape hex Reads ASCII hex codes on stdin, writes those octets to stdout. -The following pipe is equivalent to "cat": - - ./hd | cut -b 11-58 | ./unhex + $ echo 68 65 6c 6c 6f 0a | unhex + hello ### xor: xor mask octets @@ -62,24 +65,27 @@ For a 16-value mask, the mask is applied to 16-octet chunks at a time. The "-x" option treats values as hex. -The following pipe is equivalent to "cat": - - ./xor 42 | ./xor -x 2A + $ printf 'hello' | xor 22; echo + ~szzy + $ printf 'hello' | xor 0x16; echo + ~szzy + $ printf 'hello' | xor -x 16; echo + ~szzy + $ printf 'bbbbbb' | xor 1 0; echo + cbcbcb ### skip: discard initial octets Throws away some initial octets from stdin, and sends the rest to stdout. + You could use `dd` for the same purpose. -This skip command: - - skip 5 - -Is equivalent to this `dd` command: - - dd skip=5 bs=1 status=none + $ echo abcdefgh | dd skip=5 bs=1 status=none + fgh + $ echo abcdefgh | skip 5 + fgh ### pcat: print text representation of pcap file @@ -120,22 +126,19 @@ writing to output. ### hex: hex-encode input -The opposite of `unhex`. +The opposite of `unhex`: +encoding all input into a single output line. -The following is the equivalent of `cat`: - - hex | unhex - - -### printfesc: printf escape input - -Reads octets, -writes a string suitable for copy-paste into printf. + $ printf "hello\nworld\n" | hex + 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a ### pyesc: python escape input Escapes input octets for pasting into a python "print" statement. +Also suitable for use as a C string, +a Go string, +and many other languages' string literals. - - + $ printf "hello\nworld\n" | pyesc + hello\nworld\n diff --git a/hex.c b/hex.c index 4097a22..3160461 100644 --- a/hex.c +++ b/hex.c @@ -11,10 +11,15 @@ main(int argc, char *argv[]) if (EOF == c) { break; } - printf("%02x ", c); - if (7 == count % 8) { + + if (count) { putchar(' '); + if (0 == count % 8) { + putchar(' '); + } } + + printf("%02x", c); } putchar('\n'); diff --git a/printfesc.c b/printfesc.c deleted file mode 100644 index aae7904..0000000 --- a/printfesc.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include - -int -main(int argc, char *argv[]) -{ - printf("printf "); - - while (!feof(stdin)) { - int c = getchar(); - - switch (c) { - case EOF: - break; - case '\n': - printf("\\n"); - break; - case '\r': - printf("\\r"); - break; - case '\t': - printf("\\t"); - break; - case '"': - printf("\\\""); - break; - default: - if (isprint(c)) { - putchar(c); - } else { - printf("\\%03o", c); - } - break; - } - } - - putchar('\n'); - - return 0; -} diff --git a/pyesc.c b/pyesc.c index a67e709..855d67a 100644 --- a/pyesc.c +++ b/pyesc.c @@ -8,7 +8,17 @@ main(int argc, char *argv[]) switch (c) { case EOF: + putchar('\n'); return 0; + case 8: + printf("\\t"); + break; + case 10: + printf("\\n"); + break; + case 13: + printf("\\r"); + break; case 134: printf("\\\\"); break;