archive_read_private.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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: src/lib/libarchive/archive_read_private.h,v 1.7 2008/12/06 06:45:15 kientzle Exp $
  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 reads the first block from the current source.
  41. * * It shows that block to each registered bidder.
  42. * * The bid manager creates a new filter structure for the winning
  43. * bidder and gives the winning bidder a chance to initialize it.
  44. * * The new filter becomes the top filter in the archive_read structure
  45. * and we repeat the process.
  46. * This ends only when no bidder provides a non-zero bid.
  47. */
  48. struct archive_read_filter_bidder {
  49. /* Configuration data for the bidder. */
  50. void *data;
  51. /* Taste the upstream filter to see if we handle this. */
  52. int (*bid)(struct archive_read_filter_bidder *,
  53. struct archive_read_filter *);
  54. /* Initialize a newly-created filter. */
  55. int (*init)(struct archive_read_filter *);
  56. /* Set an option for the filter bidder. */
  57. int (*options)(struct archive_read_filter_bidder *,
  58. const char *key, const char *value);
  59. /* Release the bidder's configuration data. */
  60. int (*free)(struct archive_read_filter_bidder *);
  61. };
  62. /*
  63. * This structure is allocated within the archive_read core
  64. * and initialized by archive_read and the init() method of the
  65. * corresponding bidder above.
  66. */
  67. struct archive_read_filter {
  68. /* Essentially all filters will need these values, so
  69. * just declare them here. */
  70. struct archive_read_filter_bidder *bidder; /* My bidder. */
  71. struct archive_read_filter *upstream; /* Who I read from. */
  72. struct archive_read *archive; /* Associated archive. */
  73. /* Return next block. */
  74. ssize_t (*read)(struct archive_read_filter *, const void **);
  75. /* Skip forward this many bytes. */
  76. int64_t (*skip)(struct archive_read_filter *self, int64_t request);
  77. /* Close (just this filter) and free(self). */
  78. int (*close)(struct archive_read_filter *self);
  79. /* My private data. */
  80. void *data;
  81. const char *name;
  82. int code;
  83. /* Used by reblocking logic. */
  84. char *buffer;
  85. size_t buffer_size;
  86. char *next; /* Current read location. */
  87. size_t avail; /* Bytes in my buffer. */
  88. const void *client_buff; /* Client buffer information. */
  89. size_t client_total;
  90. const char *client_next;
  91. size_t client_avail;
  92. int64_t position;
  93. char end_of_file;
  94. char fatal;
  95. };
  96. /*
  97. * The client looks a lot like a filter, so we just wrap it here.
  98. *
  99. * TODO: Make archive_read_filter and archive_read_client identical so
  100. * that users of the library can easily register their own
  101. * transformation filters. This will probably break the API/ABI and
  102. * so should be deferred at least until libarchive 3.0.
  103. */
  104. struct archive_read_client {
  105. archive_read_callback *reader;
  106. archive_skip_callback *skipper;
  107. archive_close_callback *closer;
  108. };
  109. struct archive_read {
  110. struct archive archive;
  111. struct archive_entry *entry;
  112. /* Dev/ino of the archive being read/written. */
  113. dev_t skip_file_dev;
  114. ino_t skip_file_ino;
  115. /*
  116. * Used by archive_read_data() to track blocks and copy
  117. * data to client buffers, filling gaps with zero bytes.
  118. */
  119. const char *read_data_block;
  120. off_t read_data_offset;
  121. off_t read_data_output_offset;
  122. size_t read_data_remaining;
  123. /* Callbacks to open/read/write/close client archive stream. */
  124. struct archive_read_client client;
  125. /* Registered filter bidders. */
  126. struct archive_read_filter_bidder bidders[8];
  127. /* Last filter in chain */
  128. struct archive_read_filter *filter;
  129. /* File offset of beginning of most recently-read header. */
  130. off_t header_position;
  131. /*
  132. * Format detection is mostly the same as compression
  133. * detection, with one significant difference: The bidders
  134. * use the read_ahead calls above to examine the stream rather
  135. * than having the supervisor hand them a block of data to
  136. * examine.
  137. */
  138. struct archive_format_descriptor {
  139. void *data;
  140. const char *name;
  141. int (*bid)(struct archive_read *);
  142. int (*options)(struct archive_read *, const char *key,
  143. const char *value);
  144. int (*read_header)(struct archive_read *, struct archive_entry *);
  145. int (*read_data)(struct archive_read *, const void **, size_t *, off_t *);
  146. int (*read_data_skip)(struct archive_read *);
  147. int (*cleanup)(struct archive_read *);
  148. } formats[8];
  149. struct archive_format_descriptor *format; /* Active format. */
  150. /*
  151. * Various information needed by archive_extract.
  152. */
  153. struct extract *extract;
  154. int (*cleanup_archive_extract)(struct archive_read *);
  155. };
  156. int __archive_read_register_format(struct archive_read *a,
  157. void *format_data,
  158. const char *name,
  159. int (*bid)(struct archive_read *),
  160. int (*options)(struct archive_read *, const char *, const char *),
  161. int (*read_header)(struct archive_read *, struct archive_entry *),
  162. int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
  163. int (*read_data_skip)(struct archive_read *),
  164. int (*cleanup)(struct archive_read *));
  165. struct archive_read_filter_bidder
  166. *__archive_read_get_bidder(struct archive_read *a);
  167. const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
  168. const void *__archive_read_filter_ahead(struct archive_read_filter *,
  169. size_t, ssize_t *);
  170. ssize_t __archive_read_consume(struct archive_read *, size_t);
  171. ssize_t __archive_read_filter_consume(struct archive_read_filter *, size_t);
  172. int64_t __archive_read_skip(struct archive_read *, int64_t);
  173. int64_t __archive_read_skip_lenient(struct archive_read *, int64_t);
  174. int64_t __archive_read_filter_skip(struct archive_read_filter *, int64_t);
  175. int __archive_read_program(struct archive_read_filter *, const char *);
  176. #endif