archive_check_magic.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*-
  2. * Copyright (c) 2003-2010 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. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: head/lib/libarchive/archive_check_magic.c 201089 2009-12-28 02:20:23Z kientzle $");
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30. #include <stdio.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #if defined(_WIN32) && !defined(__CYGWIN__)
  41. #include <windows.h>
  42. #include <winbase.h>
  43. #endif
  44. #include "archive_private.h"
  45. static void
  46. errmsg(const char *m)
  47. {
  48. size_t s = strlen(m);
  49. ssize_t written;
  50. while (s > 0) {
  51. written = write(2, m, strlen(m));
  52. if (written <= 0)
  53. return;
  54. m += written;
  55. s -= written;
  56. }
  57. }
  58. static __LA_DEAD void
  59. diediedie(void)
  60. {
  61. #if defined(_WIN32) && !defined(__CYGWIN__) && defined(_DEBUG)
  62. /* Cause a breakpoint exception */
  63. DebugBreak();
  64. #endif
  65. abort(); /* Terminate the program abnormally. */
  66. }
  67. static const char *
  68. state_name(unsigned s)
  69. {
  70. switch (s) {
  71. case ARCHIVE_STATE_NEW: return ("new");
  72. case ARCHIVE_STATE_HEADER: return ("header");
  73. case ARCHIVE_STATE_DATA: return ("data");
  74. case ARCHIVE_STATE_EOF: return ("eof");
  75. case ARCHIVE_STATE_CLOSED: return ("closed");
  76. case ARCHIVE_STATE_FATAL: return ("fatal");
  77. default: return ("??");
  78. }
  79. }
  80. static const char *
  81. archive_handle_type_name(unsigned m)
  82. {
  83. switch (m) {
  84. case ARCHIVE_WRITE_MAGIC: return ("archive_write");
  85. case ARCHIVE_READ_MAGIC: return ("archive_read");
  86. case ARCHIVE_WRITE_DISK_MAGIC: return ("archive_write_disk");
  87. case ARCHIVE_READ_DISK_MAGIC: return ("archive_read_disk");
  88. case ARCHIVE_MATCH_MAGIC: return ("archive_match");
  89. default: return NULL;
  90. }
  91. }
  92. static char *
  93. write_all_states(char *buff, unsigned int states)
  94. {
  95. unsigned int lowbit;
  96. buff[0] = '\0';
  97. /* A trick for computing the lowest set bit. */
  98. while ((lowbit = states & (1 + ~states)) != 0) {
  99. states &= ~lowbit; /* Clear the low bit. */
  100. strcat(buff, state_name(lowbit));
  101. if (states != 0)
  102. strcat(buff, "/");
  103. }
  104. return buff;
  105. }
  106. /*
  107. * Check magic value and current state.
  108. * Magic value mismatches are fatal and result in calls to abort().
  109. * State mismatches return ARCHIVE_FATAL.
  110. * Otherwise, returns ARCHIVE_OK.
  111. *
  112. * This is designed to catch serious programming errors that violate
  113. * the libarchive API.
  114. */
  115. int
  116. __archive_check_magic(struct archive *a, unsigned int magic,
  117. unsigned int state, const char *function)
  118. {
  119. char states1[64];
  120. char states2[64];
  121. const char *handle_type;
  122. /*
  123. * If this isn't some form of archive handle,
  124. * then the library user has screwed up so bad that
  125. * we don't even have a reliable way to report an error.
  126. */
  127. handle_type = archive_handle_type_name(a->magic);
  128. if (!handle_type) {
  129. errmsg("PROGRAMMER ERROR: Function ");
  130. errmsg(function);
  131. errmsg(" invoked with invalid archive handle.\n");
  132. diediedie();
  133. }
  134. if (a->magic != magic) {
  135. archive_set_error(a, -1,
  136. "PROGRAMMER ERROR: Function '%s' invoked"
  137. " on '%s' archive object, which is not supported.",
  138. function,
  139. handle_type);
  140. a->state = ARCHIVE_STATE_FATAL;
  141. return (ARCHIVE_FATAL);
  142. }
  143. if ((a->state & state) == 0) {
  144. /* If we're already FATAL, don't overwrite the error. */
  145. if (a->state != ARCHIVE_STATE_FATAL)
  146. archive_set_error(a, -1,
  147. "INTERNAL ERROR: Function '%s' invoked with"
  148. " archive structure in state '%s',"
  149. " should be in state '%s'",
  150. function,
  151. write_all_states(states1, a->state),
  152. write_all_states(states2, state));
  153. a->state = ARCHIVE_STATE_FATAL;
  154. return (ARCHIVE_FATAL);
  155. }
  156. return ARCHIVE_OK;
  157. }