archive_read_private.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 ARCHIVE_READ_PRIVATE_H_INCLUDED
  28. #define ARCHIVE_READ_PRIVATE_H_INCLUDED
  29. #ifndef __LIBARCHIVE_BUILD
  30. #ifndef __LIBARCHIVE_TEST
  31. #error This header is only to be used internally to libarchive.
  32. #endif
  33. #endif
  34. #include "archive.h"
  35. #include "archive_string.h"
  36. #include "archive_private.h"
  37. struct archive_read;
  38. struct archive_read_filter_bidder;
  39. struct archive_read_filter;
  40. struct archive_read_filter_bidder_vtable {
  41. /* Taste the upstream filter to see if we handle this. */
  42. int (*bid)(struct archive_read_filter_bidder *,
  43. struct archive_read_filter *);
  44. /* Initialize a newly-created filter. */
  45. int (*init)(struct archive_read_filter *);
  46. /* Release the bidder's configuration data. */
  47. void (*free)(struct archive_read_filter_bidder *);
  48. };
  49. /*
  50. * How bidding works for filters:
  51. * * The bid manager initializes the client-provided reader as the
  52. * first filter.
  53. * * It invokes the bidder for each registered filter with the
  54. * current head filter.
  55. * * The bidders can use archive_read_filter_ahead() to peek ahead
  56. * at the incoming data to compose their bids.
  57. * * The bid manager creates a new filter structure for the winning
  58. * bidder and gives the winning bidder a chance to initialize it.
  59. * * The new filter becomes the new top filter and we repeat the
  60. * process.
  61. * This ends only when no bidder provides a non-zero bid. Then
  62. * we perform a similar dance with the registered format handlers.
  63. */
  64. struct archive_read_filter_bidder {
  65. /* Configuration data for the bidder. */
  66. void *data;
  67. /* Name of the filter */
  68. const char *name;
  69. const struct archive_read_filter_bidder_vtable *vtable;
  70. };
  71. struct archive_read_filter_vtable {
  72. /* Return next block. */
  73. ssize_t (*read)(struct archive_read_filter *, const void **);
  74. /* Close (just this filter) and free(self). */
  75. int (*close)(struct archive_read_filter *self);
  76. /* Read any header metadata if available. */
  77. int (*read_header)(struct archive_read_filter *self, struct archive_entry *entry);
  78. };
  79. /*
  80. * This structure is allocated within the archive_read core
  81. * and initialized by archive_read and the init() method of the
  82. * corresponding bidder above.
  83. */
  84. struct archive_read_filter {
  85. int64_t position;
  86. /* Essentially all filters will need these values, so
  87. * just declare them here. */
  88. struct archive_read_filter_bidder *bidder; /* My bidder. */
  89. struct archive_read_filter *upstream; /* Who I read from. */
  90. struct archive_read *archive; /* Associated archive. */
  91. const struct archive_read_filter_vtable *vtable;
  92. /* My private data. */
  93. void *data;
  94. const char *name;
  95. int code;
  96. int can_skip;
  97. int can_seek;
  98. /* Used by reblocking logic. */
  99. char *buffer;
  100. size_t buffer_size;
  101. char *next; /* Current read location. */
  102. size_t avail; /* Bytes in my buffer. */
  103. const void *client_buff; /* Client buffer information. */
  104. size_t client_total;
  105. const char *client_next;
  106. size_t client_avail;
  107. char end_of_file;
  108. char closed;
  109. char fatal;
  110. };
  111. /*
  112. * The client looks a lot like a filter, so we just wrap it here.
  113. *
  114. * TODO: Make archive_read_filter and archive_read_client identical so
  115. * that users of the library can easily register their own
  116. * transformation filters. This will probably break the API/ABI and
  117. * so should be deferred at least until libarchive 3.0.
  118. */
  119. struct archive_read_data_node {
  120. int64_t begin_position;
  121. int64_t total_size;
  122. void *data;
  123. };
  124. struct archive_read_client {
  125. archive_open_callback *opener;
  126. archive_read_callback *reader;
  127. archive_skip_callback *skipper;
  128. archive_seek_callback *seeker;
  129. archive_close_callback *closer;
  130. archive_switch_callback *switcher;
  131. unsigned int nodes;
  132. unsigned int cursor;
  133. int64_t position;
  134. struct archive_read_data_node *dataset;
  135. };
  136. struct archive_read_passphrase {
  137. char *passphrase;
  138. struct archive_read_passphrase *next;
  139. };
  140. struct archive_read_extract {
  141. struct archive *ad; /* archive_write_disk object */
  142. /* Progress function invoked during extract. */
  143. void (*extract_progress)(void *);
  144. void *extract_progress_user_data;
  145. };
  146. struct archive_read {
  147. struct archive archive;
  148. struct archive_entry *entry;
  149. /* Dev/ino of the archive being read/written. */
  150. int skip_file_set;
  151. int64_t skip_file_dev;
  152. int64_t skip_file_ino;
  153. /* Callbacks to open/read/write/close client archive streams. */
  154. struct archive_read_client client;
  155. /* Registered filter bidders. */
  156. struct archive_read_filter_bidder bidders[16];
  157. /* Last filter in chain */
  158. struct archive_read_filter *filter;
  159. /* Whether to bypass filter bidding process */
  160. int bypass_filter_bidding;
  161. /* File offset of beginning of most recently-read header. */
  162. int64_t header_position;
  163. /* Nodes and offsets of compressed data block */
  164. unsigned int data_start_node;
  165. unsigned int data_end_node;
  166. /*
  167. * Format detection is mostly the same as compression
  168. * detection, with one significant difference: The bidders
  169. * use the read_ahead calls above to examine the stream rather
  170. * than having the supervisor hand them a block of data to
  171. * examine.
  172. */
  173. struct archive_format_descriptor {
  174. void *data;
  175. const char *name;
  176. int (*bid)(struct archive_read *, int best_bid);
  177. int (*options)(struct archive_read *, const char *key,
  178. const char *value);
  179. int (*read_header)(struct archive_read *, struct archive_entry *);
  180. int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *);
  181. int (*read_data_skip)(struct archive_read *);
  182. int64_t (*seek_data)(struct archive_read *, int64_t, int);
  183. int (*cleanup)(struct archive_read *);
  184. int (*format_capabilties)(struct archive_read *);
  185. int (*has_encrypted_entries)(struct archive_read *);
  186. } formats[16];
  187. struct archive_format_descriptor *format; /* Active format. */
  188. /*
  189. * Various information needed by archive_extract.
  190. */
  191. struct archive_read_extract *extract;
  192. int (*cleanup_archive_extract)(struct archive_read *);
  193. /*
  194. * Decryption passphrase.
  195. */
  196. struct {
  197. struct archive_read_passphrase *first;
  198. struct archive_read_passphrase **last;
  199. int candidate;
  200. archive_passphrase_callback *callback;
  201. void *client_data;
  202. } passphrases;
  203. };
  204. int __archive_read_register_format(struct archive_read *a,
  205. void *format_data,
  206. const char *name,
  207. int (*bid)(struct archive_read *, int),
  208. int (*options)(struct archive_read *, const char *, const char *),
  209. int (*read_header)(struct archive_read *, struct archive_entry *),
  210. int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
  211. int (*read_data_skip)(struct archive_read *),
  212. int64_t (*seek_data)(struct archive_read *, int64_t, int),
  213. int (*cleanup)(struct archive_read *),
  214. int (*format_capabilities)(struct archive_read *),
  215. int (*has_encrypted_entries)(struct archive_read *));
  216. int __archive_read_register_bidder(struct archive_read *a,
  217. void *bidder_data,
  218. const char *name,
  219. const struct archive_read_filter_bidder_vtable *vtable);
  220. const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
  221. const void *__archive_read_filter_ahead(struct archive_read_filter *,
  222. size_t, ssize_t *);
  223. int64_t __archive_read_seek(struct archive_read*, int64_t, int);
  224. int64_t __archive_read_filter_seek(struct archive_read_filter *, int64_t, int);
  225. int64_t __archive_read_consume(struct archive_read *, int64_t);
  226. int64_t __archive_read_filter_consume(struct archive_read_filter *, int64_t);
  227. int __archive_read_header(struct archive_read *, struct archive_entry *);
  228. int __archive_read_program(struct archive_read_filter *, const char *);
  229. void __archive_read_free_filters(struct archive_read *);
  230. struct archive_read_extract *__archive_read_get_extract(struct archive_read *);
  231. /*
  232. * Get a decryption passphrase.
  233. */
  234. void __archive_read_reset_passphrase(struct archive_read *a);
  235. const char * __archive_read_next_passphrase(struct archive_read *a);
  236. #endif