summaryrefslogtreecommitdiffstats
path: root/deinter.c
blob: d3d62d675675880e5e2ae0a9a0dcbe8b94c50b71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

/*
 * deinter.c
 *
 * Undo software interleaving
 */

int main(int argc, char *argv[])
{
  int sectsize;
  int ileave;
  int ilsize;
  FILE *f;
  char *buf;
  int i, j;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s file [ileave [ilsize [sector]]]\n", argv[0]);
    exit(1);
  }

  f = fopen(argv[1], "rb");
  if (!f) {
    perror(argv[1]);
    exit(1);
  }

  ileave   = (argc > 2) ? atoi(argv[2]) : 7;
  ilsize   = (argc > 3) ? atoi(argv[3]) : 16;
  sectsize = (argc > 4) ? atoi(argv[4]) : 256;

  buf = malloc(sectsize * ilsize);

  while (fread(buf, 1, sectsize * ilsize, f))
    for (i = 0, j = 0; i < ilsize; i++, j = (j + ileave) % ilsize)
      fwrite(buf + j*sectsize, 1, sectsize, stdout);

  fclose(f);

  return 0;
}