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

unlzh.c File Reference

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

Go to the source code of this file.

Defines

#define DICBIT   13
#define DICSIZ   ((unsigned) 1 << DICBIT)
#define CHAR_BIT   8
#define UCHAR_MAX   255
#define BITBUFSIZ   (CHAR_BIT * 2 * sizeof(char))
#define MAXMATCH   256
#define THRESHOLD   3
#define NC   (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
#define CBIT   9
#define CODE_BIT   16
#define NP   (DICBIT + 1)
#define NT   (CODE_BIT + 3)
#define PBIT   4
#define TBIT   5
#define NPT   (1 << TBIT)
#define left   prev
#define right   head
#define c_len   outbuf
#define c_table   d_buf

Functions

local unsigned decode OF ((unsigned count, uch buffer[]))
local void fillbuf (int n)
local unsigned getbits (int n)
local void init_getbits ()
local void make_table (int nchar, bitlen, int tablebits, table)
local void read_pt_len (int nn, int nbit, int i_special)
local void read_c_len ()
local unsigned decode_c ()
local unsigned decode_p ()
local void huf_decode_start ()
local void decode_start ()
local unsigned decode (unsigned count, buffer)
int unlzh (int in, int out)

Variables

local uch pt_len [NPT]
local unsigned blocksize
local ush pt_table [256]
local ush bitbuf
local unsigned subbitbuf
local int bitcount
local int j
local int done


Define Documentation

#define BITBUFSIZ   (CHAR_BIT * 2 * sizeof(char))
 

Definition at line 51 of file unlzh.c.

Referenced by decode_c(), decode_p(), getbits(), init_getbits(), read_c_len(), and read_pt_len().

#define c_len   outbuf
 

Definition at line 83 of file unlzh.c.

Referenced by decode_c(), and read_c_len().

#define c_table   d_buf
 

Definition at line 93 of file unlzh.c.

Referenced by decode_c(), and read_c_len().

#define CBIT   9
 

Definition at line 65 of file unlzh.c.

Referenced by read_c_len().

#define CHAR_BIT   8
 

Definition at line 44 of file unlzh.c.

#define CODE_BIT   16
 

Definition at line 66 of file unlzh.c.

#define DICBIT   13
 

Definition at line 40 of file unlzh.c.

#define DICSIZ   ((unsigned) 1 << DICBIT)
 

Definition at line 41 of file unlzh.c.

Referenced by decode(), and unlzh().

#define left   prev
 

Definition at line 76 of file unlzh.c.

Referenced by decode_c(), decode_p(), make_table(), and read_c_len().

#define MAXMATCH   256
 

Definition at line 58 of file unlzh.c.

#define NC   (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
 

Definition at line 63 of file unlzh.c.

Referenced by decode(), decode_c(), and read_c_len().

#define NP   (DICBIT + 1)
 

Definition at line 68 of file unlzh.c.

Referenced by decode_c(), and decode_p().

#define NPT   (1 << TBIT)
 

Definition at line 72 of file unlzh.c.

#define NT   (CODE_BIT + 3)
 

Definition at line 69 of file unlzh.c.

Referenced by decode_c(), and read_c_len().

#define PBIT   4
 

Definition at line 70 of file unlzh.c.

Referenced by decode_c().

#define right   head
 

Definition at line 77 of file unlzh.c.

Referenced by decode_c(), decode_p(), make_table(), and read_c_len().

#define TBIT   5
 

Definition at line 71 of file unlzh.c.

Referenced by decode_c().

#define THRESHOLD   3
 

Definition at line 59 of file unlzh.c.

Referenced by decode().

#define UCHAR_MAX   255
 

Definition at line 48 of file unlzh.c.

Referenced by decode().


Function Documentation

local unsigned decode unsigned  count,
buffer 
 

Definition at line 340 of file unlzh.c.

References decode_c(), decode_p(), DICSIZ, local, NC, THRESHOLD, and UCHAR_MAX.

Referenced by unlzh().

00351 {
00352     local unsigned i;
00353     unsigned r, c;
00354 
00355     r = 0;
00356     while (--j >= 0) {
00357         buffer[r] = buffer[i];
00358         i = (i + 1) & (DICSIZ - 1);
00359         if (++r == count) return r;
00360     }
00361     for ( ; ; ) {
00362         c = decode_c();
00363         if (c == NC) {
00364             done = 1;
00365             return r;
00366         }
00367         if (c <= UCHAR_MAX) {
00368             buffer[r] = c;
00369             if (++r == count) return r;
00370         } else {
00371             j = c - (UCHAR_MAX + 1 - THRESHOLD);
00372             i = (r - decode_p() - 1) & (DICSIZ - 1);
00373             while (--j >= 0) {
00374                 buffer[r] = buffer[i];
00375                 i = (i + 1) & (DICSIZ - 1);
00376                 if (++r == count) return r;
00377             }
00378         }
00379     }
00380 }

local unsigned decode_c  ) 
 

Definition at line 274 of file unlzh.c.

References BITBUFSIZ, c_len, c_table, fillbuf(), getbits(), j, left, NC, NP, NT, PBIT, read_c_len(), read_pt_len(), right, and TBIT.

Referenced by decode().

00275 {
00276     unsigned j, mask;
00277 
00278     if (blocksize == 0) {
00279         blocksize = getbits(16);
00280         if (blocksize == 0) {
00281             return NC; /* end of file */
00282         }
00283         read_pt_len(NT, TBIT, 3);
00284         read_c_len();
00285         read_pt_len(NP, PBIT, -1);
00286     }
00287     blocksize--;
00288     j = c_table[bitbuf >> (BITBUFSIZ - 12)];
00289     if (j >= NC) {
00290         mask = (unsigned) 1 << (BITBUFSIZ - 1 - 12);
00291         do {
00292             if (bitbuf & mask) j = right[j];
00293             else               j = left [j];
00294             mask >>= 1;
00295         } while (j >= NC);
00296     }
00297     fillbuf((int) c_len[j]);
00298     return j;
00299 }

local unsigned decode_p  ) 
 

Definition at line 301 of file unlzh.c.

References BITBUFSIZ, fillbuf(), getbits(), j, left, NP, and right.

Referenced by decode().

00302 {
00303     unsigned j, mask;
00304 
00305     j = pt_table[bitbuf >> (BITBUFSIZ - 8)];
00306     if (j >= NP) {
00307         mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
00308         do {
00309             if (bitbuf & mask) j = right[j];
00310             else               j = left [j];
00311             mask >>= 1;
00312         } while (j >= NP);
00313     }
00314     fillbuf((int) pt_len[j]);
00315     if (j != 0) j = ((unsigned) 1 << (j - 1)) + getbits((int) (j - 1));
00316     return j;
00317 }

local void decode_start  ) 
 

Definition at line 331 of file unlzh.c.

References huf_decode_start().

Referenced by unlzh().

00332 {
00333     huf_decode_start();
00334     j = 0;
00335     done = 0;
00336 }

local void fillbuf int  n  ) 
 

Definition at line 106 of file unlzh.c.

References CHAR_BIT, and try_byte.

Referenced by decode_c(), decode_p(), getbits(), init_getbits(), read_c_len(), and read_pt_len().

00108 {
00109     bitbuf <<= n;
00110     while (n > bitcount) {
00111         bitbuf |= subbitbuf << (n -= bitcount);
00112         subbitbuf = (unsigned)try_byte();
00113         if ((int)subbitbuf == EOF) subbitbuf = 0;
00114         bitcount = CHAR_BIT;
00115     }
00116     bitbuf |= subbitbuf >> (bitcount -= n);
00117 }

local unsigned getbits int  n  ) 
 

Definition at line 119 of file unlzh.c.

References BITBUFSIZ, and fillbuf().

Referenced by decode_c(), decode_p(), read_c_len(), and read_pt_len().

00121 {
00122     unsigned x;
00123 
00124     x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
00125     return x;
00126 }

local void huf_decode_start  ) 
 

Definition at line 319 of file unlzh.c.

References init_getbits().

Referenced by decode_start().

00320 {
00321     init_getbits();  blocksize = 0;
00322 }

local void init_getbits  ) 
 

Definition at line 128 of file unlzh.c.

References BITBUFSIZ, and fillbuf().

Referenced by huf_decode_start().

00129 {
00130     bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
00131     fillbuf(BITBUFSIZ);
00132 }

local void make_table int  nchar,
bitlen  ,
int  tablebits,
table 
 

Definition at line 138 of file unlzh.c.

References gzip_error(), left, and right.

Referenced by read_c_len(), and read_pt_len().

00143 {
00144     ush count[17], weight[17], start[18], *p;
00145     unsigned i, k, len, ch, jutbits, avail, nextcode, mask;
00146 
00147     for (i = 1; i <= 16; i++) count[i] = 0;
00148     for (i = 0; i < (unsigned)nchar; i++) count[bitlen[i]]++;
00149 
00150     start[1] = 0;
00151     for (i = 1; i <= 16; i++)
00152         start[i + 1] = start[i] + (count[i] << (16 - i));
00153     if ((start[17] & 0xffff) != 0)
00154       gzip_error ("Bad table\n");
00155 
00156     jutbits = 16 - tablebits;
00157     for (i = 1; i <= (unsigned)tablebits; i++) {
00158         start[i] >>= jutbits;
00159         weight[i] = (unsigned) 1 << (tablebits - i);
00160     }
00161     while (i <= 16) {
00162         weight[i] = (unsigned) 1 << (16 - i);
00163         i++;
00164     }
00165 
00166     i = start[tablebits + 1] >> jutbits;
00167     if (i != 0) {
00168         k = 1 << tablebits;
00169         while (i != k) table[i++] = 0;
00170     }
00171 
00172     avail = nchar;
00173     mask = (unsigned) 1 << (15 - tablebits);
00174     for (ch = 0; ch < (unsigned)nchar; ch++) {
00175         if ((len = bitlen[ch]) == 0) continue;
00176         nextcode = start[len] + weight[len];
00177         if (len <= (unsigned)tablebits) {
00178             if ((unsigned) 1 << tablebits < nextcode)
00179               gzip_error ("Bad table\n");
00180             for (i = start[len]; i < nextcode; i++) table[i] = ch;
00181         } else {
00182             k = start[len];
00183             p = &table[k >> jutbits];
00184             i = len - tablebits;
00185             while (i != 0) {
00186                 if (*p == 0) {
00187                     right[avail] = left[avail] = 0;
00188                     *p = avail++;
00189                 }
00190                 if (k & mask) p = &right[*p];
00191                 else          p = &left[*p];
00192                 k <<= 1;  i--;
00193             }
00194             *p = ch;
00195         }
00196         start[len] = nextcode;
00197     }
00198 }

local unsigned decode OF (unsigned count, uch buffer[])   ) 
 

local void read_c_len  ) 
 

Definition at line 239 of file unlzh.c.

References BITBUFSIZ, c_len, c_table, CBIT, fillbuf(), getbits(), left, make_table(), NC, NT, and right.

Referenced by decode_c().

00240 {
00241     int i, c, n;
00242     unsigned mask;
00243 
00244     n = getbits(CBIT);
00245     if (n == 0) {
00246         c = getbits(CBIT);
00247         for (i = 0; i < NC; i++) c_len[i] = 0;
00248         for (i = 0; i < 4096; i++) c_table[i] = c;
00249     } else {
00250         i = 0;
00251         while (i < n) {
00252             c = pt_table[bitbuf >> (BITBUFSIZ - 8)];
00253             if (c >= NT) {
00254                 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
00255                 do {
00256                     if (bitbuf & mask) c = right[c];
00257                     else               c = left [c];
00258                     mask >>= 1;
00259                 } while (c >= NT);
00260             }
00261             fillbuf((int) pt_len[c]);
00262             if (c <= 2) {
00263                 if      (c == 0) c = 1;
00264                 else if (c == 1) c = getbits(4) + 3;
00265                 else             c = getbits(CBIT) + 20;
00266                 while (--c >= 0) c_len[i++] = 0;
00267             } else c_len[i++] = c - 2;
00268         }
00269         while (i < NC) c_len[i++] = 0;
00270         make_table(NC, c_len, 12, c_table);
00271     }
00272 }

local void read_pt_len int  nn,
int  nbit,
int  i_special
 

Definition at line 204 of file unlzh.c.

References BITBUFSIZ, fillbuf(), getbits(), gzip_error(), and make_table().

Referenced by decode_c().

00208 {
00209     int i, c, n;
00210     unsigned mask;
00211 
00212     n = getbits(nbit);
00213     if (n == 0) {
00214         c = getbits(nbit);
00215         for (i = 0; i < nn; i++) pt_len[i] = 0;
00216         for (i = 0; i < 256; i++) pt_table[i] = c;
00217     } else {
00218         i = 0;
00219         while (i < n) {
00220             c = bitbuf >> (BITBUFSIZ - 3);
00221             if (c == 7) {
00222                 mask = (unsigned) 1 << (BITBUFSIZ - 1 - 3);
00223                 while (mask & bitbuf) {  mask >>= 1;  c++;  }
00224                 if (16 < c)
00225                   gzip_error ("Bad table\n");
00226             }
00227             fillbuf((c < 7) ? 3 : c - 3);
00228             pt_len[i++] = c;
00229             if (i == i_special) {
00230                 c = getbits(2);
00231                 while (--c >= 0) pt_len[i++] = 0;
00232             }
00233         }
00234         while (i < nn) pt_len[i++] = 0;
00235         make_table(nn, pt_len, 8, pt_table);
00236     }
00237 }

int unlzh int  in,
int  out
 

Definition at line 386 of file unlzh.c.

References decode(), decode_start(), DICSIZ, ifd, ofd, OK, test, and write_buf().

Referenced by get_method().

00389 {
00390     unsigned n;
00391     ifd = in;
00392     ofd = out;
00393 
00394     decode_start();
00395     while (!done) {
00396         n = decode((unsigned) DICSIZ, window);
00397         if (!test && n > 0) {
00398             write_buf(out, (char*)window, n);
00399         }
00400     }
00401     return OK;
00402 }


Variable Documentation

local ush bitbuf
 

Definition at line 102 of file unlzh.c.

local int bitcount
 

Definition at line 104 of file unlzh.c.

local unsigned blocksize
 

Definition at line 89 of file unlzh.c.

local int done
 

Definition at line 329 of file unlzh.c.

local int j
 

Definition at line 328 of file unlzh.c.

Referenced by decode_c(), decode_p(), huft_build(), inflate_dynamic(), lm_init(), and pqdownheap().

local uch pt_len[NPT]
 

Definition at line 88 of file unlzh.c.

local ush pt_table[256]
 

Definition at line 90 of file unlzh.c.

local unsigned subbitbuf
 

Definition at line 103 of file unlzh.c.


© sourcejam.com 2005-2008