1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
31
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
38
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 {0x5441, "frv"}, {46, "h8300"}, {164, "hexagon"}, {50, "ia64"},
43 {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"}, {22, "s390"}, {0xa390, "s390-old"}, {135, "score"},
48 {42, "sh"}, {2, "sparc"}, {18, "sparc8+"}, {43, "sparc9"}, {188, "tile"},
49 {191, "tilegx"}, {3, "386"}, {6, "486"}, {62, "x86-64"}, {94, "xtensa"},
50 {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
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
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
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
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
92 if (!((bits&1)==bits && endian)) {
93 printf(", corrupt?\n");
94 return;
95 }
96
97
98
99
100 if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0)))
101 goto bad;
102
103
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
112
113 if (phnum && (phentsize != 32+24*bits)) {
114 printf(", corrupt phentsize %d?", phentsize);
115 goto bad;
116 }
117
118
119
120
121 if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0)))
122 goto bad;
123
124
125
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 ) dynamic = 1;
133 if (p_type!=3 && p_type!=4 ) 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 ) {
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
147
148
149
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 ) {
158 stripped = 0;
159 break;
160 } else if (sh_type == 7 ) {
161 char *note = map+sh_offset;
162
163 if (sh_offset+sh_size>TT.len) goto bad;
164
165
166
167
168
169 while (sh_size >= 3*4) {
170 int n_namesz = elf_int(note, 4);
171 int n_descsz = elf_int(note+4, 4);
172 int n_type = elf_int(note+8, 4);
173 int notesz = 3*4 + ((n_namesz+3)&~3) + ((n_descsz+3)&~3);
174
175 if (n_namesz==4 && !memcmp(note+12, "GNU", 4)) {
176 if (n_type==3 ) {
177 if (n_descsz+16>sh_size) goto bad;
178 printf(", BuildID=");
179 for (j = 0; j < n_descsz; ++j) printf("%02x", note[16 + j]);
180 }
181 } else if (n_namesz==8 && !memcmp(note+12, "Android", 8)) {
182 if (n_type==1 ) {
183 if (n_descsz+24+64>sh_size) goto bad;
184 printf(", for Android %d", (int)elf_int(note+20, 4));
185 if (n_descsz > 24)
186 printf(", built by NDK %.64s (%.64s)", note+24, note+24+64);
187 }
188 }
189
190 note += notesz;
191 sh_size -= notesz;
192 }
193 }
194 }
195 printf(", %sstripped", stripped ? "" : "not ");
196bad:
197 xputc('\n');
198
199 if (map && map != MAP_FAILED) munmap(map, TT.len);
200}
201
202static void do_regular_file(int fd, char *name)
203{
204 char *s;
205 int len, magic;
206
207
208 memset(toybuf, 0, 80);
209 if ((len = readall(fd, s = toybuf, sizeof(toybuf)))<0) perror_msg("%s", name);
210
211 if (!len) xputs("empty");
212
213 else if (len>=45 && strstart(&s, "\177ELF")) do_elf_file(fd);
214 else if (len>=8 && strstart(&s, "!<arch>\n")) xprintf("ar archive\n");
215 else if (len>28 && strstart(&s, "\x89PNG\x0d\x0a\x1a\x0a")) {
216
217 int chunk_length = peek_be(s, 4);
218
219 xprintf("PNG image data");
220
221
222 s += 4;
223 if (chunk_length == 13 && strstart(&s, "IHDR")) {
224
225 char *c = 0, *colors[] = {"grayscale", 0, "color RGB", "indexed color",
226 "grayscale with alpha", 0, "color RGBA"};
227
228 if (s[9]<ARRAY_LEN(colors)) c = colors[s[9]];
229 if (!c) c = "unknown";
230
231 xprintf(", %d x %d, %d-bit/%s, %sinterlaced", (int)peek_be(s, 4),
232 (int)peek_be(s+4, 4), s[8], c, s[12] ? "" : "non-");
233 }
234
235 xputc('\n');
236
237
238 } else if (len>16 && (strstart(&s, "GIF87a") || strstart(&s, "GIF89a")))
239 xprintf("GIF image data, %d x %d\n",
240 (int)peek_le(s, 2), (int)peek_le(s+8, 2));
241
242
243 else if (len>32 && !memcmp(toybuf, "\xff\xd8", 2)) xputs("JPEG image data");
244
245
246 else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe"))
247 xprintf("Java class file, version %d.%d\n",
248 (int)peek_be(s+2, 2), (int)peek_be(s, 2));
249
250
251
252
253 else if (len>85 && strstart(&s, "07070")) {
254 char *cpioformat = "unknown type";
255
256 if (toybuf[5] == '7') cpioformat = "pre-SVR4 or odc";
257 else if (toybuf[5] == '1') cpioformat = "SVR4 with no CRC";
258 else if (toybuf[5] == '2') cpioformat = "SVR4 with CRC";
259 xprintf("ASCII cpio archive (%s)\n", cpioformat);
260 } else if (len>33 && (magic=peek(&s,2), magic==0143561 || magic==070707)) {
261 if (magic == 0143561) printf("byte-swapped ");
262 xprintf("cpio archive\n");
263
264 } else if (len>500 && !strncmp(s+257, "ustar", 5))
265 xprintf("POSIX tar archive%s\n", strncmp(s+262," ",2)?"":" (GNU)");
266
267 else if (len>5 && strstart(&s, "PK\03\04")) {
268 int ver = toybuf[4];
269
270 xprintf("Zip archive data");
271 if (ver) xprintf(", requires at least v%d.%d to extract", ver/10, ver%10);
272 xputc('\n');
273 } else if (len>4 && strstart(&s, "BZh") && isdigit(*s))
274 xprintf("bzip2 compressed data, block size = %c00k\n", *s);
275 else if (len>10 && strstart(&s, "\x1f\x8b")) xputs("gzip compressed data");
276 else if (len>32 && !memcmp(s+1, "\xfa\xed\xfe", 3)) {
277 int bit = s[0]=='\xce'?32:64;
278 char *what;
279
280 xprintf("Mach-O %d-bit ", bit);
281
282 if (s[4] == 7) what = (bit==32)?"x86":"x86-";
283 else if (s[4] == 12) what = "arm";
284 else if (s[4] == 18) what = "ppc";
285 else what = NULL;
286 if (what) xprintf("%s%s ", what, (bit==32)?"":"64");
287 else xprintf("(bad arch %d) ", s[4]);
288
289 if (s[12] == 1) what = "object";
290 else if (s[12] == 2) what = "executable";
291 else if (s[12] == 6) what = "shared library";
292 else what = NULL;
293 if (what) xprintf("%s\n", what);
294 else xprintf("(bad type %d)\n", s[9]);
295 } else if (len>36 && !memcmp(s, "OggS\x00\x02", 6)) {
296 xprintf("Ogg data");
297
298 if (!memcmp(s+28, "CELT ", 8)) xprintf(", celt audio");
299 if (!memcmp(s+28, "CMML ", 8)) xprintf(", cmml text");
300 if (!memcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video");
301 if (!memcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio");
302 if (!memcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video");
303 if (!memcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text");
304 if (!memcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text");
305 if (!memcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video");
306 if (!memcmp(s+28, "OpusHead", 8)) xprintf(", opus audio");
307 if (!memcmp(s+28, "PCM ", 8)) xprintf(", pcm audio");
308 if (!memcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video");
309 if (!memcmp(s+28, "Speex ", 8)) xprintf(", speex audio");
310 if (!memcmp(s+28, "\x80theora", 7)) xprintf(", theora video");
311 if (!memcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio");
312 if (!memcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video");
313 xputc('\n');
314 } else if (len>12 && !memcmp(s, "\x00\x01\x00\x00", 4)) {
315 xputs("TrueType font");
316 } else if (len>12 && !memcmp(s, "ttcf\x00", 5)) {
317 xprintf("TrueType font collection, version %d, %d fonts\n",
318 (int)peek_be(s+4, 2), (int)peek_be(s+8, 4));
319 } else if (len>4 && !memcmp(s, "BC\xc0\xde", 4)) {
320 xputs("LLVM IR bitcode");
321 } else if (strstart(&s, "-----BEGIN CERTIFICATE-----")) {
322 xputs("PEM certificate");
323
324
325 } else if (len>0x70 && !memcmp(s, "MZ", 2) &&
326 (magic=peek_le(s+0x3c,4))<len-4 && !memcmp(s+magic, "\x50\x45\0\0", 4)) {
327 xprintf("MS PE32%s executable %s", (peek_le(s+magic+24, 2)==0x20b)?"+":"",
328 (peek_le(s+magic+22, 2)&0x2000)?"(DLL) ":"");
329 if (peek_le(s+magic+20, 2)>70) {
330 char *types[] = {0, "native", "GUI", "console", "OS/2", "driver", "CE",
331 "EFI", "EFI boot", "EFI runtime", "EFI ROM", "XBOX", 0, "boot"};
332 int type = peek_le(s+magic+92, 2);
333 char *name = (type>0 && type<ARRAY_LEN(types))?types[type]:0;
334
335 xprintf("(%s) ", name?name:"unknown");
336 }
337 xprintf("%s\n", (peek_le(s+magic+4, 2)==0x14c)?"x86":"x86-64");
338 } else {
339 char *what = 0;
340 int i, bytes;
341
342
343 if (len>3 && strstart(&s, "#!")) {
344
345 while (isspace(*s)) s++;
346 if (strstart(&s, "/usr/bin/env")) while (isspace(*s)) s++;
347 for (what = s; (s-toybuf)<len && !isspace(*s); s++);
348 strcpy(s, " script");
349
350
351 } else for (i = 0; i<len; ++i) {
352 if (!(isprint(toybuf[i]) || isspace(toybuf[i]))) {
353 wchar_t wc;
354 if ((bytes = utf8towc(&wc, s+i, len-i))>0 && wcwidth(wc)>=0) {
355 i += bytes-1;
356 if (!what) what = "UTF-8 text";
357 } else {
358 what = "data";
359 break;
360 }
361 }
362 }
363 xputs(what ? what : "ASCII text");
364 }
365}
366
367void file_main(void)
368{
369 char **arg;
370
371 for (arg = toys.optargs; *arg; ++arg) {
372 int name_len = strlen(*arg);
373
374 if (name_len > TT.max_name_len) TT.max_name_len = name_len;
375 }
376
377
378 for (arg = toys.optargs; *arg; arg++) {
379 char *name = *arg, *what = "cannot open";
380 struct stat sb;
381 int fd = !strcmp(name, "-");
382
383 xprintf("%s: %*s", name, (int)(TT.max_name_len - strlen(name)), "");
384
385 sb.st_size = 0;
386 if (fd || !((toys.optflags & FLAG_L) ? stat : lstat)(name, &sb)) {
387 if (fd || S_ISREG(sb.st_mode)) {
388 TT.len = sb.st_size;
389
390 if (!fd && !sb.st_size) what = "empty";
391 else if ((fd = openro(name, O_RDONLY)) != -1) {
392 do_regular_file(fd, name);
393 if (fd) close(fd);
394 continue;
395 }
396 } else if (S_ISFIFO(sb.st_mode)) what = "fifo";
397 else if (S_ISBLK(sb.st_mode)) what = "block special";
398 else if (S_ISCHR(sb.st_mode)) what = "character special";
399 else if (S_ISDIR(sb.st_mode)) what = "directory";
400 else if (S_ISSOCK(sb.st_mode)) what = "socket";
401 else if (S_ISLNK(sb.st_mode)) what = "symbolic link";
402 else what = "unknown";
403 }
404
405 xputs(what);
406 }
407}
408