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 | |
Damien Miller | ea61b21 | 2013-11-21 14:25:15 +1100 | [diff] [blame] | 17 | /* $OpenBSD: modpipe.c,v 1.6 2013/11/21 03:16:47 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 | eac1bbd | 2016-07-18 17:12:22 +1000 | [diff] [blame] | 28 | #ifdef HAVE_ERR_H |
| 29 | # include <err.h> |
| 30 | #endif |
Darren Tucker | ccfdfce | 2013-05-10 16:28:55 +1000 | [diff] [blame] | 31 | #include "openbsd-compat/getopt_long.c" |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 32 | |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 33 | static void |
| 34 | usage(void) |
| 35 | { |
Damien Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 36 | fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n"); |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 37 | 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 |
| 44 | struct modification { |
| 45 | enum { MOD_XOR, MOD_AND_OR } what; |
Damien Miller | ea61b21 | 2013-11-21 14:25:15 +1100 | [diff] [blame] | 46 | unsigned long long offset; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 47 | u_int8_t m1, m2; |
| 48 | }; |
| 49 | |
| 50 | static void |
| 51 | parse_modification(const char *s, struct modification *m) |
| 52 | { |
| 53 | char what[16+1]; |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 54 | int n, m1, m2; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 55 | |
| 56 | bzero(m, sizeof(*m)); |
Damien Miller | ea61b21 | 2013-11-21 14:25:15 +1100 | [diff] [blame] | 57 | if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i", |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 58 | what, &m->offset, &m1, &m2)) < 3) |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 59 | errx(1, "Invalid modification spec \"%s\"", s); |
| 60 | if (strcasecmp(what, "xor") == 0) { |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 61 | if (n > 3) |
| 62 | errx(1, "Invalid modification spec \"%s\"", s); |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 63 | if (m1 < 0 || m1 > 0xff) |
| 64 | errx(1, "Invalid XOR modification value"); |
| 65 | m->what = MOD_XOR; |
| 66 | m->m1 = m1; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 67 | } else if (strcasecmp(what, "andor") == 0) { |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 68 | if (n != 4) |
| 69 | errx(1, "Invalid modification spec \"%s\"", s); |
Damien Miller | 43e5e60 | 2013-03-05 09:49:00 +1100 | [diff] [blame] | 70 | 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 Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 77 | } else |
| 78 | errx(1, "Invalid modification type \"%s\"", what); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main(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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 89 | u_int i, wflag = 0, num_mods = 0; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 90 | |
Damien Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 91 | while ((ch = getopt(argc, argv, "wm:")) != -1) { |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 92 | 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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 98 | case 'w': |
| 99 | wflag = 1; |
| 100 | break; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 101 | 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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 109 | break; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 110 | 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 Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 132 | break; |
Damien Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 133 | if (r < 0) { |
| 134 | if (errno == EAGAIN || errno == EINTR) |
| 135 | continue; |
| 136 | err(1, "write"); |
| 137 | } |
| 138 | } |
| 139 | total += s; |
| 140 | } |
Damien Miller | 283e575 | 2013-02-20 21:13:27 +1100 | [diff] [blame] | 141 | /* 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 Miller | 1fb593a | 2012-12-12 10:54:37 +1100 | [diff] [blame] | 150 | } |