archive_check_magic.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_check_magic.c,v 1.9 2008/12/06 05:52:01 kientzle Exp $");
  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 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 void
  81. write_all_states(unsigned int states)
  82. {
  83. unsigned int lowbit;
  84. /* A trick for computing the lowest set bit. */
  85. while ((lowbit = states & (-states)) != 0) {
  86. states &= ~lowbit; /* Clear the low bit. */
  87. errmsg(state_name(lowbit));
  88. if (states != 0)
  89. errmsg("/");
  90. }
  91. }
  92. /*
  93. * Check magic value and current state; bail if it isn't valid.
  94. *
  95. * This is designed to catch serious programming errors that violate
  96. * the libarchive API.
  97. */
  98. void
  99. __archive_check_magic(struct archive *a, unsigned int magic,
  100. unsigned int state, const char *function)
  101. {
  102. if (a->magic != magic) {
  103. errmsg("INTERNAL ERROR: Function ");
  104. errmsg(function);
  105. errmsg(" invoked with invalid struct archive structure.\n");
  106. diediedie();
  107. }
  108. if (state == ARCHIVE_STATE_ANY)
  109. return;
  110. if ((a->state & state) == 0) {
  111. errmsg("INTERNAL ERROR: Function '");
  112. errmsg(function);
  113. errmsg("' invoked with archive structure in state '");
  114. write_all_states(a->state);
  115. errmsg("', should be in state '");
  116. write_all_states(state);
  117. errmsg("'\n");
  118. diediedie();
  119. }
  120. }