| 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 |
#include <stdio.h> |
| 51 |
#include <stdlib.h> |
| 52 |
#include <string.h> |
| 53 |
#include "zlib.h" |
| 54 |
|
| 55 |
#define local static |
| 56 |
|
| 57 |
#define SPAN 1048576L |
| 58 |
#define WINSIZE 32768U |
| 59 |
#define CHUNK 16384 |
| 60 |
|
| 61 |
|
| 62 |
struct point { |
| 63 |
off_t out; |
| 64 |
off_t in; |
| 65 |
int bits; |
| 66 |
unsigned char window[WINSIZE]; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
struct access { |
| 71 |
int have; |
| 72 |
int size; |
| 73 |
struct point *list; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
local void free_index(struct access *index) |
| 78 |
{ |
| 79 |
if (index != NULL) { |
| 80 |
free(index->list); |
| 81 |
free(index); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
local struct access *addpoint(struct access *index, int bits, |
| 88 |
off_t in, off_t out, unsigned left, unsigned char *window) |
| 89 |
{ |
| 90 |
struct point *next; |
| 91 |
|
| 92 |
|
| 93 |
if (index == NULL) { |
| 94 |
index = malloc(sizeof(struct access)); |
| 95 |
if (index == NULL) return NULL; |
| 96 |
index->list = malloc(sizeof(struct point) << 3); |
| 97 |
if (index->list == NULL) { |
| 98 |
free(index); |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
index->size = 8; |
| 102 |
index->have = 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
else if (index->have == index->size) { |
| 107 |
index->size <<= 1; |
| 108 |
next = realloc(index->list, sizeof(struct point) * index->size); |
| 109 |
if (next == NULL) { |
| 110 |
free_index(index); |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
index->list = next; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
next = index->list + index->have; |
| 118 |
next->bits = bits; |
| 119 |
next->in = in; |
| 120 |
next->out = out; |
| 121 |
if (left) |
| 122 |
memcpy(next->window, window + WINSIZE - left, left); |
| 123 |
if (left < WINSIZE) |
| 124 |
memcpy(next->window + left, window, WINSIZE - left); |
| 125 |
index->have++; |
| 126 |
|
| 127 |
|
| 128 |
return index; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
local int build_index(FILE *in, off_t span, struct access **built) |
| 140 |
{ |
| 141 |
int ret; |
| 142 |
off_t totin, totout; |
| 143 |
off_t last; |
| 144 |
struct access *index; |
| 145 |
z_stream strm; |
| 146 |
unsigned char input[CHUNK]; |
| 147 |
unsigned char window[WINSIZE]; |
| 148 |
|
| 149 |
|
| 150 |
strm.zalloc = Z_NULL; |
| 151 |
strm.zfree = Z_NULL; |
| 152 |
strm.opaque = Z_NULL; |
| 153 |
strm.avail_in = 0; |
| 154 |
strm.next_in = Z_NULL; |
| 155 |
ret = inflateInit2(&strm, 47); |
| 156 |
if (ret != Z_OK) |
| 157 |
return ret; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
totin = totout = last = 0; |
| 163 |
index = NULL; |
| 164 |
strm.avail_out = 0; |
| 165 |
do { |
| 166 |
|
| 167 |
strm.avail_in = fread(input, 1, CHUNK, in); |
| 168 |
if (ferror(in)) { |
| 169 |
ret = Z_ERRNO; |
| 170 |
goto build_index_error; |
| 171 |
} |
| 172 |
if (strm.avail_in == 0) { |
| 173 |
ret = Z_DATA_ERROR; |
| 174 |
goto build_index_error; |
| 175 |
} |
| 176 |
strm.next_in = input; |
| 177 |
|
| 178 |
|
| 179 |
do { |
| 180 |
|
| 181 |
if (strm.avail_out == 0) { |
| 182 |
strm.avail_out = WINSIZE; |
| 183 |
strm.next_out = window; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
totin += strm.avail_in; |
| 189 |
totout += strm.avail_out; |
| 190 |
ret = inflate(&strm, Z_BLOCK); |
| 191 |
totin -= strm.avail_in; |
| 192 |
totout -= strm.avail_out; |
| 193 |
if (ret == Z_NEED_DICT) |
| 194 |
ret = Z_DATA_ERROR; |
| 195 |
if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR) |
| 196 |
goto build_index_error; |
| 197 |
if (ret == Z_STREAM_END) |
| 198 |
break; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if ((strm.data_type & 128) && !(strm.data_type & 64) && |
| 210 |
(totout == 0 || totout - last > span)) { |
| 211 |
index = addpoint(index, strm.data_type & 7, totin, |
| 212 |
totout, strm.avail_out, window); |
| 213 |
if (index == NULL) { |
| 214 |
ret = Z_MEM_ERROR; |
| 215 |
goto build_index_error; |
| 216 |
} |
| 217 |
last = totout; |
| 218 |
} |
| 219 |
} while (strm.avail_in != 0); |
| 220 |
} while (ret != Z_STREAM_END); |
| 221 |
|
| 222 |
|
| 223 |
(void)inflateEnd(&strm); |
| 224 |
index = realloc(index, sizeof(struct point) * index->have); |
| 225 |
index->size = index->have; |
| 226 |
*built = index; |
| 227 |
return index->size; |
| 228 |
|
| 229 |
|
| 230 |
build_index_error: |
| 231 |
(void)inflateEnd(&strm); |
| 232 |
if (index != NULL) |
| 233 |
free_index(index); |
| 234 |
return ret; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
local int extract(FILE *in, struct access *index, off_t offset, |
| 245 |
unsigned char *buf, int len) |
| 246 |
{ |
| 247 |
int ret, skip; |
| 248 |
z_stream strm; |
| 249 |
struct point *here; |
| 250 |
unsigned char input[CHUNK]; |
| 251 |
unsigned char discard[WINSIZE]; |
| 252 |
|
| 253 |
|
| 254 |
if (len < 0) |
| 255 |
return 0; |
| 256 |
|
| 257 |
|
| 258 |
here = index->list; |
| 259 |
ret = index->have; |
| 260 |
while (--ret && here[1].out <= offset) |
| 261 |
here++; |
| 262 |
|
| 263 |
|
| 264 |
strm.zalloc = Z_NULL; |
| 265 |
strm.zfree = Z_NULL; |
| 266 |
strm.opaque = Z_NULL; |
| 267 |
strm.avail_in = 0; |
| 268 |
strm.next_in = Z_NULL; |
| 269 |
ret = inflateInit2(&strm, -15); |
| 270 |
if (ret != Z_OK) |
| 271 |
return ret; |
| 272 |
ret = fseeko(in, here->in - (here->bits ? 1 : 0), SEEK_SET); |
| 273 |
if (ret == -1) |
| 274 |
goto extract_ret; |
| 275 |
if (here->bits) { |
| 276 |
ret = getc(in); |
| 277 |
if (ret == -1) { |
| 278 |
ret = ferror(in) ? Z_ERRNO : Z_DATA_ERROR; |
| 279 |
goto extract_ret; |
| 280 |
} |
| 281 |
(void)inflatePrime(&strm, here->bits, ret >> (8 - here->bits)); |
| 282 |
} |
| 283 |
(void)inflateSetDictionary(&strm, here->window, WINSIZE); |
| 284 |
|
| 285 |
|
| 286 |
offset -= here->out; |
| 287 |
strm.avail_in = 0; |
| 288 |
skip = 1; |
| 289 |
do { |
| 290 |
|
| 291 |
if (offset == 0 && skip) { |
| 292 |
strm.avail_out = len; |
| 293 |
strm.next_out = buf; |
| 294 |
skip = 0; |
| 295 |
} |
| 296 |
if (offset > WINSIZE) { |
| 297 |
strm.avail_out = WINSIZE; |
| 298 |
strm.next_out = discard; |
| 299 |
offset -= WINSIZE; |
| 300 |
} |
| 301 |
else if (offset != 0) { |
| 302 |
strm.avail_out = (unsigned)offset; |
| 303 |
strm.next_out = discard; |
| 304 |
offset = 0; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
do { |
| 309 |
if (strm.avail_in == 0) { |
| 310 |
strm.avail_in = fread(input, 1, CHUNK, in); |
| 311 |
if (ferror(in)) { |
| 312 |
ret = Z_ERRNO; |
| 313 |
goto extract_ret; |
| 314 |
} |
| 315 |
if (strm.avail_in == 0) { |
| 316 |
ret = Z_DATA_ERROR; |
| 317 |
goto extract_ret; |
| 318 |
} |
| 319 |
strm.next_in = input; |
| 320 |
} |
| 321 |
ret = inflate(&strm, Z_NO_FLUSH); |
| 322 |
if (ret == Z_NEED_DICT) |
| 323 |
ret = Z_DATA_ERROR; |
| 324 |
if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR) |
| 325 |
goto extract_ret; |
| 326 |
if (ret == Z_STREAM_END) |
| 327 |
break; |
| 328 |
} while (strm.avail_out != 0); |
| 329 |
|
| 330 |
|
| 331 |
if (ret == Z_STREAM_END) |
| 332 |
break; |
| 333 |
|
| 334 |
|
| 335 |
} while (skip); |
| 336 |
|
| 337 |
|
| 338 |
ret = skip ? 0 : len - strm.avail_out; |
| 339 |
|
| 340 |
|
| 341 |
extract_ret: |
| 342 |
(void)inflateEnd(&strm); |
| 343 |
return ret; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
int main(int argc, char **argv) |
| 350 |
{ |
| 351 |
int len; |
| 352 |
off_t offset; |
| 353 |
FILE *in; |
| 354 |
struct access *index; |
| 355 |
unsigned char buf[CHUNK]; |
| 356 |
|
| 357 |
|
| 358 |
if (argc != 2) { |
| 359 |
fprintf(stderr, "usage: zran file.gz\n"); |
| 360 |
return 1; |
| 361 |
} |
| 362 |
in = fopen(argv[1], "rb"); |
| 363 |
if (in == NULL) { |
| 364 |
fprintf(stderr, "zran: could not open %s for reading\n", argv[1]); |
| 365 |
return 1; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
len = build_index(in, SPAN, &index); |
| 370 |
if (len < 0) { |
| 371 |
fclose(in); |
| 372 |
switch (len) { |
| 373 |
case Z_MEM_ERROR: |
| 374 |
fprintf(stderr, "zran: out of memory\n"); |
| 375 |
break; |
| 376 |
case Z_DATA_ERROR: |
| 377 |
fprintf(stderr, "zran: compressed data error in %s\n", argv[1]); |
| 378 |
break; |
| 379 |
case Z_ERRNO: |
| 380 |
fprintf(stderr, "zran: read error on %s\n", argv[1]); |
| 381 |
break; |
| 382 |
default: |
| 383 |
fprintf(stderr, "zran: error %d while building index\n", len); |
| 384 |
} |
| 385 |
return 1; |
| 386 |
} |
| 387 |
fprintf(stderr, "zran: built index with %d access points\n", len); |
| 388 |
|
| 389 |
|
| 390 |
offset = (index->list[index->have - 1].out << 1) / 3; |
| 391 |
len = extract(in, index, offset, buf, CHUNK); |
| 392 |
if (len < 0) |
| 393 |
fprintf(stderr, "zran: extraction failed: %s error\n", |
| 394 |
len == Z_MEM_ERROR ? "out of memory" : "input corrupted"); |
| 395 |
else { |
| 396 |
fwrite(buf, 1, len, stdout); |
| 397 |
fprintf(stderr, "zran: extracted %d bytes at %llu\n", len, offset); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
free_index(index); |
| 402 |
fclose(in); |
| 403 |
return 0; |
| 404 |
} |