is_tar.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef lint
  41. FILE_RCSID("@(#)$File: is_tar.c,v 1.47 2022/09/13 18:46:07 christos Exp $")
  42. #endif
  43. #include "magic.h"
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include "tar.h"
  47. #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
  48. private int is_tar(const unsigned char *, size_t);
  49. private int from_oct(const char *, size_t); /* Decode octal number */
  50. static const char tartype[][32] = { /* should be equal to messages */
  51. "tar archive", /* found in ../magic/Magdir/archive */
  52. "POSIX tar archive",
  53. "POSIX tar archive (GNU)", /* */
  54. };
  55. protected int
  56. file_is_tar(struct magic_set *ms, const struct buffer *b)
  57. {
  58. const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
  59. size_t nbytes = b->flen;
  60. /*
  61. * Do the tar test first, because if the first file in the tar
  62. * archive starts with a dot, we can confuse it with an nroff file.
  63. */
  64. int tar;
  65. int mime = ms->flags & MAGIC_MIME;
  66. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
  67. return 0;
  68. tar = is_tar(buf, nbytes);
  69. if (tar < 1 || tar > 3)
  70. return 0;
  71. if (mime == MAGIC_MIME_ENCODING)
  72. return 1;
  73. if (file_printf(ms, "%s", mime ? "application/x-tar" :
  74. tartype[tar - 1]) == -1)
  75. return -1;
  76. return 1;
  77. }
  78. /*
  79. * Return
  80. * 0 if the checksum is bad (i.e., probably not a tar archive),
  81. * 1 for old UNIX tar file,
  82. * 2 for Unix Std (POSIX) tar file,
  83. * 3 for GNU tar file.
  84. */
  85. private int
  86. is_tar(const unsigned char *buf, size_t nbytes)
  87. {
  88. static const char gpkg_match[] = "/gpkg-1";
  89. const union record *header = RCAST(const union record *,
  90. RCAST(const void *, buf));
  91. size_t i;
  92. int sum, recsum;
  93. const unsigned char *p, *ep;
  94. const char *nulp;
  95. if (nbytes < sizeof(*header))
  96. return 0;
  97. /* If the file looks like Gentoo GLEP 78 binary package (GPKG),
  98. * don't waste time on further checks and fall back to magic rules.
  99. */
  100. nulp = CAST(const char *,
  101. memchr(header->header.name, 0, sizeof(header->header.name)));
  102. if (nulp != NULL && nulp >= header->header.name + sizeof(gpkg_match) &&
  103. memcmp(nulp - sizeof(gpkg_match) + 1, gpkg_match,
  104. sizeof(gpkg_match)) == 0)
  105. return 0;
  106. recsum = from_oct(header->header.chksum, sizeof(header->header.chksum));
  107. sum = 0;
  108. p = header->charptr;
  109. ep = header->charptr + sizeof(*header);
  110. while (p < ep)
  111. sum += *p++;
  112. /* Adjust checksum to count the "chksum" field as blanks. */
  113. for (i = 0; i < sizeof(header->header.chksum); i++)
  114. sum -= header->header.chksum[i];
  115. sum += ' ' * sizeof(header->header.chksum);
  116. if (sum != recsum)
  117. return 0; /* Not a tar archive */
  118. if (strncmp(header->header.magic, GNUTMAGIC,
  119. sizeof(header->header.magic)) == 0)
  120. return 3; /* GNU Unix Standard tar archive */
  121. if (strncmp(header->header.magic, TMAGIC,
  122. sizeof(header->header.magic)) == 0)
  123. return 2; /* Unix Standard tar archive */
  124. return 1; /* Old fashioned tar archive */
  125. }
  126. /*
  127. * Quick and dirty octal conversion.
  128. *
  129. * Result is -1 if the field is invalid (all blank, or non-octal).
  130. */
  131. private int
  132. from_oct(const char *where, size_t digs)
  133. {
  134. int value;
  135. if (digs == 0)
  136. return -1;
  137. while (isspace(CAST(unsigned char, *where))) { /* Skip spaces */
  138. where++;
  139. if (digs-- == 0)
  140. return -1; /* All blank field */
  141. }
  142. value = 0;
  143. while (digs > 0 && isodigit(*where)) { /* Scan til non-octal */
  144. value = (value << 3) | (*where++ - '0');
  145. digs--;
  146. }
  147. if (digs > 0 && *where && !isspace(CAST(unsigned char, *where)))
  148. return -1; /* Ended on non-(space/NUL) */
  149. return value;
  150. }