maint: include <config.h> first in many files
[gzip.git] / sample / add.c
blobcbde9152ad1f047c105ddbe5cd8497712e36df4d
1 /* add.c not copyrighted (n) 1993 by Mark Adler */
2 /* version 1.1 11 Jun 1993 */
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. */
9 #include <config.h>
10 #include <stdio.h>
12 #define MAGIC1 'S' /* sub data */
13 #define MAGIC2 26 /* ^Z */
14 #define MAX_DIST 16384
16 char a[MAX_DIST]; /* last byte buffer for up to MAX_DIST differences */
18 int main()
20 int n; /* number of differences */
21 int i; /* difference counter */
22 int c; /* byte from input */
24 /* check magic word */
25 if (getchar() != MAGIC1 || getchar() != MAGIC2)
27 fputs("add: input stream not made by sub\n", stderr);
28 exit(1);
31 /* get number of differences from data */
32 if ((n = getchar()) == EOF || (i = getchar()) == EOF) {
33 fputs("add: unexpected end of file\n", stderr);
34 exit(1);
36 n += (i<<8);
37 if (n <= 0 || n > MAX_DIST) {
38 fprintf(stderr, "add: incorrect distance %d\n", n);
39 exit(1);
42 /* initialize last byte */
43 i = n;
44 do {
45 a[--i] = 0;
46 } while (i);
48 /* read differenced data and restore original */
49 while ((c = getchar()) != EOF)
51 c = (a[i++] += c) & 0xff; /* restore data, save last byte */
52 putchar(c); /* write original */
53 if (i == n) /* cycle on n differences */
54 i = 0;
56 exit(0);
57 return 0; /* avoid warning */