is_tar.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * is_tar() -- figure out whether file is a tar archive.
  30. *
  31. * Stolen (by the author!) from the public domain tar program:
  32. * Public Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
  33. *
  34. * @(#)list.c 1.18 9/23/86 Public Domain - gnu
  35. *
  36. * Comments changed and some code/comments reformatted
  37. * for file command by Ian Darwin.
  38. */
  39. #include "file.h"
  40. #include "magic.h"
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <sys/types.h>
  44. #include "tar.h"
  45. #ifndef lint
  46. FILE_RCSID("@(#)$File: is_tar.c,v 1.29 2007/10/17 19:33:31 christos Exp $")
  47. #endif
  48. #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
  49. private int is_tar(const unsigned char *, size_t);
  50. private int from_oct(int, const char *); /* Decode octal number */
  51. static const char *tartype[] = {
  52. "tar archive",
  53. "POSIX tar archive",
  54. "POSIX tar archive (GNU)",
  55. };
  56. protected int
  57. file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
  58. {
  59. /*
  60. * Do the tar test first, because if the first file in the tar
  61. * archive starts with a dot, we can confuse it with an nroff file.
  62. */
  63. int tar = is_tar(buf, nbytes);
  64. int mime = ms->flags & MAGIC_MIME;
  65. if (tar < 1 || tar > 3)
  66. return 0;
  67. if (mime == MAGIC_MIME_ENCODING)
  68. return 0;
  69. if (file_printf(ms, mime ? "application/x-tar" :
  70. tartype[tar - 1]) == -1)
  71. return -1;
  72. return 1;
  73. }
  74. /*
  75. * Return
  76. * 0 if the checksum is bad (i.e., probably not a tar archive),
  77. * 1 for old UNIX tar file,
  78. * 2 for Unix Std (POSIX) tar file.
  79. */
  80. private int
  81. is_tar(const unsigned char *buf, size_t nbytes)
  82. {
  83. const union record *header = (const union record *)(const void *)buf;
  84. int i;
  85. int sum, recsum;
  86. const char *p;
  87. if (nbytes < sizeof(union record))
  88. return 0;
  89. recsum = from_oct(8, header->header.chksum);
  90. sum = 0;
  91. p = header->charptr;
  92. for (i = sizeof(union record); --i >= 0;) {
  93. /*
  94. * We cannot use unsigned char here because of old compilers,
  95. * e.g. V7.
  96. */
  97. sum += 0xFF & *p++;
  98. }
  99. /* Adjust checksum to count the "chksum" field as blanks. */
  100. for (i = sizeof(header->header.chksum); --i >= 0;)
  101. sum -= 0xFF & header->header.chksum[i];
  102. sum += ' '* sizeof header->header.chksum;
  103. if (sum != recsum)
  104. return 0; /* Not a tar archive */
  105. if (strcmp(header->header.magic, GNUTMAGIC) == 0)
  106. return 3; /* GNU Unix Standard tar archive */
  107. if (strcmp(header->header.magic, TMAGIC) == 0)
  108. return 2; /* Unix Standard tar archive */
  109. return 1; /* Old fashioned tar archive */
  110. }
  111. /*
  112. * Quick and dirty octal conversion.
  113. *
  114. * Result is -1 if the field is invalid (all blank, or nonoctal).
  115. */
  116. private int
  117. from_oct(int digs, const char *where)
  118. {
  119. int value;
  120. while (isspace((unsigned char)*where)) { /* Skip spaces */
  121. where++;
  122. if (--digs <= 0)
  123. return -1; /* All blank field */
  124. }
  125. value = 0;
  126. while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
  127. value = (value << 3) | (*where++ - '0');
  128. --digs;
  129. }
  130. if (digs > 0 && *where && !isspace((unsigned char)*where))
  131. return -1; /* Ended on non-space/nul */
  132. return value;
  133. }