]> git.cworth.org Git - gzip/blob - sample/add.c
Avoid creating an undersized buffer for the hufts table.
[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 <stdio.h>
10
11 #define MAGIC1    'S' /* sub data */
12 #define MAGIC2    26  /* ^Z */
13 #define MAX_DIST  16384
14
15 char a[MAX_DIST];       /* last byte buffer for up to MAX_DIST differences */
16
17 int main()
18 {
19   int n;                /* number of differences */
20   int i;                /* difference counter */
21   int c;                /* byte from input */
22
23   /* check magic word */
24   if (getchar() != MAGIC1 || getchar() != MAGIC2)
25   {
26     fputs("add: input stream not made by sub\n", stderr);
27     exit(1);
28   }
29
30   /* get number of differences from data */
31   if ((n = getchar()) == EOF || (i = getchar()) == EOF) {
32     fputs("add: unexpected end of file\n", stderr);
33     exit(1);
34   }
35   n += (i<<8);
36   if (n <= 0 || n > MAX_DIST) {
37     fprintf(stderr, "add: incorrect distance %d\n", n);
38     exit(1);
39   }
40
41   /* initialize last byte */
42   i = n;
43   do {
44     a[--i] = 0;
45   } while (i);
46
47   /* read differenced data and restore original */
48   while ((c = getchar()) != EOF)
49   {
50     c = (a[i++] += c) & 0xff;   /* restore data, save last byte */
51     putchar(c);                 /* write original */
52     if (i == n)                 /* cycle on n differences */
53       i = 0;
54   }
55   exit(0);
56   return 0;                     /* avoid warning */
57 }