zran.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* zran.h -- example of zlib/gzip stream indexing and random access
  2. * Copyright (C) 2005, 2012, 2018 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. * Version 1.2 14 Oct 2018 Mark Adler */
  5. #include <stdio.h>
  6. #include "zlib.h"
  7. /* Access point list. */
  8. struct deflate_index {
  9. int have; /* number of list entries */
  10. int gzip; /* 1 if the index is of a gzip file, 0 if it is of a
  11. zlib stream */
  12. off_t length; /* total length of uncompressed data */
  13. void *list; /* allocated list of entries */
  14. };
  15. /* Make one entire pass through a zlib or gzip compressed stream and build an
  16. index, with access points about every span bytes of uncompressed output.
  17. gzip files with multiple members are indexed in their entirety. span should
  18. be chosen to balance the speed of random access against the memory
  19. requirements of the list, about 32K bytes per access point. The return value
  20. is the number of access points on success (>= 1), Z_MEM_ERROR for out of
  21. memory, Z_DATA_ERROR for an error in the input file, or Z_ERRNO for a file
  22. read error. On success, *built points to the resulting index. */
  23. int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);
  24. /* Deallocate an index built by deflate_index_build() */
  25. void deflate_index_free(struct deflate_index *index);
  26. /* Use the index to read len bytes from offset into buf. Return bytes read or
  27. negative for error (Z_DATA_ERROR or Z_MEM_ERROR). If data is requested past
  28. the end of the uncompressed data, then deflate_index_extract() will return a
  29. value less than len, indicating how much was actually read into buf. This
  30. function should not return a data error unless the file was modified since
  31. the index was generated, since deflate_index_build() validated all of the
  32. input. deflate_index_extract() will return Z_ERRNO if there is an error on
  33. reading or seeking the input file. */
  34. int deflate_index_extract(FILE *in, struct deflate_index *index, off_t offset,
  35. unsigned char *buf, int len);