OSDN Git Service

pass the lha-test4
[lha/olha.git] / decode.c
1 /***********************************************************
2         decode.c
3 ***********************************************************/
4 #include "ar.h"
5
6 static int j;                   /* remaining bytes to copy */
7
8 extern struct lha_opts opts;
9
10 void
11 decode_start(void)
12 {
13     huf_decode_start(opts.method);
14     j = 0;
15 }
16
17 void
18 decode(uint count, uchar buffer[])
19         /* The calling function must keep the number of
20            bytes to be processed.  This function decodes
21            either 'count' bytes or 'DICSIZ' bytes, whichever
22            is smaller, into the array 'buffer[]' of size
23            'DICSIZ' or more.
24            Call decode_start() once for each new file
25            before calling this function. */
26 {
27     static uint i;
28     uint r, c;
29
30     r = 0;
31     while (--j >= 0) {
32         buffer[r] = buffer[i];
33         i = (i + 1) & (MAXDICSIZ - 1);
34         if (++r == count)
35             return;
36     }
37     for (;;) {
38         c = decode_c();
39         if (c <= UCHAR_MAX) {
40             buffer[r] = c;
41             if (++r == count)
42                 return;
43         }
44         else {
45             j = c - (UCHAR_MAX + 1 - THRESHOLD);
46             i = (r - decode_p() - 1) & (MAXDICSIZ - 1);
47             while (--j >= 0) {
48                 buffer[r] = buffer[i];
49                 i = (i + 1) & (MAXDICSIZ - 1);
50                 if (++r == count)
51                     return;
52             }
53         }
54     }
55 }