All pastes #1323170 Raw Edit

rawdiff.c

public c v1 · immutable
#1323170 ·published 2009-01-30 22:59 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#define SF(var, func, bad, badout, args...) { \    var = func(args); \    if (var == bad) { \        perror(badout); \        exit(1); \    } \}#define BUFLEN 921600int main(int argc, char **argv){    FILE *i1, *i2, *o;    int mul;    unsigned char i1b[BUFLEN], i2b[BUFLEN], ob[BUFLEN];    if (argc < 4) {        fprintf(stderr, "Use: rawdiff <in 1> <in 2> <out> [mul]\n");        return 1;    }    if (argc >= 5) {        mul = atoi(argv[4]);    } else {        mul = 1;    }    /* open the files */    SF(i1, fopen, NULL, argv[1], argv[1], "rb");    SF(i2, fopen, NULL, argv[2], argv[2], "rb");    SF(o,  fopen, NULL, argv[3], argv[3], "wb");        /* then diff them */    while (1) {        int i;        /* fill the buffer */        if (fread(i1b, 1, BUFLEN, i1) == 0 || fread(i2b, 1, BUFLEN, i2) == 0) {            break;        }        /* diff them */        for (i = 0; i < BUFLEN; i++) {            unsigned char d, inc;            if (i1b[i] >= i2b[i]) {                d = i1b[i] - i2b[i];                inc = 0;            } else {                d = i2b[i] - i1b[i];                inc = 1;            }            d *= mul;            if (inc) {                if (i1b[i] + d < i1b[i]) {                    d = (unsigned char) -1;                } else {                    d += i1b[i];                }            } else {                if (d >= i1b[i]) {                    d = 0;                } else {                    d = i1b[i] - d;                }            }            ob[i] = d;        }        /* and output them */        fwrite(ob, 1, BUFLEN, o);    }    return 0;}