* xor: new option "-s"

+ use a string of characters instead of separated byte values
  + updated man page
This commit is contained in:
pi-rho 2012-05-03 19:50:10 -05:00
parent 4b8f395392
commit 1ed800f42a
2 changed files with 38 additions and 11 deletions

View File

@ -30,7 +30,10 @@
.Nm xor .Nm xor
.Op Fl x .Op Fl x
.Ar MASKBYTE .Ar MASKBYTE
.Op Ar MASKBYTES ... .Op Ar MASKBYTE ...
.Nm xor
.Fl s
.Ar MASKSTRING
. .
.Sh DESCRIPTION .Sh DESCRIPTION
The basic concept for this utility is to apply a set of mask bytes, repeatedly, The basic concept for this utility is to apply a set of mask bytes, repeatedly,
@ -43,6 +46,8 @@ As a filter with the hexadecimal mask bytes
applied to the input stream (i.e. applied to the input stream (i.e.
.Bq 20 , 2f , 20 , 2f , ... Ns ) .Bq 20 , 2f , 20 , 2f , ... Ns )
.D1 ... | Nm xor Fl x Ar 20 Ar 2f | Li ... .D1 ... | Nm xor Fl x Ar 20 Ar 2f | Li ...
As a filter with a string of characters used as a mask
.D1 ... | Nm xor Fl s Qo Ar " key " Qc | Li ...
. .
.Sh OPTIONS .Sh OPTIONS
A summary of the options supported by A summary of the options supported by
@ -56,8 +61,12 @@ usage information
the program's version the program's version
.It Fl x .It Fl x
explicity interpret mask bytes as hexadecimal digits explicity interpret mask bytes as hexadecimal digits
.It Ar MASKBYTE Op Ar MASKBYTES ... .It Fl s
use a string of characters as the mask
.It Ar MASKBYTE Op Ar MASKBYTE ...
a list of mask bytes to apply to the input stream a list of mask bytes to apply to the input stream
.It Ar MASKSTRING
a string of characters
.El .El
. .
.Sh SEE ALSO .Sh SEE ALSO

View File

@ -9,6 +9,7 @@
* *
*/ */
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -26,20 +27,25 @@ int version(bool error) {
int usage(bool error, char *prog) { int usage(bool error, char *prog) {
int retval = version(error); int retval = version(error);
fprintf(WHICHOUT(error), "Usage: %s [-x] MASK [MASK]*\n", prog); fprintf(WHICHOUT(error), "Usage:\t%s [-x] MASK [MASK]*\n", prog);
fprintf(WHICHOUT(error), "\t%s -s MASKSTRING\n", prog);
fprintf(WHICHOUT(error), "\t-x\tmask bytes are hexadecimal\n"); fprintf(WHICHOUT(error), "\t-x\tmask bytes are hexadecimal\n");
fprintf(WHICHOUT(error), "\tMASK\ta mask byte\n"); fprintf(WHICHOUT(error), "\t-s\tuse a string of characters as a mask\n");
fprintf(WHICHOUT(error), "\n\tMASK\t\ta mask byte\n");
fprintf(WHICHOUT(error), "\tMASKSTRING\ta string of mask characters\n");
return retval; return retval;
} }
void do_xor(uint8_t mask[], int len) { void do_xor(uint8_t mask[], int len) {
int i = 0; int i = 0;
while (true) { while (true) {
int c = getchar(); int c = getchar();
if (EOF == c) break; if (EOF == c) break;
c ^= mask[i]; c ^= mask[i];
putchar(c); putchar(c);
i = (i + 1) % len; i = (i + 1) % len;
} }
@ -52,11 +58,14 @@ int main(int argc, char *argv[]) {
int i; int i;
/* option parsing */ /* option parsing */
while ((opt = getopt(argc, argv, "hvx")) != -1) { while ((opt = getopt(argc, argv, "xsvh")) != -1) {
switch (opt) { switch (opt) {
case 'x': case 'x': /* hex */
base = 16; base = 16;
break; break;
case 's': /* string */
base = -1;
break;
case 'v': return version(false); case 'v': return version(false);
case 'h': return usage(false, argv[0]); case 'h': return usage(false, argv[0]);
default: return usage(true, argv[0]); default: return usage(true, argv[0]);
@ -65,12 +74,21 @@ int main(int argc, char *argv[]) {
if (optind >= argc) return usage(true, argv[0]); if (optind >= argc) return usage(true, argv[0]);
masklen = argc - optind; if (base != -1) {
uint8_t mask[masklen]; masklen = argc - optind;
for (i = optind; i < argc; i++) uint8_t mask[masklen];
mask[i-optind] = (uint8_t)strtol(argv[i], NULL, base); for (i = optind; i < argc; i++)
mask[i-optind] = (uint8_t)strtol(argv[i], NULL, base);
do_xor(mask, masklen); do_xor(mask, masklen);
} else { /* string */
masklen = strlen(argv[optind]);
uint8_t mask[masklen];
for (i = 0; i < masklen; i++)
mask[i] = (uint8_t)argv[optind][i];
do_xor(mask, masklen);
}
return EX_OK; return EX_OK;
} }