Main Page | Class List | Directories | File List | Class Members | File Members

unlzw.c File Reference

#include <config.h>
#include "tailor.h"
#include "gzip.h"
#include "lzw.h"

Go to the source code of this file.

Classes

union  bytes

Defines

#define MAXCODE(n)   (1L << (n))
#define REGISTERS   2
#define REG1
#define REG2
#define REG3
#define REG4
#define REG5
#define REG6
#define REG7
#define REG8
#define REG9
#define REG10
#define REG11
#define REG12
#define REG13
#define REG14
#define REG15
#define REG16
#define BYTEORDER   0000
#define NOALLIGN   0
#define input(b, o, c, n, m)
#define tab_prefixof(i)   tab_prefix[i]
#define clear_tab_prefixof()   memzero(tab_prefix, 256);
#define de_stack   ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
#define tab_suffixof(i)   tab_suffix[i]

Typedefs

typedef unsigned char char_type
typedef long code_int
typedef unsigned long count_int
typedef unsigned short count_short
typedef unsigned long cmp_code_int

Functions

int unlzw (int in, int out)

Variables

int block_mode = BLOCK_MODE


Define Documentation

#define BYTEORDER   0000
 

Definition at line 120 of file unlzw.c.

 
#define clear_tab_prefixof  )     memzero(tab_prefix, 256);
 

Definition at line 167 of file unlzw.c.

Referenced by unlzw().

#define de_stack   ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
 

Definition at line 177 of file unlzw.c.

Referenced by unlzw().

#define input b,
o,
c,
n,
 ) 
 

Value:

{ \
     REG1 char_type *p = &(b)[(o)>>3]; \
     (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
     ((long)(p[2])<<16))>>((o)&0x7))&(m); \
     (o) += (n); \
   }

Definition at line 156 of file unlzw.c.

Referenced by unlzw().

#define MAXCODE  )     (1L << (n))
 

Definition at line 33 of file unlzw.c.

Referenced by unlzw().

#define NOALLIGN   0
 

Definition at line 124 of file unlzw.c.

#define REG1
 

Definition at line 56 of file unlzw.c.

Referenced by unlzw().

#define REG10
 

Definition at line 47 of file unlzw.c.

#define REG11
 

Definition at line 48 of file unlzw.c.

Referenced by unlzw().

#define REG12
 

Definition at line 49 of file unlzw.c.

Referenced by unlzw().

#define REG13
 

Definition at line 50 of file unlzw.c.

Referenced by unlzw().

#define REG14
 

Definition at line 51 of file unlzw.c.

Referenced by unlzw().

#define REG15
 

Definition at line 52 of file unlzw.c.

Referenced by unlzw().

#define REG16
 

Definition at line 53 of file unlzw.c.

Referenced by unlzw().

#define REG2
 

Definition at line 60 of file unlzw.c.

Referenced by unlzw().

#define REG3
 

Definition at line 40 of file unlzw.c.

Referenced by unlzw().

#define REG4
 

Definition at line 41 of file unlzw.c.

Referenced by unlzw().

#define REG5
 

Definition at line 42 of file unlzw.c.

Referenced by unlzw().

#define REG6
 

Definition at line 43 of file unlzw.c.

Referenced by unlzw().

#define REG7
 

Definition at line 44 of file unlzw.c.

Referenced by unlzw().

#define REG8
 

Definition at line 45 of file unlzw.c.

Referenced by unlzw().

#define REG9
 

Definition at line 46 of file unlzw.c.

Referenced by unlzw().

#define REGISTERS   2
 

Definition at line 36 of file unlzw.c.

#define tab_prefixof  )     tab_prefix[i]
 

Definition at line 166 of file unlzw.c.

Referenced by unlzw().

#define tab_suffixof  )     tab_suffix[i]
 

Definition at line 178 of file unlzw.c.

Referenced by unlzw().


Typedef Documentation

typedef unsigned char char_type
 

Definition at line 27 of file unlzw.c.

typedef unsigned long cmp_code_int
 

Definition at line 31 of file unlzw.c.

typedef long code_int
 

Definition at line 28 of file unlzw.c.

typedef unsigned long count_int
 

Definition at line 29 of file unlzw.c.

typedef unsigned short count_short
 

Definition at line 30 of file unlzw.c.


Function Documentation

int unlzw int  in,
int  out
 

Definition at line 191 of file unlzw.c.

References BIT_MASK, BITS, BLOCK_MODE, block_mode, bytes_in, bytes_out, CLEAR, clear_tab_prefixof, de_stack, ERROR, exit_code, FIRST, get_byte, gzip_error(), ifname, INBUF_EXTRA, INBUFSIZ, INIT_BITS, inptr, input, insize, LZW_RESERVED, maxbits, MAXCODE, memcpy, OK, OUTBUFSIZ, program_name, read_buffer(), read_error(), REG1, REG11, REG12, REG13, REG14, REG15, REG16, REG2, REG3, REG4, REG5, REG6, REG7, REG8, REG9, tab_prefix, tab_prefixof, tab_suffixof, test, to_stdout, Tracev, WARN, and write_buf().

Referenced by get_method().

00193 {
00194     REG2   char_type  *stackp;
00195     REG3   code_int   code;
00196     REG4   int        finchar;
00197     REG5   code_int   oldcode;
00198     REG6   code_int   incode;
00199     REG7   long       inbits;
00200     REG8   long       posbits;
00201     REG9   int        outpos;
00202 /*  REG10  int        insize; (global) */
00203     REG11  unsigned   bitmask;
00204     REG12  code_int   free_ent;
00205     REG13  code_int   maxcode;
00206     REG14  code_int   maxmaxcode;
00207     REG15  int        n_bits;
00208     REG16  int        rsize;
00209 
00210 #ifdef MAXSEG_64K
00211     tab_prefix[0] = tab_prefix0;
00212     tab_prefix[1] = tab_prefix1;
00213 #endif
00214     maxbits = get_byte();
00215     block_mode = maxbits & BLOCK_MODE;
00216     if ((maxbits & LZW_RESERVED) != 0) {
00217         WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
00218               program_name, ifname, maxbits & LZW_RESERVED));
00219     }
00220     maxbits &= BIT_MASK;
00221     maxmaxcode = MAXCODE(maxbits);
00222 
00223     if (maxbits > BITS) {
00224         fprintf(stderr,
00225                 "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
00226                 program_name, ifname, maxbits, BITS);
00227         exit_code = ERROR;
00228         return ERROR;
00229     }
00230     rsize = insize;
00231     maxcode = MAXCODE(n_bits = INIT_BITS)-1;
00232     bitmask = (1<<n_bits)-1;
00233     oldcode = -1;
00234     finchar = 0;
00235     outpos = 0;
00236     posbits = inptr<<3;
00237 
00238     free_ent = ((block_mode) ? FIRST : 256);
00239 
00240     clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
00241 
00242     for (code = 255 ; code >= 0 ; --code) {
00243         tab_suffixof(code) = (char_type)code;
00244     }
00245     do {
00246         REG1 int i;
00247         int  e;
00248         int  o;
00249 
00250     resetbuf:
00251         e = insize-(o = (posbits>>3));
00252 
00253         for (i = 0 ; i < e ; ++i) {
00254             inbuf[i] = inbuf[i+o];
00255         }
00256         insize = e;
00257         posbits = 0;
00258 
00259         if (insize < INBUF_EXTRA) {
00260             rsize = read_buffer (in, (char *) inbuf + insize, INBUFSIZ);
00261             if (rsize == -1) {
00262                 read_error();
00263             }
00264             insize += rsize;
00265             bytes_in += (off_t)rsize;
00266         }
00267         inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 :
00268                   ((long)insize<<3)-(n_bits-1));
00269 
00270         while (inbits > posbits) {
00271             if (free_ent > maxcode) {
00272                 posbits = ((posbits-1) +
00273                            ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
00274                 ++n_bits;
00275                 if (n_bits == maxbits) {
00276                     maxcode = maxmaxcode;
00277                 } else {
00278                     maxcode = MAXCODE(n_bits)-1;
00279                 }
00280                 bitmask = (1<<n_bits)-1;
00281                 goto resetbuf;
00282             }
00283             input(inbuf,posbits,code,n_bits,bitmask);
00284             Tracev((stderr, "%d ", code));
00285 
00286             if (oldcode == -1) {
00287                 if (256 <= code)
00288                   gzip_error ("corrupt input.");
00289                 outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
00290                 continue;
00291             }
00292             if (code == CLEAR && block_mode) {
00293                 clear_tab_prefixof();
00294                 free_ent = FIRST - 1;
00295                 posbits = ((posbits-1) +
00296                            ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
00297                 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
00298                 bitmask = (1<<n_bits)-1;
00299                 goto resetbuf;
00300             }
00301             incode = code;
00302             stackp = de_stack;
00303 
00304             if (code >= free_ent) { /* Special case for KwKwK string. */
00305                 if (code > free_ent) {
00306 #ifdef DEBUG
00307                     char_type *p;
00308 
00309                     posbits -= n_bits;
00310                     p = &inbuf[posbits>>3];
00311                     fprintf(stderr,
00312                             "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
00313                             code, free_ent, n_bits, insize);
00314                     fprintf(stderr,
00315                             "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
00316                             posbits, p[-1],p[0],p[1],p[2],p[3]);
00317 #endif
00318                     if (!test && outpos > 0) {
00319                         write_buf(out, (char*)outbuf, outpos);
00320                         bytes_out += (off_t)outpos;
00321                     }
00322                     gzip_error (to_stdout
00323                                 ? "corrupt input."
00324                                 : "corrupt input. Use zcat to recover some data.");
00325                 }
00326                 *--stackp = (char_type)finchar;
00327                 code = oldcode;
00328             }
00329 
00330             while ((cmp_code_int)code >= (cmp_code_int)256) {
00331                 /* Generate output characters in reverse order */
00332                 *--stackp = tab_suffixof(code);
00333                 code = tab_prefixof(code);
00334             }
00335             *--stackp = (char_type)(finchar = tab_suffixof(code));
00336 
00337             /* And put them out in forward order */
00338             {
00339                 REG1 int        i;
00340 
00341                 if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
00342                     do {
00343                         if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
00344 
00345                         if (i > 0) {
00346                             memcpy(outbuf+outpos, stackp, i);
00347                             outpos += i;
00348                         }
00349                         if (outpos >= OUTBUFSIZ) {
00350                             if (!test) {
00351                                 write_buf(out, (char*)outbuf, outpos);
00352                                 bytes_out += (off_t)outpos;
00353                             }
00354                             outpos = 0;
00355                         }
00356                         stackp+= i;
00357                     } while ((i = (de_stack-stackp)) > 0);
00358                 } else {
00359                     memcpy(outbuf+outpos, stackp, i);
00360                     outpos += i;
00361                 }
00362             }
00363 
00364             if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
00365 
00366                 tab_prefixof(code) = (unsigned short)oldcode;
00367                 tab_suffixof(code) = (char_type)finchar;
00368                 free_ent = code+1;
00369             }
00370             oldcode = incode;   /* Remember previous code.      */
00371         }
00372     } while (rsize != 0);
00373 
00374     if (!test && outpos > 0) {
00375         write_buf(out, (char*)outbuf, outpos);
00376         bytes_out += (off_t)outpos;
00377     }
00378     return OK;
00379 }


Variable Documentation

int block_mode = BLOCK_MODE
 

Definition at line 180 of file unlzw.c.

Referenced by unlzw().


© sourcejam.com 2005-2008