document mingw linker fix and close associated bug
[debian/gzip] / sample / add.c
1 /* add.c   not copyrighted (n) 1993 by Mark Adler */
2 /* version 1.1   11 Jun 1993 */
3
4 /* This filter reverses the effect of the sub filter.  It requires no
5    arguments, since sub puts the information necessary for extraction
6    in the stream.  See sub.c for what the filtering is and what it's
7    good for. */
8
9 #include <config.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #define MAGIC1    'S' /* sub data */
14 #define MAGIC2    26  /* ^Z */
15 #define MAX_DIST  16384
16
17 char a[MAX_DIST];       /* last byte buffer for up to MAX_DIST differences */
18
19 int main()
20 {
21   int n;                /* number of differences */
22   int i;                /* difference counter */
23   int c;                /* byte from input */
24
25   /* check magic word */
26   if (getchar() != MAGIC1 || getchar() != MAGIC2)
27   {
28     fputs("add: input stream not made by sub\n", stderr);
29     exit(EXIT_FAILURE);
30   }
31
32   /* get number of differences from data */
33   if ((n = getchar()) == EOF || (i = getchar()) == EOF) {
34     fputs("add: unexpected end of file\n", stderr);
35     exit(EXIT_FAILURE);
36   }
37   n += (i<<8);
38   if (n <= 0 || n > MAX_DIST) {
39     fprintf(stderr, "add: incorrect distance %d\n", n);
40     exit(EXIT_FAILURE);
41   }
42
43   /* initialize last byte */
44   i = n;
45   do {
46     a[--i] = 0;
47   } while (i);
48
49   /* read differenced data and restore original */
50   while ((c = getchar()) != EOF)
51   {
52     c = (a[i++] += c) & 0xff;   /* restore data, save last byte */
53     putchar(c);                 /* write original */
54     if (i == n)                 /* cycle on n differences */
55       i = 0;
56   }
57   exit(EXIT_SUCCESS);
58   return 0;                     /* avoid warning */
59 }