Annotation of pkgsrc/pkgtools/binpatch/files/binpatch.c, revision 1.1
1.1 ! atatat 1: /* $NetBSD$ */
! 2:
! 3: /*
! 4: * ------------------------------------------------------------------------
! 5: * "THE BEER-WARE LICENSE" (Revision 42):
! 6: * Andrew Brown <atatat@NetBSD.org> wrote this file. As long as you
! 7: * retain this notice you can do whatever you want with this stuff.
! 8: * If we meet some day, and you think this stuff is worth it, you can
! 9: * buy me a beer in return.
! 10: * ------------------------------------------------------------------------
! 11: */
! 12:
! 13: #include <sys/types.h>
! 14: #include <sys/stat.h>
! 15:
! 16: #include <errno.h>
! 17: #include <fcntl.h>
! 18: #include <stdio.h>
! 19: #include <stdlib.h>
! 20: #include <string.h>
! 21:
! 22: static int
! 23: die(int rc, const char *msg)
! 24: {
! 25: if (rc)
! 26: perror(msg);
! 27: else
! 28: fprintf(stderr, "%s\n", msg);
! 29: exit(1);
! 30: }
! 31:
! 32: static void
! 33: cvt(const char *s, const char *i, unsigned char *o, size_t l)
! 34: {
! 35: int t, x;
! 36:
! 37: for (t = 0; t < l; t++) {
! 38: x = i[2 * t];
! 39: if (x >= '0' && x <= '9')
! 40: x -= '0';
! 41: else if (x >= 'a' && x <= 'f')
! 42: x -= 'a' - 10;
! 43: else if (x >= 'A' && x <= 'F')
! 44: x -= 'A' - 10;
! 45: else
! 46: die(0, s);
! 47: o[t] = x * 16;
! 48:
! 49: x = i[2 * t + 1];
! 50: if (x >= '0' && x <= '9')
! 51: x -= '0';
! 52: else if (x >= 'a' && x <= 'f')
! 53: x -= 'a' - 10;
! 54: else if (x >= 'A' && x <= 'F')
! 55: x -= 'A' - 10;
! 56: else
! 57: die(0, s);
! 58: o[t] += x;
! 59: }
! 60: }
! 61:
! 62: int
! 63: main(int argc, char *argv[])
! 64: {
! 65: struct stat st;
! 66: char *key, *value;
! 67: int f, i;
! 68: unsigned char *buf, *get, *put;
! 69: size_t lget, lput;
! 70: off_t sz, cmp_off, skip_off;
! 71:
! 72: f = -1;
! 73: get = put = NULL;
! 74: lget = lput = 0;
! 75: sz = cmp_off = skip_off = -1;
! 76:
! 77: while (--argc > 0) {
! 78: key = *++argv;
! 79: if ((value = strchr(key, '=')) == NULL)
! 80: die(0, "value required");
! 81: else
! 82: *value++ = '\0';
! 83:
! 84: if (strcmp(key, "file") == 0) {
! 85: f = open(value, O_RDWR);
! 86: if (f == -1)
! 87: die(1, "file");
! 88: }
! 89: else if (strcmp(key, "size") == 0) {
! 90: char *t;
! 91: errno = 0;
! 92: sz = strtol(value, &t, 0);
! 93: if (errno != 0)
! 94: die(1, "size");
! 95: }
! 96: else if (strcmp(key, "offset") == 0) {
! 97: char *t;
! 98: errno = 0;
! 99: cmp_off = strtol(value, &t, 0);
! 100: if (errno != 0)
! 101: die(1, "offset");
! 102: }
! 103: else if (strcmp(key, "compare") == 0) {
! 104: lget = strlen(value);
! 105: if (lget % 2 != 0)
! 106: die(0, "compare");
! 107: lget /= 2;
! 108: get = malloc(lget);
! 109: buf = malloc(lget);
! 110: cvt("compare", value, get, lget);
! 111: }
! 112: else if (strcmp(key, "skip") == 0) {
! 113: char *t;
! 114: errno = 0;
! 115: skip_off = strtol(value, &t, 0);
! 116: if (errno != 0)
! 117: die(1, "offset");
! 118: }
! 119: else if (strcmp(key, "replace") == 0) {
! 120: lput = strlen(value);
! 121: if (lput % 2 != 0)
! 122: die(0, "replace");
! 123: lput /= 2;
! 124: put = malloc(lput);
! 125: cvt("replace", value, put, lput);
! 126: }
! 127: }
! 128:
! 129: /*
! 130: * ./binpatch
! 131: * file=${MOZILLA_HOME}/netscape
! 132: * size=13823336
! 133: * offset=0x008073e9
! 134: * compare=6a00e82406a3ffe81f0ca3ff
! 135: * skip=2
! 136: * replace=9090909090
! 137: */
! 138:
! 139: if (f == -1)
! 140: die(0, "file missing");
! 141: if (get == NULL || lget == 0)
! 142: die(0, "compare missing");
! 143: if (put == NULL || lput == 0)
! 144: die(0, "replace missing");
! 145: if (sz == -1)
! 146: die(0, "size missing");
! 147: if (cmp_off == -1)
! 148: die(0, "offset missing");
! 149: if (skip_off == -1)
! 150: die(0, "skip missing");
! 151: if (skip_off < 0 ||
! 152: (skip_off == 0 && lput >= lget) ||
! 153: (skip_off > 0 && skip_off + lput > lget))
! 154: die(0, "illegal skip");
! 155:
! 156: if (fstat(f, &st) == -1)
! 157: die(1, "fstat");
! 158: if (st.st_size != sz)
! 159: die(0, "wrong size");
! 160: if (lseek(f, cmp_off, SEEK_SET) == -1)
! 161: die(1, "lseek");
! 162: if (read(f, buf, lget) != lget)
! 163: die(1, "read");
! 164: if (memcmp(buf, get, lget) != 0)
! 165: die(0, "instructions not found");
! 166: if (lseek(f, cmp_off + skip_off, SEEK_SET) == -1)
! 167: die(1, "lseek");
! 168: if (write(f, put, lput) != lput)
! 169: die(1, "write");
! 170: if (close(f) != 0)
! 171: die(1, "close");
! 172:
! 173: return (0);
! 174: }
CVSweb <webmaster@jp.NetBSD.org>