OSDN Git Service

Fix warning message on extracting symlink.
[lha/lha.git] / src / pm2.c
1 /***********************************************************
2         pm2.c -- extract pmext2 coding
3 ***********************************************************/
4 /*
5   Copyright (c) 1999 Maarten ter Huurne
6
7   Permission is hereby granted, free of charge, to any person
8   obtaining a copy of this software and associated documentation files
9   (the "Software"), to deal in the Software without restriction,
10   including without limitation the rights to use, copy, modify, merge,
11   publish, distribute, sublicense, and/or sell copies of the Software,
12   and to permit persons to whom the Software is furnished to do so,
13   subject to the following conditions:
14
15   The above copyright notice and this permission notice shall be
16   included in all copies or substantial portions of the Software.
17
18   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25   SOFTWARE.
26 */
27 #include "lha.h"
28
29 static off_t nextcount;
30 static unsigned long lastupdate;
31
32 /* repeated from slide.c */
33 static unsigned int dicsiz1;
34 #define offset (0x100 - 2)
35
36 void
37 decode_start_pm2(void)
38 {
39     dicsiz1 = (1 << dicbit) - 1;
40     init_getbits();
41     hist_init();
42     nextcount = 0;
43     lastupdate = 0;
44     getbits(1);                 /* discard bit */
45 }
46
47
48 static unsigned char gettree1;
49
50 static int historyBits[8] = {   3,   3,   4,   5,   5,   5,   6,   6 };
51 static int historyBase[8] = {   0,   8,  16,  32,  64,  96, 128, 192 };
52 static int repeatBits[6]  = {   3,   3,   5,   6,   7,   0 };
53 static int repeatBase[6]  = {  17,  25,  33,  65, 129, 256 };
54
55 unsigned short
56 decode_c_pm2(void)
57 {
58     /* various admin: */
59     while (lastupdate != loc) {
60         hist_update(dtext[lastupdate]);
61         lastupdate = (lastupdate + 1) & dicsiz1;
62     }
63
64     while (decode_count >= nextcount) {
65         /* Actually it will never loop, because decode_count doesn't grow that fast.
66            However, this is the way LHA does it.
67            Probably other encoding methods can have repeats larger than 256 bytes.
68            Note: LHA puts this code in decode_p...
69         */
70
71         switch (nextcount) {
72         case 0x0000:
73             maketree1();
74             maketree2(5);
75             nextcount = 0x0400;
76             break;
77         case 0x0400:
78             maketree2(6);
79             nextcount = 0x0800;
80             break;
81         case 0x0800:
82             maketree2(7);
83             nextcount = 0x1000;
84             break;
85         case 0x1000:
86             if (getbits(1) != 0)
87                 maketree1();
88             maketree2(8);
89             nextcount = 0x2000;
90             break;
91         default:                /* 0x2000, 0x3000, 0x4000, ... */
92             if (getbits(1) != 0) {
93                 maketree1();
94                 maketree2(8);
95             }
96             nextcount += 0x1000;
97             break;
98         }
99     }
100     gettree1 = tree1_get();        /* value preserved for decode_p */
101     if (gettree1 >= 29) {
102         error("Bad table");
103         exit(1);
104     }
105
106     /* direct value (ret <= UCHAR_MAX) */
107     if (gettree1 < 8)
108         return hist_lookup(historyBase[gettree1] +
109                            getbits(historyBits[gettree1]));
110     /* repeats: (ret > UCHAR_MAX) */
111     if (gettree1 < 23)
112         return offset + 2 + (gettree1 - 8);
113
114     return offset + repeatBase[gettree1 - 23]
115         + getbits(repeatBits[gettree1 - 23]);
116 }
117
118 unsigned short
119 decode_p_pm2(void)
120 {
121     /* gettree1 value preserved from decode_c */
122     int nbits, delta, gettree2;
123     if (gettree1 == 8) {        /* 2-byte repeat with offset 0..63 */
124         nbits = 6;
125         delta = 0;
126     }
127     else if (gettree1 < 28) {   /* n-byte repeat with offset 0..8191 */
128         gettree2 = tree2_get();
129         if (gettree2 == 0) {
130             nbits = 6;
131             delta = 0;
132         }
133         else {                  /* 1..7 */
134             nbits = 5 + gettree2;
135             delta = 1 << nbits;
136         }
137     }
138     else {                      /* 256 bytes repeat with offset 0 */
139         nbits = 0;
140         delta = 0;
141     }
142
143     return delta + getbits(nbits);
144 }