toybox/toys/posix/file.c
<<
>>
Prefs
   1/* file.c - describe file type
   2 *
   3 * Copyright 2016 The Android Open Source Project
   4 *
   5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html
   6
   7USE_FILE(NEWTOY(file, "<1hL[!hL]", TOYFLAG_USR|TOYFLAG_BIN))
   8
   9config FILE
  10  bool "file"
  11  default y
  12  help
  13    usage: file [-hL] [file...]
  14
  15    Examine the given files and describe their content types.
  16
  17    -h  Don't follow symlinks (default)
  18    -L  Follow symlinks
  19*/
  20
  21#define FOR_file
  22#include "toys.h"
  23
  24GLOBALS(
  25  int max_name_len;
  26
  27  off_t len;
  28)
  29
  30// We don't trust elf.h to be there, and two codepaths for 32/64 is awkward
  31// anyway, so calculate struct offsets manually. (It's a fixed ABI.)
  32static void do_elf_file(int fd)
  33{
  34  int endian = toybuf[5], bits = toybuf[4], i, j, dynamic = 0, stripped = 1,
  35      phentsize, phnum, shsize, shnum;
  36  int64_t (*elf_int)(void *ptr, unsigned size);
  37  // Values from include/linux/elf-em.h (plus arch/*/include/asm/elf.h)
  38  // Names are linux/arch/ directory (sometimes before 32/64 bit merges)
  39  struct {int val; char *name;} type[] = {{0x9026, "alpha"}, {93, "arc"},
  40    {195, "arcv2"}, {40, "arm"}, {183, "arm64"}, {0x18ad, "avr32"},
  41    {247, "bpf"}, {106, "blackfin"}, {140, "c6x"}, {23, "cell"}, {76, "cris"},
  42    {252, "csky"}, {0x5441, "frv"}, {46, "h8300"}, {164, "hexagon"},
  43    {50, "ia64"}, {88, "m32r"}, {0x9041, "m32r"}, {4, "m68k"}, {174, "metag"},
  44    {189, "microblaze"}, {0xbaab, "microblaze-old"}, {8, "mips"},
  45    {10, "mips-old"}, {89, "mn10300"}, {0xbeef, "mn10300-old"}, {113, "nios2"},
  46    {92, "openrisc"}, {0x8472, "openrisc-old"}, {15, "parisc"}, {20, "ppc"},
  47    {21, "ppc64"}, {243, "riscv"}, {22, "s390"}, {0xa390, "s390-old"},
  48    {135, "score"}, {42, "sh"}, {2, "sparc"}, {18, "sparc8+"}, {43, "sparc9"},
  49    {188, "tile"}, {191, "tilegx"}, {3, "386"}, {6, "486"}, {62, "x86-64"},
  50    {94, "xtensa"}, {0xabc7, "xtensa-old"}
  51  };
  52  char *map = 0;
  53  off_t phoff, shoff;
  54
  55  printf("ELF ");
  56  elf_int = (endian==2) ? peek_be : peek_le;
  57
  58  // executable type
  59  i = elf_int(toybuf+16, 2);
  60  if (i == 1) printf("relocatable");
  61  else if (i == 2) printf("executable");
  62  else if (i == 3) printf("shared object");
  63  else if (i == 4) printf("core dump");
  64  else printf("(bad type %d)", i);
  65  if (elf_int(toybuf+36+12*(bits==2), 4) & 0x8000) printf(" (fdpic)");
  66  printf(", ");
  67
  68  // "64-bit"
  69  if (bits == 1) printf("32-bit ");
  70  else if (bits == 2) printf("64-bit ");
  71  else {
  72    printf("(bad class %d) ", bits);
  73    bits = 0;
  74  }
  75
  76  // "LSB"
  77  if (endian == 1) printf("LSB ");
  78  else if (endian == 2) printf("MSB ");
  79  else {
  80    printf("(bad endian %d) \n", endian);
  81    endian = 0;
  82  }
  83
  84  // e_machine, ala "x86", from big table above
  85  j = elf_int(toybuf+18, 2);
  86  for (i = 0; i<ARRAY_LEN(type); i++) if (j==type[i].val) break;
  87  if (i<ARRAY_LEN(type)) printf("%s", type[i].name);
  88  else printf("(unknown arch %d)", j);
  89
  90  bits--;
  91  // If what we've seen so far doesn't seem consistent, bail.
  92  if (!((bits&1)==bits && endian)) {
  93    printf(", corrupt?\n");
  94    return;
  95  }
  96
  97  // Parsing ELF means following tables that may point to data earlier in
  98  // the file, so sequential reading involves buffering unknown amounts of
  99  // data. Just skip it if we can't mmap.
 100  if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0)))
 101    goto bad;
 102
 103  // Stash what we need from the header; it's okay to reuse toybuf after this.
 104  phentsize = elf_int(toybuf+42+12*bits, 2);
 105  phnum = elf_int(toybuf+44+12*bits, 2);
 106  phoff = elf_int(toybuf+28+4*bits, 4+4*bits);
 107  shsize = elf_int(toybuf+46+12*bits, 2);
 108  shnum = elf_int(toybuf+48+12*bits, 2);
 109  shoff = elf_int(toybuf+32+8*bits, 4+4*bits);
 110
 111  // With binutils, phentsize seems to only be non-zero if phnum is non-zero.
 112  // Such ELF files are rare, but do exist. (Android's crtbegin files, say.)
 113  if (phnum && (phentsize != 32+24*bits)) {
 114    printf(", corrupt phentsize %d?", phentsize);
 115    goto bad;
 116  }
 117
 118  // Parsing ELF means following tables that may point to data earlier in
 119  // the file, so sequential reading involves buffering unknown amounts of
 120  // data. Just skip it if we can't mmap.
 121  if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0)))
 122    goto bad;
 123
 124  // We need to read the phdrs for dynamic vs static.
 125  // (Note: fields got reordered for 64 bit)
 126  if (phoff+phnum*phentsize>TT.len) goto bad;
 127  for (i = 0; i<phnum; i++) {
 128    char *phdr = map+phoff+i*phentsize;
 129    int p_type = elf_int(phdr, 4);
 130    long long p_offset, p_filesz;
 131
 132    if (p_type==2 /*PT_DYNAMIC*/) dynamic = 1;
 133    if (p_type!=3 /*PT_INTERP*/ && p_type!=4 /*PT_NOTE*/) continue;
 134
 135    j = bits+1;
 136    p_offset = elf_int(phdr+4*j, 4*j);
 137    p_filesz = elf_int(phdr+16*j, 4*j);
 138
 139    if (p_type==3 /*PT_INTERP*/) {
 140      if (p_offset+p_filesz>TT.len) goto bad;
 141      printf(", dynamic (%.*s)", (int)p_filesz, map+p_offset);
 142    }
 143  }
 144  if (!dynamic) printf(", static");
 145
 146  // We need to read the shdrs for stripped/unstripped and any notes.
 147  // Notes are in program headers *and* section headers, but some files don't
 148  // contain program headers, so we prefer to check here.
 149  // (Note: fields got reordered for 64 bit)
 150  if (shoff+i*shnum>TT.len) goto bad;
 151  for (i = 0; i<shnum; i++) {
 152    char *shdr = map+shoff+i*shsize;
 153    int sh_type = elf_int(shdr+4, 4);
 154    long sh_offset = elf_int(shdr+8+8*(bits+1), 4*(bits+1));
 155    int sh_size = elf_int(shdr+8+12*(bits+1), 4);
 156
 157    if (sh_type == 2 /*SHT_SYMTAB*/) {
 158      stripped = 0;
 159      break;
 160    } else if (sh_type == 7 /*SHT_NOTE*/) {
 161      char *note = map+sh_offset;
 162
 163      // An ELF note is a sequence of entries, each consisting of an
 164      // ndhr followed by n_namesz+n_descsz bytes of data (each of those
 165      // rounded up to the next 4 bytes, without this being reflected in
 166      // the header byte counts themselves).
 167      while (sh_size >= 3*4) { // Don't try to read a truncated entry.
 168        unsigned n_namesz, n_descsz, n_type, notesz;
 169
 170        if (sh_offset+sh_size>TT.len) goto bad;
 171
 172        n_namesz = elf_int(note, 4);
 173        n_descsz = elf_int(note+4, 4);
 174        n_type = elf_int(note+8, 4);
 175        notesz = 3*4 + ((n_namesz+3)&~3) + ((n_descsz+3)&~3);
 176
 177        // Does the claimed size of this note actually fit in the section?
 178        if (notesz > sh_size) goto bad;
 179
 180        if (n_namesz==4 && !memcmp(note+12, "GNU", 4)) {
 181          if (n_type==3 /*NT_GNU_BUILD_ID*/) {
 182            printf(", BuildID=");
 183            for (j = 0; j < n_descsz; ++j) printf("%02x", note[16 + j]);
 184          }
 185        } else if (n_namesz==8 && !memcmp(note+12, "Android", 8)) {
 186          if (n_type==1 /*.android.note.ident*/ && n_descsz >= 4) {
 187            printf(", for Android %d", (int)elf_int(note+20, 4));
 188            // NDK r14 and later also include NDK version info. OS binaries
 189            // and binaries built by older NDKs don't have this.
 190            if (n_descsz >= 4+64+64)
 191              printf(", built by NDK %.64s (%.64s)", note+24, note+24+64);
 192          }
 193        }
 194
 195        note += notesz;
 196        sh_size -= notesz;
 197      }
 198    }
 199  }
 200  printf(", %sstripped", stripped ? "" : "not ");
 201bad:
 202  xputc('\n');
 203
 204  if (map && map != MAP_FAILED) munmap(map, TT.len);
 205}
 206
 207static void do_regular_file(int fd, char *name)
 208{
 209  char *s;
 210  int len, magic;
 211
 212  // zero through elf shnum, just in case
 213  memset(toybuf, 0, 80);
 214  if ((len = readall(fd, s = toybuf, sizeof(toybuf)))<0) perror_msg("%s", name);
 215
 216  if (!len) xputs("empty");
 217  // 45 bytes: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
 218  else if (len>=45 && strstart(&s, "\177ELF")) do_elf_file(fd);
 219  else if (len>=8 && strstart(&s, "!<arch>\n")) xprintf("ar archive\n");
 220  else if (len>28 && strstart(&s, "\x89PNG\x0d\x0a\x1a\x0a")) {
 221    // PNG is big-endian: https://www.w3.org/TR/PNG/#7Integers-and-byte-order
 222    int chunk_length = peek_be(s, 4);
 223
 224    xprintf("PNG image data");
 225
 226    // The IHDR chunk comes first: https://www.w3.org/TR/PNG/#11IHDR
 227    s += 4;
 228    if (chunk_length == 13 && strstart(&s, "IHDR")) {
 229      // https://www.w3.org/TR/PNG/#6Colour-values
 230      char *c = 0, *colors[] = {"grayscale", 0, "color RGB", "indexed color",
 231                                "grayscale with alpha", 0, "color RGBA"};
 232
 233      if (s[9]<ARRAY_LEN(colors)) c = colors[s[9]];
 234      if (!c) c = "unknown";
 235
 236      xprintf(", %d x %d, %d-bit/%s, %sinterlaced", (int)peek_be(s, 4),
 237        (int)peek_be(s+4, 4), s[8], c, s[12] ? "" : "non-");
 238    }
 239
 240    xputc('\n');
 241
 242  // https://www.w3.org/Graphics/GIF/spec-gif89a.txt
 243  } else if (len>16 && (strstart(&s, "GIF87a") || strstart(&s, "GIF89a")))
 244    xprintf("GIF image data, %d x %d\n",
 245      (int)peek_le(s, 2), (int)peek_le(s+8, 2));
 246
 247  // TODO: parsing JPEG for width/height is harder than GIF or PNG.
 248  else if (len>32 && !memcmp(toybuf, "\xff\xd8", 2)) xputs("JPEG image data");
 249
 250  // https://en.wikipedia.org/wiki/Java_class_file#General_layout
 251  else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe"))
 252    xprintf("Java class file, version %d.%d (Java 1.%d)\n",
 253      (int)peek_be(s+2, 2), (int)peek_be(s, 2), (int)peek_be(s+2, 2)-44);
 254
 255  // https://source.android.com/devices/tech/dalvik/dex-format#dex-file-magic
 256  else if (len>8 && strstart(&s, "dex\n") && s[3] == 0)
 257    xprintf("Android dex file, version %s\n", s);
 258
 259  // https://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt
 260  // the lengths for cpio are size of header + 9 bytes, since any valid
 261  // cpio archive ends with a record for "TARGET!!!"
 262  else if (len>85 && strstart(&s, "07070")) {
 263    char *cpioformat = "unknown type";
 264
 265    if (toybuf[5] == '7') cpioformat = "pre-SVR4 or odc";
 266    else if (toybuf[5] == '1') cpioformat = "SVR4 with no CRC";
 267    else if (toybuf[5] == '2') cpioformat = "SVR4 with CRC";
 268    xprintf("ASCII cpio archive (%s)\n", cpioformat);
 269  } else if (len>33 && (magic=peek(&s,2), magic==0143561 || magic==070707)) {
 270    if (magic == 0143561) printf("byte-swapped ");
 271    xprintf("cpio archive\n");
 272  // tar archive (ustar/pax or gnu)
 273  } else if (len>500 && !strncmp(s+257, "ustar", 5))
 274    xprintf("POSIX tar archive%s\n", strncmp(s+262,"  ",2)?"":" (GNU)");
 275  // zip/jar/apk archive, ODF/OOXML document, or such
 276  else if (len>5 && strstart(&s, "PK\03\04")) {
 277    int ver = toybuf[4];
 278
 279    xprintf("Zip archive data");
 280    if (ver) xprintf(", requires at least v%d.%d to extract", ver/10, ver%10);
 281    xputc('\n');
 282  } else if (len>4 && strstart(&s, "BZh") && isdigit(*s))
 283    xprintf("bzip2 compressed data, block size = %c00k\n", *s);
 284  else if (len>10 && strstart(&s, "\x1f\x8b")) xputs("gzip compressed data");
 285  else if (len>32 && !memcmp(s+1, "\xfa\xed\xfe", 3)) {
 286    int bit = s[0]=='\xce'?32:64;
 287    char *what;
 288
 289    xprintf("Mach-O %d-bit ", bit);
 290
 291    if (s[4] == 7) what = (bit==32)?"x86":"x86-";
 292    else if (s[4] == 12) what = "arm";
 293    else if (s[4] == 18) what = "ppc";
 294    else what = NULL;
 295    if (what) xprintf("%s%s ", what, (bit==32)?"":"64");
 296    else xprintf("(bad arch %d) ", s[4]);
 297
 298    if (s[12] == 1) what = "object";
 299    else if (s[12] == 2) what = "executable";
 300    else if (s[12] == 6) what = "shared library";
 301    else what = NULL;
 302    if (what) xprintf("%s\n", what);
 303    else xprintf("(bad type %d)\n", s[9]);
 304  } else if (len>36 && !memcmp(s, "OggS\x00\x02", 6)) {
 305    xprintf("Ogg data");
 306    // https://wiki.xiph.org/MIMETypesCodecs
 307    if (!memcmp(s+28, "CELT    ", 8)) xprintf(", celt audio");
 308    else if (!memcmp(s+28, "CMML    ", 8)) xprintf(", cmml text");
 309    else if (!memcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video");
 310    else if (!memcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio");
 311    else if (!memcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video");
 312    else if (!memcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text");
 313    else if (!memcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text");
 314    else if (!memcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video");
 315    else if (!memcmp(s+28, "OpusHead", 8)) xprintf(", opus audio");
 316    else if (!memcmp(s+28, "PCM     ", 8)) xprintf(", pcm audio");
 317    else if (!memcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video");
 318    else if (!memcmp(s+28, "Speex   ", 8)) xprintf(", speex audio");
 319    else if (!memcmp(s+28, "\x80theora", 7)) xprintf(", theora video");
 320    else if (!memcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio");
 321    else if (!memcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video");
 322    xputc('\n');
 323  } else if (len>32 && !memcmp(s, "RIF", 3) && !memcmp(s+8, "WAVEfmt ", 8)) {
 324    // https://en.wikipedia.org/wiki/WAV
 325    int le = (s[3] == 'F');
 326    int format = le ? peek_le(s+20,2) : peek_be(s+20,2);
 327    int channels = le ? peek_le(s+22,2) : peek_be(s+22,2);
 328    int hz = le ? peek_le(s+24,4) : peek_be(s+24,4);
 329    int bits = le ? peek_le(s+34,2) : peek_be(s+34,2);
 330
 331    xprintf("WAV audio, %s, ", le ? "LE" : "BE");
 332    if (bits != 0) xprintf("%d-bit, ", bits);
 333    if (channels==1||channels==2) xprintf("%s, ", channels==1?"mono":"stereo");
 334    else xprintf("%d-channel, ", channels);
 335    xprintf("%d Hz, ", hz);
 336    // See https://tools.ietf.org/html/rfc2361, though there appear to be bugs
 337    // in the RFC. This assumes wikipedia's example files are more correct.
 338    if (format == 0x01) xprintf("PCM");
 339    else if (format == 0x03) xprintf("IEEE float");
 340    else if (format == 0x06) xprintf("A-law");
 341    else if (format == 0x07) xprintf("ยต-law");
 342    else if (format == 0x11) xprintf("ADPCM");
 343    else if (format == 0x22) xprintf("Truespeech");
 344    else if (format == 0x31) xprintf("GSM");
 345    else if (format == 0x55) xprintf("MP3");
 346    else if (format == 0x70) xprintf("CELP");
 347    else if (format == 0xfffe) xprintf("extensible");
 348    else xprintf("unknown format %d", format);
 349    xputc('\n');
 350  } else if (len>12 && !memcmp(s, "\x00\x01\x00\x00", 4)) {
 351    xputs("TrueType font");
 352  } else if (len>12 && !memcmp(s, "ttcf\x00", 5)) {
 353    xprintf("TrueType font collection, version %d, %d fonts\n",
 354            (int)peek_be(s+4, 2), (int)peek_be(s+8, 4));
 355  } else if (len>4 && !memcmp(s, "BC\xc0\xde", 4)) {
 356    xputs("LLVM IR bitcode");
 357  } else if (strstart(&s, "-----BEGIN CERTIFICATE-----")) {
 358    xputs("PEM certificate");
 359
 360  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx
 361  } else if (len>0x70 && !memcmp(s, "MZ", 2) &&
 362      (magic=peek_le(s+0x3c,4))<len-4 && !memcmp(s+magic, "\x50\x45\0\0", 4)) {
 363    xprintf("MS PE32%s executable %s", (peek_le(s+magic+24, 2)==0x20b)?"+":"",
 364        (peek_le(s+magic+22, 2)&0x2000)?"(DLL) ":"");
 365    if (peek_le(s+magic+20, 2)>70) {
 366      char *types[] = {0, "native", "GUI", "console", "OS/2", "driver", "CE",
 367          "EFI", "EFI boot", "EFI runtime", "EFI ROM", "XBOX", 0, "boot"};
 368      int type = peek_le(s+magic+92, 2);
 369      char *name = (type>0 && type<ARRAY_LEN(types))?types[type]:0;
 370
 371      xprintf("(%s) ", name?name:"unknown");
 372    }
 373    xprintf("%s\n", (peek_le(s+magic+4, 2)==0x14c)?"x86":"x86-64");
 374
 375    // https://en.wikipedia.org/wiki/BMP_file_format
 376  } else if (len > 0x32 && !memcmp(s, "BM", 2) && !memcmp(s+6, "\0\0\0\0", 4)) {
 377    int w = peek_le(s+0x12,4), h = peek_le(s+0x16,4), bpp = peek_le(s+0x1c,2);
 378
 379    xprintf("BMP image, %d x %d, %d bpp\n", w, h, bpp);
 380  } else {
 381    char *what = 0;
 382    int i, bytes;
 383
 384    // If shell script, report which interpreter
 385    if (len>3 && strstart(&s, "#!")) {
 386      // Whitespace is allowed between the #! and the interpreter
 387      while (isspace(*s)) s++;
 388      if (strstart(&s, "/usr/bin/env")) while (isspace(*s)) s++;
 389      for (what = s; (s-toybuf)<len && !isspace(*s); s++);
 390      strcpy(s, " script");
 391
 392    // Distinguish ASCII text, UTF-8 text, or data
 393    } else for (i = 0; i<len; ++i) {
 394      if (!(isprint(toybuf[i]) || isspace(toybuf[i]))) {
 395        wchar_t wc;
 396        if ((bytes = utf8towc(&wc, s+i, len-i))>0 && wcwidth(wc)>=0) {
 397          i += bytes-1;
 398          if (!what) what = "UTF-8 text";
 399        } else {
 400          what = "data";
 401          break;
 402        }
 403      }
 404    }
 405    xputs(what ? what : "ASCII text");
 406  }
 407}
 408
 409void file_main(void)
 410{
 411  char **arg;
 412
 413  for (arg = toys.optargs; *arg; ++arg) {
 414    int name_len = strlen(*arg);
 415
 416    if (name_len > TT.max_name_len) TT.max_name_len = name_len;
 417  }
 418
 419  // Can't use loopfiles here because it doesn't call function when can't open
 420  for (arg = toys.optargs; *arg; arg++) {
 421    char *name = *arg, *what = "cannot open";
 422    struct stat sb;
 423    int fd = !strcmp(name, "-");
 424
 425    xprintf("%s: %*s", name, (int)(TT.max_name_len - strlen(name)), "");
 426
 427    sb.st_size = 0;
 428    if (fd || !((toys.optflags & FLAG_L) ? stat : lstat)(name, &sb)) {
 429      if (fd || S_ISREG(sb.st_mode)) {
 430        TT.len = sb.st_size;
 431        // This test identifies an empty file we don't have permission to read
 432        if (!fd && !sb.st_size) what = "empty";
 433        else if ((fd = openro(name, O_RDONLY)) != -1) {
 434          do_regular_file(fd, name);
 435          if (fd) close(fd);
 436          continue;
 437        }
 438      } else if (S_ISFIFO(sb.st_mode)) what = "fifo";
 439      else if (S_ISBLK(sb.st_mode)) what = "block special";
 440      else if (S_ISCHR(sb.st_mode)) what = "character special";
 441      else if (S_ISDIR(sb.st_mode)) what = "directory";
 442      else if (S_ISSOCK(sb.st_mode)) what = "socket";
 443      else if (S_ISLNK(sb.st_mode)) what = "symbolic link";
 444      else what = "unknown";
 445    }
 446
 447    xputs(what);
 448  }
 449}
 450