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
17262566Sdes/* $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>
28255670Sdes#include "openbsd-compat/getopt_long.c"
29248613Sdes
30248613Sdesstatic void err(int, const char *, ...) __attribute__((format(printf, 2, 3)));
31248613Sdesstatic void errx(int, const char *, ...) __attribute__((format(printf, 2, 3)));
32248613Sdes
33248613Sdesstatic void
34248613Sdeserr(int r, const char *fmt, ...)
35248613Sdes{
36248613Sdes	va_list args;
37248613Sdes
38248613Sdes	va_start(args, fmt);
39248613Sdes	fprintf(stderr, "%s: ", strerror(errno));
40248613Sdes	vfprintf(stderr, fmt, args);
41248613Sdes	fputc('\n', stderr);
42248613Sdes	va_end(args);
43248613Sdes	exit(r);
44248613Sdes}
45248613Sdes
46248613Sdesstatic void
47248613Sdeserrx(int r, const char *fmt, ...)
48248613Sdes{
49248613Sdes	va_list args;
50248613Sdes
51248613Sdes	va_start(args, fmt);
52248613Sdes	vfprintf(stderr, fmt, args);
53248613Sdes	fputc('\n', stderr);
54248613Sdes	va_end(args);
55248613Sdes	exit(r);
56248613Sdes}
57248613Sdes
58248613Sdesstatic void
59248613Sdesusage(void)
60248613Sdes{
61248613Sdes	fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
62248613Sdes	fprintf(stderr, "modspec is one of:\n");
63248613Sdes	fprintf(stderr, "    xor:offset:value       - XOR \"value\" at \"offset\"\n");
64248613Sdes	fprintf(stderr, "    andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n");
65248613Sdes	exit(1);
66248613Sdes}
67248613Sdes
68248613Sdes#define MAX_MODIFICATIONS 256
69248613Sdesstruct modification {
70248613Sdes	enum { MOD_XOR, MOD_AND_OR } what;
71262566Sdes	unsigned long long offset;
72248613Sdes	u_int8_t m1, m2;
73248613Sdes};
74248613Sdes
75248613Sdesstatic void
76248613Sdesparse_modification(const char *s, struct modification *m)
77248613Sdes{
78248613Sdes	char what[16+1];
79248613Sdes	int n, m1, m2;
80248613Sdes
81248613Sdes	bzero(m, sizeof(*m));
82262566Sdes	if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
83248613Sdes	    what, &m->offset, &m1, &m2)) < 3)
84248613Sdes		errx(1, "Invalid modification spec \"%s\"", s);
85248613Sdes	if (strcasecmp(what, "xor") == 0) {
86248613Sdes		if (n > 3)
87248613Sdes			errx(1, "Invalid modification spec \"%s\"", s);
88248613Sdes		if (m1 < 0 || m1 > 0xff)
89248613Sdes			errx(1, "Invalid XOR modification value");
90248613Sdes		m->what = MOD_XOR;
91248613Sdes		m->m1 = m1;
92248613Sdes	} else if (strcasecmp(what, "andor") == 0) {
93248613Sdes		if (n != 4)
94248613Sdes			errx(1, "Invalid modification spec \"%s\"", s);
95248613Sdes		if (m1 < 0 || m1 > 0xff)
96248613Sdes			errx(1, "Invalid AND modification value");
97248613Sdes		if (m2 < 0 || m2 > 0xff)
98248613Sdes			errx(1, "Invalid OR modification value");
99248613Sdes		m->what = MOD_AND_OR;
100248613Sdes		m->m1 = m1;
101248613Sdes		m->m2 = m2;
102248613Sdes	} else
103248613Sdes		errx(1, "Invalid modification type \"%s\"", what);
104248613Sdes}
105248613Sdes
106248613Sdesint
107248613Sdesmain(int argc, char **argv)
108248613Sdes{
109248613Sdes	int ch;
110248613Sdes	u_char buf[8192];
111248613Sdes	size_t total;
112248613Sdes	ssize_t r, s, o;
113248613Sdes	struct modification mods[MAX_MODIFICATIONS];
114248613Sdes	u_int i, wflag = 0, num_mods = 0;
115248613Sdes
116248613Sdes	while ((ch = getopt(argc, argv, "wm:")) != -1) {
117248613Sdes		switch (ch) {
118248613Sdes		case 'm':
119248613Sdes			if (num_mods >= MAX_MODIFICATIONS)
120248613Sdes				errx(1, "Too many modifications");
121248613Sdes			parse_modification(optarg, &(mods[num_mods++]));
122248613Sdes			break;
123248613Sdes		case 'w':
124248613Sdes			wflag = 1;
125248613Sdes			break;
126248613Sdes		default:
127248613Sdes			usage();
128248613Sdes			/* NOTREACHED */
129248613Sdes		}
130248613Sdes	}
131248613Sdes	for (total = 0;;) {
132248613Sdes		r = s = read(STDIN_FILENO, buf, sizeof(buf));
133248613Sdes		if (r == 0)
134248613Sdes			break;
135248613Sdes		if (r < 0) {
136248613Sdes			if (errno == EAGAIN || errno == EINTR)
137248613Sdes				continue;
138248613Sdes			err(1, "read");
139248613Sdes		}
140248613Sdes		for (i = 0; i < num_mods; i++) {
141248613Sdes			if (mods[i].offset < total ||
142248613Sdes			    mods[i].offset >= total + s)
143248613Sdes				continue;
144248613Sdes			switch (mods[i].what) {
145248613Sdes			case MOD_XOR:
146248613Sdes				buf[mods[i].offset - total] ^= mods[i].m1;
147248613Sdes				break;
148248613Sdes			case MOD_AND_OR:
149248613Sdes				buf[mods[i].offset - total] &= mods[i].m1;
150248613Sdes				buf[mods[i].offset - total] |= mods[i].m2;
151248613Sdes				break;
152248613Sdes			}
153248613Sdes		}
154248613Sdes		for (o = 0; o < s; o += r) {
155248613Sdes			r = write(STDOUT_FILENO, buf, s - o);
156248613Sdes			if (r == 0)
157248613Sdes				break;
158248613Sdes			if (r < 0) {
159248613Sdes				if (errno == EAGAIN || errno == EINTR)
160248613Sdes					continue;
161248613Sdes				err(1, "write");
162248613Sdes			}
163248613Sdes		}
164248613Sdes		total += s;
165248613Sdes	}
166248613Sdes	/* Warn if modifications not reached in input stream */
167248613Sdes	r = 0;
168248613Sdes	for (i = 0; wflag && i < num_mods; i++) {
169248613Sdes		if (mods[i].offset < total)
170248613Sdes			continue;
171248613Sdes		r = 1;
172248613Sdes		fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);
173248613Sdes	}
174248613Sdes	return r;
175248613Sdes}
176