[BACK]Return to zopen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / compress

Diff for /src/usr.bin/compress/zopen.c between version 1.5 and 1.6

version 1.5, 1997/07/06 20:23:00 version 1.6, 1999/03/04 15:04:45
Line 83 
Line 83 
   
 #define BITS            16              /* Default bits. */  #define BITS            16              /* Default bits. */
 #define HSIZE           69001           /* 95% occupancy */  #define HSIZE           69001           /* 95% occupancy */
   #define ZBUFSIZ         8192            /* I/O buffer size */
   
 /* A code_int must be able to hold 2**BITS values of type int, and also -1. */  /* A code_int must be able to hold 2**BITS values of type int, and also -1. */
 typedef long code_int;  typedef long code_int;
Line 124 
Line 125 
         int zs_clear_flg;          int zs_clear_flg;
         long zs_ratio;          long zs_ratio;
         count_int zs_checkpoint;          count_int zs_checkpoint;
         int zs_offset;  
         long zs_in_count;               /* Length of input. */          long zs_in_count;               /* Length of input. */
         long zs_bytes_out;              /* Length of compressed output. */          long zs_bytes_out;              /* Length of compressed output. */
         long zs_out_count;              /* # of codes output (for debugging).*/          long zs_out_count;              /* # of codes output (for debugging).*/
         u_char zs_buf[BITS];          u_char zs_buf[ZBUFSIZ];         /* I/O buffer */
           u_char *zs_bp;                  /* Current I/O window in the zs_buf */
           int zs_offset;                  /* Number of bits in the zs_buf */
         union {          union {
                 struct {                  struct {
                         long zs_fcode;                          long zs_fcode;
Line 137 
Line 139 
                         int zs_hshift;                          int zs_hshift;
                 } w;                    /* Write paramenters */                  } w;                    /* Write paramenters */
                 struct {                  struct {
                         u_char *zs_stackp;                          u_char *zs_stackp, *zs_ebp;
                         int zs_finchar;                          int zs_finchar;
                         code_int zs_code, zs_oldcode, zs_incode;                          code_int zs_code, zs_oldcode, zs_incode;
                         int zs_roffset, zs_size;                          int zs_size;
                 } r;                    /* Read parameters */                  } r;                    /* Read parameters */
         } u;          } u;
 };  };
Line 155 
Line 157 
 #define zs_code         u.r.zs_code  #define zs_code         u.r.zs_code
 #define zs_oldcode      u.r.zs_oldcode  #define zs_oldcode      u.r.zs_oldcode
 #define zs_incode       u.r.zs_incode  #define zs_incode       u.r.zs_incode
 #define zs_roffset      u.r.zs_roffset  
 #define zs_size         u.r.zs_size  #define zs_size         u.r.zs_size
   #define zs_ebp          u.r.zs_ebp
   
 /*  /*
  * To save much memory, we overlay the table used by compress() with those   * To save much memory, we overlay the table used by compress() with those
Line 227 
Line 229 
         u_char tmp;          u_char tmp;
         int count;          int count;
   
         if (num == 0)  
                 return (0);  
   
         zs = cookie;          zs = cookie;
         count = num;          count = num;
         bp = (u_char *)wbp;          bp = (u_char *)wbp;
         if (zs->zs_state == S_MIDDLE)          switch (zs->zs_state) {
                 goto middle;          case S_EOF:
         zs->zs_state = S_MIDDLE;                  return 0;
           case S_START:
                   zs->zs_state = S_MIDDLE;
   
         zs->zs_maxmaxcode = 1L << zs->zs_maxbits;                  zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
         if (write(zs->zs_fd, z_magic, sizeof(z_magic)) != sizeof(z_magic))                  if (write(zs->zs_fd, z_magic, sizeof(z_magic)) !=
                 return (-1);                      sizeof(z_magic))
         tmp = (u_char)(zs->zs_maxbits | zs->zs_block_compress);                          return (-1);
         if (write(zs->zs_fd, &tmp, sizeof(tmp)) != sizeof(tmp))                  tmp = (u_char)(zs->zs_maxbits | zs->zs_block_compress);
                 return (-1);                  if (write(zs->zs_fd, &tmp, sizeof(tmp)) != sizeof(tmp))
                           return (-1);
   
         zs->zs_offset = 0;                  zs->zs_bp = zs->zs_buf;
         zs->zs_bytes_out = 3;           /* Includes 3-byte header mojo. */                  zs->zs_offset = 0;
         zs->zs_out_count = 0;                  zs->zs_bytes_out = 3;   /* Includes 3-byte header mojo. */
         zs->zs_clear_flg = 0;                  zs->zs_out_count = 0;
         zs->zs_ratio = 0;                  zs->zs_clear_flg = 0;
         zs->zs_in_count = 1;                  zs->zs_ratio = 0;
         zs->zs_checkpoint = CHECK_GAP;                  zs->zs_in_count = 1;
         zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);                  zs->zs_checkpoint = CHECK_GAP;
         zs->zs_free_ent = ((zs->zs_block_compress) ? FIRST : 256);                  zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
                   zs->zs_free_ent = ((zs->zs_block_compress) ? FIRST : 256);
   
         zs->zs_ent = *bp++;                  zs->zs_ent = *bp++;
         --count;                  --count;
   
         zs->zs_hshift = 0;                  zs->zs_hshift = 0;
         for (zs->zs_fcode = (long)zs->zs_hsize; zs->zs_fcode < 65536L;                  for (zs->zs_fcode = (long)zs->zs_hsize; zs->zs_fcode < 65536L;
              zs->zs_fcode *= 2L)                       zs->zs_fcode *= 2L)
                 zs->zs_hshift++;                          zs->zs_hshift++;
         zs->zs_hshift = 8 - zs->zs_hshift; /* Set hash code range bound. */                  /* Set hash code range bound. */
                   zs->zs_hshift = 8 - zs->zs_hshift;
   
         zs->zs_hsize_reg = zs->zs_hsize;                  zs->zs_hsize_reg = zs->zs_hsize;
         cl_hash(zs, (count_int)zs->zs_hsize_reg);       /* Clear hash table. */                  /* Clear hash table. */
                   cl_hash(zs, (count_int)zs->zs_hsize_reg);
   
 middle: for (i = 0; count--;) {          case S_MIDDLE:
                 c = *bp++;                  for (i = 0; count-- > 0;) {
                 zs->zs_in_count++;                          c = *bp++;
                 zs->zs_fcode = (long)(((long)c << zs->zs_maxbits) +                          zs->zs_in_count++;
                                       zs->zs_ent);                          zs->zs_fcode = (long)(((long)c << zs->zs_maxbits) +
                 i = ((c << zs->zs_hshift) ^ zs->zs_ent); /* Xor hashing. */                                                zs->zs_ent);
                           /* Xor hashing. */
                 if (htabof(i) == zs->zs_fcode) {                          i = ((c << zs->zs_hshift) ^ zs->zs_ent);
                         zs->zs_ent = codetabof(i);  
                         continue;                          if (htabof(i) == zs->zs_fcode) {
                 } else if ((long)htabof(i) < 0) /* Empty slot. */                                  zs->zs_ent = codetabof(i);
                         goto nomatch;                                  continue;
                 /* Secondary hash (after G. Knott). */                          } else if ((long)htabof(i) < 0) /* Empty slot. */
                 disp = zs->zs_hsize_reg - i;                                  goto nomatch;
                 if (i == 0)                          /* Secondary hash (after G. Knott). */
                           disp = zs->zs_hsize_reg - i;
                           if (i == 0)
                         disp = 1;                          disp = 1;
 probe:          if ((i -= disp) < 0)  probe:                  if ((i -= disp) < 0)
                         i += zs->zs_hsize_reg;                                  i += zs->zs_hsize_reg;
   
                 if (htabof(i) == zs->zs_fcode) {                          if (htabof(i) == zs->zs_fcode) {
                         zs->zs_ent = codetabof(i);                                  zs->zs_ent = codetabof(i);
                         continue;                                  continue;
                 }                          }
                 if ((long)htabof(i) >= 0)                          if ((long)htabof(i) >= 0)
                         goto probe;                                  goto probe;
 nomatch:        if (output(zs, (code_int) zs->zs_ent) == -1)  nomatch:                if (output(zs, (code_int) zs->zs_ent) == -1)
                         return (-1);  
                 zs->zs_out_count++;  
                 zs->zs_ent = c;  
                 if (zs->zs_free_ent < zs->zs_maxmaxcode) {  
                         /* code -> hashtable */  
                         codetabof(i) = zs->zs_free_ent++;  
                         htabof(i) = zs->zs_fcode;  
                 } else if ((count_int)zs->zs_in_count >=  
                     zs->zs_checkpoint && zs->zs_block_compress) {  
                         if (cl_block(zs) == -1)  
                                 return (-1);                                  return (-1);
                           zs->zs_out_count++;
                           zs->zs_ent = c;
                           if (zs->zs_free_ent < zs->zs_maxmaxcode) {
                                   /* code -> hashtable */
                                   codetabof(i) = zs->zs_free_ent++;
                                   htabof(i) = zs->zs_fcode;
                           } else if ((count_int)zs->zs_in_count >=
                               zs->zs_checkpoint && zs->zs_block_compress) {
                                   if (cl_block(zs) == -1)
                                           return (-1);
                           }
                 }                  }
         }          }
         return (num);          return (num);
Line 349 
Line 357 
  * code in turn.  When the buffer fills up empty it and start over.   * code in turn.  When the buffer fills up empty it and start over.
  */   */
   
 static u_char lmask[9] =  static const u_char lmask[9] =
         {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};          {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
 static u_char rmask[9] =  static const u_char rmask[9] =
         {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};          {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
   
 static int  static int
Line 359 
Line 367 
         register struct s_zstate *zs;          register struct s_zstate *zs;
         code_int ocode;          code_int ocode;
 {  {
         register int bits, r_off;          register int bits;
         register u_char *bp;  
   
         r_off = zs->zs_offset;  
         bits = zs->zs_n_bits;  
         bp = zs->zs_buf;  
         if (ocode >= 0) {          if (ocode >= 0) {
                   register int r_off;
                   register u_char *bp;
   
                 /* Get to the first byte. */                  /* Get to the first byte. */
                 bp += (r_off >> 3);                  bp = zs->zs_bp + (zs->zs_offset >> 3);
                 r_off &= 7;                  r_off = zs->zs_offset & 7;
                   bits = zs->zs_n_bits;
   
                 /*                  /*
                  * Since ocode is always >= 8 bits, only need to mask the first                   * Since ocode is always >= 8 bits, only need to mask the first
                  * hunk on the left.                   * hunk on the left.
Line 388 
Line 397 
                         *bp = ocode;                          *bp = ocode;
                 zs->zs_offset += zs->zs_n_bits;                  zs->zs_offset += zs->zs_n_bits;
                 if (zs->zs_offset == (zs->zs_n_bits << 3)) {                  if (zs->zs_offset == (zs->zs_n_bits << 3)) {
                         bp = zs->zs_buf;                          zs->zs_bp += zs->zs_n_bits;
                         bits = zs->zs_n_bits;  
                         zs->zs_bytes_out += bits;  
                         if (write(zs->zs_fd, bp, bits) != bits)  
                                 return (-1);  
                         bp += bits;  
                         bits = 0;  
                         zs->zs_offset = 0;                          zs->zs_offset = 0;
                 }                  }
                 /*                  /*
Line 408 
Line 411 
                         * discover the size increase until after it has read it                          * discover the size increase until after it has read it
                         */                          */
                         if (zs->zs_offset > 0) {                          if (zs->zs_offset > 0) {
                                 if (write(zs->zs_fd, zs->zs_buf, zs->zs_n_bits)                                  zs->zs_bp += zs->zs_n_bits;
                                     != zs->zs_n_bits)                                  zs->zs_offset = 0;
                                         return (-1);  
                                 zs->zs_bytes_out += zs->zs_n_bits;  
                         }                          }
                         zs->zs_offset = 0;  
   
                         if (zs->zs_clear_flg) {                          if (zs->zs_clear_flg) {
                                 zs->zs_maxcode =                                  zs->zs_maxcode =
Line 428 
Line 428 
                                                 MAXCODE(zs->zs_n_bits);                                                  MAXCODE(zs->zs_n_bits);
                         }                          }
                 }                  }
   
                   if (zs->zs_bp + zs->zs_n_bits > &zs->zs_buf[ZBUFSIZ]) {
                           bits = zs->zs_bp - zs->zs_buf;
                           if (write(zs->zs_fd, zs->zs_buf, bits) != bits)
                                   return (-1);
                           zs->zs_bytes_out += bits;
                           if (zs->zs_offset > 0)
                                   fprintf (stderr, "zs_offset != 0\n");
                           zs->zs_bp = zs->zs_buf;
                   }
         } else {          } else {
                 /* At EOF, write the rest of the buffer. */                  /* At EOF, write the rest of the buffer. */
                 if (zs->zs_offset > 0) {                  if (zs->zs_offset > 0)
                         zs->zs_offset = (zs->zs_offset + 7) / 8;                          zs->zs_bp += (zs->zs_offset + 7) / 8;
                         if (write(zs->zs_fd, zs->zs_buf, zs->zs_offset)                  if (zs->zs_bp > zs->zs_buf) {
                             != zs->zs_offset)                          bits = zs->zs_bp - zs->zs_buf;
                           if (write(zs->zs_fd, zs->zs_buf, bits) != bits)
                                 return (-1);                                  return (-1);
                         zs->zs_bytes_out += zs->zs_offset;                          zs->zs_bytes_out += bits;
                 }                  }
                 zs->zs_offset = 0;                  zs->zs_offset = 0;
                   zs->zs_bp = zs->zs_buf;
         }          }
         return (0);          return (0);
 }  }
Line 467 
Line 479 
         switch (zs->zs_state) {          switch (zs->zs_state) {
         case S_START:          case S_START:
                 zs->zs_state = S_MIDDLE;                  zs->zs_state = S_MIDDLE;
                   zs->zs_bp = zs->zs_buf;
                 break;                  break;
         case S_MIDDLE:          case S_MIDDLE:
                 goto middle;                  goto middle;
Line 567 
Line 580 
         register int r_off, bits;          register int r_off, bits;
         register u_char *bp;          register u_char *bp;
   
         bp = zs->zs_buf;          if (zs->zs_clear_flg > 0 || zs->zs_offset >= zs->zs_size ||
         if (zs->zs_clear_flg > 0 || zs->zs_roffset >= zs->zs_size ||  
             zs->zs_free_ent > zs->zs_maxcode) {              zs->zs_free_ent > zs->zs_maxcode) {
   
                   zs->zs_bp += zs->zs_n_bits;
                 /*                  /*
                  * If the next entry will be too big for the current gcode                   * If the next entry will be too big for the current gcode
                  * size, then we must increase the size.  This implies reading                   * size, then we must increase the size.  This implies reading
Line 586 
Line 600 
                         zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);                          zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
                         zs->zs_clear_flg = 0;                          zs->zs_clear_flg = 0;
                 }                  }
                 zs->zs_size = read(zs->zs_fd, zs->zs_buf, zs->zs_n_bits);  
                 if (zs->zs_size <= 0)                   /* End of file. */                  /* fill the buffer up to the neck */
                         return (-1);                  if (zs->zs_bp + zs->zs_n_bits > zs->zs_ebp) {
                 zs->zs_roffset = 0;                          for (bp = zs->zs_buf; zs->zs_bp < zs->zs_ebp;
                                   *bp++ = *zs->zs_bp++);
                           if ((bits = read(zs->zs_fd, bp, ZBUFSIZ -
                                            (bp - zs->zs_buf))) < 0)
                                   return -1;
                           zs->zs_bp = zs->zs_buf;
                           zs->zs_ebp = bp + bits;
                   }
                   zs->zs_offset = 0;
                   zs->zs_size = MIN(zs->zs_n_bits, zs->zs_ebp - zs->zs_bp);
                   if (zs->zs_size == 0)
                           return -1;
                 /* Round size down to integral number of codes. */                  /* Round size down to integral number of codes. */
                 zs->zs_size = (zs->zs_size << 3) - (zs->zs_n_bits - 1);                  zs->zs_size = (zs->zs_size << 3) - (zs->zs_n_bits - 1);
         }          }
         r_off = zs->zs_roffset;  
           bp = zs->zs_bp;
           r_off = zs->zs_offset;
         bits = zs->zs_n_bits;          bits = zs->zs_n_bits;
   
         /* Get to the first byte. */          /* Get to the first byte. */
Line 614 
Line 641 
   
         /* High order bits. */          /* High order bits. */
         gcode |= (*bp & rmask[bits]) << r_off;          gcode |= (*bp & rmask[bits]) << r_off;
         zs->zs_roffset += zs->zs_n_bits;          zs->zs_offset += zs->zs_n_bits;
   
         return (gcode);          return (gcode);
 }  }
Line 731 
Line 758 
         zs->zs_in_count = 1;            /* Length of input. */          zs->zs_in_count = 1;            /* Length of input. */
         zs->zs_out_count = 0;           /* # of codes output (for debugging).*/          zs->zs_out_count = 0;           /* # of codes output (for debugging).*/
         zs->zs_state = S_START;          zs->zs_state = S_START;
         zs->zs_roffset = 0;          zs->zs_offset = 0;
         zs->zs_size = 0;          zs->zs_size = 0;
         zs->zs_mode = mode[0];          zs->zs_mode = mode[0];
           zs->zs_bp = zs->zs_ebp = zs->zs_buf;
   
         zs->zs_fd = fd;          zs->zs_fd = fd;
         return zs;          return zs;

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6