~ubuntu-branches/ubuntu/hardy/clamav/hardy

« back to all changes in this revision

Viewing changes to libclamunrar/unrar.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-03-08 19:29:19 UTC
  • mfrom: (0.27.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080308192919-ldi2nlmr5rgt4q5l
Tags: 0.92.1~dfsg2-0.1
* Non-maintainer upload.
* Remove non-free unrar files and repack orig.tar.gz (Closes: #470073)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Extract RAR archives
3
 
 *
4
 
 *  Copyright (C) 2005-2006 trog@uncon.org
5
 
 *
6
 
 *  This code is based on the work of Alexander L. Roshal (C)
7
 
 *
8
 
 *  The unRAR sources may be used in any software to handle RAR
9
 
 *  archives without limitations free of charge, but cannot be used
10
 
 *  to re-create the RAR compression algorithm, which is proprietary.
11
 
 *  Distribution of modified unRAR sources in separate form or as a
12
 
 *  part of other software is permitted, provided that it is clearly
13
 
 *  stated in the documentation and source comments that the code may
14
 
 *  not be used to develop a RAR (WinRAR) compatible archiver.
15
 
 *
16
 
 */
17
 
 
18
 
#include <stdio.h>
19
 
#include <sys/types.h>
20
 
#include <sys/stat.h>
21
 
#include <fcntl.h>
22
 
#ifdef HAVE_UNISTD_H
23
 
#include <unistd.h>
24
 
#endif
25
 
#include <errno.h>
26
 
#include <stdlib.h>
27
 
#include <string.h>
28
 
 
29
 
#include "libclamunrar/unrar.h"
30
 
#include "libclamunrar/unrarppm.h"
31
 
#include "libclamunrar/unrarvm.h"
32
 
#include "libclamunrar/unrarfilter.h"
33
 
#include "libclamunrar/unrar20.h"
34
 
#include "libclamunrar/unrar15.h"
35
 
 
36
 
#define int64to32(x) ((unsigned int)(x))
37
 
 
38
 
#ifdef RAR_HIGH_DEBUG
39
 
#define rar_dbgmsg printf
40
 
#else
41
 
static void rar_dbgmsg(const char* fmt,...){}
42
 
#endif
43
 
 
44
 
static void dump_tables(unpack_data_t *unpack_data)
45
 
{
46
 
        int i;
47
 
        
48
 
        /* Dump LD table */
49
 
        rar_dbgmsg("LD Table MaxNum=%d\n", unpack_data->LD.MaxNum);
50
 
        rar_dbgmsg("\tDecodeLen:");
51
 
        for (i=0 ; i < 16; i++) {
52
 
                rar_dbgmsg(" %.8d", unpack_data->LD.DecodeLen[i]);
53
 
        }
54
 
        rar_dbgmsg("\n\tDecodePos:");
55
 
        for (i=0 ; i < 16; i++) {
56
 
                rar_dbgmsg(" %.8d", unpack_data->LD.DecodePos[i]);
57
 
        }
58
 
        rar_dbgmsg("\n\tDecodeNum:");
59
 
        for (i=0 ; i < NC; i++) {
60
 
                rar_dbgmsg(" %.8d", unpack_data->LD.DecodeNum[i]);
61
 
        }
62
 
        
63
 
        
64
 
        rar_dbgmsg("\nDD Table MaxNum=%d\n", unpack_data->DD.MaxNum);
65
 
        rar_dbgmsg("\tDecodeLen:");
66
 
        for (i=0 ; i < 16; i++) {
67
 
                rar_dbgmsg(" %.8d", unpack_data->DD.DecodeLen[i]);
68
 
        }
69
 
        rar_dbgmsg("\n\tDecodePos:");
70
 
        for (i=0 ; i < 16; i++) {
71
 
                rar_dbgmsg(" %.8d", unpack_data->DD.DecodePos[i]);
72
 
        }
73
 
        rar_dbgmsg("\n\tDecodeNum:");
74
 
        for (i=0 ; i < DC; i++) {
75
 
                rar_dbgmsg(" %.8d", unpack_data->DD.DecodeNum[i]);
76
 
        }
77
 
        
78
 
        rar_dbgmsg("\nLDD Table MaxNum=%d\n", unpack_data->LDD.MaxNum);
79
 
        rar_dbgmsg("\tDecodeLen:");
80
 
        for (i=0 ; i < 16; i++) {
81
 
                rar_dbgmsg(" %.8d", unpack_data->LDD.DecodeLen[i]);
82
 
        }
83
 
        rar_dbgmsg("\n\tDecodePos:");
84
 
        for (i=0 ; i < 16; i++) {
85
 
                rar_dbgmsg(" %.8d", unpack_data->LDD.DecodePos[i]);
86
 
        }
87
 
        rar_dbgmsg("\n\tDecodeNum:");
88
 
        for (i=0 ; i < LDC; i++) {
89
 
                rar_dbgmsg(" %.8d", unpack_data->LDD.DecodeNum[i]);
90
 
        }
91
 
        
92
 
        rar_dbgmsg("\nRD Table MaxNum=%d\n", unpack_data->RD.MaxNum);
93
 
        rar_dbgmsg("\tDecodeLen:");
94
 
        for (i=0 ; i < 16; i++) {
95
 
                rar_dbgmsg(" %.8d", unpack_data->RD.DecodeLen[i]);
96
 
        }
97
 
        rar_dbgmsg("\n\tDecodePos:");
98
 
        for (i=0 ; i < 16; i++) {
99
 
                rar_dbgmsg(" %.8d", unpack_data->RD.DecodePos[i]);
100
 
        }
101
 
        rar_dbgmsg("\n\tDecodeNum:");
102
 
        for (i=0 ; i < RC; i++) {
103
 
                rar_dbgmsg(" %.8d", unpack_data->RD.DecodeNum[i]);
104
 
        }
105
 
        
106
 
        rar_dbgmsg("\nBD Table MaxNum=%d\n", unpack_data->BD.MaxNum);
107
 
        rar_dbgmsg("\tDecodeLen:");
108
 
        for (i=0 ; i < 16; i++) {
109
 
                rar_dbgmsg(" %.8d", unpack_data->BD.DecodeLen[i]);
110
 
        }
111
 
        rar_dbgmsg("\n\tDecodePos:");
112
 
        for (i=0 ; i < 16; i++) {
113
 
                rar_dbgmsg(" %.8d", unpack_data->BD.DecodePos[i]);
114
 
        }
115
 
        rar_dbgmsg("\n\tDecodeNum:");
116
 
        for (i=0 ; i < BC; i++) {
117
 
                rar_dbgmsg(" %.8d", unpack_data->BD.DecodeNum[i]);
118
 
        }
119
 
        rar_dbgmsg("\n");
120
 
}
121
 
 
122
 
static void insert_old_dist(unpack_data_t *unpack_data, unsigned int distance)
123
 
{
124
 
        unpack_data->old_dist[3] = unpack_data->old_dist[2];
125
 
        unpack_data->old_dist[2] = unpack_data->old_dist[1];
126
 
        unpack_data->old_dist[1] = unpack_data->old_dist[0];
127
 
        unpack_data->old_dist[0] = distance;
128
 
}
129
 
 
130
 
static void insert_last_match(unpack_data_t *unpack_data, unsigned int length, unsigned int distance)
131
 
{
132
 
        unpack_data->last_dist = distance;
133
 
        unpack_data->last_length = length;
134
 
}
135
 
 
136
 
static void copy_string(unpack_data_t *unpack_data, unsigned int length, unsigned int distance)
137
 
{
138
 
        unsigned int dest_ptr;
139
 
        
140
 
        dest_ptr = unpack_data->unp_ptr - distance;
141
 
        if (dest_ptr < MAXWINSIZE-260 && unpack_data->unp_ptr < MAXWINSIZE - 260) {
142
 
                unpack_data->window[unpack_data->unp_ptr++] = unpack_data->window[dest_ptr++];
143
 
                while (--length > 0) {
144
 
                        unpack_data->window[unpack_data->unp_ptr++] = unpack_data->window[dest_ptr++];
145
 
                }
146
 
        } else {
147
 
                while (length--) {
148
 
                        unpack_data->window[unpack_data->unp_ptr] =
149
 
                                                unpack_data->window[dest_ptr++ & MAXWINMASK];
150
 
                        unpack_data->unp_ptr = (unpack_data->unp_ptr + 1) & MAXWINMASK;
151
 
                }
152
 
        }
153
 
}
154
 
 
155
 
void rar_addbits(unpack_data_t *unpack_data, int bits)
156
 
{
157
 
 
158
 
        /*rar_dbgmsg("rar_addbits: in_addr=%d in_bit=%d\n", unpack_data->in_addr, unpack_data->in_bit);*/
159
 
        bits += unpack_data->in_bit;
160
 
        unpack_data->in_addr += bits >> 3;
161
 
        unpack_data->in_bit = bits & 7;
162
 
}
163
 
 
164
 
unsigned int rar_getbits(unpack_data_t *unpack_data)
165
 
{
166
 
        unsigned int bit_field;
167
 
 
168
 
        /*rar_dbgmsg("rar_getbits: in_addr=%d in_bit=%d\n", unpack_data->in_addr, unpack_data->in_bit);*/
169
 
        bit_field = (unsigned int) unpack_data->in_buf[unpack_data->in_addr] << 16;
170
 
        bit_field |= (unsigned int) unpack_data->in_buf[unpack_data->in_addr+1] << 8;
171
 
        bit_field |= (unsigned int) unpack_data->in_buf[unpack_data->in_addr+2];
172
 
        bit_field >>= (8-unpack_data->in_bit);
173
 
        /*rar_dbgmsg("rar_getbits return(%d)\n", BitField & 0xffff);*/
174
 
        return(bit_field & 0xffff);
175
 
}
176
 
 
177
 
int rar_unp_read_buf(int fd, unpack_data_t *unpack_data)
178
 
{
179
 
        int data_size, retval;
180
 
        unsigned int read_size;
181
 
 
182
 
        data_size = unpack_data->read_top - unpack_data->in_addr;
183
 
        if (data_size < 0) {
184
 
                return FALSE;
185
 
        }
186
 
        
187
 
        /* Is buffer read pos more than half way? */
188
 
        if (unpack_data->in_addr > MAX_BUF_SIZE/2) {
189
 
                if (data_size > 0) {
190
 
                        memmove(unpack_data->in_buf, unpack_data->in_buf+unpack_data->in_addr,
191
 
                                        data_size);
192
 
                }
193
 
                unpack_data->in_addr = 0;
194
 
                unpack_data->read_top = data_size;
195
 
        } else {
196
 
                data_size = unpack_data->read_top;
197
 
        }
198
 
        /* RAR2 depends on us only reading upto the end of the current compressed file */
199
 
        if (unpack_data->pack_size < ((MAX_BUF_SIZE-data_size)&~0xf)) {
200
 
                read_size = unpack_data->pack_size;
201
 
        } else {
202
 
                read_size = (MAX_BUF_SIZE-data_size)&~0xf;
203
 
        }
204
 
        retval = read(fd, unpack_data->in_buf+data_size, read_size);    
205
 
        if (retval > 0) {
206
 
                unpack_data->read_top += retval;
207
 
                unpack_data->pack_size -= retval;
208
 
        }
209
 
        unpack_data->read_border = unpack_data->read_top - 30;
210
 
        if(unpack_data->read_border < unpack_data->in_addr) {
211
 
                const ssize_t fill = ((unpack_data->read_top + 30) < MAX_BUF_SIZE) ? 30 : (MAX_BUF_SIZE - unpack_data->read_top);
212
 
                if(fill)
213
 
                        memset(unpack_data->in_buf + unpack_data->read_top, 0, fill);
214
 
        }
215
 
        return (retval!=-1);
216
 
}
217
 
 
218
 
unsigned int rar_get_char(int fd, unpack_data_t *unpack_data)
219
 
{
220
 
        if (unpack_data->in_addr > MAX_BUF_SIZE-30) {
221
 
                if (!rar_unp_read_buf(fd, unpack_data)) {
222
 
                        rar_dbgmsg("rar_get_char: rar_unp_read_buf FAILED\n"); /* FIXME: cli_errmsg */
223
 
                        return -1;
224
 
                }
225
 
        }
226
 
        rar_dbgmsg("rar_get_char = %u\n", unpack_data->in_buf[unpack_data->in_addr]);
227
 
        return(unpack_data->in_buf[unpack_data->in_addr++]);
228
 
}
229
 
 
230
 
static void unp_write_data(unpack_data_t *unpack_data, uint8_t *data, int size)
231
 
{
232
 
        rar_dbgmsg("in unp_write_data length=%d\n", size);
233
 
        write(unpack_data->ofd, data, size);
234
 
        unpack_data->written_size += size;
235
 
        unpack_data->unp_crc = rar_crc(unpack_data->unp_crc, data, size);
236
 
}
237
 
 
238
 
static void unp_write_area(unpack_data_t *unpack_data, unsigned int start_ptr, unsigned int end_ptr)
239
 
{
240
 
        if (end_ptr < start_ptr) {
241
 
                unp_write_data(unpack_data, &unpack_data->window[start_ptr], -start_ptr & MAXWINMASK);
242
 
                unp_write_data(unpack_data, unpack_data->window, end_ptr);
243
 
        } else {
244
 
                unp_write_data(unpack_data, &unpack_data->window[start_ptr], end_ptr-start_ptr);
245
 
        }
246
 
}
247
 
 
248
 
void rar_unp_write_buf_old(unpack_data_t *unpack_data)
249
 
{
250
 
        rar_dbgmsg("in rar_unp_write_buf_old\n");
251
 
        if (unpack_data->unp_ptr < unpack_data->wr_ptr) {
252
 
                unp_write_data(unpack_data, &unpack_data->window[unpack_data->wr_ptr],
253
 
                                -unpack_data->wr_ptr & MAXWINMASK);
254
 
                unp_write_data(unpack_data, unpack_data->window, unpack_data->unp_ptr);
255
 
        } else {
256
 
                unp_write_data(unpack_data, &unpack_data->window[unpack_data->wr_ptr],
257
 
                                unpack_data->unp_ptr - unpack_data->wr_ptr);
258
 
        }
259
 
        unpack_data->wr_ptr = unpack_data->unp_ptr;
260
 
}
261
 
 
262
 
static void execute_code(unpack_data_t *unpack_data, struct rarvm_prepared_program *prg)
263
 
{
264
 
        rar_dbgmsg("in execute_code\n");
265
 
        rar_dbgmsg("global_size: %ld\n", prg->global_size);
266
 
        if (prg->global_size > 0) {
267
 
                prg->init_r[6] = int64to32(unpack_data->written_size);
268
 
                rarvm_set_value(FALSE, (unsigned int *)&prg->global_data[0x24],
269
 
                                int64to32(unpack_data->written_size));
270
 
                rarvm_set_value(FALSE, (unsigned int *)&prg->global_data[0x28],
271
 
                                int64to32(unpack_data->written_size>>32));
272
 
                rarvm_execute(&unpack_data->rarvm_data, prg);
273
 
        }
274
 
}
275
 
 
276
 
                
277
 
static void unp_write_buf(unpack_data_t *unpack_data)
278
 
{
279
 
        unsigned int written_border, part_length, filtered_size;
280
 
        unsigned int write_size, block_start, block_length, block_end;
281
 
        struct UnpackFilter *flt, *next_filter;
282
 
        struct rarvm_prepared_program *prg, *next_prg;
283
 
        uint8_t *filtered_data;
284
 
        int i, j;
285
 
        
286
 
        rar_dbgmsg("in unp_write_buf\n");
287
 
        written_border = unpack_data->wr_ptr;
288
 
        write_size = (unpack_data->unp_ptr - written_border) & MAXWINMASK;
289
 
        for (i=0 ; i < unpack_data->PrgStack.num_items ; i++) {
290
 
                flt = unpack_data->PrgStack.array[i];
291
 
                if (flt == NULL) {
292
 
                        continue;
293
 
                }
294
 
                if (flt->next_window) {
295
 
                        flt->next_window = FALSE;
296
 
                        continue;
297
 
                }
298
 
                block_start = flt->block_start;
299
 
                block_length = flt->block_length;
300
 
                if (((block_start-written_border)&MAXWINMASK) < write_size) {
301
 
                        if (written_border != block_start) {
302
 
                                unp_write_area(unpack_data, written_border, block_start);
303
 
                                written_border = block_start;
304
 
                                write_size = (unpack_data->unp_ptr - written_border) & MAXWINMASK;
305
 
                        }
306
 
                        if (block_length <= write_size) {
307
 
                                block_end = (block_start + block_length) & MAXWINMASK;
308
 
                                if (block_start < block_end || block_end==0) {
309
 
                                        rarvm_set_memory(&unpack_data->rarvm_data, 0,
310
 
                                                        unpack_data->window+block_start, block_length);
311
 
                                } else {
312
 
                                        part_length = MAXWINMASK - block_start;
313
 
                                        rarvm_set_memory(&unpack_data->rarvm_data, 0,
314
 
                                                        unpack_data->window+block_start, part_length);
315
 
                                        rarvm_set_memory(&unpack_data->rarvm_data, part_length,
316
 
                                                        unpack_data->window, block_end);
317
 
                                }
318
 
                                prg = &flt->prg;
319
 
                                execute_code(unpack_data, prg);
320
 
                                
321
 
                                filtered_data = prg->filtered_data;
322
 
                                filtered_size = prg->filtered_data_size;
323
 
                                
324
 
                                rar_filter_delete(unpack_data->PrgStack.array[i]);
325
 
                                unpack_data->PrgStack.array[i] = NULL;
326
 
                                while (i+1 < unpack_data->PrgStack.num_items) {
327
 
                                        next_filter = unpack_data->PrgStack.array[i+1];
328
 
                                        if (next_filter==NULL ||
329
 
                                                        next_filter->block_start!=block_start ||
330
 
                                                        next_filter->block_length!=filtered_size ||
331
 
                                                        next_filter->next_window) {
332
 
                                                break;
333
 
                                        }
334
 
                                        rarvm_set_memory(&unpack_data->rarvm_data, 0,
335
 
                                                        filtered_data, filtered_size);
336
 
                                        next_prg = &unpack_data->PrgStack.array[i+1]->prg;
337
 
                                        execute_code(unpack_data, next_prg);
338
 
                                        filtered_data = next_prg->filtered_data;
339
 
                                        filtered_size = next_prg->filtered_data_size;
340
 
                                        i++;
341
 
                                        rar_filter_delete(unpack_data->PrgStack.array[i]);
342
 
                                        unpack_data->PrgStack.array[i] = NULL;
343
 
                                }
344
 
                                unp_write_data(unpack_data, filtered_data, filtered_size);
345
 
                                written_border = block_end;
346
 
                                write_size = (unpack_data->unp_ptr - written_border) & MAXWINMASK;
347
 
                        } else {
348
 
                                for (j=i ; j < unpack_data->PrgStack.num_items ; j++) {
349
 
                                        flt = unpack_data->PrgStack.array[j];
350
 
                                        if (flt != NULL && flt->next_window) {
351
 
                                                flt->next_window = FALSE;
352
 
                                        }
353
 
                                }
354
 
                                unpack_data->wr_ptr = written_border;
355
 
                                return;
356
 
                                
357
 
                        }
358
 
                }
359
 
        }
360
 
        unp_write_area(unpack_data, written_border, unpack_data->unp_ptr);
361
 
        unpack_data->wr_ptr = unpack_data->unp_ptr;
362
 
}
363
 
 
364
 
void rar_make_decode_tables(unsigned char *len_tab, struct Decode *decode, int size)
365
 
{
366
 
        int len_count[16], tmp_pos[16], i;
367
 
        long m,n;
368
 
        
369
 
        memset(len_count, 0, sizeof(len_count));
370
 
        memset(decode->DecodeNum,0,size*sizeof(*decode->DecodeNum));
371
 
        for (i=0 ; i < size ; i++) {
372
 
                len_count[len_tab[i] & 0x0f]++;
373
 
        }
374
 
        
375
 
        len_count[0]=0;
376
 
        for (tmp_pos[0]=decode->DecodePos[0]=decode->DecodeLen[0]=0,n=0,i=1;i<16;i++) {
377
 
                n=2*(n+len_count[i]);
378
 
                m=n<<(15-i);
379
 
                if (m>0xFFFF) {
380
 
                        m=0xFFFF;
381
 
                }
382
 
                decode->DecodeLen[i]=(unsigned int)m;
383
 
                tmp_pos[i]=decode->DecodePos[i]=decode->DecodePos[i-1]+len_count[i-1];
384
 
        }
385
 
        
386
 
        for (i=0;i<size;i++) {
387
 
                if (len_tab[i]!=0) {
388
 
                        decode->DecodeNum[tmp_pos[len_tab[i] & 0x0f]++]=i;
389
 
                }
390
 
        }
391
 
        decode->MaxNum=size;
392
 
}
393
 
 
394
 
int rar_decode_number(unpack_data_t *unpack_data, struct Decode *decode)
395
 
{
396
 
        unsigned int bits, bit_field, n;
397
 
        
398
 
        bit_field = rar_getbits(unpack_data) & 0xfffe;
399
 
        rar_dbgmsg("rar_decode_number BitField=%u\n", bit_field);
400
 
        if (bit_field < decode->DecodeLen[8])
401
 
                if (bit_field < decode->DecodeLen[4])
402
 
                        if (bit_field < decode->DecodeLen[2])
403
 
                                if (bit_field < decode->DecodeLen[1])
404
 
                                        bits=1;
405
 
                                else
406
 
                                        bits=2;
407
 
                        else
408
 
                                if (bit_field < decode->DecodeLen[3])
409
 
                                        bits=3;
410
 
                                else
411
 
                                        bits=4;
412
 
                else
413
 
                        if (bit_field < decode->DecodeLen[6])
414
 
                                if (bit_field < decode->DecodeLen[5])
415
 
                                        bits=5;
416
 
                                else
417
 
                                        bits=6;
418
 
                        else
419
 
                                if (bit_field < decode->DecodeLen[7])
420
 
                                        bits=7;
421
 
                                else
422
 
                                        bits=8;
423
 
        else
424
 
                if (bit_field < decode->DecodeLen[12])
425
 
                        if (bit_field < decode->DecodeLen[10])
426
 
                                if (bit_field < decode->DecodeLen[9])
427
 
                                        bits=9;
428
 
                                else
429
 
                                        bits=10;
430
 
                        else
431
 
                                if (bit_field < decode->DecodeLen[11])
432
 
                                        bits=11;
433
 
                                else
434
 
                                        bits=12;
435
 
                else
436
 
                        if (bit_field < decode->DecodeLen[14])
437
 
                                if (bit_field < decode->DecodeLen[13])
438
 
                                        bits=13;
439
 
                                else
440
 
                                        bits=14;
441
 
                        else
442
 
                                bits=15;
443
 
 
444
 
        rar_dbgmsg("rar_decode_number: bits=%d\n", bits);
445
 
 
446
 
        rar_addbits(unpack_data, bits);
447
 
        n=decode->DecodePos[bits]+((bit_field-decode->DecodeLen[bits-1])>>(16-bits));
448
 
        if (n >= decode->MaxNum) {
449
 
                n=0;
450
 
        }
451
 
        /*rar_dbgmsg("rar_decode_number return(%d)\n", decode->DecodeNum[n]);*/
452
 
 
453
 
        return(decode->DecodeNum[n]);
454
 
}
455
 
 
456
 
static int read_tables(int fd, unpack_data_t *unpack_data)
457
 
{
458
 
        uint8_t bit_length[BC];
459
 
        unsigned char table[HUFF_TABLE_SIZE];
460
 
        unsigned int bit_field;
461
 
        int i, length, zero_count, number, n;
462
 
        const int table_size=HUFF_TABLE_SIZE;
463
 
        
464
 
        rar_dbgmsg("in read_tables Offset=%ld in_addr=%d read_top=%d\n", lseek(fd, 0, SEEK_CUR),
465
 
                                unpack_data->in_addr, unpack_data->read_top);
466
 
        if (unpack_data->in_addr > unpack_data->read_top-25) {
467
 
                if (!rar_unp_read_buf(fd, unpack_data)) {
468
 
                        rar_dbgmsg("ERROR: read_tables rar_unp_read_buf failed\n");
469
 
                        return FALSE;
470
 
                }
471
 
        }
472
 
        rar_addbits(unpack_data, (8-unpack_data->in_bit) & 7);
473
 
        bit_field = rar_getbits(unpack_data);
474
 
        rar_dbgmsg("BitField = 0x%x\n", bit_field);
475
 
        if (bit_field & 0x8000) {
476
 
                unpack_data->unp_block_type = BLOCK_PPM;
477
 
                rar_dbgmsg("Calling ppm_decode_init\n");
478
 
                if(!ppm_decode_init(&unpack_data->ppm_data, fd, unpack_data, &unpack_data->ppm_esc_char)) {
479
 
                    rar_dbgmsg("unrar: read_tables: ppm_decode_init failed\n");
480
 
                    return FALSE;
481
 
                }
482
 
                return(TRUE);
483
 
        }
484
 
        unpack_data->unp_block_type = BLOCK_LZ;
485
 
        unpack_data->prev_low_dist = 0;
486
 
        unpack_data->low_dist_rep_count = 0;
487
 
 
488
 
        if (!(bit_field & 0x4000)) {
489
 
                memset(unpack_data->unp_old_table, 0, sizeof(unpack_data->unp_old_table));
490
 
        }
491
 
        rar_addbits(unpack_data, 2);
492
 
        
493
 
        for (i=0 ; i < BC ; i++) {
494
 
                length = (uint8_t)(rar_getbits(unpack_data) >> 12);
495
 
                rar_addbits(unpack_data, 4);
496
 
                if (length == 15) {
497
 
                        zero_count = (uint8_t)(rar_getbits(unpack_data) >> 12);
498
 
                        rar_addbits(unpack_data, 4);
499
 
                        if (zero_count == 0) {
500
 
                                bit_length[i] = 15;
501
 
                        } else {
502
 
                                zero_count += 2;
503
 
                                while (zero_count-- > 0 &&
504
 
                                                i<sizeof(bit_length)/sizeof(bit_length[0])) {
505
 
                                        bit_length[i++]=0;
506
 
                                }
507
 
                                i--;
508
 
                        }
509
 
                } else {
510
 
                        bit_length[i] = length;
511
 
                }
512
 
        }
513
 
        rar_make_decode_tables(bit_length,(struct Decode *)&unpack_data->BD,BC);
514
 
        
515
 
        for (i=0;i<table_size;) {
516
 
                if (unpack_data->in_addr > unpack_data->read_top-5) {
517
 
                        if (!rar_unp_read_buf(fd, unpack_data)) {
518
 
                                rar_dbgmsg("ERROR: read_tables rar_unp_read_buf failed 2\n");
519
 
                                return FALSE;
520
 
                        }
521
 
                }
522
 
                number = rar_decode_number(unpack_data, (struct Decode *)&unpack_data->BD);
523
 
                if (number < 16) {
524
 
                        table[i] = (number+unpack_data->unp_old_table[i]) & 0xf;
525
 
                        i++;
526
 
                } else if (number < 18) {
527
 
                        if (number == 16) {
528
 
                                n = (rar_getbits(unpack_data) >> 13) + 3;
529
 
                                rar_addbits(unpack_data, 3);
530
 
                        } else {
531
 
                                n = (rar_getbits(unpack_data) >> 9) + 11;
532
 
                                rar_addbits(unpack_data, 7);
533
 
                        }
534
 
                        while (n-- > 0 && i < table_size) {
535
 
                                table[i] = table[i-1];
536
 
                                i++;
537
 
                        }
538
 
                } else {
539
 
                        if (number == 18) {
540
 
                                n = (rar_getbits(unpack_data) >> 13) + 3;
541
 
                                rar_addbits(unpack_data, 3);
542
 
                        } else {
543
 
                                n = (rar_getbits(unpack_data) >> 9) + 11;
544
 
                                rar_addbits(unpack_data, 7);
545
 
                        }
546
 
                        while (n-- > 0 && i < table_size) {
547
 
                                table[i++] = 0;
548
 
                        }
549
 
                }
550
 
        }
551
 
        unpack_data->tables_read = TRUE;
552
 
        if (unpack_data->in_addr > unpack_data->read_top) {
553
 
                rar_dbgmsg("ERROR: read_tables check failed\n");
554
 
                return FALSE;
555
 
        }
556
 
        rar_make_decode_tables(&table[0], (struct Decode *)&unpack_data->LD,NC);
557
 
        rar_make_decode_tables(&table[NC], (struct Decode *)&unpack_data->DD,DC);
558
 
        rar_make_decode_tables(&table[NC+DC], (struct Decode *)&unpack_data->LDD,LDC);
559
 
        rar_make_decode_tables(&table[NC+DC+LDC], (struct Decode *)&unpack_data->RD,RC);
560
 
        memcpy(unpack_data->unp_old_table,table,sizeof(unpack_data->unp_old_table));
561
 
        
562
 
 
563
 
        /*dump_tables(unpack_data);*/
564
 
        rar_dbgmsg("ReadTables finished\n");
565
 
        return TRUE;
566
 
}
567
 
 
568
 
static int read_end_of_block(int fd, unpack_data_t *unpack_data)
569
 
{
570
 
        unsigned int bit_field;
571
 
        int new_table, new_file=FALSE;
572
 
        
573
 
        bit_field = rar_getbits(unpack_data);
574
 
        if (bit_field & 0x8000) {
575
 
                new_table = TRUE;
576
 
                rar_addbits(unpack_data, 1);
577
 
        } else {
578
 
                new_file = TRUE;
579
 
                new_table = (bit_field & 0x4000);
580
 
                rar_addbits(unpack_data, 2);
581
 
        }
582
 
        unpack_data->tables_read = !new_table;
583
 
        rar_dbgmsg("NewFile=%d NewTable=%d TablesRead=%d\n", new_file,
584
 
                        new_table, unpack_data->tables_read);
585
 
        return !(new_file || (new_table && !read_tables(fd, unpack_data)));
586
 
}
587
 
 
588
 
void rar_init_filters(unpack_data_t *unpack_data)
589
 
{       
590
 
        if (unpack_data->old_filter_lengths) {
591
 
                free(unpack_data->old_filter_lengths);
592
 
                unpack_data->old_filter_lengths = NULL;
593
 
        }
594
 
        unpack_data->old_filter_lengths_size = 0;
595
 
        unpack_data->last_filter = 0;
596
 
        
597
 
        rar_filter_array_reset(&unpack_data->Filters);
598
 
        rar_filter_array_reset(&unpack_data->PrgStack);
599
 
}
600
 
 
601
 
static int add_vm_code(unpack_data_t *unpack_data, unsigned int first_byte,
602
 
                        unsigned char *vmcode, int code_size)
603
 
{
604
 
        rarvm_input_t rarvm_input;
605
 
        unsigned int filter_pos, new_filter, block_start, init_mask, cur_size;
606
 
        struct UnpackFilter *filter, *stack_filter;
607
 
        int i, empty_count, stack_pos, vm_codesize, static_size, data_size;
608
 
        unsigned char *vm_code, *global_data;
609
 
        
610
 
        rar_dbgmsg("in add_vm_code first_byte=0x%x code_size=%d\n", first_byte, code_size);
611
 
        rarvm_input.in_buf = vmcode;
612
 
        rarvm_input.buf_size = code_size;
613
 
        rarvm_input.in_addr = 0;
614
 
        rarvm_input.in_bit = 0;
615
 
 
616
 
        if (first_byte & 0x80) {
617
 
                filter_pos = rarvm_read_data(&rarvm_input);
618
 
                if (filter_pos == 0) {
619
 
                        rar_init_filters(unpack_data);
620
 
                } else {
621
 
                        filter_pos--;
622
 
                }
623
 
        } else {
624
 
                filter_pos = unpack_data->last_filter;
625
 
        }
626
 
        rar_dbgmsg("filter_pos = %u\n", filter_pos);
627
 
        if (filter_pos > unpack_data->Filters.num_items ||
628
 
                        filter_pos > unpack_data->old_filter_lengths_size) {
629
 
                rar_dbgmsg("filter_pos check failed\n");
630
 
                return FALSE;
631
 
        }
632
 
        unpack_data->last_filter = filter_pos;
633
 
        new_filter = (filter_pos == unpack_data->Filters.num_items);
634
 
        rar_dbgmsg("Filters.num_items=%d\n", unpack_data->Filters.num_items);
635
 
        rar_dbgmsg("new_filter=%d\n", new_filter);
636
 
        if (new_filter) {
637
 
                if (!rar_filter_array_add(&unpack_data->Filters, 1)) {
638
 
                        rar_dbgmsg("rar_filter_array_add failed\n");
639
 
                        return FALSE;
640
 
                }
641
 
                unpack_data->Filters.array[unpack_data->Filters.num_items-1] =
642
 
                                        filter = rar_filter_new();
643
 
                if (!unpack_data->Filters.array[unpack_data->Filters.num_items-1]) {
644
 
                        rar_dbgmsg("rar_filter_new failed\n");
645
 
                        return FALSE;
646
 
                }       
647
 
                unpack_data->old_filter_lengths_size++;
648
 
                unpack_data->old_filter_lengths = (int *) rar_realloc2(unpack_data->old_filter_lengths,
649
 
                                sizeof(int) * unpack_data->old_filter_lengths_size);
650
 
                if(!unpack_data->old_filter_lengths) {
651
 
                    rar_dbgmsg("unrar: add_vm_code: rar_realloc2 failed for unpack_data->old_filter_lengths\n");
652
 
                    return FALSE;
653
 
                }
654
 
                unpack_data->old_filter_lengths[unpack_data->old_filter_lengths_size-1] = 0;
655
 
                filter->exec_count = 0;
656
 
        } else {
657
 
                filter = unpack_data->Filters.array[filter_pos];
658
 
                filter->exec_count++;
659
 
        }
660
 
        
661
 
        stack_filter = rar_filter_new();
662
 
 
663
 
        empty_count = 0;
664
 
        for (i=0 ; i < unpack_data->PrgStack.num_items; i++) {
665
 
                unpack_data->PrgStack.array[i-empty_count] = unpack_data->PrgStack.array[i];
666
 
                if (unpack_data->PrgStack.array[i] == NULL) {
667
 
                        empty_count++;
668
 
                }
669
 
                if (empty_count > 0) {
670
 
                        unpack_data->PrgStack.array[i] = NULL;
671
 
                }
672
 
        }
673
 
        
674
 
        if (empty_count == 0) {
675
 
                rar_filter_array_add(&unpack_data->PrgStack, 1);
676
 
                empty_count = 1;
677
 
        }
678
 
        stack_pos = unpack_data->PrgStack.num_items - empty_count;
679
 
        unpack_data->PrgStack.array[stack_pos] = stack_filter;
680
 
        stack_filter->exec_count = filter->exec_count;
681
 
        
682
 
        block_start = rarvm_read_data(&rarvm_input);
683
 
        rar_dbgmsg("block_start=%u\n", block_start);
684
 
        if (first_byte & 0x40) {
685
 
                block_start += 258;
686
 
        }
687
 
        stack_filter->block_start = (block_start + unpack_data->unp_ptr) & MAXWINMASK;
688
 
        if (first_byte & 0x20) {
689
 
                stack_filter->block_length = rarvm_read_data(&rarvm_input);
690
 
        } else {
691
 
                stack_filter->block_length = filter_pos < unpack_data->old_filter_lengths_size ?
692
 
                                unpack_data->old_filter_lengths[filter_pos] : 0;
693
 
        }
694
 
        rar_dbgmsg("block_length=%u\n", stack_filter->block_length);
695
 
        stack_filter->next_window = unpack_data->wr_ptr != unpack_data->unp_ptr &&
696
 
                ((unpack_data->wr_ptr - unpack_data->unp_ptr) & MAXWINMASK) <= block_start;
697
 
                
698
 
        unpack_data->old_filter_lengths[filter_pos] = stack_filter->block_length;
699
 
        
700
 
        memset(stack_filter->prg.init_r, 0, sizeof(stack_filter->prg.init_r));
701
 
        stack_filter->prg.init_r[3] = VM_GLOBALMEMADDR;
702
 
        stack_filter->prg.init_r[4] = stack_filter->block_length;
703
 
        stack_filter->prg.init_r[5] = stack_filter->exec_count;
704
 
        if (first_byte & 0x10) {
705
 
                init_mask = rarvm_getbits(&rarvm_input) >> 9;
706
 
                rarvm_addbits(&rarvm_input, 7);
707
 
                for (i=0 ; i<7 ; i++) {
708
 
                        if (init_mask & (1<<i)) {
709
 
                                stack_filter->prg.init_r[i] =
710
 
                                        rarvm_read_data(&rarvm_input);
711
 
                                rar_dbgmsg("prg.init_r[%d] = %u\n", i, stack_filter->prg.init_r[i]);
712
 
                        }
713
 
                }
714
 
        }
715
 
        if (new_filter) {
716
 
                vm_codesize = rarvm_read_data(&rarvm_input);
717
 
                if (vm_codesize >= 0x1000 || vm_codesize == 0 || (vm_codesize > rarvm_input.buf_size)) {
718
 
                        rar_dbgmsg("ERROR: vm_codesize=0x%x buf_size=0x%x\n", vm_codesize, rarvm_input.buf_size);
719
 
                        return FALSE;
720
 
                }
721
 
                vm_code = (unsigned char *) rar_malloc(vm_codesize);
722
 
                if(!vm_code) {
723
 
                    rar_dbgmsg("unrar: add_vm_code: rar_malloc failed for vm_code\n");
724
 
                    return FALSE;
725
 
                }
726
 
                for (i=0 ; i < vm_codesize ; i++) {
727
 
                        vm_code[i] = rarvm_getbits(&rarvm_input) >> 8;
728
 
                        rarvm_addbits(&rarvm_input, 8);
729
 
                }
730
 
                if(!rarvm_prepare(&unpack_data->rarvm_data, &rarvm_input, &vm_code[0], vm_codesize, &filter->prg)) {
731
 
                    rar_dbgmsg("unrar: add_vm_code: rarvm_prepare failed\n");
732
 
                    free(vm_code);
733
 
                    return FALSE;
734
 
                }
735
 
                free(vm_code);
736
 
        }
737
 
        stack_filter->prg.alt_cmd = &filter->prg.cmd.array[0];
738
 
        stack_filter->prg.cmd_count = filter->prg.cmd_count;
739
 
        
740
 
        static_size = filter->prg.static_size;
741
 
        if (static_size > 0 && static_size < VM_GLOBALMEMSIZE) {
742
 
                stack_filter->prg.static_data = rar_malloc(static_size);
743
 
                if(!stack_filter->prg.static_data) {
744
 
                    rar_dbgmsg("unrar: add_vm_code: rar_malloc failed for stack_filter->prg.static_data\n");
745
 
                    return FALSE;
746
 
                }
747
 
                memcpy(stack_filter->prg.static_data, filter->prg.static_data, static_size);
748
 
        }
749
 
        
750
 
        if (stack_filter->prg.global_size < VM_FIXEDGLOBALSIZE) {
751
 
                free(stack_filter->prg.global_data);
752
 
                stack_filter->prg.global_data = rar_malloc(VM_FIXEDGLOBALSIZE);
753
 
                if(!stack_filter->prg.global_data) {
754
 
                    rar_dbgmsg("unrar: add_vm_code: rar_malloc failed for stack_filter->prg.global_data\n");
755
 
                    return FALSE;
756
 
                }
757
 
                memset(stack_filter->prg.global_data, 0, VM_FIXEDGLOBALSIZE);
758
 
                stack_filter->prg.global_size = VM_FIXEDGLOBALSIZE;
759
 
        }
760
 
        global_data = &stack_filter->prg.global_data[0];
761
 
        for (i=0 ; i<7 ; i++) {
762
 
                rar_dbgmsg("init_r[%d]=%u\n", i, stack_filter->prg.init_r[i]);
763
 
                rarvm_set_value(FALSE, (unsigned int *)&global_data[i*4],
764
 
                                stack_filter->prg.init_r[i]);
765
 
        }
766
 
        rarvm_set_value(FALSE, (unsigned int *)&global_data[0x1c], stack_filter->block_length);
767
 
        rarvm_set_value(FALSE, (unsigned int *)&global_data[0x20], 0);
768
 
        rarvm_set_value(FALSE, (unsigned int *)&global_data[0x2c], stack_filter->exec_count);
769
 
        memset(&global_data[0x30], 0, 16);
770
 
        for (i=0 ; i< 30 ; i++) {
771
 
                rar_dbgmsg("global_data[%d] = %d\n", i, global_data[i]);
772
 
        }
773
 
        if (first_byte & 8) {
774
 
                data_size = rarvm_read_data(&rarvm_input);
775
 
                if (data_size >= 0x10000) {
776
 
                        return FALSE;
777
 
                }
778
 
                cur_size = stack_filter->prg.global_size;
779
 
                if (cur_size < data_size+VM_FIXEDGLOBALSIZE) {
780
 
                        stack_filter->prg.global_size += data_size+VM_FIXEDGLOBALSIZE-cur_size;
781
 
                        stack_filter->prg.global_data = rar_realloc2(stack_filter->prg.global_data,
782
 
                                stack_filter->prg.global_size);
783
 
                        if(!stack_filter->prg.global_data) {
784
 
                            rar_dbgmsg("unrar: add_vm_code: rar_realloc2 failed for stack_filter->prg.global_data\n");
785
 
                            return FALSE;
786
 
                        }
787
 
                }
788
 
                global_data = &stack_filter->prg.global_data[VM_FIXEDGLOBALSIZE];
789
 
                for (i=0 ; i< data_size ; i++) {
790
 
                        if ((rarvm_input.in_addr+2) > rarvm_input.buf_size) {
791
 
                                rar_dbgmsg("Buffer truncated\n");
792
 
                                return FALSE;
793
 
                        }
794
 
                        global_data[i] = rarvm_getbits(&rarvm_input) >> 8;
795
 
                        rar_dbgmsg("global_data[%d] = %d\n", i, global_data[i]);
796
 
                        rarvm_addbits(&rarvm_input, 8);
797
 
                }
798
 
        }
799
 
        return TRUE;
800
 
}
801
 
 
802
 
static int read_vm_code(unpack_data_t *unpack_data, int fd)
803
 
{
804
 
        unsigned int first_byte;
805
 
        int length, i, retval;
806
 
        unsigned char *vmcode;
807
 
        
808
 
        first_byte = rar_getbits(unpack_data)>>8;
809
 
        rar_addbits(unpack_data, 8);
810
 
        length = (first_byte & 7) + 1;
811
 
        if (length == 7) {
812
 
                length = (rar_getbits(unpack_data) >> 8) + 7;
813
 
                rar_addbits(unpack_data, 8);
814
 
        } else if (length == 8) {
815
 
                length = rar_getbits(unpack_data);
816
 
                rar_addbits(unpack_data, 16);
817
 
        }
818
 
        vmcode = (unsigned char *) rar_malloc(length + 2);
819
 
        rar_dbgmsg("VM code length: %d\n", length);
820
 
        if (!vmcode) {
821
 
                return FALSE;
822
 
        }
823
 
        for (i=0 ; i < length ; i++) {
824
 
                if (unpack_data->in_addr >= unpack_data->read_top-1 &&
825
 
                                !rar_unp_read_buf(fd, unpack_data) && i<length-1) {
826
 
                        return FALSE;
827
 
                }
828
 
                vmcode[i] = rar_getbits(unpack_data) >> 8;
829
 
                rar_addbits(unpack_data, 8);
830
 
        }
831
 
        retval = add_vm_code(unpack_data, first_byte, vmcode, length);
832
 
        free(vmcode);
833
 
        return retval;
834
 
}
835
 
 
836
 
static int read_vm_code_PPM(unpack_data_t *unpack_data, int fd)
837
 
{
838
 
        unsigned int first_byte;
839
 
        int length, i, ch, retval, b1, b2;
840
 
        unsigned char *vmcode;
841
 
        
842
 
        first_byte = ppm_decode_char(&unpack_data->ppm_data, fd, unpack_data);
843
 
        if ((int)first_byte == -1) {
844
 
                return FALSE;
845
 
        }
846
 
        length = (first_byte & 7) + 1;
847
 
        if (length == 7) {
848
 
                b1 = ppm_decode_char(&unpack_data->ppm_data, fd, unpack_data);
849
 
                if (b1 == -1) {
850
 
                        return FALSE;
851
 
                }
852
 
                length = b1 + 7;
853
 
        } else if (length == 8) {
854
 
                b1 = ppm_decode_char(&unpack_data->ppm_data, fd, unpack_data);
855
 
                if (b1 == -1) {
856
 
                        return FALSE;
857
 
                }
858
 
                b2 = ppm_decode_char(&unpack_data->ppm_data, fd, unpack_data);
859
 
                if (b2 == -1) {
860
 
                        return FALSE;
861
 
                }
862
 
                length = b1*256 + b2;
863
 
        }
864
 
        vmcode = (unsigned char *) rar_malloc(length + 2);
865
 
        rar_dbgmsg("VM PPM code length: %d\n", length);
866
 
        if (!vmcode) {
867
 
                return FALSE;
868
 
        }
869
 
        for (i=0 ; i < length ; i++) {
870
 
                ch = ppm_decode_char(&unpack_data->ppm_data, fd, unpack_data);
871
 
                if (ch == -1) {
872
 
                        free(vmcode);
873
 
                        return FALSE;
874
 
                }
875
 
                vmcode[i] = ch;
876
 
        }
877
 
        retval = add_vm_code(unpack_data, first_byte, vmcode, length);
878
 
        free(vmcode);
879
 
        return retval;
880
 
}
881
 
 
882
 
void rar_unpack_init_data(int solid, unpack_data_t *unpack_data)
883
 
{
884
 
        if (!solid) {
885
 
                unpack_data->tables_read = FALSE;
886
 
                memset(unpack_data->old_dist, 0, sizeof(unpack_data->old_dist));
887
 
                unpack_data->old_dist_ptr= 0;
888
 
                memset(unpack_data->unp_old_table, 0, sizeof(unpack_data->unp_old_table));
889
 
                unpack_data->last_dist= 0;
890
 
                unpack_data->last_length=0;
891
 
                unpack_data->ppm_esc_char = 2;
892
 
                unpack_data->unp_ptr = 0;
893
 
                unpack_data->wr_ptr = 0;
894
 
                rar_init_filters(unpack_data);
895
 
        }
896
 
        unpack_data->in_bit = 0;
897
 
        unpack_data->in_addr = 0;
898
 
        unpack_data->read_top = 0;
899
 
        unpack_data->ppm_error = FALSE;
900
 
        
901
 
        unpack_data->written_size = 0;
902
 
        rarvm_init(&unpack_data->rarvm_data);
903
 
        unpack_data->unp_crc = 0xffffffff;
904
 
        
905
 
        unpack_init_data20(solid, unpack_data);
906
 
 
907
 
}
908
 
 
909
 
static int rar_unpack29(int fd, int solid, unpack_data_t *unpack_data)
910
 
{
911
 
        unsigned char ldecode[]={0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,
912
 
                        32,40,48,56,64,80,96,112,128,160,192,224};
913
 
        unsigned char lbits[]=  {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5};
914
 
        int ddecode[DC]={0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,
915
 
                1536,2048,3072,4096,6144,8192,12288,16384,24576,32768,49152,65536,
916
 
                98304,131072,196608,262144,327680,393216,458752,524288,589824,655360,
917
 
                720896,786432,851968,917504,983040,1048576,1310720,1572864,
918
 
                1835008,2097152,2359296,2621440,2883584,3145728,3407872,3670016,3932160};
919
 
        uint8_t dbits[DC]= {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,
920
 
                11,11,12,12,13,13,14,14,15,15,16,16,16,16,16,16,16,16,16,
921
 
                16,16,16,16,16,18,18,18,18,18,18,18,18,18,18,18,18};
922
 
        unsigned char sddecode[]={0,4,8,16,32,64,128,192};
923
 
        unsigned char sdbits[]=  {2,2,3, 4, 5, 6,  6,  6};
924
 
        unsigned int bits, distance;
925
 
        int retval=TRUE, i, number, length, dist_number, low_dist, ch, next_ch;
926
 
        int length_number, failed;
927
 
 
928
 
        rar_dbgmsg("Offset: %ld\n", lseek(fd, 0, SEEK_CUR));
929
 
        if (!solid) {
930
 
                rar_dbgmsg("Not solid\n");
931
 
        }
932
 
        rar_unpack_init_data(solid, unpack_data);
933
 
        if (!rar_unp_read_buf(fd, unpack_data)) {
934
 
                return FALSE;
935
 
        }
936
 
        if (!solid || !unpack_data->tables_read) {
937
 
                rar_dbgmsg("Read tables\n");
938
 
                if (!read_tables(fd, unpack_data)) {
939
 
                        return FALSE;
940
 
                }
941
 
        }
942
 
 
943
 
        rar_dbgmsg("init done\n");
944
 
        while(1) {
945
 
                unpack_data->unp_ptr &= MAXWINMASK;
946
 
                rar_dbgmsg("UnpPtr = %d\n", unpack_data->unp_ptr);
947
 
                if (unpack_data->in_addr > unpack_data->read_border) {
948
 
                        if (!rar_unp_read_buf(fd, unpack_data)) {
949
 
                                retval = FALSE;
950
 
                                break;
951
 
                        }
952
 
                }
953
 
                if (((unpack_data->wr_ptr - unpack_data->unp_ptr) & MAXWINMASK) < 260 &&
954
 
                                unpack_data->wr_ptr != unpack_data->unp_ptr) {
955
 
                        unp_write_buf(unpack_data);
956
 
                }
957
 
                if (unpack_data->unp_block_type == BLOCK_PPM) {
958
 
                        ch = ppm_decode_char(&unpack_data->ppm_data, fd, unpack_data);
959
 
                        rar_dbgmsg("PPM char: %d\n", ch);
960
 
                        if (ch == -1) {
961
 
                                retval = FALSE;
962
 
                                unpack_data->ppm_error = TRUE;
963
 
                                break;
964
 
                        }
965
 
                        if (ch == unpack_data->ppm_esc_char) {
966
 
                                next_ch = ppm_decode_char(&unpack_data->ppm_data,
967
 
                                                        fd, unpack_data);
968
 
                                rar_dbgmsg("PPM next char: %d\n", next_ch);
969
 
                                if (next_ch == -1) {
970
 
                                        retval = FALSE;
971
 
                                        unpack_data->ppm_error = TRUE;
972
 
                                        break;
973
 
                                }
974
 
                                if (next_ch == 0) {
975
 
                                        if (!read_tables(fd, unpack_data)) {
976
 
                                                retval = FALSE;
977
 
                                                break;
978
 
                                        }
979
 
                                        continue;
980
 
                                }
981
 
                                if (next_ch == 2 || next_ch == -1) {
982
 
                                        break;
983
 
                                }
984
 
                                if (next_ch == 3) {
985
 
                                        if (!read_vm_code_PPM(unpack_data, fd)) {
986
 
                                                retval = FALSE;
987
 
                                                break;
988
 
                                        }
989
 
                                        continue;
990
 
                                }
991
 
                                if (next_ch == 4) {
992
 
                                        unsigned int length;
993
 
                                        distance = 0;
994
 
                                        failed = FALSE;
995
 
                                        for (i=0 ; i < 4 && !failed; i++) {
996
 
                                                ch = ppm_decode_char(&unpack_data->ppm_data,
997
 
                                                                fd, unpack_data);
998
 
                                                if (ch == -1) {
999
 
                                                        failed = TRUE;
1000
 
                                                } else {
1001
 
                                                        if (i==3) {
1002
 
                                                                length = (uint8_t)ch;
1003
 
                                                        } else {
1004
 
                                                                distance = (distance << 8) +
1005
 
                                                                                (uint8_t)ch;
1006
 
                                                        }
1007
 
                                                }
1008
 
                                        }
1009
 
                                        if (failed) {
1010
 
                                                retval = FALSE;
1011
 
                                                break;
1012
 
                                        }
1013
 
                                        copy_string(unpack_data, length+32, distance+2);
1014
 
                                        continue;
1015
 
                                }
1016
 
                                if (next_ch == 5) {
1017
 
                                        int length = ppm_decode_char(&unpack_data->ppm_data,
1018
 
                                                                fd, unpack_data);
1019
 
                                        rar_dbgmsg("PPM length: %d\n", length);
1020
 
                                        if (length == -1) {
1021
 
                                                retval = FALSE;
1022
 
                                                break;
1023
 
                                        }
1024
 
                                        copy_string(unpack_data, length+4, 1);
1025
 
                                        continue;
1026
 
                                }
1027
 
                        }
1028
 
                        unpack_data->window[unpack_data->unp_ptr++] = ch;
1029
 
                        continue;
1030
 
                } else {
1031
 
                        number = rar_decode_number(unpack_data, (struct Decode *)&unpack_data->LD);
1032
 
                        rar_dbgmsg("number = %d\n", number);
1033
 
                        if (number < 256) {
1034
 
                                unpack_data->window[unpack_data->unp_ptr++] = (uint8_t) number;
1035
 
                                continue;
1036
 
                        }
1037
 
                        if (number >= 271) {
1038
 
                                length = ldecode[number-=271]+3;
1039
 
                                if ((bits=lbits[number]) > 0) {
1040
 
                                        length += rar_getbits(unpack_data) >> (16-bits);
1041
 
                                        rar_addbits(unpack_data, bits);
1042
 
                                }
1043
 
                                dist_number = rar_decode_number(unpack_data,
1044
 
                                                        (struct Decode *)&unpack_data->DD);
1045
 
                                distance = ddecode[dist_number] + 1;
1046
 
                                if ((bits = dbits[dist_number]) > 0) {
1047
 
                                        if (dist_number > 9) {
1048
 
                                                if (bits > 4) {
1049
 
                                                        distance += ((rar_getbits(unpack_data) >>
1050
 
                                                                        (20-bits)) << 4);
1051
 
                                                        rar_addbits(unpack_data, bits-4);
1052
 
                                                }
1053
 
                                                if (unpack_data->low_dist_rep_count > 0) {
1054
 
                                                        unpack_data->low_dist_rep_count--;
1055
 
                                                        distance += unpack_data->prev_low_dist;
1056
 
                                                } else {
1057
 
                                                        low_dist = rar_decode_number(unpack_data,
1058
 
                                                                (struct Decode *) &unpack_data->LDD);
1059
 
                                                        if (low_dist == 16) {
1060
 
                                                                unpack_data->low_dist_rep_count =
1061
 
                                                                        LOW_DIST_REP_COUNT-1;
1062
 
                                                                distance += unpack_data->prev_low_dist;
1063
 
                                                        } else {
1064
 
                                                                distance += low_dist;
1065
 
                                                                unpack_data->prev_low_dist = low_dist;
1066
 
                                                        }
1067
 
                                                }
1068
 
                                        } else {
1069
 
                                                distance += rar_getbits(unpack_data) >> (16-bits);
1070
 
                                                rar_addbits(unpack_data, bits);
1071
 
                                        }
1072
 
                                }
1073
 
                                
1074
 
                                if (distance >= 0x2000) {
1075
 
                                        length++;
1076
 
                                        if (distance >= 0x40000L) {
1077
 
                                                length++;
1078
 
                                        }
1079
 
                                }
1080
 
                                
1081
 
                                insert_old_dist(unpack_data, distance);
1082
 
                                insert_last_match(unpack_data, length, distance);
1083
 
                                copy_string(unpack_data, length, distance);
1084
 
                                continue;
1085
 
                        }
1086
 
                        if (number == 256) {
1087
 
                                if (!read_end_of_block(fd, unpack_data)) {
1088
 
                                        break;
1089
 
                                }
1090
 
                                continue;
1091
 
                        }
1092
 
                        if (number == 257) {
1093
 
                                if (!read_vm_code(unpack_data, fd)) {
1094
 
                                        retval = FALSE;
1095
 
                                        break;
1096
 
                                }
1097
 
                                continue;
1098
 
                        }
1099
 
                        if (number == 258) {
1100
 
                                if (unpack_data->last_length != 0) {
1101
 
                                        copy_string(unpack_data, unpack_data->last_length,
1102
 
                                                        unpack_data->last_dist);
1103
 
                                }
1104
 
                                continue;
1105
 
                        }
1106
 
                        if (number < 263) {
1107
 
                                dist_number = number-259;
1108
 
                                distance = unpack_data->old_dist[dist_number];
1109
 
                                for (i=dist_number ; i > 0 ; i--) {
1110
 
                                        unpack_data->old_dist[i] = unpack_data->old_dist[i-1];
1111
 
                                }
1112
 
                                unpack_data->old_dist[0] = distance;
1113
 
                                
1114
 
                                length_number = rar_decode_number(unpack_data,
1115
 
                                                        (struct Decode *)&unpack_data->RD);
1116
 
                                length = ldecode[length_number]+2;
1117
 
                                if ((bits = lbits[length_number]) > 0) {
1118
 
                                        length += rar_getbits(unpack_data) >> (16-bits);
1119
 
                                        rar_addbits(unpack_data, bits);
1120
 
                                }
1121
 
                                insert_last_match(unpack_data, length, distance);
1122
 
                                copy_string(unpack_data, length, distance);
1123
 
                                continue;
1124
 
                        }
1125
 
                        if (number < 272) {
1126
 
                                distance = sddecode[number-=263]+1;
1127
 
                                if ((bits = sdbits[number]) > 0) {
1128
 
                                        distance += rar_getbits(unpack_data) >> (16-bits);
1129
 
                                        rar_addbits(unpack_data, bits);
1130
 
                                }
1131
 
                                insert_old_dist(unpack_data, distance);
1132
 
                                insert_last_match(unpack_data, 2, distance);
1133
 
                                copy_string(unpack_data, 2, distance);
1134
 
                                continue;
1135
 
                        }
1136
 
        
1137
 
                }
1138
 
        }
1139
 
        if (retval) {
1140
 
                unp_write_buf(unpack_data);
1141
 
        }
1142
 
        rar_dbgmsg("Finished length: %ld\n", unpack_data->written_size);
1143
 
        return retval;
1144
 
}
1145
 
 
1146
 
int rar_unpack(int fd, int method, int solid, unpack_data_t *unpack_data)
1147
 
{
1148
 
        int retval = FALSE;
1149
 
        switch(method) {
1150
 
        case 15:
1151
 
                retval = rar_unpack15(fd, solid, unpack_data);
1152
 
                break;
1153
 
        case 20:
1154
 
        case 26:
1155
 
                retval = rar_unpack20(fd, solid, unpack_data);
1156
 
                break;
1157
 
        case 29:
1158
 
                retval = rar_unpack29(fd, solid, unpack_data);
1159
 
                break;
1160
 
        default:
1161
 
                break;
1162
 
        }
1163
 
        return retval;
1164
 
}