2 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
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.
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.
17 /* $OpenBSD: modpipe.c,v 1.6 2013/11/21 03:16:47 djm Exp $ */
21 #include <sys/types.h>
28 #include "openbsd-compat/getopt_long.c"
30 static void err(int, const char *, ...) __attribute__((format(printf, 2, 3)));
31 static void errx(int, const char *, ...) __attribute__((format(printf, 2, 3)));
34 err(int r, const char *fmt, ...)
39 fprintf(stderr, "%s: ", strerror(errno));
40 vfprintf(stderr, fmt, args);
47 errx(int r, const char *fmt, ...)
52 vfprintf(stderr, fmt, args);
61 fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
62 fprintf(stderr, "modspec is one of:\n");
63 fprintf(stderr, " xor:offset:value - XOR \"value\" at \"offset\"\n");
64 fprintf(stderr, " andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n");
68 #define MAX_MODIFICATIONS 256
70 enum { MOD_XOR, MOD_AND_OR } what;
71 unsigned long long offset;
76 parse_modification(const char *s, struct modification *m)
82 if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
83 what, &m->offset, &m1, &m2)) < 3)
84 errx(1, "Invalid modification spec \"%s\"", s);
85 if (strcasecmp(what, "xor") == 0) {
87 errx(1, "Invalid modification spec \"%s\"", s);
88 if (m1 < 0 || m1 > 0xff)
89 errx(1, "Invalid XOR modification value");
92 } else if (strcasecmp(what, "andor") == 0) {
94 errx(1, "Invalid modification spec \"%s\"", s);
95 if (m1 < 0 || m1 > 0xff)
96 errx(1, "Invalid AND modification value");
97 if (m2 < 0 || m2 > 0xff)
98 errx(1, "Invalid OR modification value");
103 errx(1, "Invalid modification type \"%s\"", what);
107 main(int argc, char **argv)
113 struct modification mods[MAX_MODIFICATIONS];
114 u_int i, wflag = 0, num_mods = 0;
116 while ((ch = getopt(argc, argv, "wm:")) != -1) {
119 if (num_mods >= MAX_MODIFICATIONS)
120 errx(1, "Too many modifications");
121 parse_modification(optarg, &(mods[num_mods++]));
132 r = s = read(STDIN_FILENO, buf, sizeof(buf));
136 if (errno == EAGAIN || errno == EINTR)
140 for (i = 0; i < num_mods; i++) {
141 if (mods[i].offset < total ||
142 mods[i].offset >= total + s)
144 switch (mods[i].what) {
146 buf[mods[i].offset - total] ^= mods[i].m1;
149 buf[mods[i].offset - total] &= mods[i].m1;
150 buf[mods[i].offset - total] |= mods[i].m2;
154 for (o = 0; o < s; o += r) {
155 r = write(STDOUT_FILENO, buf, s - o);
159 if (errno == EAGAIN || errno == EINTR)
166 /* Warn if modifications not reached in input stream */
168 for (i = 0; wflag && i < num_mods; i++) {
169 if (mods[i].offset < total)
172 fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);