10f66f451Sopenharmony_ci/* file.c - describe file type 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2016 The Android Open Source Project 40f66f451Sopenharmony_ci * 50f66f451Sopenharmony_ci * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ciUSE_FILE(NEWTOY(file, "<1bhLs[!hL]", TOYFLAG_USR|TOYFLAG_BIN)) 80f66f451Sopenharmony_ci 90f66f451Sopenharmony_ciconfig FILE 100f66f451Sopenharmony_ci bool "file" 110f66f451Sopenharmony_ci default y 120f66f451Sopenharmony_ci help 130f66f451Sopenharmony_ci usage: file [-bhLs] [file...] 140f66f451Sopenharmony_ci 150f66f451Sopenharmony_ci Examine the given files and describe their content types. 160f66f451Sopenharmony_ci 170f66f451Sopenharmony_ci -b Brief (no filename) 180f66f451Sopenharmony_ci -h Don't follow symlinks (default) 190f66f451Sopenharmony_ci -L Follow symlinks 200f66f451Sopenharmony_ci -s Show block/char device contents 210f66f451Sopenharmony_ci*/ 220f66f451Sopenharmony_ci 230f66f451Sopenharmony_ci#define FOR_file 240f66f451Sopenharmony_ci#include "toys.h" 250f66f451Sopenharmony_ci 260f66f451Sopenharmony_ciGLOBALS( 270f66f451Sopenharmony_ci int max_name_len; 280f66f451Sopenharmony_ci 290f66f451Sopenharmony_ci off_t len; 300f66f451Sopenharmony_ci) 310f66f451Sopenharmony_ci 320f66f451Sopenharmony_ci// We don't trust elf.h to be there, and two codepaths for 32/64 is awkward 330f66f451Sopenharmony_ci// anyway, so calculate struct offsets manually. (It's a fixed ABI.) 340f66f451Sopenharmony_cistatic void do_elf_file(int fd) 350f66f451Sopenharmony_ci{ 360f66f451Sopenharmony_ci int endian = toybuf[5], bits = toybuf[4], i, j, dynamic = 0, stripped = 1, 370f66f451Sopenharmony_ci phentsize, phnum, shsize, shnum; 380f66f451Sopenharmony_ci int64_t (*elf_int)(void *ptr, unsigned size); 390f66f451Sopenharmony_ci // Values from include/linux/elf-em.h (plus arch/*/include/asm/elf.h) 400f66f451Sopenharmony_ci // Names are linux/arch/ directory (sometimes before 32/64 bit merges) 410f66f451Sopenharmony_ci struct {int val; char *name;} type[] = {{0x9026, "alpha"}, {93, "arc"}, 420f66f451Sopenharmony_ci {195, "arcv2"}, {40, "arm"}, {183, "arm64"}, {0x18ad, "avr32"}, 430f66f451Sopenharmony_ci {247, "bpf"}, {106, "blackfin"}, {140, "c6x"}, {23, "cell"}, {76, "cris"}, 440f66f451Sopenharmony_ci {252, "csky"}, {0x5441, "frv"}, {46, "h8300"}, {164, "hexagon"}, 450f66f451Sopenharmony_ci {50, "ia64"}, {88, "m32r"}, {0x9041, "m32r"}, {4, "m68k"}, {174, "metag"}, 460f66f451Sopenharmony_ci {189, "microblaze"}, {0xbaab, "microblaze-old"}, {8, "mips"}, 470f66f451Sopenharmony_ci {10, "mips-old"}, {89, "mn10300"}, {0xbeef, "mn10300-old"}, {113, "nios2"}, 480f66f451Sopenharmony_ci {92, "openrisc"}, {0x8472, "openrisc-old"}, {15, "parisc"}, {20, "ppc"}, 490f66f451Sopenharmony_ci {21, "ppc64"}, {243, "riscv"}, {22, "s390"}, {0xa390, "s390-old"}, 500f66f451Sopenharmony_ci {135, "score"}, {42, "sh"}, {2, "sparc"}, {18, "sparc8+"}, {43, "sparc9"}, 510f66f451Sopenharmony_ci {188, "tile"}, {191, "tilegx"}, {3, "386"}, {6, "486"}, {62, "x86-64"}, 520f66f451Sopenharmony_ci {94, "xtensa"}, {0xabc7, "xtensa-old"} 530f66f451Sopenharmony_ci }; 540f66f451Sopenharmony_ci char *map = 0; 550f66f451Sopenharmony_ci off_t phoff, shoff; 560f66f451Sopenharmony_ci 570f66f451Sopenharmony_ci printf("ELF "); 580f66f451Sopenharmony_ci elf_int = (endian==2) ? peek_be : peek_le; 590f66f451Sopenharmony_ci 600f66f451Sopenharmony_ci // executable type 610f66f451Sopenharmony_ci i = elf_int(toybuf+16, 2); 620f66f451Sopenharmony_ci if (i == 1) printf("relocatable"); 630f66f451Sopenharmony_ci else if (i == 2) printf("executable"); 640f66f451Sopenharmony_ci else if (i == 3) printf("shared object"); 650f66f451Sopenharmony_ci else if (i == 4) printf("core dump"); 660f66f451Sopenharmony_ci else printf("(bad type %d)", i); 670f66f451Sopenharmony_ci if (elf_int(toybuf+36+12*(bits==2), 4) & 0x8000) printf(" (fdpic)"); 680f66f451Sopenharmony_ci printf(", "); 690f66f451Sopenharmony_ci 700f66f451Sopenharmony_ci // "64-bit" 710f66f451Sopenharmony_ci if (bits == 1) printf("32-bit "); 720f66f451Sopenharmony_ci else if (bits == 2) printf("64-bit "); 730f66f451Sopenharmony_ci else { 740f66f451Sopenharmony_ci printf("(bad class %d) ", bits); 750f66f451Sopenharmony_ci bits = 0; 760f66f451Sopenharmony_ci } 770f66f451Sopenharmony_ci 780f66f451Sopenharmony_ci // "LSB" 790f66f451Sopenharmony_ci if (endian == 1) printf("LSB "); 800f66f451Sopenharmony_ci else if (endian == 2) printf("MSB "); 810f66f451Sopenharmony_ci else { 820f66f451Sopenharmony_ci printf("(bad endian %d) \n", endian); 830f66f451Sopenharmony_ci endian = 0; 840f66f451Sopenharmony_ci } 850f66f451Sopenharmony_ci 860f66f451Sopenharmony_ci // e_machine, ala "x86", from big table above 870f66f451Sopenharmony_ci j = elf_int(toybuf+18, 2); 880f66f451Sopenharmony_ci for (i = 0; i<ARRAY_LEN(type); i++) if (j==type[i].val) break; 890f66f451Sopenharmony_ci if (i<ARRAY_LEN(type)) printf("%s", type[i].name); 900f66f451Sopenharmony_ci else printf("(unknown arch %d)", j); 910f66f451Sopenharmony_ci 920f66f451Sopenharmony_ci bits--; 930f66f451Sopenharmony_ci // If what we've seen so far doesn't seem consistent, bail. 940f66f451Sopenharmony_ci if (!((bits&1)==bits && endian)) { 950f66f451Sopenharmony_ci printf(", corrupt?\n"); 960f66f451Sopenharmony_ci return; 970f66f451Sopenharmony_ci } 980f66f451Sopenharmony_ci 990f66f451Sopenharmony_ci // Parsing ELF means following tables that may point to data earlier in 1000f66f451Sopenharmony_ci // the file, so sequential reading involves buffering unknown amounts of 1010f66f451Sopenharmony_ci // data. Just skip it if we can't mmap. 1020f66f451Sopenharmony_ci if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0))) 1030f66f451Sopenharmony_ci goto bad; 1040f66f451Sopenharmony_ci 1050f66f451Sopenharmony_ci // Stash what we need from the header; it's okay to reuse toybuf after this. 1060f66f451Sopenharmony_ci phentsize = elf_int(toybuf+42+12*bits, 2); 1070f66f451Sopenharmony_ci phnum = elf_int(toybuf+44+12*bits, 2); 1080f66f451Sopenharmony_ci phoff = elf_int(toybuf+28+4*bits, 4+4*bits); 1090f66f451Sopenharmony_ci shsize = elf_int(toybuf+46+12*bits, 2); 1100f66f451Sopenharmony_ci shnum = elf_int(toybuf+48+12*bits, 2); 1110f66f451Sopenharmony_ci shoff = elf_int(toybuf+32+8*bits, 4+4*bits); 1120f66f451Sopenharmony_ci 1130f66f451Sopenharmony_ci // With binutils, phentsize seems to only be non-zero if phnum is non-zero. 1140f66f451Sopenharmony_ci // Such ELF files are rare, but do exist. (Android's crtbegin files, say.) 1150f66f451Sopenharmony_ci if (phnum && (phentsize != 32+24*bits)) { 1160f66f451Sopenharmony_ci printf(", corrupt phentsize %d?", phentsize); 1170f66f451Sopenharmony_ci goto bad; 1180f66f451Sopenharmony_ci } 1190f66f451Sopenharmony_ci 1200f66f451Sopenharmony_ci // Parsing ELF means following tables that may point to data earlier in 1210f66f451Sopenharmony_ci // the file, so sequential reading involves buffering unknown amounts of 1220f66f451Sopenharmony_ci // data. Just skip it if we can't mmap. 1230f66f451Sopenharmony_ci if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0))) 1240f66f451Sopenharmony_ci goto bad; 1250f66f451Sopenharmony_ci 1260f66f451Sopenharmony_ci // We need to read the phdrs for dynamic vs static. 1270f66f451Sopenharmony_ci // (Note: fields got reordered for 64 bit) 1280f66f451Sopenharmony_ci if (phoff+phnum*phentsize>TT.len) goto bad; 1290f66f451Sopenharmony_ci for (i = 0; i<phnum; i++) { 1300f66f451Sopenharmony_ci char *phdr = map+phoff+i*phentsize; 1310f66f451Sopenharmony_ci int p_type = elf_int(phdr, 4); 1320f66f451Sopenharmony_ci long long p_offset, p_filesz; 1330f66f451Sopenharmony_ci 1340f66f451Sopenharmony_ci if (p_type==2 /*PT_DYNAMIC*/) dynamic = 1; 1350f66f451Sopenharmony_ci if (p_type!=3 /*PT_INTERP*/ && p_type!=4 /*PT_NOTE*/) continue; 1360f66f451Sopenharmony_ci 1370f66f451Sopenharmony_ci j = bits+1; 1380f66f451Sopenharmony_ci p_offset = elf_int(phdr+4*j, 4*j); 1390f66f451Sopenharmony_ci p_filesz = elf_int(phdr+16*j, 4*j); 1400f66f451Sopenharmony_ci 1410f66f451Sopenharmony_ci if (p_type==3 /*PT_INTERP*/) { 1420f66f451Sopenharmony_ci if (p_offset+p_filesz>TT.len) goto bad; 1430f66f451Sopenharmony_ci printf(", dynamic (%.*s)", (int)p_filesz, map+p_offset); 1440f66f451Sopenharmony_ci } 1450f66f451Sopenharmony_ci } 1460f66f451Sopenharmony_ci if (!dynamic) printf(", static"); 1470f66f451Sopenharmony_ci 1480f66f451Sopenharmony_ci // We need to read the shdrs for stripped/unstripped and any notes. 1490f66f451Sopenharmony_ci // Notes are in program headers *and* section headers, but some files don't 1500f66f451Sopenharmony_ci // contain program headers, so we prefer to check here. 1510f66f451Sopenharmony_ci // (Note: fields got reordered for 64 bit) 1520f66f451Sopenharmony_ci if (shoff+i*shnum>TT.len) goto bad; 1530f66f451Sopenharmony_ci for (i = 0; i<shnum; i++) { 1540f66f451Sopenharmony_ci char *shdr = map+shoff+i*shsize; 1550f66f451Sopenharmony_ci int sh_type = elf_int(shdr+4, 4); 1560f66f451Sopenharmony_ci long sh_offset = elf_int(shdr+8+8*(bits+1), 4*(bits+1)); 1570f66f451Sopenharmony_ci int sh_size = elf_int(shdr+8+12*(bits+1), 4); 1580f66f451Sopenharmony_ci 1590f66f451Sopenharmony_ci if (sh_type == 2 /*SHT_SYMTAB*/) { 1600f66f451Sopenharmony_ci stripped = 0; 1610f66f451Sopenharmony_ci break; 1620f66f451Sopenharmony_ci } else if (sh_type == 7 /*SHT_NOTE*/) { 1630f66f451Sopenharmony_ci char *note = map+sh_offset; 1640f66f451Sopenharmony_ci 1650f66f451Sopenharmony_ci // An ELF note is a sequence of entries, each consisting of an 1660f66f451Sopenharmony_ci // ndhr followed by n_namesz+n_descsz bytes of data (each of those 1670f66f451Sopenharmony_ci // rounded up to the next 4 bytes, without this being reflected in 1680f66f451Sopenharmony_ci // the header byte counts themselves). 1690f66f451Sopenharmony_ci while (sh_size >= 3*4) { // Don't try to read a truncated entry. 1700f66f451Sopenharmony_ci unsigned n_namesz, n_descsz, n_type, notesz; 1710f66f451Sopenharmony_ci 1720f66f451Sopenharmony_ci if (sh_offset+sh_size>TT.len) goto bad; 1730f66f451Sopenharmony_ci 1740f66f451Sopenharmony_ci n_namesz = elf_int(note, 4); 1750f66f451Sopenharmony_ci n_descsz = elf_int(note+4, 4); 1760f66f451Sopenharmony_ci n_type = elf_int(note+8, 4); 1770f66f451Sopenharmony_ci notesz = 3*4 + ((n_namesz+3)&~3) + ((n_descsz+3)&~3); 1780f66f451Sopenharmony_ci 1790f66f451Sopenharmony_ci // Does the claimed size of this note actually fit in the section? 1800f66f451Sopenharmony_ci if (notesz > sh_size) goto bad; 1810f66f451Sopenharmony_ci 1820f66f451Sopenharmony_ci if (n_namesz==4 && !memcmp(note+12, "GNU", 4)) { 1830f66f451Sopenharmony_ci if (n_type==3 /*NT_GNU_BUILD_ID*/) { 1840f66f451Sopenharmony_ci printf(", BuildID="); 1850f66f451Sopenharmony_ci for (j = 0; j < n_descsz; ++j) printf("%02x", note[16 + j]); 1860f66f451Sopenharmony_ci } 1870f66f451Sopenharmony_ci } else if (n_namesz==8 && !memcmp(note+12, "Android", 8)) { 1880f66f451Sopenharmony_ci if (n_type==1 /*.android.note.ident*/ && n_descsz >= 4) { 1890f66f451Sopenharmony_ci printf(", for Android %d", (int)elf_int(note+20, 4)); 1900f66f451Sopenharmony_ci // NDK r14 and later also include NDK version info. OS binaries 1910f66f451Sopenharmony_ci // and binaries built by older NDKs don't have this. 1920f66f451Sopenharmony_ci if (n_descsz >= 4+64+64) 1930f66f451Sopenharmony_ci printf(", built by NDK %.64s (%.64s)", note+24, note+24+64); 1940f66f451Sopenharmony_ci } 1950f66f451Sopenharmony_ci } 1960f66f451Sopenharmony_ci 1970f66f451Sopenharmony_ci note += notesz; 1980f66f451Sopenharmony_ci sh_size -= notesz; 1990f66f451Sopenharmony_ci } 2000f66f451Sopenharmony_ci } 2010f66f451Sopenharmony_ci } 2020f66f451Sopenharmony_ci printf(", %sstripped", stripped ? "" : "not "); 2030f66f451Sopenharmony_cibad: 2040f66f451Sopenharmony_ci xputc('\n'); 2050f66f451Sopenharmony_ci 2060f66f451Sopenharmony_ci if (map && map != MAP_FAILED) munmap(map, TT.len); 2070f66f451Sopenharmony_ci} 2080f66f451Sopenharmony_ci 2090f66f451Sopenharmony_cistatic void do_regular_file(int fd, char *name) 2100f66f451Sopenharmony_ci{ 2110f66f451Sopenharmony_ci char *s; 2120f66f451Sopenharmony_ci int len, magic; 2130f66f451Sopenharmony_ci 2140f66f451Sopenharmony_ci // zero through elf shnum, just in case 2150f66f451Sopenharmony_ci memset(toybuf, 0, 80); 2160f66f451Sopenharmony_ci if ((len = readall(fd, s = toybuf, sizeof(toybuf)))<0) perror_msg("%s", name); 2170f66f451Sopenharmony_ci 2180f66f451Sopenharmony_ci if (!len) xputs("empty"); 2190f66f451Sopenharmony_ci // 45 bytes: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html 2200f66f451Sopenharmony_ci else if (len>=45 && strstart(&s, "\177ELF")) do_elf_file(fd); 2210f66f451Sopenharmony_ci else if (len>=8 && strstart(&s, "!<arch>\n")) xputs("ar archive"); 2220f66f451Sopenharmony_ci else if (len>28 && strstart(&s, "\x89PNG\x0d\x0a\x1a\x0a")) { 2230f66f451Sopenharmony_ci // PNG is big-endian: https://www.w3.org/TR/PNG/#7Integers-and-byte-order 2240f66f451Sopenharmony_ci int chunk_length = peek_be(s, 4); 2250f66f451Sopenharmony_ci 2260f66f451Sopenharmony_ci xprintf("PNG image data"); 2270f66f451Sopenharmony_ci 2280f66f451Sopenharmony_ci // The IHDR chunk comes first: https://www.w3.org/TR/PNG/#11IHDR 2290f66f451Sopenharmony_ci s += 4; 2300f66f451Sopenharmony_ci if (chunk_length == 13 && strstart(&s, "IHDR")) { 2310f66f451Sopenharmony_ci // https://www.w3.org/TR/PNG/#6Colour-values 2320f66f451Sopenharmony_ci char *c = 0, *colors[] = {"grayscale", 0, "color RGB", "indexed color", 2330f66f451Sopenharmony_ci "grayscale with alpha", 0, "color RGBA"}; 2340f66f451Sopenharmony_ci 2350f66f451Sopenharmony_ci if (s[9]<ARRAY_LEN(colors)) c = colors[s[9]]; 2360f66f451Sopenharmony_ci if (!c) c = "unknown"; 2370f66f451Sopenharmony_ci 2380f66f451Sopenharmony_ci xprintf(", %d x %d, %d-bit/%s, %sinterlaced", (int)peek_be(s, 4), 2390f66f451Sopenharmony_ci (int)peek_be(s+4, 4), s[8], c, s[12] ? "" : "non-"); 2400f66f451Sopenharmony_ci } 2410f66f451Sopenharmony_ci 2420f66f451Sopenharmony_ci xputc('\n'); 2430f66f451Sopenharmony_ci 2440f66f451Sopenharmony_ci // https://www.w3.org/Graphics/GIF/spec-gif89a.txt 2450f66f451Sopenharmony_ci } else if (len>16 && (strstart(&s, "GIF87a") || strstart(&s, "GIF89a"))) 2460f66f451Sopenharmony_ci xprintf("GIF image data, %d x %d\n", 2470f66f451Sopenharmony_ci (int)peek_le(s, 2), (int)peek_le(s+8, 2)); 2480f66f451Sopenharmony_ci 2490f66f451Sopenharmony_ci // TODO: parsing JPEG for width/height is harder than GIF or PNG. 2500f66f451Sopenharmony_ci else if (len>32 && !memcmp(toybuf, "\xff\xd8", 2)) xputs("JPEG image data"); 2510f66f451Sopenharmony_ci 2520f66f451Sopenharmony_ci // https://en.wikipedia.org/wiki/Java_class_file#General_layout 2530f66f451Sopenharmony_ci else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe")) 2540f66f451Sopenharmony_ci xprintf("Java class file, version %d.%d (Java 1.%d)\n", 2550f66f451Sopenharmony_ci (int)peek_be(s+2, 2), (int)peek_be(s, 2), (int)peek_be(s+2, 2)-44); 2560f66f451Sopenharmony_ci 2570f66f451Sopenharmony_ci // https://source.android.com/devices/tech/dalvik/dex-format#dex-file-magic 2580f66f451Sopenharmony_ci else if (len>8 && strstart(&s, "dex\n") && s[3] == 0) 2590f66f451Sopenharmony_ci xprintf("Android dex file, version %s\n", s); 2600f66f451Sopenharmony_ci 2610f66f451Sopenharmony_ci // https://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt 2620f66f451Sopenharmony_ci // the lengths for cpio are size of header + 9 bytes, since any valid 2630f66f451Sopenharmony_ci // cpio archive ends with a record for "TARGET!!!" 2640f66f451Sopenharmony_ci else if (len>85 && strstart(&s, "07070")) { 2650f66f451Sopenharmony_ci char *cpioformat = "unknown type"; 2660f66f451Sopenharmony_ci 2670f66f451Sopenharmony_ci if (toybuf[5] == '7') cpioformat = "pre-SVR4 or odc"; 2680f66f451Sopenharmony_ci else if (toybuf[5] == '1') cpioformat = "SVR4 with no CRC"; 2690f66f451Sopenharmony_ci else if (toybuf[5] == '2') cpioformat = "SVR4 with CRC"; 2700f66f451Sopenharmony_ci xprintf("ASCII cpio archive (%s)\n", cpioformat); 2710f66f451Sopenharmony_ci } else if (len>33 && (magic=peek(&s,2), magic==0143561 || magic==070707)) { 2720f66f451Sopenharmony_ci if (magic == 0143561) printf("byte-swapped "); 2730f66f451Sopenharmony_ci xputs("cpio archive"); 2740f66f451Sopenharmony_ci // tar archive (old, ustar/pax, or gnu) 2750f66f451Sopenharmony_ci } else if (len>500 && is_tar_header(s)) 2760f66f451Sopenharmony_ci xprintf("%s tar archive%s\n", s[257] ? "POSIX" : "old", 2770f66f451Sopenharmony_ci strncmp(s+262," ",2)?"":" (GNU)"); 2780f66f451Sopenharmony_ci // zip/jar/apk archive, ODF/OOXML document, or such 2790f66f451Sopenharmony_ci else if (len>5 && strstart(&s, "PK\03\04")) { 2800f66f451Sopenharmony_ci int ver = toybuf[4]; 2810f66f451Sopenharmony_ci 2820f66f451Sopenharmony_ci xprintf("Zip archive data"); 2830f66f451Sopenharmony_ci if (ver) xprintf(", requires at least v%d.%d to extract", ver/10, ver%10); 2840f66f451Sopenharmony_ci xputc('\n'); 2850f66f451Sopenharmony_ci } else if (len>4 && strstart(&s, "BZh") && isdigit(*s)) 2860f66f451Sopenharmony_ci xprintf("bzip2 compressed data, block size = %c00k\n", *s); 2870f66f451Sopenharmony_ci else if (len > 31 && peek_be(s, 7) == 0xfd377a585a0000) 2880f66f451Sopenharmony_ci xputs("xz compressed data"); 2890f66f451Sopenharmony_ci else if (len>10 && strstart(&s, "\x1f\x8b")) xputs("gzip compressed data"); 2900f66f451Sopenharmony_ci else if (len>32 && !memcmp(s+1, "\xfa\xed\xfe", 3)) { 2910f66f451Sopenharmony_ci int bit = s[0]=='\xce'?32:64; 2920f66f451Sopenharmony_ci char *what; 2930f66f451Sopenharmony_ci 2940f66f451Sopenharmony_ci xprintf("Mach-O %d-bit ", bit); 2950f66f451Sopenharmony_ci 2960f66f451Sopenharmony_ci if (s[4] == 7) what = (bit==32)?"x86":"x86-"; 2970f66f451Sopenharmony_ci else if (s[4] == 12) what = "arm"; 2980f66f451Sopenharmony_ci else if (s[4] == 18) what = "ppc"; 2990f66f451Sopenharmony_ci else what = NULL; 3000f66f451Sopenharmony_ci if (what) xprintf("%s%s ", what, (bit==32)?"":"64"); 3010f66f451Sopenharmony_ci else xprintf("(bad arch %d) ", s[4]); 3020f66f451Sopenharmony_ci 3030f66f451Sopenharmony_ci if (s[12] == 1) what = "object"; 3040f66f451Sopenharmony_ci else if (s[12] == 2) what = "executable"; 3050f66f451Sopenharmony_ci else if (s[12] == 6) what = "shared library"; 3060f66f451Sopenharmony_ci else what = NULL; 3070f66f451Sopenharmony_ci if (what) xprintf("%s\n", what); 3080f66f451Sopenharmony_ci else xprintf("(bad type %d)\n", s[9]); 3090f66f451Sopenharmony_ci } else if (len>36 && !memcmp(s, "OggS\x00\x02", 6)) { 3100f66f451Sopenharmony_ci xprintf("Ogg data"); 3110f66f451Sopenharmony_ci // https://wiki.xiph.org/MIMETypesCodecs 3120f66f451Sopenharmony_ci if (!memcmp(s+28, "CELT ", 8)) xprintf(", celt audio"); 3130f66f451Sopenharmony_ci else if (!memcmp(s+28, "CMML ", 8)) xprintf(", cmml text"); 3140f66f451Sopenharmony_ci else if (!memcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video"); 3150f66f451Sopenharmony_ci else if (!memcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio"); 3160f66f451Sopenharmony_ci else if (!memcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video"); 3170f66f451Sopenharmony_ci else if (!memcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text"); 3180f66f451Sopenharmony_ci else if (!memcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text"); 3190f66f451Sopenharmony_ci else if (!memcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video"); 3200f66f451Sopenharmony_ci else if (!memcmp(s+28, "OpusHead", 8)) xprintf(", opus audio"); 3210f66f451Sopenharmony_ci else if (!memcmp(s+28, "PCM ", 8)) xprintf(", pcm audio"); 3220f66f451Sopenharmony_ci else if (!memcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video"); 3230f66f451Sopenharmony_ci else if (!memcmp(s+28, "Speex ", 8)) xprintf(", speex audio"); 3240f66f451Sopenharmony_ci else if (!memcmp(s+28, "\x80theora", 7)) xprintf(", theora video"); 3250f66f451Sopenharmony_ci else if (!memcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio"); 3260f66f451Sopenharmony_ci else if (!memcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video"); 3270f66f451Sopenharmony_ci xputc('\n'); 3280f66f451Sopenharmony_ci } else if (len>32 && !memcmp(s, "RIF", 3) && !memcmp(s+8, "WAVEfmt ", 8)) { 3290f66f451Sopenharmony_ci // https://en.wikipedia.org/wiki/WAV 3300f66f451Sopenharmony_ci int le = (s[3] == 'F'); 3310f66f451Sopenharmony_ci int format = le ? peek_le(s+20,2) : peek_be(s+20,2); 3320f66f451Sopenharmony_ci int channels = le ? peek_le(s+22,2) : peek_be(s+22,2); 3330f66f451Sopenharmony_ci int hz = le ? peek_le(s+24,4) : peek_be(s+24,4); 3340f66f451Sopenharmony_ci int bits = le ? peek_le(s+34,2) : peek_be(s+34,2); 3350f66f451Sopenharmony_ci 3360f66f451Sopenharmony_ci xprintf("WAV audio, %s, ", le ? "LE" : "BE"); 3370f66f451Sopenharmony_ci if (bits != 0) xprintf("%d-bit, ", bits); 3380f66f451Sopenharmony_ci if (channels==1||channels==2) xprintf("%s, ", channels==1?"mono":"stereo"); 3390f66f451Sopenharmony_ci else xprintf("%d-channel, ", channels); 3400f66f451Sopenharmony_ci xprintf("%d Hz, ", hz); 3410f66f451Sopenharmony_ci // See https://tools.ietf.org/html/rfc2361, though there appear to be bugs 3420f66f451Sopenharmony_ci // in the RFC. This assumes wikipedia's example files are more correct. 3430f66f451Sopenharmony_ci if (format == 0x01) xprintf("PCM"); 3440f66f451Sopenharmony_ci else if (format == 0x03) xprintf("IEEE float"); 3450f66f451Sopenharmony_ci else if (format == 0x06) xprintf("A-law"); 3460f66f451Sopenharmony_ci else if (format == 0x07) xprintf("µ-law"); 3470f66f451Sopenharmony_ci else if (format == 0x11) xprintf("ADPCM"); 3480f66f451Sopenharmony_ci else if (format == 0x22) xprintf("Truespeech"); 3490f66f451Sopenharmony_ci else if (format == 0x31) xprintf("GSM"); 3500f66f451Sopenharmony_ci else if (format == 0x55) xprintf("MP3"); 3510f66f451Sopenharmony_ci else if (format == 0x70) xprintf("CELP"); 3520f66f451Sopenharmony_ci else if (format == 0xfffe) xprintf("extensible"); 3530f66f451Sopenharmony_ci else xprintf("unknown format %d", format); 3540f66f451Sopenharmony_ci xputc('\n'); 3550f66f451Sopenharmony_ci } else if (len>12 && !memcmp(s, "\x00\x01\x00\x00", 4)) { 3560f66f451Sopenharmony_ci xputs("TrueType font"); 3570f66f451Sopenharmony_ci } else if (len>12 && !memcmp(s, "ttcf\x00", 5)) { 3580f66f451Sopenharmony_ci xprintf("TrueType font collection, version %d, %d fonts\n", 3590f66f451Sopenharmony_ci (int)peek_be(s+4, 2), (int)peek_be(s+8, 4)); 3600f66f451Sopenharmony_ci 3610f66f451Sopenharmony_ci // https://docs.microsoft.com/en-us/typography/opentype/spec/otff 3620f66f451Sopenharmony_ci } else if (len>12 && !memcmp(s, "OTTO", 4)) { 3630f66f451Sopenharmony_ci xputs("OpenType font"); 3640f66f451Sopenharmony_ci } else if (len>4 && !memcmp(s, "BC\xc0\xde", 4)) { 3650f66f451Sopenharmony_ci xputs("LLVM IR bitcode"); 3660f66f451Sopenharmony_ci } else if (strstart(&s, "-----BEGIN CERTIFICATE-----")) { 3670f66f451Sopenharmony_ci xputs("PEM certificate"); 3680f66f451Sopenharmony_ci 3690f66f451Sopenharmony_ci // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx 3700f66f451Sopenharmony_ci } else if (len>0x70 && !memcmp(s, "MZ", 2) && 3710f66f451Sopenharmony_ci (magic=peek_le(s+0x3c,4))<len-4 && !memcmp(s+magic, "\x50\x45\0\0", 4)) { 3720f66f451Sopenharmony_ci xprintf("MS PE32%s executable %s", (peek_le(s+magic+24, 2)==0x20b)?"+":"", 3730f66f451Sopenharmony_ci (peek_le(s+magic+22, 2)&0x2000)?"(DLL) ":""); 3740f66f451Sopenharmony_ci if (peek_le(s+magic+20, 2)>70) { 3750f66f451Sopenharmony_ci char *types[] = {0, "native", "GUI", "console", "OS/2", "driver", "CE", 3760f66f451Sopenharmony_ci "EFI", "EFI boot", "EFI runtime", "EFI ROM", "XBOX", 0, "boot"}; 3770f66f451Sopenharmony_ci int type = peek_le(s+magic+92, 2); 3780f66f451Sopenharmony_ci char *name = (type>0 && type<ARRAY_LEN(types))?types[type]:0; 3790f66f451Sopenharmony_ci 3800f66f451Sopenharmony_ci xprintf("(%s) ", name?name:"unknown"); 3810f66f451Sopenharmony_ci } 3820f66f451Sopenharmony_ci xprintf("%s\n", (peek_le(s+magic+4, 2)==0x14c)?"x86":"x86-64"); 3830f66f451Sopenharmony_ci 3840f66f451Sopenharmony_ci // https://en.wikipedia.org/wiki/BMP_file_format 3850f66f451Sopenharmony_ci } else if (len > 0x32 && !memcmp(s, "BM", 2) && !memcmp(s+6, "\0\0\0\0", 4)) { 3860f66f451Sopenharmony_ci int w = peek_le(s+0x12,4), h = peek_le(s+0x16,4), bpp = peek_le(s+0x1c,2); 3870f66f451Sopenharmony_ci 3880f66f451Sopenharmony_ci xprintf("BMP image, %d x %d, %d bpp\n", w, h, bpp); 3890f66f451Sopenharmony_ci 3900f66f451Sopenharmony_ci // https://github.com/torvalds/linux/blob/master/tools/perf/Documentation/perf.data-file-format.txt 3910f66f451Sopenharmony_ci } else if (len>=104 && !memcmp(s, "PERFILE2", 8)) { 3920f66f451Sopenharmony_ci xputs("Linux perf data"); 3930f66f451Sopenharmony_ci 3940f66f451Sopenharmony_ci // https://android.googlesource.com/platform/system/core/+/master/libsparse/sparse_format.h 3950f66f451Sopenharmony_ci } else if (len>28 && peek_le(s, 4) == 0xed26ff3a) { 3960f66f451Sopenharmony_ci xprintf("Android sparse image v%d.%d, %d %d-byte blocks (%d chunks)\n", 3970f66f451Sopenharmony_ci (int) peek_le(s+4, 2), (int) peek_le(s+6, 2), (int) peek_le(s+16, 4), 3980f66f451Sopenharmony_ci (int) peek_le(s+12, 4), (int) peek_le(s+20, 4)); 3990f66f451Sopenharmony_ci 4000f66f451Sopenharmony_ci // https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/master/include/bootimg/bootimg.h 4010f66f451Sopenharmony_ci } else if (len>1632 && !memcmp(s, "ANDROID!", 8)) { 4020f66f451Sopenharmony_ci xprintf("Android boot image v%d\n", (int) peek_le(s+40, 4)); 4030f66f451Sopenharmony_ci 4040f66f451Sopenharmony_ci // https://source.android.com/devices/architecture/dto/partitions 4050f66f451Sopenharmony_ci } else if (len>32 && peek_be(s, 4) == 0xd7b7ab1e) { 4060f66f451Sopenharmony_ci xprintf("Android DTB/DTBO v%d, %d entries\n", (int) peek_be(s+28, 4), 4070f66f451Sopenharmony_ci (int) peek_be(s+16, 4)); 4080f66f451Sopenharmony_ci 4090f66f451Sopenharmony_ci } else { 4100f66f451Sopenharmony_ci char *what = 0; 4110f66f451Sopenharmony_ci int i, bytes; 4120f66f451Sopenharmony_ci 4130f66f451Sopenharmony_ci // If shell script, report which interpreter 4140f66f451Sopenharmony_ci if (len>3 && strstart(&s, "#!")) { 4150f66f451Sopenharmony_ci // Whitespace is allowed between the #! and the interpreter 4160f66f451Sopenharmony_ci while (isspace(*s)) s++; 4170f66f451Sopenharmony_ci if (strstart(&s, "/usr/bin/env")) while (isspace(*s)) s++; 4180f66f451Sopenharmony_ci for (what = s; (s-toybuf)<len && !isspace(*s); s++); 4190f66f451Sopenharmony_ci strcpy(s, " script"); 4200f66f451Sopenharmony_ci 4210f66f451Sopenharmony_ci // Distinguish ASCII text, UTF-8 text, or data 4220f66f451Sopenharmony_ci } else for (i = 0; i<len; ++i) { 4230f66f451Sopenharmony_ci if (!(isprint(toybuf[i]) || isspace(toybuf[i]))) { 4240f66f451Sopenharmony_ci unsigned wc; 4250f66f451Sopenharmony_ci if ((bytes = utf8towc(&wc, s+i, len-i))>0 && wcwidth(wc)>=0) { 4260f66f451Sopenharmony_ci i += bytes-1; 4270f66f451Sopenharmony_ci if (!what) what = "UTF-8 text"; 4280f66f451Sopenharmony_ci } else { 4290f66f451Sopenharmony_ci what = "data"; 4300f66f451Sopenharmony_ci break; 4310f66f451Sopenharmony_ci } 4320f66f451Sopenharmony_ci } 4330f66f451Sopenharmony_ci } 4340f66f451Sopenharmony_ci xputs(what ? what : "ASCII text"); 4350f66f451Sopenharmony_ci } 4360f66f451Sopenharmony_ci} 4370f66f451Sopenharmony_ci 4380f66f451Sopenharmony_civoid file_main(void) 4390f66f451Sopenharmony_ci{ 4400f66f451Sopenharmony_ci char **arg; 4410f66f451Sopenharmony_ci 4420f66f451Sopenharmony_ci for (arg = toys.optargs; *arg; ++arg) { 4430f66f451Sopenharmony_ci int name_len = strlen(*arg); 4440f66f451Sopenharmony_ci 4450f66f451Sopenharmony_ci if (name_len > TT.max_name_len) TT.max_name_len = name_len; 4460f66f451Sopenharmony_ci } 4470f66f451Sopenharmony_ci 4480f66f451Sopenharmony_ci // Can't use loopfiles here because it doesn't call function when can't open 4490f66f451Sopenharmony_ci for (arg = toys.optargs; *arg; arg++) { 4500f66f451Sopenharmony_ci char *name = *arg, *what = "unknown"; 4510f66f451Sopenharmony_ci struct stat sb; 4520f66f451Sopenharmony_ci int fd = !strcmp(name, "-"); 4530f66f451Sopenharmony_ci 4540f66f451Sopenharmony_ci if (!FLAG(b)) 4550f66f451Sopenharmony_ci xprintf("%s: %*s", name, (int)(TT.max_name_len - strlen(name)), ""); 4560f66f451Sopenharmony_ci 4570f66f451Sopenharmony_ci sb.st_size = 0; 4580f66f451Sopenharmony_ci if (fd || !(FLAG(L) ? stat : lstat)(name, &sb)) { 4590f66f451Sopenharmony_ci if (!fd && !FLAG(s) && (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))) { 4600f66f451Sopenharmony_ci sprintf(what = toybuf, "%s special (%u/%u)", 4610f66f451Sopenharmony_ci S_ISBLK(sb.st_mode) ? "block" : "character", 4620f66f451Sopenharmony_ci dev_major(sb.st_rdev), dev_minor(sb.st_rdev)); 4630f66f451Sopenharmony_ci } else if (fd || S_ISREG(sb.st_mode)) { 4640f66f451Sopenharmony_ci TT.len = sb.st_size; 4650f66f451Sopenharmony_ci // This test identifies an empty file we don't have permission to read 4660f66f451Sopenharmony_ci if (!fd && !sb.st_size) what = "empty"; 4670f66f451Sopenharmony_ci else if ((fd = openro(name, O_RDONLY)) != -1) { 4680f66f451Sopenharmony_ci do_regular_file(fd, name); 4690f66f451Sopenharmony_ci if (fd) close(fd); 4700f66f451Sopenharmony_ci continue; 4710f66f451Sopenharmony_ci } 4720f66f451Sopenharmony_ci } else if (S_ISFIFO(sb.st_mode)) what = "fifo"; 4730f66f451Sopenharmony_ci else if (S_ISDIR(sb.st_mode)) what = "directory"; 4740f66f451Sopenharmony_ci else if (S_ISSOCK(sb.st_mode)) what = "socket"; 4750f66f451Sopenharmony_ci else if (S_ISLNK(sb.st_mode)) { 4760f66f451Sopenharmony_ci char *lnk = xreadlink(name); 4770f66f451Sopenharmony_ci 4780f66f451Sopenharmony_ci sprintf(what = toybuf, "%ssymbolic link to %s", 4790f66f451Sopenharmony_ci stat(lnk, &sb) ? "broken " : "", lnk); 4800f66f451Sopenharmony_ci free(lnk); 4810f66f451Sopenharmony_ci } 4820f66f451Sopenharmony_ci xputs(what); 4830f66f451Sopenharmony_ci } else xprintf("cannot open: %s\n", strerror(errno)); 4840f66f451Sopenharmony_ci } 4850f66f451Sopenharmony_ci} 486