Home | History | Annotate | Download | only in regress
      1 /*
      2  * Copyright (c) 2012 Damien Miller <djm (at) mindrot.org>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 /* $OpenBSD: modpipe.c,v 1.6 2013/11/21 03:16:47 djm Exp $ */
     18 
     19 #include "includes.h"
     20 
     21 #include <sys/types.h>
     22 #include <unistd.h>
     23 #include <stdio.h>
     24 #include <string.h>
     25 #include <stdarg.h>
     26 #include <stdlib.h>
     27 #include <errno.h>
     28 #ifdef HAVE_ERR_H
     29 # include <err.h>
     30 #endif
     31 #include "openbsd-compat/getopt_long.c"
     32 
     33 static void
     34 usage(void)
     35 {
     36 	fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
     37 	fprintf(stderr, "modspec is one of:\n");
     38 	fprintf(stderr, "    xor:offset:value       - XOR \"value\" at \"offset\"\n");
     39 	fprintf(stderr, "    andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n");
     40 	exit(1);
     41 }
     42 
     43 #define MAX_MODIFICATIONS 256
     44 struct modification {
     45 	enum { MOD_XOR, MOD_AND_OR } what;
     46 	unsigned long long offset;
     47 	u_int8_t m1, m2;
     48 };
     49 
     50 static void
     51 parse_modification(const char *s, struct modification *m)
     52 {
     53 	char what[16+1];
     54 	int n, m1, m2;
     55 
     56 	bzero(m, sizeof(*m));
     57 	if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
     58 	    what, &m->offset, &m1, &m2)) < 3)
     59 		errx(1, "Invalid modification spec \"%s\"", s);
     60 	if (strcasecmp(what, "xor") == 0) {
     61 		if (n > 3)
     62 			errx(1, "Invalid modification spec \"%s\"", s);
     63 		if (m1 < 0 || m1 > 0xff)
     64 			errx(1, "Invalid XOR modification value");
     65 		m->what = MOD_XOR;
     66 		m->m1 = m1;
     67 	} else if (strcasecmp(what, "andor") == 0) {
     68 		if (n != 4)
     69 			errx(1, "Invalid modification spec \"%s\"", s);
     70 		if (m1 < 0 || m1 > 0xff)
     71 			errx(1, "Invalid AND modification value");
     72 		if (m2 < 0 || m2 > 0xff)
     73 			errx(1, "Invalid OR modification value");
     74 		m->what = MOD_AND_OR;
     75 		m->m1 = m1;
     76 		m->m2 = m2;
     77 	} else
     78 		errx(1, "Invalid modification type \"%s\"", what);
     79 }
     80 
     81 int
     82 main(int argc, char **argv)
     83 {
     84 	int ch;
     85 	u_char buf[8192];
     86 	size_t total;
     87 	ssize_t r, s, o;
     88 	struct modification mods[MAX_MODIFICATIONS];
     89 	u_int i, wflag = 0, num_mods = 0;
     90 
     91 	while ((ch = getopt(argc, argv, "wm:")) != -1) {
     92 		switch (ch) {
     93 		case 'm':
     94 			if (num_mods >= MAX_MODIFICATIONS)
     95 				errx(1, "Too many modifications");
     96 			parse_modification(optarg, &(mods[num_mods++]));
     97 			break;
     98 		case 'w':
     99 			wflag = 1;
    100 			break;
    101 		default:
    102 			usage();
    103 			/* NOTREACHED */
    104 		}
    105 	}
    106 	for (total = 0;;) {
    107 		r = s = read(STDIN_FILENO, buf, sizeof(buf));
    108 		if (r == 0)
    109 			break;
    110 		if (r < 0) {
    111 			if (errno == EAGAIN || errno == EINTR)
    112 				continue;
    113 			err(1, "read");
    114 		}
    115 		for (i = 0; i < num_mods; i++) {
    116 			if (mods[i].offset < total ||
    117 			    mods[i].offset >= total + s)
    118 				continue;
    119 			switch (mods[i].what) {
    120 			case MOD_XOR:
    121 				buf[mods[i].offset - total] ^= mods[i].m1;
    122 				break;
    123 			case MOD_AND_OR:
    124 				buf[mods[i].offset - total] &= mods[i].m1;
    125 				buf[mods[i].offset - total] |= mods[i].m2;
    126 				break;
    127 			}
    128 		}
    129 		for (o = 0; o < s; o += r) {
    130 			r = write(STDOUT_FILENO, buf, s - o);
    131 			if (r == 0)
    132 				break;
    133 			if (r < 0) {
    134 				if (errno == EAGAIN || errno == EINTR)
    135 					continue;
    136 				err(1, "write");
    137 			}
    138 		}
    139 		total += s;
    140 	}
    141 	/* Warn if modifications not reached in input stream */
    142 	r = 0;
    143 	for (i = 0; wflag && i < num_mods; i++) {
    144 		if (mods[i].offset < total)
    145 			continue;
    146 		r = 1;
    147 		fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);
    148 	}
    149 	return r;
    150 }
    151