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 | |
| 17 | /* $Id: modpipe.c,v 1.1 2012/12/11 23:54:40 djm Exp $ */ |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <err.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | static void |
| 28 | usage(void) |
| 29 | { |
| 30 | fprintf(stderr, "Usage: modpipe [-m modspec ...] < in > out\n"); |
| 31 | fprintf(stderr, "modspec is one of:\n"); |
| 32 | fprintf(stderr, " xor:offset:value - XOR \"value\" at \"offset\"\n"); |
| 33 | fprintf(stderr, " andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n"); |
| 34 | exit(1); |
| 35 | } |
| 36 | |
| 37 | #define MAX_MODIFICATIONS 256 |
| 38 | struct modification { |
| 39 | enum { MOD_XOR, MOD_AND_OR } what; |
| 40 | u_int64_t offset; |
| 41 | u_int8_t m1, m2; |
| 42 | }; |
| 43 | |
| 44 | static void |
| 45 | parse_modification(const char *s, struct modification *m) |
| 46 | { |
| 47 | char what[16+1]; |
| 48 | int n; |
| 49 | |
| 50 | bzero(m, sizeof(*m)); |
| 51 | if ((n = sscanf(s, "%16[^:]%*[:]%lli%*[:]%hhi%*[:]%hhi", |
| 52 | what, &m->offset, &m->m1, &m->m2)) < 3) |
| 53 | errx(1, "Invalid modification spec \"%s\"", s); |
| 54 | if (strcasecmp(what, "xor") == 0) { |
| 55 | m->what = MOD_XOR; |
| 56 | if (n > 3) |
| 57 | errx(1, "Invalid modification spec \"%s\"", s); |
| 58 | } else if (strcasecmp(what, "andor") == 0) { |
| 59 | m->what = MOD_AND_OR; |
| 60 | if (n != 4) |
| 61 | errx(1, "Invalid modification spec \"%s\"", s); |
| 62 | } else |
| 63 | errx(1, "Invalid modification type \"%s\"", what); |
| 64 | } |
| 65 | |
| 66 | int |
| 67 | main(int argc, char **argv) |
| 68 | { |
| 69 | int ch; |
| 70 | u_char buf[8192]; |
| 71 | size_t total; |
| 72 | ssize_t r, s, o; |
| 73 | struct modification mods[MAX_MODIFICATIONS]; |
| 74 | u_int i, num_mods = 0; |
| 75 | |
| 76 | while ((ch = getopt(argc, argv, "m:")) != -1) { |
| 77 | switch (ch) { |
| 78 | case 'm': |
| 79 | if (num_mods >= MAX_MODIFICATIONS) |
| 80 | errx(1, "Too many modifications"); |
| 81 | parse_modification(optarg, &(mods[num_mods++])); |
| 82 | break; |
| 83 | default: |
| 84 | usage(); |
| 85 | /* NOTREACHED */ |
| 86 | } |
| 87 | } |
| 88 | for (total = 0;;) { |
| 89 | r = s = read(STDIN_FILENO, buf, sizeof(buf)); |
| 90 | if (r == 0) |
| 91 | return 0; |
| 92 | if (r < 0) { |
| 93 | if (errno == EAGAIN || errno == EINTR) |
| 94 | continue; |
| 95 | err(1, "read"); |
| 96 | } |
| 97 | for (i = 0; i < num_mods; i++) { |
| 98 | if (mods[i].offset < total || |
| 99 | mods[i].offset >= total + s) |
| 100 | continue; |
| 101 | switch (mods[i].what) { |
| 102 | case MOD_XOR: |
| 103 | buf[mods[i].offset - total] ^= mods[i].m1; |
| 104 | break; |
| 105 | case MOD_AND_OR: |
| 106 | buf[mods[i].offset - total] &= mods[i].m1; |
| 107 | buf[mods[i].offset - total] |= mods[i].m2; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | for (o = 0; o < s; o += r) { |
| 112 | r = write(STDOUT_FILENO, buf, s - o); |
| 113 | if (r == 0) |
| 114 | return 0; |
| 115 | if (r < 0) { |
| 116 | if (errno == EAGAIN || errno == EINTR) |
| 117 | continue; |
| 118 | err(1, "write"); |
| 119 | } |
| 120 | } |
| 121 | total += s; |
| 122 | } |
| 123 | return 0; |
| 124 | } |