1 /***********************************************************
2 pm2.c -- extract pmext2 coding
3 ***********************************************************/
5 Copyright (c) 1999 Maarten ter Huurne
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:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
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
29 static off_t nextcount;
30 static unsigned long lastupdate;
32 /* repeated from slide.c */
33 static unsigned int dicsiz1;
34 #define offset (0x100 - 2)
37 decode_start_pm2(void)
39 dicsiz1 = (1 << dicbit) - 1;
44 getbits(1); /* discard bit */
48 static unsigned char gettree1;
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 };
59 while (lastupdate != loc) {
60 hist_update(dtext[lastupdate]);
61 lastupdate = (lastupdate + 1) & dicsiz1;
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...
91 default: /* 0x2000, 0x3000, 0x4000, ... */
92 if (getbits(1) != 0) {
100 gettree1 = tree1_get(); /* value preserved for decode_p */
101 if (gettree1 >= 29) {
106 /* direct value (ret <= UCHAR_MAX) */
108 return hist_lookup(historyBase[gettree1] +
109 getbits(historyBits[gettree1]));
110 /* repeats: (ret > UCHAR_MAX) */
112 return offset + 2 + (gettree1 - 8);
114 return offset + repeatBase[gettree1 - 23]
115 + getbits(repeatBits[gettree1 - 23]);
121 /* gettree1 value preserved from decode_c */
122 int nbits, delta, gettree2;
123 if (gettree1 == 8) { /* 2-byte repeat with offset 0..63 */
127 else if (gettree1 < 28) { /* n-byte repeat with offset 0..8191 */
128 gettree2 = tree2_get();
134 nbits = 5 + gettree2;
138 else { /* 256 bytes repeat with offset 0 */
143 return delta + getbits(nbits);