xref: /freebsd/contrib/file/magic/Magdir/cafebabe (revision ae316d1d1cffd71ab7751f94e10118777a88e027)
1
2#------------------------------------------------------------------------------
3# $File: cafebabe,v 1.31 2024/08/30 16:53:22 christos Exp $
4# Cafe Babes unite!
5#
6# Since Java bytecode and Mach-O universal binaries have the same magic number,
7# the test must be performed in the same "magic" sequence to get both right.
8# The long at offset 4 in a Mach-O universal binary tells the number of
9# architectures; the short at offset 4 in a Java bytecode file is the JVM minor
10# version and the short at offset 6 is the JVM major version.  Since there are only
11# only 18 labeled Mach-O architectures at current, and the first released
12# Java class format was version 43.0, we can safely choose any number
13# between 18 and 39 to test the number of architectures against
14# (and use as a hack). Let's not use 18, because the Mach-O people
15# might add another one or two as time goes by...
16#
17
18### MACH-O START ###
19# URL:		https://en.wikipedia.org/wiki/Mach-O
20
210	name		mach-o		\b [
22# for debugging purpose CPU type as hexadecimal
23#>0	ubequad		x		CPU=%16.16llx
24# display CPU type as string like: i386 x86_64 ... armv7 armv7k ...
25>0	use		mach-o-cpu	\b
26# for debugging purpose print offset to 1st mach_header like:
27# 1000h 4000h seldom 2d000h 88000h 5b000h 10e000 h
28#>8	ubelong		x		at %#x offset
29>(8.L)	indirect	x		\b:
30>0	belong		x		\b]
31
32# Reference:	https://opensource.apple.com/source/cctools/cctools-949.0.1/
33#		include/mach-o/fat.h
34#		include/mach/machine.h
350	belong		0xcafebabe
36>4	belong		1		Mach-O universal binary with 1 architecture:
37!:mime application/x-mach-binary
38>>8	use		mach-o		\b
39# nfat_arch; number of CPU architectures; highest is 18 for CPU_TYPE_POWERPC in 2020
40>4	ubelong		>1
41>>4	ubelong		<20		Mach-O universal binary with %d architectures:
42!:mime application/x-mach-binary
43>>>8	use		mach-o		\b
44>>>4	ubelong		>1
45>>>>28	use		mach-o		\b
46>>>4	ubelong		>2
47>>>>48	use		mach-o		\b
48>>>4	ubelong		>3
49>>>>68	use		mach-o		\b
50>>>4	ubelong		>4
51>>>>88	use		mach-o		\b
52>>>4	ubelong		>5
53>>>>108	use		mach-o		\b
54### MACH-O END ###
55### JAVA START ###
56# Reference:	http://en.wikipedia.org/wiki/Java_class_file
57# Update:	Joerg Jenderek
58>4	ubelong		>30		compiled Java class data,
59!:mime	application/x-java-applet
60#!:mime	application/java-byte-code
61!:ext	class
62>>6	ubeshort	x	        version %d.
63>>4	ubeshort	x       	\b%d
64# for debugging purpose version as hexadecimal to compare with Mach-O universal binary
65#>>4	ubelong		x       	(%#8.8x)
66# Which is which?
67# https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html
68#>>4	belong		0x002b		(Java 0.?)
69#>>4	belong		0x032d		(Java 1.0)
70#>>4	belong		0x032d		(Java 1.1)
71>>4	belong		0x002e		(Java 1.2)
72>>4	belong		0x002f		(Java 1.3)
73>>4	belong		0x0030		(Java 1.4)
74>>4	belong		0x0031		(Java 1.5)
75>>4	belong		0x0032		(Java 1.6)
76>>4	belong		0x0033		(Java 1.7)
77>>4	belong		0x0034		(Java 1.8)
78>>4	belong		0x0035		(Java SE 9)
79>>4	belong		0x0036		(Java SE 10)
80>>4	belong		0x0037		(Java SE 11)
81>>4	belong		0x0038		(Java SE 12)
82>>4	belong		0x0039		(Java SE 13)
83>>4	belong		0x003A		(Java SE 14)
84>>4	belong		0x003B		(Java SE 15)
85>>4	belong		0x003C		(Java SE 16)
86>>4	belong		0x003D		(Java SE 17)
87>>4	belong		0x003E		(Java SE 18)
88>>4	belong		0x003F		(Java SE 19)
89>>4	belong		0x0040		(Java SE 20)
90>>4	belong		0x0041		(Java SE 21)
91>>4	belong		0x0042		(Java SE 22)
92>>4	belong		0x0043		(Java SE 23)
93>>4	belong		0x0044		(Java SE 24)
94>>4	belong		0x0045		(Java SE 25)
95>>4	belong		0x0046		(Java SE 26)
96# pool count unequal zero
97#>>8	beshort		x	        \b, pool count %#x
98# pool table
99#>>10	ubequad		x	        \b, pool %#16.16llx...
100
1010	belong		0xcafed00d	JAR compressed with pack200,
102>5	byte		x		version %d.
103>4	byte		x		\b%d
104!:mime	application/x-java-pack200
105
106### JAVA END ###
107