1248613Sdes/*
2248613Sdes * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
3248613Sdes *
4248613Sdes * Permission to use, copy, modify, and distribute this software for any
5248613Sdes * purpose with or without fee is hereby granted, provided that the above
6248613Sdes * copyright notice and this permission notice appear in all copies.
7248613Sdes *
8248613Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9248613Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10248613Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11248613Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12248613Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13248613Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14248613Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15248613Sdes */
16248613Sdes
17261320Sdes/* $OpenBSD: modpipe.c,v 1.6 2013/11/21 03:16:47 djm Exp $ */
18248613Sdes
19248613Sdes#include "includes.h"
20248613Sdes
21248613Sdes#include <sys/types.h>
22248613Sdes#include <unistd.h>
23248613Sdes#include <stdio.h>
24248613Sdes#include <string.h>
25248613Sdes#include <stdarg.h>
26248613Sdes#include <stdlib.h>
27248613Sdes#include <errno.h>
28323129Sdes#ifdef HAVE_ERR_H
29323129Sdes# include <err.h>
30323129Sdes#endif
31255670Sdes#include "openbsd-compat/getopt_long.c"
32248613Sdes
33248613Sdesstatic void
34248613Sdesusage(void)
35248613Sdes{
36248613Sdes	fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
37248613Sdes	fprintf(stderr, "modspec is one of:\n");
38248613Sdes	fprintf(stderr, "    xor:offset:value       - XOR \"value\" at \"offset\"\n");
39248613Sdes	fprintf(stderr, "    andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n");
40248613Sdes	exit(1);
41248613Sdes}
42248613Sdes
43248613Sdes#define MAX_MODIFICATIONS 256
44248613Sdesstruct modification {
45248613Sdes	enum { MOD_XOR, MOD_AND_OR } what;
46261320Sdes	unsigned long long offset;
47248613Sdes	u_int8_t m1, m2;
48248613Sdes};
49248613Sdes
50248613Sdesstatic void
51248613Sdesparse_modification(const char *s, struct modification *m)
52248613Sdes{
53248613Sdes	char what[16+1];
54248613Sdes	int n, m1, m2;
55248613Sdes
56248613Sdes	bzero(m, sizeof(*m));
57261320Sdes	if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
58248613Sdes	    what, &m->offset, &m1, &m2)) < 3)
59248613Sdes		errx(1, "Invalid modification spec \"%s\"", s);
60248613Sdes	if (strcasecmp(what, "xor") == 0) {
61248613Sdes		if (n > 3)
62248613Sdes			errx(1, "Invalid modification spec \"%s\"", s);
63248613Sdes		if (m1 < 0 || m1 > 0xff)
64248613Sdes			errx(1, "Invalid XOR modification value");
65248613Sdes		m->what = MOD_XOR;
66248613Sdes		m->m1 = m1;
67248613Sdes	} else if (strcasecmp(what, "andor") == 0) {
68248613Sdes		if (n != 4)
69248613Sdes			errx(1, "Invalid modification spec \"%s\"", s);
70248613Sdes		if (m1 < 0 || m1 > 0xff)
71248613Sdes			errx(1, "Invalid AND modification value");
72248613Sdes		if (m2 < 0 || m2 > 0xff)
73248613Sdes			errx(1, "Invalid OR modification value");
74248613Sdes		m->what = MOD_AND_OR;
75248613Sdes		m->m1 = m1;
76248613Sdes		m->m2 = m2;
77248613Sdes	} else
78248613Sdes		errx(1, "Invalid modification type \"%s\"", what);
79248613Sdes}
80248613Sdes
81248613Sdesint
82248613Sdesmain(int argc, char **argv)
83248613Sdes{
84248613Sdes	int ch;
85248613Sdes	u_char buf[8192];
86248613Sdes	size_t total;
87248613Sdes	ssize_t r, s, o;
88248613Sdes	struct modification mods[MAX_MODIFICATIONS];
89248613Sdes	u_int i, wflag = 0, num_mods = 0;
90248613Sdes
91248613Sdes	while ((ch = getopt(argc, argv, "wm:")) != -1) {
92248613Sdes		switch (ch) {
93248613Sdes		case 'm':
94248613Sdes			if (num_mods >= MAX_MODIFICATIONS)
95248613Sdes				errx(1, "Too many modifications");
96248613Sdes			parse_modification(optarg, &(mods[num_mods++]));
97248613Sdes			break;
98248613Sdes		case 'w':
99248613Sdes			wflag = 1;
100248613Sdes			break;
101248613Sdes		default:
102248613Sdes			usage();
103248613Sdes			/* NOTREACHED */
104248613Sdes		}
105248613Sdes	}
106248613Sdes	for (total = 0;;) {
107248613Sdes		r = s = read(STDIN_FILENO, buf, sizeof(buf));
108248613Sdes		if (r == 0)
109248613Sdes			break;
110248613Sdes		if (r < 0) {
111248613Sdes			if (errno == EAGAIN || errno == EINTR)
112248613Sdes				continue;
113248613Sdes			err(1, "read");
114248613Sdes		}
115248613Sdes		for (i = 0; i < num_mods; i++) {
116248613Sdes			if (mods[i].offset < total ||
117248613Sdes			    mods[i].offset >= total + s)
118248613Sdes				continue;
119248613Sdes			switch (mods[i].what) {
120248613Sdes			case MOD_XOR:
121248613Sdes				buf[mods[i].offset - total] ^= mods[i].m1;
122248613Sdes				break;
123248613Sdes			case MOD_AND_OR:
124248613Sdes				buf[mods[i].offset - total] &= mods[i].m1;
125248613Sdes				buf[mods[i].offset - total] |= mods[i].m2;
126248613Sdes				break;
127248613Sdes			}
128248613Sdes		}
129248613Sdes		for (o = 0; o < s; o += r) {
130248613Sdes			r = write(STDOUT_FILENO, buf, s - o);
131248613Sdes			if (r == 0)
132248613Sdes				break;
133248613Sdes			if (r < 0) {
134248613Sdes				if (errno == EAGAIN || errno == EINTR)
135248613Sdes					continue;
136248613Sdes				err(1, "write");
137248613Sdes			}
138248613Sdes		}
139248613Sdes		total += s;
140248613Sdes	}
141248613Sdes	/* Warn if modifications not reached in input stream */
142248613Sdes	r = 0;
143248613Sdes	for (i = 0; wflag && i < num_mods; i++) {
144248613Sdes		if (mods[i].offset < total)
145248613Sdes			continue;
146248613Sdes		r = 1;
147248613Sdes		fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);
148248613Sdes	}
149248613Sdes	return r;
150248613Sdes}
151