2012-12-11 23:54:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Damien Miller <djm@mindrot.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-02-20 10:16:07 +00:00
|
|
|
/* $OpenBSD: modpipe.c,v 1.4 2013/02/20 08:29:27 djm Exp $ */
|
2012-12-11 23:54:37 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2013-02-19 02:15:01 +00:00
|
|
|
#include <stdarg.h>
|
2012-12-11 23:54:37 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2013-02-20 03:01:51 +00:00
|
|
|
#include "openbsd-compat/getopt.c"
|
2012-12-11 23:54:37 +00:00
|
|
|
|
2013-02-19 02:15:01 +00:00
|
|
|
static void err(int, const char *, ...) __attribute__((format(printf, 2, 3)));
|
|
|
|
static void errx(int, const char *, ...) __attribute__((format(printf, 2, 3)));
|
|
|
|
|
|
|
|
static void
|
|
|
|
err(int r, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
fprintf(stderr, "%s: ", strerror(errno));
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
va_end(args);
|
|
|
|
exit(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
errx(int r, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
va_end(args);
|
|
|
|
exit(r);
|
|
|
|
}
|
|
|
|
|
2012-12-11 23:54:37 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-02-20 10:13:27 +00:00
|
|
|
fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
|
2012-12-11 23:54:37 +00:00
|
|
|
fprintf(stderr, "modspec is one of:\n");
|
|
|
|
fprintf(stderr, " xor:offset:value - XOR \"value\" at \"offset\"\n");
|
|
|
|
fprintf(stderr, " andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_MODIFICATIONS 256
|
|
|
|
struct modification {
|
|
|
|
enum { MOD_XOR, MOD_AND_OR } what;
|
|
|
|
u_int64_t offset;
|
|
|
|
u_int8_t m1, m2;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_modification(const char *s, struct modification *m)
|
|
|
|
{
|
|
|
|
char what[16+1];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
bzero(m, sizeof(*m));
|
|
|
|
if ((n = sscanf(s, "%16[^:]%*[:]%lli%*[:]%hhi%*[:]%hhi",
|
|
|
|
what, &m->offset, &m->m1, &m->m2)) < 3)
|
|
|
|
errx(1, "Invalid modification spec \"%s\"", s);
|
|
|
|
if (strcasecmp(what, "xor") == 0) {
|
|
|
|
m->what = MOD_XOR;
|
|
|
|
if (n > 3)
|
|
|
|
errx(1, "Invalid modification spec \"%s\"", s);
|
|
|
|
} else if (strcasecmp(what, "andor") == 0) {
|
|
|
|
m->what = MOD_AND_OR;
|
|
|
|
if (n != 4)
|
|
|
|
errx(1, "Invalid modification spec \"%s\"", s);
|
|
|
|
} else
|
|
|
|
errx(1, "Invalid modification type \"%s\"", what);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
u_char buf[8192];
|
|
|
|
size_t total;
|
|
|
|
ssize_t r, s, o;
|
|
|
|
struct modification mods[MAX_MODIFICATIONS];
|
2013-02-20 10:13:27 +00:00
|
|
|
u_int i, wflag = 0, num_mods = 0;
|
2012-12-11 23:54:37 +00:00
|
|
|
|
2013-02-20 10:13:27 +00:00
|
|
|
while ((ch = getopt(argc, argv, "wm:")) != -1) {
|
2012-12-11 23:54:37 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'm':
|
|
|
|
if (num_mods >= MAX_MODIFICATIONS)
|
|
|
|
errx(1, "Too many modifications");
|
|
|
|
parse_modification(optarg, &(mods[num_mods++]));
|
|
|
|
break;
|
2013-02-20 10:13:27 +00:00
|
|
|
case 'w':
|
|
|
|
wflag = 1;
|
|
|
|
break;
|
2012-12-11 23:54:37 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (total = 0;;) {
|
|
|
|
r = s = read(STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
if (r == 0)
|
2013-02-20 10:13:27 +00:00
|
|
|
break;
|
2012-12-11 23:54:37 +00:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
err(1, "read");
|
|
|
|
}
|
|
|
|
for (i = 0; i < num_mods; i++) {
|
|
|
|
if (mods[i].offset < total ||
|
|
|
|
mods[i].offset >= total + s)
|
|
|
|
continue;
|
|
|
|
switch (mods[i].what) {
|
|
|
|
case MOD_XOR:
|
|
|
|
buf[mods[i].offset - total] ^= mods[i].m1;
|
|
|
|
break;
|
|
|
|
case MOD_AND_OR:
|
|
|
|
buf[mods[i].offset - total] &= mods[i].m1;
|
|
|
|
buf[mods[i].offset - total] |= mods[i].m2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (o = 0; o < s; o += r) {
|
|
|
|
r = write(STDOUT_FILENO, buf, s - o);
|
|
|
|
if (r == 0)
|
2013-02-20 10:13:27 +00:00
|
|
|
break;
|
2012-12-11 23:54:37 +00:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
err(1, "write");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total += s;
|
|
|
|
}
|
2013-02-20 10:13:27 +00:00
|
|
|
/* Warn if modifications not reached in input stream */
|
|
|
|
r = 0;
|
|
|
|
for (i = 0; wflag && i < num_mods; i++) {
|
|
|
|
if (mods[i].offset < total)
|
|
|
|
continue;
|
|
|
|
r = 1;
|
|
|
|
fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);
|
|
|
|
}
|
|
|
|
return r;
|
2012-12-11 23:54:37 +00:00
|
|
|
}
|