1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
#include <lib9.h>
#include <a.out.h>
#include "squeeze.h"
/*
* forsyth@vitanuova.com
*/
/*
* for details of `unsqueeze' see:
*
* %A Mark Taunton
* %T Compressed Executables: An Exercise in Thinking Small
* %P 385-404
* %I USENIX
* %B USENIX Conference Proceedings
* %D Summer 1991
* %C Nashville, TN
*
* several of the unimplemented improvements described in the paper
* have been implemented here
*
* there is a further transformation on the powerpc (QFLAG!=0) to shuffle bits
* in certain instructions so as to push the fixed bits to the top of the word.
*/
typedef struct Squeeze Squeeze;
struct Squeeze {
int n;
ulong tab[7*256];
};
enum {
CHECK = 1 /* check precise bounds in Squeeze array */
};
#define GET4(p) (((((((p)[0]<<8)|(p)[1])<<8)|(p)[2])<<8)|(p)[3])
static uchar out[3*1024*1024];
static uchar bigb[1024*1024];
static ulong top;
static int qflag = 1;
static int islittle = 0;
static ulong chksum, oldsum;
static int rdtab(int, Squeeze*, int);
static long unsqueezefd(int, void*);
static uchar* unsqueeze(uchar*, uchar*, uchar*, Squeeze*, Squeeze*, ulong);
static uchar* unsqzseg(int, uchar*, long, ulong);
void
main(int argc, char **argv)
{
int fd;
long n;
if(argc < 2)
exits("args");
fd = open(argv[1], OREAD);
if(fd < 0)
exits("open");
n = unsqueezefd(fd, out);
if(n < 0){
fprint(2, "zqs: can't unsqueeze\n");
exits("err");
}
if(write(1, out, n) != n){
fprint(2, "zqs: write error: %r\n");
exits("err");
}
fprint(2, "%ld bytes, %8.8lux csum\n", n, chksum);
exits(0);
}
static long
unsqueezefd(int fd, void *v)
{
uchar *wp, *out;
ulong toptxt, topdat;
long asis, nst, nsd;
Sqhdr sqh;
Exec ex;
out = (uchar*)v;
if(read(fd, &sqh, SQHDRLEN) != SQHDRLEN)
return -1;
if(GET4(sqh.magic) != SQMAGIC)
return -1;
if(read(fd, &ex, sizeof(Exec)) != sizeof(Exec))
return -1;
toptxt = GET4(sqh.toptxt);
topdat = GET4(sqh.topdat);
oldsum = GET4(sqh.sum);
asis = GET4(sqh.asis);
if(asis < 0)
asis = 0;
nst = GET4(sqh.text);
nsd = GET4(sqh.data);
switch(GET4((uchar*)&ex.magic)){
case Q_MAGIC:
if(qflag)
fprint(2, "PowerPC mode\n");
islittle = 0;
break;
case E_MAGIC:
case 0xA0E1: /* arm AIF */
islittle = 1;
qflag = 0;
break;
default:
fprint(2, "Unknown magic: %8.8ux\n", GET4((uchar*)&ex.magic));
qflag = 0;
break;
}
memmove(out, &ex, sizeof(ex));
wp = unsqzseg(fd, out + sizeof(ex), nst, toptxt);
if(wp == nil)
return -1;
wp = unsqzseg(fd, wp, nsd, topdat);
if(wp == nil)
return -1;
if(asis){
if(read(fd, wp, asis) != asis)
return -1;
wp += asis;
}
return wp-out;
}
static uchar*
unsqzseg(int fd, uchar *wp, long ns, ulong top)
{
Squeeze sq3, sq4;
if(ns == 0)
return wp;
if(rdtab(fd, &sq3, 0) < 0)
return nil;
if(rdtab(fd, &sq4, 8) < 0)
return nil;
fprint(2, "tables: %d %d\n", sq3.n, sq4.n);
if(read(fd, bigb, ns) != ns)
return nil;
return unsqueeze(wp, bigb, bigb+ns, &sq3, &sq4, top);
}
static uchar*
unsqueeze(uchar *wp, uchar *rp, uchar *ep, Squeeze *sq3, Squeeze *sq4, ulong top)
{
ulong nx;
int code, n;
if(qflag){
QREMAP(top); /* adjust top just once, outside the loop */
}
while(rp < ep){
code = *rp++;
n = 0;
nx = code>>4;
do{
if(nx == 0){
nx = top;
}else{
if(nx==1){
if(rp+3 >= ep)
return nil;
nx = (((((rp[3]<<8)|rp[2])<<8)|rp[1])<<8)|rp[0];
rp += 4;
}else if(nx <= 8){ /* 2 to 8 */
if(rp+1 >= ep)
return nil;
nx = ((nx-2)<<8) | rp[0];
if(CHECK && nx >= sq4->n)
return nil; /* corrupted file */
nx = sq4->tab[nx] | rp[1];
rp += 2;
}else{ /* 9 to 15 */
if(rp >= ep)
return nil; /* corrupted file */
nx = ((nx-9)<<8) | rp[0];
if(CHECK && nx >= sq3->n)
return nil; /* corrupted file */
nx = sq3->tab[nx];
rp++;
}
if(rp > ep)
return nil; /* corrupted file */
if(qflag){
QREMAP(nx);
}
}
if(islittle){
wp[0] = nx;
wp[1] = nx>>8;
wp[2] = nx>>16;
wp[3] = nx>>24;
}else{
wp[0] = nx>>24;
wp[1] = nx>>16;
wp[2] = nx>>8;
wp[3] = nx;
}
wp += 4;
chksum += nx;
nx = code & 0xF;
}while(++n == 1);
}
return wp;
}
static int
rdtab(int fd, Squeeze *sq, int shift)
{
uchar b[7*256*5], *p, *ep;
ulong v, w;
int i;
if(read(fd, b, 2) != 2)
return -1;
i = (b[0]<<8) | b[1];
if(1)
fprint(2, "table: %d\n", i);
if((i -= 2) > 0){
if(read(fd, b, i) != i)
return -1;
}
sq->n = 0;
p = b;
ep = b+i;
v = 0;
while(p < ep){
w = 0;
do{
if(p >= ep)
return -1;
w = (w<<7) | (*p & 0x7F);
}while(*p++ & 0x80);
v += w;
if(0)
fprint(2, "%d %8.8lux %8.8lux\n", sq->n, v, w);
sq->tab[sq->n++] = v << shift;
}
return 0;
}
|