OSDN Git Service

added a test.sh
[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 void
9 decode_start(void)
10 {
11     huf_decode_start();
12     j = 0;
13 }
14
15 void
16 decode(uint count, uchar buffer[])
17         /* The calling function must keep the number of
18            bytes to be processed.  This function decodes
19            either 'count' bytes or 'DICSIZ' bytes, whichever
20            is smaller, into the array 'buffer[]' of size
21            'DICSIZ' or more.
22            Call decode_start() once for each new file
23            before calling this function. */
24 {
25     static uint i;
26     uint r, c;
27
28     r = 0;
29     while (--j >= 0) {
30         buffer[r] = buffer[i];
31         i = (i + 1) & (DICSIZ - 1);
32         if (++r == count)
33             return;
34     }
35     for (;;) {
36         c = decode_c();
37         if (c <= UCHAR_MAX) {
38             buffer[r] = c;
39             if (++r == count)
40                 return;
41         }
42         else {
43             j = c - (UCHAR_MAX + 1 - THRESHOLD);
44             i = (r - decode_p() - 1) & (DICSIZ - 1);
45             while (--j >= 0) {
46                 buffer[r] = buffer[i];
47                 i = (i + 1) & (DICSIZ - 1);
48                 if (++r == count)
49                     return;
50             }
51         }
52     }
53 }