OSDN Git Service

seek_lha_header() should search the PMA archive
[lha/lha.git] / src / pm2.c
1 /***********************************************************
2         pm2.c -- extract pmext2 coding
3 ***********************************************************/
4 #include "lha.h"
5 #include "pm2hist.h"
6 #include "pm2tree.h"
7
8 static unsigned long nextcount;
9 static unsigned short lastupdate;
10
11 /* repeated from slide.c */
12 static unsigned short dicsiz1;
13 #define offset (0x100 - 2)
14
15 void decode_start_pm2(void)
16 {
17     dicsiz1 = (1 << dicbit) - 1;
18     init_getbits();
19     hist_init();
20     nextcount = 0;
21     lastupdate = 0;
22     getbits(1); /* discard bit */
23 }
24
25
26 static unsigned char gettree1;
27
28 static int historyBits[8] = {   3,   3,   4,   5,   5,   5,   6,   6 };
29 static int historyBase[8] = {   0,   8,  16,  32,  64,  96, 128, 192 };
30 static int repeatBits[6]  = {   3,   3,   5,   6,   7,   0 };
31 static int repeatBase[6]  = {  17,  25,  33,  65, 129, 256 };
32
33 unsigned short decode_c_pm2(void)
34 {
35     /* various admin: */
36     while (lastupdate != loc)
37     {
38         hist_update(dtext[lastupdate]);
39         lastupdate = (lastupdate + 1) & dicsiz1;
40     }
41     while (decode_count >= nextcount)
42     /* Actually it will never loop, because count doesn't grow that fast.
43        However, this is the way LHA does it.
44        Probably other encoding methods can have repeats larger than 256 bytes.
45        Note: LHA puts this code in decode_p...
46     */
47     {
48         if (nextcount == 0x0000)
49         {
50             maketree1();
51             maketree2(5);
52             nextcount = 0x0400;
53         }
54         else if (nextcount == 0x0400)
55         {
56             maketree2(6);
57             nextcount = 0x0800;
58         }
59         else if (nextcount == 0x0800)
60         {
61             maketree2(7);
62             nextcount = 0x1000;
63         }
64         else if (nextcount == 0x1000)
65         {
66             if (getbits(1) != 0) maketree1();
67             maketree2(8);
68             nextcount = 0x2000;
69         }
70         else
71         { /* 0x2000, 0x3000, 0x4000, ... */
72             if (getbits(1) != 0)
73             {
74                 maketree1();
75                 maketree2(8);
76             }
77             nextcount += 0x1000;
78         }
79     }
80     gettree1 = tree_get(&tree1); /* value preserved for decode_p */
81         
82     /* direct value (ret <= UCHAR_MAX) */
83     if (gettree1 < 8) return hist_lookup(
84         historyBase[gettree1] + getbits(historyBits[gettree1]) );
85     /* repeats: (ret > UCHAR_MAX) */
86     if (gettree1 < 23) return offset + 2 + (gettree1 - 8);
87     return offset + repeatBase[gettree1 - 23]
88         + getbits(repeatBits[gettree1 - 23]);
89 }
90
91 unsigned short decode_p_pm2(void)
92 {
93     /* gettree1 value preserved from decode_c */
94     int nbits, delta, gettree2;
95     if (gettree1 == 8)
96     { /* 2-byte repeat with offset 0..63 */
97         nbits = 6; delta = 0;
98     }
99     else if (gettree1 < 28)
100     { /* n-byte repeat with offset 0..8191 */
101         gettree2 = tree_get(&tree2);
102         if (gettree2 == 0)
103         {
104             nbits = 6; delta = 0;
105         }
106         else
107         { /* 1..7 */
108             nbits = 5 + gettree2; delta = 1 << nbits;
109         }
110     }
111     else
112     { /* 256 bytes repeat with offset 0 */
113             nbits = 0; delta = 0;
114     }
115     return delta + getbits(nbits);
116                         
117 }