📄 file.c
字号:
char *key; char *filetype; int length; char *mime;} file_string[] ={ "!<arch>\n__.SYMDEF", "archive random library", 16, "application/octet-stream", "!<arch>\n", "archive", 8, "application/octet-stream", "070707", "cpio archive - ascii header", 6, "application/octet-stream", "#!/bin/rc", "rc executable file", 9, "text/plain", "#!/bin/sh", "sh executable file", 9, "text/plain", "%!", "postscript", 2, "application/postscript", "\004%!", "postscript", 3, "application/postscript", "x T post", "troff output for post", 8, "application/troff", "x T Latin1", "troff output for Latin1", 10, "application/troff", "x T utf", "troff output for UTF", 7, "application/troff", "x T 202", "troff output for 202", 7, "application/troff", "x T aps", "troff output for aps", 7, "application/troff", "GIF", "GIF image", 3, "image/gif", "\0PC Research, Inc\0", "ghostscript fax file", 18, "application/ghostscript", "%PDF", "PDF", 4, "application/pdf", "<html>\n", "HTML file", 7, "text/html", "<HTML>\n", "HTML file", 7, "text/html", "compressed\n", "Compressed image or subfont", 11, "application/octet-stream", "\111\111\052\000", "tiff", 4, "image/tiff", "\115\115\000\052", "tiff", 4, "image/tiff", "\377\330\377\340", "jpeg", 4, "image/jpeg", "\377\330\377\341", "jpeg", 4, "image/jpeg", "\377\330\377\333", "jpeg", 4, "image/jpeg", "BM", "bmp", 2, "image/bmp", "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", "microsoft office document", 8, "application/octet-stream", "<MakerFile ", "FrameMaker file", 11, "application/framemaker", "\033%-12345X", "HPJCL file", 9, "application/hpjcl", "ID3", "mp3 audio with id3", 3, "audio/mpeg", "\211PNG", "PNG image", 4, "image/png", "P3\n", "ppm", 3, "image/ppm", "P6\n", "ppm", 3, "image/ppm", "/* XPM */\n", "xbm", 10, "image/xbm", ".HTML ", "troff -ms input", 6, "text/plain", ".LP", "troff -ms input", 3, "text/plain", ".ND", "troff -ms input", 3, "text/plain", ".PP", "troff -ms input", 3, "text/plain", ".TL", "troff -ms input", 3, "text/plain", ".TR", "troff -ms input", 3, "text/plain", ".TH", "manual page", 3, "text/plain", ".\\\"", "troff input", 3, "text/plain", ".de", "troff input", 3, "text/plain", ".if", "troff input", 3, "text/plain", ".nr", "troff input", 3, "text/plain", ".tr", "troff input", 3, "text/plain", "vac:", "venti score", 4, "text/plain", 0,0,0,0};intistring(void){ int i; struct FILE_STRING *p; for(p = file_string; p->key; p++) { if(nbuf >= p->length && !memcmp(buf, p->key, p->length)) { if(mime) print("%s\n", p->mime); else print("%s\n", p->filetype); return 1; } } if(strncmp((char*)buf, "TYPE=", 5) == 0) { /* td */ for(i = 5; i < nbuf; i++) if(buf[i] == '\n') break; if(mime) print(OCTET); else print("%.*s picture\n", utfnlen((char*)buf+5, i-5), (char*)buf+5); return 1; } return 0;}struct offstr{ ulong off; struct FILE_STRING;} offstrs[] = { 32*1024, "\001CD001\001", "ISO9660 CD image", 7, OCTET, 0, 0, 0, 0, 0};intisoffstr(void){ int n; char buf[256]; struct offstr *p; for(p = offstrs; p->key; p++) { seek(fd, p->off, 0); n = p->length; if (n > sizeof buf) n = sizeof buf; if (read(fd, buf, n) != n) continue; if(memcmp(buf, p->key, n) == 0) { if(mime) print("%s\n", p->mime); else print("%s\n", p->filetype); return 1; } } return 0;}intiff(void){ if (strncmp((char*)buf, "FORM", 4) == 0 && strncmp((char*)buf+8, "AIFF", 4) == 0) { print("%s\n", mime? "audio/x-aiff": "aiff audio"); return 1; } return 0;}char* html_string[] ={ "title", "body", "head", "strong", "h1", "h2", "h3", "h4", "h5", "h6", "ul", "li", "dl", "br", "em", 0,};intishtml(void){ uchar *p, *q; int i, count; /* compare strings between '<' and '>' to html table */ count = 0; p = buf; for(;;) { while (p < buf+nbuf && *p != '<') p++; p++; if (p >= buf+nbuf) break; if(*p == '/') p++; q = p; while(p < buf+nbuf && *p != '>') p++; if (p >= buf+nbuf) break; for(i = 0; html_string[i]; i++) { if(cistrncmp(html_string[i], (char*)q, p-q) == 0) { if(count++ > 4) { print(mime ? "text/html\n" : "HTML file\n"); return 1; } break; } } p++; } return 0;}char* rfc822_string[] ={ "from:", "date:", "to:", "subject:", "received:", "reply to:", "sender:", 0,};intisrfc822(void){ char *p, *q, *r; int i, count; count = 0; p = (char*)buf; for(;;) { q = strchr(p, '\n'); if(q == nil) break; *q = 0; if(p == (char*)buf && strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ")){ count++; *q = '\n'; p = q+1; continue; } *q = '\n'; if(*p != '\t' && *p != ' '){ r = strchr(p, ':'); if(r == 0 || r > q) break; for(i = 0; rfc822_string[i]; i++) { if(cistrncmp(p, rfc822_string[i], strlen(rfc822_string[i])) == 0){ count++; break; } } } p = q+1; } if(count >= 3){ print(mime ? "message/rfc822\n" : "email file\n"); return 1; } return 0;}intismbox(void){ char *p, *q; p = (char*)buf; q = strchr(p, '\n'); if(q == nil) return 0; *q = 0; if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){ print(mime ? "text/plain\n" : "mail box\n"); return 1; } *q = '\n'; return 0;}intiscint(void){ int type; char *name; Biobuf b; if(Binit(&b, fd, OREAD) == Beof) return 0; seek(fd, 0, 0); type = objtype(&b, &name); if(type < 0) return 0; if(mime) print(OCTET); else print("%s intermediate\n", name); return 1;}intisc(void){ int n; n = wfreq[I1]; /* * includes */ if(n >= 2 && wfreq[I2] >= n && wfreq[I3] >= n && cfreq['.'] >= n) goto yes; if(n >= 1 && wfreq[Alword] >= n && wfreq[I3] >= n && cfreq['.'] >= n) goto yes; /* * declarations */ if(wfreq[Cword] >= 5 && cfreq[';'] >= 5) goto yes; /* * assignments */ if(cfreq[';'] >= 10 && cfreq['='] >= 10 && wfreq[Cword] >= 1) goto yes; return 0;yes: if(mime){ print(PLAIN); return 1; } if(wfreq[Alword] > 0) print("alef program\n"); else print("c program\n"); return 1;}intislimbo(void){ /* * includes */ if(wfreq[Lword] < 4) return 0; print(mime ? PLAIN : "limbo program\n"); return 1;}intisas(void){ /* * includes */ if(wfreq[Aword] < 2) return 0; print(mime ? PLAIN : "as program\n"); return 1;}/* * low entropy means encrypted */intismung(void){ int i, bucket[8]; float cs; if(nbuf < 64) return 0; memset(bucket, 0, sizeof(bucket)); for(i=nbuf-64; i<nbuf; i++) bucket[(buf[i]>>5)&07] += 1; cs = 0.; for(i=0; i<8; i++) cs += (bucket[i]-8)*(bucket[i]-8); cs /= 8.; if(cs <= 24.322) { if(buf[0]==0x1f && buf[1]==0x9d) print(mime ? OCTET : "compressed\n"); else if(buf[0]==0x1f && buf[1]==0x8b) print(mime ? OCTET : "gzip compressed\n"); else if(buf[0]=='B' && buf[1]=='Z' && buf[2]=='h') print(mime ? OCTET : "bzip2 compressed\n"); else print(mime ? OCTET : "encrypted\n"); return 1; } return 0;}/* * english by punctuation and frequencies */intisenglish(void){ int vow, comm, rare, badpun, punct; char *p; if(guess != Fascii && guess != Feascii) return 0; badpun = 0; punct = 0; for(p = (char *)buf; p < (char *)buf+nbuf-1; p++) switch(*p) { case '.': case ',': case ')': case '%': case ';': case ':': case '?': punct++; if(p[1] != ' ' && p[1] != '\n') badpun++; } if(badpun*5 > punct) return 0; if(cfreq['>']+cfreq['<']+cfreq['/'] > cfreq['e']) /* shell file test */ return 0; if(2*cfreq[';'] > cfreq['e']) return 0; vow = 0; for(p="AEIOU"; *p; p++) { vow += cfreq[*p]; vow += cfreq[tolower(*p)]; } comm = 0; for(p="ETAION"; *p; p++) { comm += cfreq[*p]; comm += cfreq[tolower(*p)]; } rare = 0; for(p="VJKQXZ"; *p; p++) { rare += cfreq[*p]; rare += cfreq[tolower(*p)]; } if(vow*5 >= nbuf-cfreq[' '] && comm >= 10*rare) { print(mime ? PLAIN : "English text\n"); return 1; } return 0;}/* * pick up a number with * syntax _*[0-9]+_ */#define P9BITLEN 12intp9bitnum(uchar *bp){ int n, c, len; len = P9BITLEN; while(*bp == ' ') { bp++; len--; if(len <= 0) return -1; } n = 0; while(len > 1) { c = *bp++; if(!isdigit(c)) return -1; n = n*10 + c-'0'; len--; } if(*bp != ' ') return -1; return n;}intdepthof(char *s, int *newp){ char *es; int d; *newp = 0; es = s+12; while(s<es && *s==' ') s++; if(s == es) return -1; if('0'<=*s && *s<='9') return 1<<strtol(s, 0, 0); *newp = 1; d = 0; while(s<es && *s!=' '){ s++; /* skip letter */ d += strtoul(s, &s, 10); } switch(d){ case 32: case 24: case 16: case 8: return d; } return -1;}intisp9bit(void){ int dep, lox, loy, hix, hiy, px, new; ulong t; long len; char *newlabel; newlabel = "old "; dep = depthof((char*)buf + 0*P9BITLEN, &new); if(new) newlabel = ""; lox = p9bitnum(buf + 1*P9BITLEN); loy = p9bitnum(buf + 2*P9BITLEN); hix = p9bitnum(buf + 3*P9BITLEN); hiy = p9bitnum(buf + 4*P9BITLEN); if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0) return 0; if(dep < 8){ px = 8/dep; /* pixels per byte */ /* set l to number of bytes of data per scan line */ if(lox >= 0) len = (hix+px-1)/px - lox/px; else{ /* make positive before divide */ t = (-lox)+px-1; t = (t/px)*px; len = (t+hix+px-1)/px; } }else len = (hix-lox)*dep/8; len *= (hiy-loy); /* col length */ len += 5*P9BITLEN; /* size of initial ascii */ /* * for image file, length is non-zero and must match calculation above * for /dev/window and /dev/screen the length is always zero * for subfont, the subfont header should follow immediately. */ if (len != 0 && mbuf->length == 0) { print("%splan 9 image\n", newlabel); return 1; } if (mbuf->length == len) { print("%splan 9 image\n", newlabel); return 1; } /* Ghostscript sometimes produces a little extra on the end */ if (mbuf->length < len+P9BITLEN) { print("%splan 9 image\n", newlabel); return 1; } if (p9subfont(buf+len)) { print("%ssubfont file\n", newlabel); return 1; } return 0;}intp9subfont(uchar *p){ int n, h, a; /* if image too big, assume it's a subfont */ if (p+3*P9BITLEN > buf+sizeof(buf)) return 1; n = p9bitnum(p + 0*P9BITLEN); /* char count */ if (n < 0) return 0; h = p9bitnum(p + 1*P9BITLEN); /* height */ if (h < 0) return 0; a = p9bitnum(p + 2*P9BITLEN); /* ascent */ if (a < 0) return 0; return 1;}#define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')intisp9font(void){ uchar *cp, *p; int i, n; char pathname[1024]; cp = buf; if (!getfontnum(cp, &cp)) /* height */ return 0; if (!getfontnum(cp, &cp)) /* ascent */ return 0; for (i = 0; cp=(uchar*)strchr((char*)cp, '\n'); i++) { if (!getfontnum(cp, &cp)) /* min */ break; if (!getfontnum(cp, &cp)) /* max */ return 0; getfontnum(cp, &cp); /* optional offset */ while (WHITESPACE(*cp)) cp++; for (p = cp; *cp && !WHITESPACE(*cp); cp++) ; /* construct a path name, if needed */ n = 0; if (*p != '/' && slash) { n = slash-fname+1; if (n < sizeof(pathname)) memcpy(pathname, fname, n); else n = 0; } if (n+cp-p+4 < sizeof(pathname)) { memcpy(pathname+n, p, cp-p); n += cp-p; pathname[n] = 0; if (access(pathname, AEXIST) < 0) { strcpy(pathname+n, ".0"); if (access(pathname, AEXIST) < 0) return 0; } } } if (i) { print(mime ? "text/plain\n" : "font file\n"); return 1; } return 0;}intgetfontnum(uchar *cp, uchar **rp){ while (WHITESPACE(*cp)) /* extract ulong delimited by whitespace */ cp++; if (*cp < '0' || *cp > '9') return 0; strtoul((char *)cp, (char **)rp, 0); if (!WHITESPACE(**rp)) { *rp = cp; return 0; } return 1;}intisrtf(void){ if(strstr((char *)buf, "\\rtf1")){ print(mime ? "application/rtf\n" : "rich text format\n"); return 1; } return 0;}intismsdos(void){ if (buf[0] == 0x4d && buf[1] == 0x5a){ print(mime ? "application/x-msdownload\n" : "MSDOS executable\n"); return 1; } return 0;}intiself(void){ static char *cpu[] = { /* NB: incomplete and arbitary list */ [1] "WE32100", [2] "SPARC", [3] "i386", [4] "M68000", [5] "M88000", [6] "i486", [7] "i860", [8] "R3000", [9] "S370", [10] "R4000", [15] "HP-PA", [18] "sparc v8+", [19] "i960", [20] "PPC-32", [21] "PPC-64", [40] "ARM", [41] "Alpha", [43] "sparc v9", [50] "IA-64", [62] "AMD64", [75] "VAX", }; static char *type[] = { [1] "relocatable object", [2] "executable", [3] "shared library", [4] "core dump", }; if (memcmp(buf, "\x7fELF", 4) == 0){ if (!mime){ int n = (buf[19] << 8) | buf[18]; char *p = "unknown"; char *t = "unknown"; if (n > 0 && n < nelem(cpu) && cpu[n]) p = cpu[n]; else { /* try the other byte order */ n = (buf[18] << 8) | buf[19]; if (n > 0 && n < nelem(cpu) && cpu[n]) p = cpu[n]; } n = buf[16]; if(n>0 && n < nelem(type) && type[n]) t = type[n]; print("%s ELF %s\n", p, t); } else print("application/x-elf-executable"); return 1; } return 0;}intisface(void){ int i, j, ldepth, l; char *p; ldepth = -1; for(j = 0; j < 3; j++){ for(p = (char*)buf, i=0; i<3; i++){ if(p[0] != '0' || p[1] != 'x') return 0; if(buf[2+8] == ',') l = 2; else if(buf[2+4] == ',') l = 1; else return 0; if(ldepth == -1) ldepth = l; if(l != ldepth) return 0; strtoul(p, &p, 16); if(*p++ != ',') return 0; while(*p == ' ' || *p == '\t') p++; } if (*p++ != '\n') return 0; } if(mime) print("application/x-face\n"); else print("face image depth %d\n", ldepth); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -