2
* $Id: decode.c,v 1.3 2003/04/12 16:15:59 andrew_belov Exp $
3
* ---------------------------------------------------------------------------
4
* The data decompression procedures are located in this module.
12
DEBUGHDR(__FILE__) /* Debug information block */
14
/* Delays for errors with garbled files */
17
#define BADTABLE_G_DELAY 2
19
#define BADTABLE_G_DELAY 5
24
static jmp_buf decode_proc; /* Jump buffer for decoding procedure */
26
#if SFX_LEVEL>=ARJSFXV
27
unsigned short FAR *c_table;
28
unsigned short FAR *pt_table;
30
unsigned short c_table[CTABLESIZE];
31
unsigned short pt_table[PTABLESIZE];
36
/* Fills the input buffer */
46
bitbuf=(bitbuf<<bitcount)|((unsigned int)byte_buf>>(8-bitcount));
53
/* This slows the things down quite a lot so we won't put this in the
54
release version (despite of what ARJ Software Inc. does in v 3.04!) */
56
errno=0; /* ASR fix 11/10/2000 -- POSIX/MS C */
59
msg_cprintf(0, M_DECODE_CRIT_ERROR);
61
msg_cprintf(0, M_DECODE_EOF);
62
byte_buf=(unsigned char)bbrc;
64
byte_buf=(unsigned char)fgetc(aistream);
67
else /* ASR improvement for RAM-to-RAM */
69
byte_buf=*packblock_ptr++;
73
garble_decode(&byte_buf, 1);
80
bitbuf=(bitbuf<<n)|(byte_buf>>(8-n));
84
/* Reads a series of bits into the input buffer */
86
static int getbits(int n)
90
rc=bitbuf>>(CODE_BIT-n);
95
/* Creates a table for decoding */
97
#if SFX_LEVEL>=ARJSFXV
98
static void NEAR make_table(int nchar, unsigned char *bitlen, int tablebits, unsigned short FAR *table, int tablesize)
100
static void NEAR make_table(int nchar, unsigned char *bitlen, int tablebits, unsigned short *table, int tablesize)
103
unsigned short count[17], weight[17], start[18];
104
#if SFX_LEVEL>=ARJSFXV
105
unsigned short FAR *p;
109
unsigned int i, k, len, ch, jutbits, avail, nextcode, mask;
113
for(i=0; (int)i<nchar; i++)
117
start[i+1]=start[i]+(count[i]<<(16-i));
118
if(start[17]!=(unsigned short)(1<<16))
122
arj_delay(BADTABLE_G_DELAY);
123
#if SFX_LEVEL>=ARJSFXV
124
msg_cprintf(H_ERR, M_BADTABLE_G);
130
#if SFX_LEVEL>=ARJSFXV
131
msg_cprintf(H_ERR, M_BADTABLE);
135
#if SFX_LEVEL>=ARJSFXV
136
longjmp(decode_proc, 1);
139
jutbits=16-tablebits;
140
for(i=1; (int)i<=tablebits; i++)
143
weight[i]=1<<(tablebits-i);
150
i=start[tablebits+1]>>jutbits;
151
if(i!=(unsigned short)(1<<16))
158
mask=1<<(15-tablebits);
159
for(ch=0; (int)ch<nchar; ch++)
161
if((len=bitlen[ch])!=0)
164
nextcode=k+weight[len];
165
if((int)len<=tablebits)
167
if(nextcode>(unsigned int)tablesize)
171
arj_delay(BADTABLE_G_DELAY);
172
#if SFX_LEVEL>=ARJSFXV
173
msg_cprintf(H_ERR, M_BADTABLE_G);
179
#if SFX_LEVEL>=ARJSFXV
180
msg_cprintf(H_ERR, M_BADTABLE);
184
longjmp(decode_proc, 1);
186
for(i=start[len]; i<nextcode; i++)
188
stop_optimizer(); /* VisualAge C++ v 3.65 fix */
194
p=&table[k>>jutbits];
200
right[avail]=left[avail]=0;
218
/* Reads length of data pending */
220
void read_pt_len(int nn, int nbit, int i_special)
229
c=(short)getbits(nbit);
232
for(i=0; i<PTABLESIZE; i++)
238
/* ASR fix to prevent overrun -- 04/12/1999 */
253
fillbuf((c<7)?3:(int)(c-3));
254
pt_len[i++]=(unsigned char)c;
264
make_table(nn, pt_len, 8, pt_table, PTABLESIZE);
268
/* Reads a character table */
281
for(i=0; i<CTABLESIZE; i++)
289
c=pt_table[bitbuf>>8];
302
fillbuf((int)(pt_len[c]));
321
c_len[i++]=(unsigned char)(c-2);
325
make_table(NC, c_len, 12, c_table, CTABLESIZE);
329
/* Decodes a single character */
331
static unsigned short NEAR decode_c()
333
unsigned short j, mask;
337
blocksize=getbits(CODE_BIT);
338
read_pt_len(NT, TBIT, 3);
340
read_pt_len(NP, PBIT, -1);
343
j=c_table[bitbuf>>4];
360
/* Decodes a control character */
362
static unsigned short NEAR decode_p()
364
unsigned short j, mask;
366
j=pt_table[bitbuf>>8];
388
/* Initializes memory for decoding */
390
static void NEAR decode_start()
393
#if SFX_LEVEL>=ARJSFXV
394
if((c_table=farcalloc((unsigned long)CTABLESIZE, (unsigned long)sizeof(short)))==NULL)
395
error(M_OUT_OF_MEMORY);
396
if((pt_table=farcalloc((unsigned long)PTABLESIZE, (unsigned long)sizeof(short)))==NULL)
397
error(M_OUT_OF_MEMORY);
402
#if SFX_LEVEL>=ARJSFXV
404
/* Releases memory used for decoding */
406
static void NEAR decode_end()
415
/* Decodes the entire file */
417
void decode(int action)
424
#if SFX_LEVEL>=ARJSFXV
425
if(!setjmp(decode_proc))
428
#if SFX_LEVEL>=ARJSFXV
429
dec_text=malloc_msg(DICSIZ);
432
display_indicator(0L);
437
if((c=decode_c())<=UCHAR_MAX)
439
dec_text[r]=(unsigned char)c;
444
display_indicator(origsize-count);
445
if(extraction_stub(dec_text, DICSIZ, action))
451
j=c-(UCHAR_MAX+1-THRESHOLD);
452
count-=(unsigned long)j;
456
if(r>i&&r<DICSIZ-MAXMATCH-1)
459
dec_text[r++]=dec_text[i++];
465
dec_text[r]=dec_text[i];
469
display_indicator(origsize-count);
470
if(extraction_stub(dec_text, DICSIZ, action))
479
#if SFX_LEVEL>=ARJSFXV
483
extraction_stub(dec_text, r, action);
485
#if SFX_LEVEL>=ARJSFXV
493
/* Backward pointer decoding */
495
static short decode_ptr()
497
short c, width, plus, pwr;
501
for(width=9; width<13; width++)
515
/* Reference length decoding */
517
static short decode_len()
519
short c, width, plus, pwr;
523
for(width=0; width<7; width++)
537
/* Decodes the entire file, using method 4 */
539
void decode_f(int action)
545
static unsigned long ncount;
548
ntext=malloc_msg(FDICSIZ);
550
display_indicator(0L);
553
while(ncount<origsize)
559
ntext[r]=(unsigned char)(bitbuf>>8);
564
display_indicator(ncount);
565
if(extraction_stub(ntext, FDICSIZ, action))
572
ncount+=(unsigned long)j;
573
if((i=r-decode_ptr()-1)<0)
581
display_indicator(ncount);
582
if(extraction_stub(ntext, FDICSIZ, action))
591
extraction_stub(ntext, r, action);
594
/* ASR fix - otherwise destroy it in final_cleanup() -- 15/08/2001 */