1
2#------------------------------------------------------------------------------
3# $File: mathematica,v 1.17 2023/06/16 19:33:58 christos Exp $
4# mathematica:  file(1) magic for mathematica files
5# "H. Nanosecond" <aldomel@ix.netcom.com>
6# Mathematica a multi-purpose math program
7# versions 2.2 and 3.0
8
90         name      wolfram
10>0        string    x         Mathematica notebook version 2.x
11!:ext mb
12!:mime application/vnd.wolfram.mathematica
13
14#mathematica .mb
150         string    \064\024\012\000\035\000\000\000
16>0        use       wolfram
170         string    \064\024\011\000\035\000\000\000
18>0        use       wolfram
19
20#
210         search/1000         Content-type:\040application/mathematica          Mathematica notebook version 2.x
22!:ext nb
23!:mime application/mathematica
24
25
26# .ma
27# multiple possibilities:
28
290         string    (*^\n\n::[\011frontEndVersion\ =
30#>41      string    >\0       %s
31>0        use       wolfram
32
33#0        string    (*^\n\n::[\011palette
34
35#0        string    (*^\n\n::[\011Information
36#>675     string    >\0       %s #doesn't work well
37
38# there may be 'cr' instead of 'nl' in some does this matter?
39
40# generic:
410         string    (*^\r\r::[\011
42>0        use       wolfram
430         string    (*^\r\n\r\n::[\011
44>0        use       wolfram
450         string    (*^\015
46>0        use       wolfram
470         string    (*^\n\r\n\r::[\011
48>0        use       wolfram
490         string    (*^\r::[\011
50>0        use       wolfram
510         string    (*^\r\n::[\011
52>0        use       wolfram
530         string    (*^\n\n::[\011
54>0        use       wolfram
550         string    (*^\n::[\011
56>0        use       wolfram
57
58
59# Mathematica .mx files
60
61#0        string    (*This\ is\ a\ Mathematica\ binary\ dump\ file.\ It\ can\ be\ loaded\ with\ Get.*)        Mathematica binary file
620         string    (*This\ is\ a\ Mathematica\ binary\     Mathematica binary file
63#>71      string \000\010\010\010\010\000\000\000\000\000\000\010\100\010\000\000\000
64# >71... is optional
65>88       string    >\0       from %s
66
67
68# Mathematica files PBF:
69# 115 115 101 120 102 106 000 001 000 000 000 203 000 001 000
700         string    MMAPBF\000\001\000\000\000\203\000\001\000        Mathematica PBF (fonts I think)
71
72# .ml files  These are menu resources I think
73# these start with "[0-9][0-9][0-9]\ A~[0-9][0-9][0-9]\
74# how to put that into a magic rule?
754         string    \ A~      MAthematica .ml file
76
77# .nb files
78#too long 0         string    (***********************************************************************\n\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Mathematica-Compatible Notebook   Mathematica 3.0 notebook
790         string    (***********************      Mathematica 3.0 notebook
80
81# other (* matches it is a comment start in these langs
82# GRR: Too weak; also matches other languages e.g. ML
83#0        string    (*        Mathematica, or Pascal, Modula-2 or 3 code text
84
85#########################
86# MatLab v5
87# URL:              http://fileformats.archiveteam.org/wiki/MAT
88# Reference:        https://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf
89# first 116 bytes of header contain text in human-readable form
900       string  MATLAB  Matlab v
91#>11      string/T  x         \b, at 11 "%.105s"
92#!:mime   application/octet-stream
93!:mime    application/x-matlab-data
94!:ext     mat
95#         https://de.mathworks.com/help/matlab/import_export/mat-file-versions.html
96# level of the MAT-file like: 5.0 7.0 or maybe 7.3
97#>7       string    x         LEVEL "%.3s"
98>7        ubyte     =0x35     \b5 mat-file
99>7        ubyte     !0x35
100>>7       string    x         \b%.3s mat-file
101>126    short   0x494d  (big endian)
102>>124   beshort x       version %#04x
103>126    short   0x4d49  (little endian)
104# 0x0100 for level 5.0 and 0x0200 for level 7.0
105>>124   leshort x       version %#04x
106# test again so that default clause works
107>126      short     x
108# created by MATLAB include Platform sometimes without leading comma (0x2C) or missing
109# like: GLNX86 PCWIN PCWIN64 SOL2 Windows\0407 nt posix
110>>20      search/2  Platform:\040       \b, platform
111>>>&0     string              x                   %-0.2s
112>>>&2               ubyte     !0x2C               \b%c
113>>>>&0              ubyte     !0x2C               \b%c
114>>>>>&0             ubyte     !0x2C               \b%c
115>>>>>>&0  ubyte     !0x2C               \b%c
116>>>>>>>&0 ubyte     !0x2C               \b%c
117>>>>>>>>&0          ubyte     !0x2C               \b%c
118>>>>>>>>>&0         ubyte     !0x2C               \b%c
119# examples without Platform tag like one_by_zero_char.mat
120>>20      default             x
121>>>11     string              x         "%s"
122# created by MATLAB include time like: Fri Feb 20 15:26:59 2009
123>34       search/9/c          created\040on:\040  \b, created
124>>&0      string    x                   %-.24s
125#         MatLab v4
126# From:   Joerg Jenderek
127# check for valid imaginary flag of Matlab matrix version 4
12813        ushort    0
129# check for valid ASCII matrix name
130>20       ubyte     >0x1F
131# skip PreviousEntries.dat with "invalid high" name \304P\344@\001
132>>20      ubyte     <0304
133# skip some Netwfw*.dat and $I3KREPH.dat by checking for non zero number of rows
134>>>4      ulong               !0
135# skip some CD-ROM filesystem like test-hfs.iso by looking for valid big endian type flag
136>>>>0     ubelong&0xFFffFF00  0x00000300
137>>>>>0    use       matlab4
138# no example for 8-bit and 16-bit integers matrix
139>>>>0     ubelong&0xFFffFF00  0x00000400
140>>>>>0    use       matlab4
141#         branch for Little-Endian variant of Matlab MATrix version 4
142# skip big endian variant by looking for valid low lttle endian type flag
143>>>>0     ulelong             <53
144# skip tokens.dat and some Netwfw*.dat by check for valid imaginary flag value of MAT version 4
145>>>>>12   ulelong             <2
146# no misidentified little endian MATrix example with "short" matrix name
147>>>>>>16  ulelong             <3
148# skip radeon firmware BONAIRE_sdma.bin HAWAII_sdma.bin KABINI_sdma.bin KAVERI_sdma.bin MULLINS_sdma.bin
149# by check for non zero matrix name length
150>>>>>>>16 ubelong             >0
151>>>>>>>>0 use       \^matlab4
152# little endian MATrix with "long" matrix name or some misidentified samples
153>>>>>>16  ulelong             >2
154# skip TileCacheLogo-*.dat with invalid 2nd character \001 of matrix name with length 96
155>>>>>>>21 ubyte     >0x1F
156>>>>>>>>0 use       \^matlab4
157# Note:             called    "MATLAB Mat File" with version "Level 4" by DROID via PUID fmt/1550
158#         display information of Matlab v4 mat-file
1590         name      matlab4             Matlab v4 mat-file
160#!:mime   application/octet-stream
161!:mime    application/x-matlab-data
162!:ext     mat
163# 20-byte header with 5 long integers that contains information describing certain attributes of the Matrix
164# type flag decimal MOPT; maximal 4052=FD4h; maximal 52=34h for little endian
165#>0       ubelong             x         \b, type flag %u
166#>0       ubelong             x         (%#x)
167# M: 0~little endian 1~Big Endian 2~VAX D-float 3~VAX G-float 4~Cray
168#>0       ubelong/1000        x         \b, M=%u
169>0        ubelong/1000        0         (little endian)
170>0        ubelong/1000        1         (big endian)
171>0        ubelong/1000        2         (VAX D-float)
172>0        ubelong/1000        3         (VAX G-float)
173>0        ubelong/1000        4         (Cray)
174# namlen; the length of the matrix name
175#>16      ubelong             x         \b, name length %u
176#>(16.L+19)         ubyte     x         \b, TERMINATING NAME CHARACTER=%#x
177# nul terminated matrix name like: fit_params testmatrix testsparsecomplex teststringarray
178#>20      string              x         \b, MATRIX NAME="%s"
179#>21                ubyte     x         \b, MAYBE 2ND CHAR=%c
180>16       pstring/L x         %s
181# T indicates the matrix type: 0~numeric 1~text 2~sparse
182#>0       ubelong%10          x         \b, T=%u
183>0        ubelong%10          0         \b, numeric
184>0        ubelong%10          1         \b, text
185>0        ubelong%10          2         \b, sparse
186# mrows; number of rows in the matrix like: 1 3 8
187>4        ubelong             x         \b, rows %u
188# ncols; number of columns in the matrix like: 1 3 4 5 9 43
189>8        ubelong             x         \b, columns %u
190# imagf; imaginary flag; 1~matrix has an imaginary part 0~only real data
191>12       ubelong             !0        \b, imaginary (%u)
192# real; Real part of the matrix consists of mrows * ncols numbers
193