archive_private.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_private.h 201098 2009-12-28 02:58:14Z kientzle $
  26. */
  27. #ifndef __LIBARCHIVE_BUILD
  28. #error This header is only to be used internally to libarchive.
  29. #endif
  30. #ifndef ARCHIVE_PRIVATE_H_INCLUDED
  31. #define ARCHIVE_PRIVATE_H_INCLUDED
  32. #if HAVE_ICONV_H
  33. #include <iconv.h>
  34. #endif
  35. #include "archive.h"
  36. #include "archive_string.h"
  37. #if defined(__GNUC__) && (__GNUC__ > 2 || \
  38. (__GNUC__ == 2 && __GNUC_MINOR__ >= 5))
  39. #define __LA_DEAD __attribute__((__noreturn__))
  40. #else
  41. #define __LA_DEAD
  42. #endif
  43. #define ARCHIVE_WRITE_MAGIC (0xb0c5c0deU)
  44. #define ARCHIVE_READ_MAGIC (0xdeb0c5U)
  45. #define ARCHIVE_WRITE_DISK_MAGIC (0xc001b0c5U)
  46. #define ARCHIVE_READ_DISK_MAGIC (0xbadb0c5U)
  47. #define ARCHIVE_MATCH_MAGIC (0xcad11c9U)
  48. #define ARCHIVE_STATE_NEW 1U
  49. #define ARCHIVE_STATE_HEADER 2U
  50. #define ARCHIVE_STATE_DATA 4U
  51. #define ARCHIVE_STATE_EOF 0x10U
  52. #define ARCHIVE_STATE_CLOSED 0x20U
  53. #define ARCHIVE_STATE_FATAL 0x8000U
  54. #define ARCHIVE_STATE_ANY (0xFFFFU & ~ARCHIVE_STATE_FATAL)
  55. struct archive_vtable {
  56. int (*archive_close)(struct archive *);
  57. int (*archive_free)(struct archive *);
  58. int (*archive_write_header)(struct archive *,
  59. struct archive_entry *);
  60. int (*archive_write_finish_entry)(struct archive *);
  61. ssize_t (*archive_write_data)(struct archive *,
  62. const void *, size_t);
  63. ssize_t (*archive_write_data_block)(struct archive *,
  64. const void *, size_t, int64_t);
  65. int (*archive_read_next_header)(struct archive *,
  66. struct archive_entry **);
  67. int (*archive_read_next_header2)(struct archive *,
  68. struct archive_entry *);
  69. int (*archive_read_data_block)(struct archive *,
  70. const void **, size_t *, int64_t *);
  71. int (*archive_filter_count)(struct archive *);
  72. int64_t (*archive_filter_bytes)(struct archive *, int);
  73. int (*archive_filter_code)(struct archive *, int);
  74. const char * (*archive_filter_name)(struct archive *, int);
  75. };
  76. struct archive_string_conv;
  77. struct archive {
  78. /*
  79. * The magic/state values are used to sanity-check the
  80. * client's usage. If an API function is called at a
  81. * ridiculous time, or the client passes us an invalid
  82. * pointer, these values allow me to catch that.
  83. */
  84. unsigned int magic;
  85. unsigned int state;
  86. /*
  87. * Some public API functions depend on the "real" type of the
  88. * archive object.
  89. */
  90. struct archive_vtable *vtable;
  91. int archive_format;
  92. const char *archive_format_name;
  93. int compression_code; /* Currently active compression. */
  94. const char *compression_name;
  95. /* Number of file entries processed. */
  96. int file_count;
  97. int archive_error_number;
  98. const char *error;
  99. struct archive_string error_string;
  100. char *current_code;
  101. unsigned current_codepage; /* Current ACP(ANSI CodePage). */
  102. unsigned current_oemcp; /* Current OEMCP(OEM CodePage). */
  103. struct archive_string_conv *sconv;
  104. /*
  105. * Used by archive_read_data() to track blocks and copy
  106. * data to client buffers, filling gaps with zero bytes.
  107. */
  108. const char *read_data_block;
  109. int64_t read_data_offset;
  110. int64_t read_data_output_offset;
  111. size_t read_data_remaining;
  112. /*
  113. * Used by formats/filters to determine the amount of data
  114. * requested from a call to archive_read_data(). This is only
  115. * useful when the format/filter has seek support.
  116. */
  117. char read_data_is_posix_read;
  118. size_t read_data_requested;
  119. };
  120. /* Check magic value and state; return(ARCHIVE_FATAL) if it isn't valid. */
  121. int __archive_check_magic(struct archive *, unsigned int magic,
  122. unsigned int state, const char *func);
  123. #define archive_check_magic(a, expected_magic, allowed_states, function_name) \
  124. do { \
  125. int magic_test = __archive_check_magic((a), (expected_magic), \
  126. (allowed_states), (function_name)); \
  127. if (magic_test == ARCHIVE_FATAL) \
  128. return ARCHIVE_FATAL; \
  129. } while (0)
  130. void __archive_errx(int retvalue, const char *msg) __LA_DEAD;
  131. void __archive_ensure_cloexec_flag(int fd);
  132. int __archive_mktemp(const char *tmpdir);
  133. int __archive_clean(struct archive *);
  134. void __archive_reset_read_data(struct archive *);
  135. #define err_combine(a,b) ((a) < (b) ? (a) : (b))
  136. #if defined(__BORLANDC__) || (defined(_MSC_VER) && _MSC_VER <= 1300)
  137. # define ARCHIVE_LITERAL_LL(x) x##i64
  138. # define ARCHIVE_LITERAL_ULL(x) x##ui64
  139. #else
  140. # define ARCHIVE_LITERAL_LL(x) x##ll
  141. # define ARCHIVE_LITERAL_ULL(x) x##ull
  142. #endif
  143. #endif