archive_read_private.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * $FreeBSD: head/lib/libarchive/archive_read_private.h 201088 2009-12-28 02:18:55Z kientzle $
  26. */
  27. #ifndef __LIBARCHIVE_BUILD
  28. #error This header is only to be used internally to libarchive.
  29. #endif
  30. #ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
  31. #define ARCHIVE_READ_PRIVATE_H_INCLUDED
  32. #include "archive.h"
  33. #include "archive_string.h"
  34. #include "archive_private.h"
  35. struct archive_read;
  36. struct archive_read_filter_bidder;
  37. struct archive_read_filter;
  38. /*
  39. * How bidding works for filters:
  40. * * The bid manager initializes the client-provided reader as the
  41. * first filter.
  42. * * It invokes the bidder for each registered filter with the
  43. * current head filter.
  44. * * The bidders can use archive_read_filter_ahead() to peek ahead
  45. * at the incoming data to compose their bids.
  46. * * The bid manager creates a new filter structure for the winning
  47. * bidder and gives the winning bidder a chance to initialize it.
  48. * * The new filter becomes the new top filter and we repeat the
  49. * process.
  50. * This ends only when no bidder provides a non-zero bid. Then
  51. * we perform a similar dance with the registered format handlers.
  52. */
  53. struct archive_read_filter_bidder {
  54. /* Configuration data for the bidder. */
  55. void *data;
  56. /* Name of the filter */
  57. const char *name;
  58. /* Taste the upstream filter to see if we handle this. */
  59. int (*bid)(struct archive_read_filter_bidder *,
  60. struct archive_read_filter *);
  61. /* Initialize a newly-created filter. */
  62. int (*init)(struct archive_read_filter *);
  63. /* Set an option for the filter bidder. */
  64. int (*options)(struct archive_read_filter_bidder *,
  65. const char *key, const char *value);
  66. /* Release the bidder's configuration data. */
  67. int (*free)(struct archive_read_filter_bidder *);
  68. };
  69. /*
  70. * This structure is allocated within the archive_read core
  71. * and initialized by archive_read and the init() method of the
  72. * corresponding bidder above.
  73. */
  74. struct archive_read_filter {
  75. int64_t position;
  76. /* Essentially all filters will need these values, so
  77. * just declare them here. */
  78. struct archive_read_filter_bidder *bidder; /* My bidder. */
  79. struct archive_read_filter *upstream; /* Who I read from. */
  80. struct archive_read *archive; /* Associated archive. */
  81. /* Open a block for reading */
  82. int (*open)(struct archive_read_filter *self);
  83. /* Return next block. */
  84. ssize_t (*read)(struct archive_read_filter *, const void **);
  85. /* Skip forward this many bytes. */
  86. int64_t (*skip)(struct archive_read_filter *self, int64_t request);
  87. /* Seek to an absolute location. */
  88. int64_t (*seek)(struct archive_read_filter *self, int64_t offset, int whence);
  89. /* Close (just this filter) and free(self). */
  90. int (*close)(struct archive_read_filter *self);
  91. /* Function that handles switching from reading one block to the next/prev */
  92. int (*sswitch)(struct archive_read_filter *self, unsigned int iindex);
  93. /* My private data. */
  94. void *data;
  95. const char *name;
  96. int code;
  97. /* Used by reblocking logic. */
  98. char *buffer;
  99. size_t buffer_size;
  100. char *next; /* Current read location. */
  101. size_t avail; /* Bytes in my buffer. */
  102. const void *client_buff; /* Client buffer information. */
  103. size_t client_total;
  104. const char *client_next;
  105. size_t client_avail;
  106. char end_of_file;
  107. char closed;
  108. char fatal;
  109. };
  110. /*
  111. * The client looks a lot like a filter, so we just wrap it here.
  112. *
  113. * TODO: Make archive_read_filter and archive_read_client identical so
  114. * that users of the library can easily register their own
  115. * transformation filters. This will probably break the API/ABI and
  116. * so should be deferred at least until libarchive 3.0.
  117. */
  118. struct archive_read_data_node {
  119. int64_t begin_position;
  120. int64_t total_size;
  121. void *data;
  122. };
  123. struct archive_read_client {
  124. archive_open_callback *opener;
  125. archive_read_callback *reader;
  126. archive_skip_callback *skipper;
  127. archive_seek_callback *seeker;
  128. archive_close_callback *closer;
  129. archive_switch_callback *switcher;
  130. unsigned int nodes;
  131. unsigned int cursor;
  132. int64_t position;
  133. struct archive_read_data_node *dataset;
  134. };
  135. struct archive_read {
  136. struct archive archive;
  137. struct archive_entry *entry;
  138. /* Dev/ino of the archive being read/written. */
  139. int skip_file_set;
  140. int64_t skip_file_dev;
  141. int64_t skip_file_ino;
  142. /*
  143. * Used by archive_read_data() to track blocks and copy
  144. * data to client buffers, filling gaps with zero bytes.
  145. */
  146. const char *read_data_block;
  147. int64_t read_data_offset;
  148. int64_t read_data_output_offset;
  149. size_t read_data_remaining;
  150. /*
  151. * Used by formats/filters to determine the amount of data
  152. * requested from a call to archive_read_data(). This is only
  153. * useful when the format/filter has seek support.
  154. */
  155. char read_data_is_posix_read;
  156. size_t read_data_requested;
  157. /* Callbacks to open/read/write/close client archive streams. */
  158. struct archive_read_client client;
  159. /* Registered filter bidders. */
  160. struct archive_read_filter_bidder bidders[14];
  161. /* Last filter in chain */
  162. struct archive_read_filter *filter;
  163. /* Whether to bypass filter bidding process */
  164. int bypass_filter_bidding;
  165. /* File offset of beginning of most recently-read header. */
  166. int64_t header_position;
  167. /* Nodes and offsets of compressed data block */
  168. unsigned int data_start_node;
  169. unsigned int data_end_node;
  170. /*
  171. * Format detection is mostly the same as compression
  172. * detection, with one significant difference: The bidders
  173. * use the read_ahead calls above to examine the stream rather
  174. * than having the supervisor hand them a block of data to
  175. * examine.
  176. */
  177. struct archive_format_descriptor {
  178. void *data;
  179. const char *name;
  180. int (*bid)(struct archive_read *, int best_bid);
  181. int (*options)(struct archive_read *, const char *key,
  182. const char *value);
  183. int (*read_header)(struct archive_read *, struct archive_entry *);
  184. int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *);
  185. int (*read_data_skip)(struct archive_read *);
  186. int64_t (*seek_data)(struct archive_read *, int64_t, int);
  187. int (*cleanup)(struct archive_read *);
  188. } formats[16];
  189. struct archive_format_descriptor *format; /* Active format. */
  190. /*
  191. * Various information needed by archive_extract.
  192. */
  193. struct extract *extract;
  194. int (*cleanup_archive_extract)(struct archive_read *);
  195. };
  196. int __archive_read_register_format(struct archive_read *a,
  197. void *format_data,
  198. const char *name,
  199. int (*bid)(struct archive_read *, int),
  200. int (*options)(struct archive_read *, const char *, const char *),
  201. int (*read_header)(struct archive_read *, struct archive_entry *),
  202. int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
  203. int (*read_data_skip)(struct archive_read *),
  204. int64_t (*seek_data)(struct archive_read *, int64_t, int),
  205. int (*cleanup)(struct archive_read *));
  206. int __archive_read_get_bidder(struct archive_read *a,
  207. struct archive_read_filter_bidder **bidder);
  208. const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
  209. const void *__archive_read_filter_ahead(struct archive_read_filter *,
  210. size_t, ssize_t *);
  211. int64_t __archive_read_seek(struct archive_read*, int64_t, int);
  212. int64_t __archive_read_filter_seek(struct archive_read_filter *, int64_t, int);
  213. int64_t __archive_read_consume(struct archive_read *, int64_t);
  214. int64_t __archive_read_filter_consume(struct archive_read_filter *, int64_t);
  215. int __archive_read_program(struct archive_read_filter *, const char *);
  216. void __archive_read_free_filters(struct archive_read *);
  217. int __archive_read_close_filters(struct archive_read *);
  218. #endif