/
runzip.c
219 lines (181 loc) · 4.86 KB
/
runzip.c
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
/*
Copyright (C) Andrew Tridgell 1998-2003
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* rzip decompression algorithm */
#include "rzip.h"
static inline uchar read_u8(void *ss, int stream)
{
uchar b;
if (read_stream(ss, stream, &b, 1) != 1) {
fatal("Stream read failed\n");
}
return b;
}
static inline unsigned read_u16(void *ss, int stream)
{
unsigned ret;
ret = read_u8(ss, stream);
ret |= read_u8(ss, stream)<<8;
return ret;
}
static inline unsigned read_u32(void *ss, int stream)
{
unsigned ret;
ret = read_u8(ss, stream);
ret |= read_u8(ss, stream)<<8;
ret |= read_u8(ss, stream)<<16;
ret |= read_u8(ss, stream)<<24;
return ret;
}
static inline unsigned read_u24(void *ss, int stream)
{
unsigned ret;
ret = read_u8(ss, stream);
ret |= read_u8(ss, stream)<<8;
ret |= read_u8(ss, stream)<<16;
return ret;
}
static int read_header(void *ss, uchar *head)
{
*head = read_u8(ss, 0);
return read_u16(ss, 0);
}
static int unzip_literal(void *ss, int len, int fd_out, uint32 *cksum, int out_is_pipe)
{
uchar *buf;
ssize_t w,r;
buf = malloc(len);
if (!buf) {
fatal("Failed to allocate literal buffer of size %d\n", len);
}
read_stream(ss, 1, buf, len);
if (write(fd_out, buf, len) != len) {
fatal("Failed to write literal buffer of size %d\n", len);
}
if (out_is_pipe) {
w=0;
while(w<len && (r=write(STDOUT_FILENO, buf+w, len-w))>0)
w+=r;
if(r<0)
fatal("Failed to write literal buffer of size %d\n", len);
}
*cksum = crc32_buffer(buf, len, *cksum);
free(buf);
return len;
}
static int unzip_match(void *ss, int len, int fd_out, int fd_hist, uint32 *cksum, int out_is_pipe)
{
unsigned offset;
int n, total=0;
off_t cur_pos = lseek(fd_out, 0, SEEK_CUR);
offset = read_u32(ss, 0);
ssize_t w,r;
if (lseek(fd_hist, cur_pos-offset, SEEK_SET) == (off_t)-1) {
fatal("Seek failed by %d from %d on history file in unzip_match - %s\n",
offset, cur_pos, strerror(errno));
}
while (len) {
uchar *buf;
n = MIN(len, offset);
buf = malloc(n);
if (!buf) {
fatal("Failed to allocate %d bytes in unzip_match\n", n);
}
if (read(fd_hist, buf, n) != n) {
fatal("Failed to read %d bytes in unzip_match\n", n);
}
if (write(fd_out, buf, n) != n) {
fatal("Failed to write %d bytes in unzip_match\n", n);
}
if (out_is_pipe) {
w=0;
while(w<n && (r=write(STDOUT_FILENO, buf+w, n-w))>0)
w+=r;
if(r<0)
fatal("Failed to write literal buffer of size %d\n", len);
}
*cksum = crc32_buffer(buf, n, *cksum);
len -= n;
free(buf);
total += n;
}
return total;
}
/* decompress a section of an open file. Call fatal() on error
return the number of bytes that have been retrieved
*/
static int runzip_chunk(int fd_in, int fd_out, int fd_hist, int out_is_pipe, int in_is_pipe)
{
uchar head;
int len;
struct stat st;
void *ss;
off_t ofs;
int total = 0;
uint32 good_cksum, cksum = 0;
int eof;
if(!in_is_pipe) {
ofs = lseek(fd_in, 0, SEEK_CUR);
if (ofs == (off_t)-1) {
fatal("Failed to seek input file in runzip_fd\n");
}
if (fstat(fd_in, &st) != 0 || st.st_size-ofs == 0) {
return 0;
}
}
ss = open_stream_in(fd_in, NUM_STREAMS, in_is_pipe, &eof);
if (!ss) {
if(eof)
return 0;
fatal(NULL);
}
while ((len = read_header(ss, &head)) || head) {
switch (head) {
case 0:
total += unzip_literal(ss, len, fd_out, &cksum, out_is_pipe);
break;
default:
total += unzip_match(ss, len, fd_out, fd_hist, &cksum, out_is_pipe);
break;
}
}
good_cksum = read_u32(ss, 0);
if (good_cksum != cksum) {
fatal("Bad checksum 0x%08x - expected 0x%08x\n", cksum, good_cksum);
}
if (close_stream_in(ss) != 0) {
fatal("Failed to close stream!\n");
}
if(out_is_pipe) {
if(lseek(fd_out,0,SEEK_SET)==-1 ||
lseek(fd_hist,0,SEEK_SET)==-1 ||
ftruncate(fd_out,0)==-1)
fatal("cannot truncate temporary file\n");
}
return total;
}
/* decompress a open file. Call fatal() on error
return the number of bytes that have been retrieved
*/
off_t runzip_fd(int fd_in, int fd_out, int fd_hist, off_t expected_size, int out_is_pipe, int in_is_pipe)
{
off_t total = 0, l;
while (total < expected_size || expected_size==0) {
l = runzip_chunk(fd_in, fd_out, fd_hist, out_is_pipe, in_is_pipe);
total += l;
if( l == 0)
break;
}
return total;
}