diff --git a/Makefile b/Makefile index e53205a..ab77c8a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ TARGETS += pyesc TARGETS += xor TARGETS += unhex TARGETS += pcat -TARGETS += drop +TARGETS += slice TARGETS += hex all: $(TARGETS) diff --git a/README.md b/README.md index 0bb9057..47266f5 100644 --- a/README.md +++ b/README.md @@ -75,21 +75,19 @@ The "-x" option treats values as hex. cbcbcb -### drop: discard octets +### slice: slice octet stream -Throws away some octets from stdin, -and sends the rest to stdout. +Slices up input octet stream, +similar to Python's slice operation. -You could use `dd` for the same purpose. - - $ echo 01234567 | drop 0 3 - 34567 - $ echo 01234567 | drop 4 7 - 01237 - $ echo 01234567 | drop 4 6 - 012367 - $ echo 01234567 | drop 3 9999 - 012 + ~/src/fluffy $ printf '0123456789abcdef' | slice 2; echo + 23456789abcdef + ~/src/fluffy $ printf '0123456789abcdef' | slice 2 6; echo + 2345 + ~/src/fluffy $ printf '0123456789abcdef' | slice 2 6 8; echo + 234589abcdef + ~/src/fluffy $ printf '0123456789abcdef' | slice 2 6 8 0xa + 234589 ### pcat: print text representation of pcap file @@ -133,6 +131,14 @@ writing to output. The opposite of `unhex`: encoding all input into a single output line. +This differs from `hexdump` in the following ways: + +* All input is encoded into a single line of output +* Does not output offsets +* Does not output glyph representations of octets + +In other words: you can feed `hex` output into `unhex` with no manipulations. + $ printf "hello\nworld\n" | hex 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a diff --git a/drop.c b/drop.c deleted file mode 100644 index b0a156a..0000000 --- a/drop.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * drop octets -- 2017 Neale Pickett - * - * This file is in the public domain. I make no promises about the functionality - * of this program. - */ - -#include -#include - -int -main(int argc, char *argv[]) -{ - unsigned long int start; - unsigned long int end; - unsigned long int pos; - - if (3 == argc) { - start = strtoul(argv[1], NULL, 0); - end = strtoul(argv[2], NULL, 0); - } else { - fprintf(stderr, "Usage: %s start end\n", argv[0]); - fprintf(stderr, "\n"); - fprintf(stderr, "Drops octets from input\n"); - return 1; - } - - for (pos = 0; ; pos += 1) { - int c = getchar(); - - if (EOF == c) { - break; - } - - if ((start <= pos) && (pos < end)) { - /* drop */ - } else { - putchar(c); - } - } - - return 0; -} diff --git a/slice.c b/slice.c new file mode 100644 index 0000000..e326d06 --- /dev/null +++ b/slice.c @@ -0,0 +1,55 @@ +/* + * slice up octet streams -- 2017 Neale Pickett + * + * This file is in the public domain. I make no promises about the functionality + * of this program. + */ + +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + int argn = 1; + unsigned long int drop = true; + unsigned long int next; + unsigned long int pos; + + if (1 == argc) { + fprintf(stderr, "Usage: %s start [end start...] [end]\n", argv[0]); + fprintf(stderr, "\n"); + fprintf(stderr, "Slices input, keeping specified ranges\n"); + return 1; + } + + next = strtoul(argv[argn], NULL, 0); + + for (pos = 0; ; pos += 1) { + int c = getchar(); + + if (EOF == c) { + break; + } + + if (next == pos) { + drop = !drop; + argn += 1; + if (argn == argc) { + next = ULONG_MAX; + } else { + next = strtoul(argv[argn], NULL, 0); + } + } + + if (drop) { + /* drop */ + } else { + putchar(c); + } + } + + return 0; +}