blob: e854f9e0707df413a3932a65a758969f8ce1f23f [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 Tuckerccfdfce2013-05-10 16:28:55 +100028#include "openbsd-compat/getopt_long.c"
Damien Miller1fb593a2012-12-12 10:54:37 +110029
Damien Millerb3764e12013-02-19 13:15:01 +110030static void err(int, const char *, ...) __attribute__((format(printf, 2, 3)));
31static void errx(int, const char *, ...) __attribute__((format(printf, 2, 3)));
32
33static void
34err(int r, const char *fmt, ...)
35{
36 va_list args;
37
38 va_start(args, fmt);
39 fprintf(stderr, "%s: ", strerror(errno));
40 vfprintf(stderr, fmt, args);
41 fputc('\n', stderr);
42 va_end(args);
43 exit(r);
44}
45
46static void
47errx(int r, const char *fmt, ...)
48{
49 va_list args;
50
51 va_start(args, fmt);
52 vfprintf(stderr, fmt, args);
53 fputc('\n', stderr);
54 va_end(args);
55 exit(r);
56}
57
Damien Miller1fb593a2012-12-12 10:54:37 +110058static void
59usage(void)
60{
Damien Miller283e5752013-02-20 21:13:27 +110061 fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
Damien Miller1fb593a2012-12-12 10:54:37 +110062 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");
65 exit(1);
66}
67
68#define MAX_MODIFICATIONS 256
69struct modification {
70 enum { MOD_XOR, MOD_AND_OR } what;
Damien Millerea61b212013-11-21 14:25:15 +110071 unsigned long long offset;
Damien Miller1fb593a2012-12-12 10:54:37 +110072 u_int8_t m1, m2;
73};
74
75static void
76parse_modification(const char *s, struct modification *m)
77{
78 char what[16+1];
Damien Miller43e5e602013-03-05 09:49:00 +110079 int n, m1, m2;
Damien Miller1fb593a2012-12-12 10:54:37 +110080
81 bzero(m, sizeof(*m));
Damien Millerea61b212013-11-21 14:25:15 +110082 if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
Damien Miller43e5e602013-03-05 09:49:00 +110083 what, &m->offset, &m1, &m2)) < 3)
Damien Miller1fb593a2012-12-12 10:54:37 +110084 errx(1, "Invalid modification spec \"%s\"", s);
85 if (strcasecmp(what, "xor") == 0) {
Damien Miller1fb593a2012-12-12 10:54:37 +110086 if (n > 3)
87 errx(1, "Invalid modification spec \"%s\"", s);
Damien Miller43e5e602013-03-05 09:49:00 +110088 if (m1 < 0 || m1 > 0xff)
89 errx(1, "Invalid XOR modification value");
90 m->what = MOD_XOR;
91 m->m1 = m1;
Damien Miller1fb593a2012-12-12 10:54:37 +110092 } else if (strcasecmp(what, "andor") == 0) {
Damien Miller1fb593a2012-12-12 10:54:37 +110093 if (n != 4)
94 errx(1, "Invalid modification spec \"%s\"", s);
Damien Miller43e5e602013-03-05 09:49:00 +110095 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");
99 m->what = MOD_AND_OR;
100 m->m1 = m1;
101 m->m2 = m2;
Damien Miller1fb593a2012-12-12 10:54:37 +1100102 } else
103 errx(1, "Invalid modification type \"%s\"", what);
104}
105
106int
107main(int argc, char **argv)
108{
109 int ch;
110 u_char buf[8192];
111 size_t total;
112 ssize_t r, s, o;
113 struct modification mods[MAX_MODIFICATIONS];
Damien Miller283e5752013-02-20 21:13:27 +1100114 u_int i, wflag = 0, num_mods = 0;
Damien Miller1fb593a2012-12-12 10:54:37 +1100115
Damien Miller283e5752013-02-20 21:13:27 +1100116 while ((ch = getopt(argc, argv, "wm:")) != -1) {
Damien Miller1fb593a2012-12-12 10:54:37 +1100117 switch (ch) {
118 case 'm':
119 if (num_mods >= MAX_MODIFICATIONS)
120 errx(1, "Too many modifications");
121 parse_modification(optarg, &(mods[num_mods++]));
122 break;
Damien Miller283e5752013-02-20 21:13:27 +1100123 case 'w':
124 wflag = 1;
125 break;
Damien Miller1fb593a2012-12-12 10:54:37 +1100126 default:
127 usage();
128 /* NOTREACHED */
129 }
130 }
131 for (total = 0;;) {
132 r = s = read(STDIN_FILENO, buf, sizeof(buf));
133 if (r == 0)
Damien Miller283e5752013-02-20 21:13:27 +1100134 break;
Damien Miller1fb593a2012-12-12 10:54:37 +1100135 if (r < 0) {
136 if (errno == EAGAIN || errno == EINTR)
137 continue;
138 err(1, "read");
139 }
140 for (i = 0; i < num_mods; i++) {
141 if (mods[i].offset < total ||
142 mods[i].offset >= total + s)
143 continue;
144 switch (mods[i].what) {
145 case MOD_XOR:
146 buf[mods[i].offset - total] ^= mods[i].m1;
147 break;
148 case MOD_AND_OR:
149 buf[mods[i].offset - total] &= mods[i].m1;
150 buf[mods[i].offset - total] |= mods[i].m2;
151 break;
152 }
153 }
154 for (o = 0; o < s; o += r) {
155 r = write(STDOUT_FILENO, buf, s - o);
156 if (r == 0)
Damien Miller283e5752013-02-20 21:13:27 +1100157 break;
Damien Miller1fb593a2012-12-12 10:54:37 +1100158 if (r < 0) {
159 if (errno == EAGAIN || errno == EINTR)
160 continue;
161 err(1, "write");
162 }
163 }
164 total += s;
165 }
Damien Miller283e5752013-02-20 21:13:27 +1100166 /* Warn if modifications not reached in input stream */
167 r = 0;
168 for (i = 0; wflag && i < num_mods; i++) {
169 if (mods[i].offset < total)
170 continue;
171 r = 1;
172 fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);
173 }
174 return r;
Damien Miller1fb593a2012-12-12 10:54:37 +1100175}