Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 1 | /* |
| 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 | |
Darren Tucker | 5e95173 | 2013-05-17 09:41:33 +1000 | [diff] [blame] | 17 | /* $OpenBSD: modpipe.c,v 1.5 2013/05/10 03:46:14 djm Exp $ */ |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 18 | |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 19 | #include "includes.h" |
| 20 | |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
Damien Miller | b3764e1 | 2013-02-19 13:15:01 +1100 | [diff] [blame] | 25 | #include <stdarg.h> |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 26 | #include <stdlib.h> |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 27 | #include <errno.h> |
Darren Tucker | ccfdfce | 2013-05-10 16:28:55 +1000 | [diff] [blame] | 28 | #include "openbsd-compat/getopt_long.c" |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 29 | |
Damien Miller | b3764e1 | 2013-02-19 13:15:01 +1100 | [diff] [blame] | 30 | static void err(int, const char *, ...) __attribute__((format(printf, 2, 3))); |
| 31 | static void errx(int, const char *, ...) __attribute__((format(printf, 2, 3))); |
| 32 | |
| 33 | static void |
| 34 | err(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 | |
| 46 | static void |
| 47 | errx(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 Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 58 | static void |
| 59 | usage(void) |
| 60 | { |
Damien Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 61 | fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n"); |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 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"); |
| 65 | exit(1); |
| 66 | } |
| 67 | |
| 68 | #define MAX_MODIFICATIONS 256 |
| 69 | struct modification { |
| 70 | enum { MOD_XOR, MOD_AND_OR } what; |
| 71 | u_int64_t offset; |
| 72 | u_int8_t m1, m2; |
| 73 | }; |
| 74 | |
| 75 | static void |
| 76 | parse_modification(const char *s, struct modification *m) |
| 77 | { |
| 78 | char what[16+1]; |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 79 | int n, m1, m2; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 80 | |
| 81 | bzero(m, sizeof(*m)); |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 82 | if ((n = sscanf(s, "%16[^:]%*[:]%lli%*[:]%i%*[:]%i", |
| 83 | what, &m->offset, &m1, &m2)) < 3) |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 84 | errx(1, "Invalid modification spec \"%s\"", s); |
| 85 | if (strcasecmp(what, "xor") == 0) { |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 86 | if (n > 3) |
| 87 | errx(1, "Invalid modification spec \"%s\"", s); |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 88 | if (m1 < 0 || m1 > 0xff) |
| 89 | errx(1, "Invalid XOR modification value"); |
| 90 | m->what = MOD_XOR; |
| 91 | m->m1 = m1; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 92 | } else if (strcasecmp(what, "andor") == 0) { |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 93 | if (n != 4) |
| 94 | errx(1, "Invalid modification spec \"%s\"", s); |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 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"); |
| 99 | m->what = MOD_AND_OR; |
| 100 | m->m1 = m1; |
| 101 | m->m2 = m2; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 102 | } else |
| 103 | errx(1, "Invalid modification type \"%s\"", what); |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | main(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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 114 | u_int i, wflag = 0, num_mods = 0; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 115 | |
Damien Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 116 | while ((ch = getopt(argc, argv, "wm:")) != -1) { |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 117 | 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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 123 | case 'w': |
| 124 | wflag = 1; |
| 125 | break; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 126 | 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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 134 | break; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 135 | 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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 157 | break; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 158 | if (r < 0) { |
| 159 | if (errno == EAGAIN || errno == EINTR) |
| 160 | continue; |
| 161 | err(1, "write"); |
| 162 | } |
| 163 | } |
| 164 | total += s; |
| 165 | } |
Damien Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 166 | /* 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 Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 175 | } |