123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "zlib.h"
- #define local static
- #define SPAN 1048576L
- #define WINSIZE 32768U
- #define CHUNK 16384
- struct point {
- off_t out;
- off_t in;
- int bits;
- unsigned char window[WINSIZE];
- };
- struct access {
- int have;
- int size;
- struct point *list;
- };
- local void free_index(struct access *index)
- {
- if (index != NULL) {
- free(index->list);
- free(index);
- }
- }
- local struct access *addpoint(struct access *index, int bits,
- off_t in, off_t out, unsigned left, unsigned char *window)
- {
- struct point *next;
-
- if (index == NULL) {
- index = malloc(sizeof(struct access));
- if (index == NULL) return NULL;
- index->list = malloc(sizeof(struct point) << 3);
- if (index->list == NULL) {
- free(index);
- return NULL;
- }
- index->size = 8;
- index->have = 0;
- }
-
- else if (index->have == index->size) {
- index->size <<= 1;
- next = realloc(index->list, sizeof(struct point) * index->size);
- if (next == NULL) {
- free_index(index);
- return NULL;
- }
- index->list = next;
- }
-
- next = index->list + index->have;
- next->bits = bits;
- next->in = in;
- next->out = out;
- if (left)
- memcpy(next->window, window + WINSIZE - left, left);
- if (left < WINSIZE)
- memcpy(next->window + left, window, WINSIZE - left);
- index->have++;
-
- return index;
- }
- local int build_index(FILE *in, off_t span, struct access **built)
- {
- int ret;
- off_t totin, totout;
- off_t last;
- struct access *index;
- z_stream strm;
- unsigned char input[CHUNK];
- unsigned char window[WINSIZE];
-
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit2(&strm, 47);
- if (ret != Z_OK)
- return ret;
-
- totin = totout = last = 0;
- index = NULL;
- strm.avail_out = 0;
- do {
-
- strm.avail_in = fread(input, 1, CHUNK, in);
- if (ferror(in)) {
- ret = Z_ERRNO;
- goto build_index_error;
- }
- if (strm.avail_in == 0) {
- ret = Z_DATA_ERROR;
- goto build_index_error;
- }
- strm.next_in = input;
-
- do {
-
- if (strm.avail_out == 0) {
- strm.avail_out = WINSIZE;
- strm.next_out = window;
- }
-
- totin += strm.avail_in;
- totout += strm.avail_out;
- ret = inflate(&strm, Z_BLOCK);
- totin -= strm.avail_in;
- totout -= strm.avail_out;
- if (ret == Z_NEED_DICT)
- ret = Z_DATA_ERROR;
- if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
- goto build_index_error;
- if (ret == Z_STREAM_END)
- break;
-
- if ((strm.data_type & 128) && !(strm.data_type & 64) &&
- (totout == 0 || totout - last > span)) {
- index = addpoint(index, strm.data_type & 7, totin,
- totout, strm.avail_out, window);
- if (index == NULL) {
- ret = Z_MEM_ERROR;
- goto build_index_error;
- }
- last = totout;
- }
- } while (strm.avail_in != 0);
- } while (ret != Z_STREAM_END);
-
- (void)inflateEnd(&strm);
- index = realloc(index, sizeof(struct point) * index->have);
- index->size = index->have;
- *built = index;
- return index->size;
-
- build_index_error:
- (void)inflateEnd(&strm);
- if (index != NULL)
- free_index(index);
- return ret;
- }
- local int extract(FILE *in, struct access *index, off_t offset,
- unsigned char *buf, int len)
- {
- int ret, skip;
- z_stream strm;
- struct point *here;
- unsigned char input[CHUNK];
- unsigned char discard[WINSIZE];
-
- if (len < 0)
- return 0;
-
- here = index->list;
- ret = index->have;
- while (--ret && here[1].out <= offset)
- here++;
-
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit2(&strm, -15);
- if (ret != Z_OK)
- return ret;
- ret = fseeko(in, here->in - (here->bits ? 1 : 0), SEEK_SET);
- if (ret == -1)
- goto extract_ret;
- if (here->bits) {
- ret = getc(in);
- if (ret == -1) {
- ret = ferror(in) ? Z_ERRNO : Z_DATA_ERROR;
- goto extract_ret;
- }
- (void)inflatePrime(&strm, here->bits, ret >> (8 - here->bits));
- }
- (void)inflateSetDictionary(&strm, here->window, WINSIZE);
-
- offset -= here->out;
- strm.avail_in = 0;
- skip = 1;
- do {
-
- if (offset == 0 && skip) {
- strm.avail_out = len;
- strm.next_out = buf;
- skip = 0;
- }
- if (offset > WINSIZE) {
- strm.avail_out = WINSIZE;
- strm.next_out = discard;
- offset -= WINSIZE;
- }
- else if (offset != 0) {
- strm.avail_out = (unsigned)offset;
- strm.next_out = discard;
- offset = 0;
- }
-
- do {
- if (strm.avail_in == 0) {
- strm.avail_in = fread(input, 1, CHUNK, in);
- if (ferror(in)) {
- ret = Z_ERRNO;
- goto extract_ret;
- }
- if (strm.avail_in == 0) {
- ret = Z_DATA_ERROR;
- goto extract_ret;
- }
- strm.next_in = input;
- }
- ret = inflate(&strm, Z_NO_FLUSH);
- if (ret == Z_NEED_DICT)
- ret = Z_DATA_ERROR;
- if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
- goto extract_ret;
- if (ret == Z_STREAM_END)
- break;
- } while (strm.avail_out != 0);
-
- if (ret == Z_STREAM_END)
- break;
-
- } while (skip);
-
- ret = skip ? 0 : len - strm.avail_out;
-
- extract_ret:
- (void)inflateEnd(&strm);
- return ret;
- }
- int main(int argc, char **argv)
- {
- int len;
- off_t offset;
- FILE *in;
- struct access *index = NULL;
- unsigned char buf[CHUNK];
-
- if (argc != 2) {
- fprintf(stderr, "usage: zran file.gz\n");
- return 1;
- }
- in = fopen(argv[1], "rb");
- if (in == NULL) {
- fprintf(stderr, "zran: could not open %s for reading\n", argv[1]);
- return 1;
- }
-
- len = build_index(in, SPAN, &index);
- if (len < 0) {
- fclose(in);
- switch (len) {
- case Z_MEM_ERROR:
- fprintf(stderr, "zran: out of memory\n");
- break;
- case Z_DATA_ERROR:
- fprintf(stderr, "zran: compressed data error in %s\n", argv[1]);
- break;
- case Z_ERRNO:
- fprintf(stderr, "zran: read error on %s\n", argv[1]);
- break;
- default:
- fprintf(stderr, "zran: error %d while building index\n", len);
- }
- return 1;
- }
- fprintf(stderr, "zran: built index with %d access points\n", len);
-
- offset = (index->list[index->have - 1].out << 1) / 3;
- len = extract(in, index, offset, buf, CHUNK);
- if (len < 0)
- fprintf(stderr, "zran: extraction failed: %s error\n",
- len == Z_MEM_ERROR ? "out of memory" : "input corrupted");
- else {
- fwrite(buf, 1, len, stdout);
- fprintf(stderr, "zran: extracted %d bytes at %llu\n", len, offset);
- }
-
- free_index(index);
- fclose(in);
- return 0;
- }
|