(root)/
gzip-1.13/
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
      20  main ()
      21  {
      22    int n;		/* number of differences */
      23    int i;		/* difference counter */
      24    int c;		/* byte from input */
      25  
      26    /* check magic word */
      27    if (getchar() != MAGIC1 || getchar() != MAGIC2)
      28    {
      29      fputs("add: input stream not made by sub\n", stderr);
      30      exit(EXIT_FAILURE);
      31    }
      32  
      33    /* get number of differences from data */
      34    if ((n = getchar()) == EOF || (i = getchar()) == EOF) {
      35      fputs("add: unexpected end of file\n", stderr);
      36      exit(EXIT_FAILURE);
      37    }
      38    n += (i<<8);
      39    if (n <= 0 || n > MAX_DIST) {
      40      fprintf(stderr, "add: incorrect distance %d\n", n);
      41      exit(EXIT_FAILURE);
      42    }
      43  
      44    /* initialize last byte */
      45    i = n;
      46    do {
      47      a[--i] = 0;
      48    } while (i);
      49  
      50    /* read differenced data and restore original */
      51    while ((c = getchar()) != EOF)
      52    {
      53      c = (a[i++] += c) & 0xff;	/* restore data, save last byte */
      54      putchar(c);			/* write original */
      55      if (i == n)			/* cycle on n differences */
      56        i = 0;
      57    }
      58    exit(EXIT_SUCCESS);
      59    return 0;			/* avoid warning */
      60  }