archive_read_private.h 9.1 KB

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