blob: 5f4824b51d024df7d31da0f51fcf9799dee887e6 [file] [log] [blame]
Damien Miller1fb593a2012-12-12 10:54:37 +11001/*
2 * Copyright (c) 2012 Damien Miller <djm@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
Damien Millerea61b212013-11-21 14:25:15 +110017/* $OpenBSD: modpipe.c,v 1.6 2013/11/21 03:16:47 djm Exp $ */
Damien Miller1fb593a2012-12-12 10:54:37 +110018
Damien Miller43e5e602013-03-05 09:49:00 +110019#include "includes.h"
20
Damien Miller1fb593a2012-12-12 10:54:37 +110021#include <sys/types.h>
22#include <unistd.h>
23#include <stdio.h>
24#include <string.h>
Damien Millerb3764e12013-02-19 13:15:01 +110025#include <stdarg.h>
Damien Miller1fb593a2012-12-12 10:54:37 +110026#include <stdlib.h>
Damien Miller1fb593a2012-12-12 10:54:37 +110027#include <errno.h>
Darren Tuckereac1bbd2016-07-18 17:12:22 +100028#ifdef HAVE_ERR_H
29# include <err.h>
30#endif
Darren Tuckerccfdfce2013-05-10 16:28:55 +100031#include "openbsd-compat/getopt_long.c"
Damien Miller1fb593a2012-12-12 10:54:37 +110032
Damien Miller1fb593a2012-12-12 10:54:37 +110033static void
34usage(void)
35{
Damien Miller283e5752013-02-20 21:13:27 +110036 fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
Damien Miller1fb593a2012-12-12 10:54:37 +110037 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
44struct modification {
45 enum { MOD_XOR, MOD_AND_OR } what;
Damien Millerea61b212013-11-21 14:25:15 +110046 unsigned long long offset;
Damien Miller1fb593a2012-12-12 10:54:37 +110047 u_int8_t m1, m2;
48};
49
50static void
51parse_modification(const char *s, struct modification *m)
52{
53 char what[16+1];
Damien Miller43e5e602013-03-05 09:49:00 +110054 int n, m1, m2;
Damien Miller1fb593a2012-12-12 10:54:37 +110055
56 bzero(m, sizeof(*m));
Damien Millerea61b212013-11-21 14:25:15 +110057 if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
Damien Miller43e5e602013-03-05 09:49:00 +110058 what, &m->offset, &m1, &m2)) < 3)
Damien Miller1fb593a2012-12-12 10:54:37 +110059 errx(1, "Invalid modification spec \"%s\"", s);
60 if (strcasecmp(what, "xor") == 0) {
Damien Miller1fb593a2012-12-12 10:54:37 +110061 if (n > 3)
62 errx(1, "Invalid modification spec \"%s\"", s);
Damien Miller43e5e602013-03-05 09:49:00 +110063 if (m1 < 0 || m1 > 0xff)
64 errx(1, "Invalid XOR modification value");
65 m->what = MOD_XOR;
66 m->m1 = m1;
Damien Miller1fb593a2012-12-12 10:54:37 +110067 } else if (strcasecmp(what, "andor") == 0) {
Damien Miller1fb593a2012-12-12 10:54:37 +110068 if (n != 4)
69 errx(1, "Invalid modification spec \"%s\"", s);
Damien Miller43e5e602013-03-05 09:49:00 +110070 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;
Damien Miller1fb593a2012-12-12 10:54:37 +110077 } else
78 errx(1, "Invalid modification type \"%s\"", what);
79}
80
81int
82main(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];
Damien Miller283e5752013-02-20 21:13:27 +110089 u_int i, wflag = 0, num_mods = 0;
Damien Miller1fb593a2012-12-12 10:54:37 +110090
Damien Miller283e5752013-02-20 21:13:27 +110091 while ((ch = getopt(argc, argv, "wm:")) != -1) {
Damien Miller1fb593a2012-12-12 10:54:37 +110092 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;
Damien Miller283e5752013-02-20 21:13:27 +110098 case 'w':
99 wflag = 1;
100 break;
Damien Miller1fb593a2012-12-12 10:54:37 +1100101 default:
102 usage();
103 /* NOTREACHED */
104 }
105 }
106 for (total = 0;;) {
107 r = s = read(STDIN_FILENO, buf, sizeof(buf));
108 if (r == 0)
Damien Miller283e5752013-02-20 21:13:27 +1100109 break;
Damien Miller1fb593a2012-12-12 10:54:37 +1100110 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)
Damien Miller283e5752013-02-20 21:13:27 +1100132 break;
Damien Miller1fb593a2012-12-12 10:54:37 +1100133 if (r < 0) {
134 if (errno == EAGAIN || errno == EINTR)
135 continue;
136 err(1, "write");
137 }
138 }
139 total += s;
140 }
Damien Miller283e5752013-02-20 21:13:27 +1100141 /* 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;
Damien Miller1fb593a2012-12-12 10:54:37 +1100150}