Repository navigation
Releases
v0.9.2: file and binwalk-ng at their newest commits
This release follows file and binwalk-ng to their newest commits, and adds rules that libmagic does not have, as an option.
go get github.com/xo/[email protected]The port follows file at b693eed3
- Upstream fixed the two bugs of file that the port copied (D48). The package reads the properties of a Compound Document again, such as the title of a Word document, and prints the flags of the mode as "setuid, setgid".
- The port takes the other changes of file to the library: bounds on the full-word check of a string, on the year of a date and on an octal offset, and the rule for v8 of RV565.
- All 116 tests of file pass. The port and the oracle differ only in 30 answers where a decoder of Go reads damaged compressed data in another way than the library of C.
The package
WithExtraRulesadds rules that libmagic does not have (D67). With it, a FictionBook file isapplication/x-fictionbook+xml, and an EMF file isimage/emf.- An error of the engine keeps the text of libmagic (D66).
container
container names 27 more formats inside a zip file or a Compound File: OpenDocument, Office Open XML (DOCX, XLSX, PPTX, VSDX and their kin), iWork (Pages, Numbers and Keynote), Visio and Publisher (D65).
The walker follows binwalk-ng at d41a0a2c
minifs, the file system of some TP-Link firmware, is new. The walker finds it and extracts it, and decodes its chunks withcompress/xz.- The walker uses the decoders of
compress(D68). It now extractslzfse, and the folders of a CAB file that are compressed with LZX, which it reads as cabextract does. - An extracted file keeps its holes. A hole does not count against
WithMaxOutput. - A stored zip member gives at most its unpacked size, as 7z does.
- binwalk-ng took the fix of a loop in its
dtbextractor, so the oracle needs no patch.
The three gates of the test module give no difference from binwalk-ng on 2366 inputs.
compress/lzx
compress/lzx follows lzxd on codeberg.org at aa723fd. It fails on a bit of a word of one byte, as lzxd does, and its Decoder has the methods Used, Overrun and Uncompressed.
compress/xz and compress/zstd did not change, and stay at v0.6.0.
v0.9.1: read the EXTH flags of a Mobipocket book
This release fixes how container reads a Mobipocket book. The package magic gives the same answers as v0.9.0.
go get github.com/xo/[email protected]container
container read the flags of the EXTH header at 0x80 of the MOBI header. The flags are at 0x80 of the first record, 16 bytes earlier, where calibre and KindleUnpack read them. So Detect gave Unknown for a book with both forms, MOBI and KF8, that calibre made. It now gives AZW3 for that book (D65).
compress/xz and compress/zstd did not change, and stay at v0.6.0.
v0.9.0: the package container
This release adds the package container, which names the format inside a container. The package magic gives the same answers as v0.8.0.
go get github.com/xo/[email protected]container
container.Detect reads a zip file, a Compound File or a Mobipocket book, within limits. It names XPS, OpenXPS, an encrypted Office Open XML file, AZW3, CBZ, APK, IPA, Krita and OpenRaster, which libmagic names only as their container (D65).
magic does not import container, and Detect does not change.
compress/xz and compress/zstd did not change, and stay at v0.6.0.
v0.8.0: the decoders of LZFSE and ADC for ivy
This release adds two decoders that ivy needs to read DMG files. The package magic gives the same answers as v0.7.0.
go get github.com/xo/[email protected]The decoders for ivy
compress/lzfsedecodes LZFSE and LZVN. It is a port of the reference code of Apple at e634ca58, with its BSD license.compress/adcdecodes the ADC chunks of older DMG files.
Both take a limit on their output. They do not register with compress, because libmagic does not use them (D63).
The formats inside a container
TestContainers holds what libmagic reports for XPS, OpenXPS, an encrypted Office Open XML file, AZW3, CBZ, APK, IPA, Krita and OpenRaster. libmagic names each of them as its container, such as a zip file. D64 decides that a package of its own, as cfb is, names these formats.
compress/xz and compress/zstd did not change, and stay at v0.6.0.
v0.7.0: the decoders and the Compound Files for ivy
This release adds the decoders and the reader of Compound Files that ivy needs, and moves the walker to binwalk-ng. The package magic gives the same answers as v0.6.1.
go get github.com/xo/[email protected]The packages for ivy
compress/palmdocdecodes the compression of PalmDOC,compress/huffcdicdecodes the Huffman compression of Mobipocket, andcompress/lzxdecodes LZX, as a port of lzxd. They do not register withcompress, because libmagic does not use them (D61).cfbreads the directory and the streams of a Compound File, the format of Office 97 to 2003, and the property sets of MS-OLEPS, such as the thumbnail of a document (D62).magicdoes not import it. Its engine keeps its own reader, which follows libmagic.
The walker follows binwalk-ng
binwalk-ng is the maintained fork of binwalk. The walker follows it at the commit 02835ae0f4, with each fix that needs no new decoder (D60).
- The scan skips runs of zero bytes, as binwalk-ng does.
- The output follows the Chroot of binwalk-ng. It cleans each path and writes relative links.
- A carved extraction that fails runs again on the data to the end of the input.
- The formats take the changes of binwalk-ng to their parsers, descriptions and extractors. eva, program_store and dlink_fw are new, and the walker now extracts srecord, srecord_generic and dms.
The three gates of the test module give no difference from binwalk-ng on 2301 inputs.
compress/xz and compress/zstd did not change, and stay at v0.6.0.
v0.6.1: find an xz stream at any offset
This release fixes the walker for xz data, and changes no answer of the package magic.
- The walker finds an xz stream at any offset. v0.6.0 missed a stream that started further into the data than its own length, because it copied a bug of binwalk: since a commit after its release 3.1.0, binwalk compares the absolute offset of the next stream with the bytes that follow the match. The bug is reported to binwalk as ReFirmLabs/binwalk#982.
- The oracle of the test module carries the same fix as a patch, so the gates still compare the walker with binwalk (D59).
cmd/walksays that it has no decoder of xz, lzma or zstd. Its module requires no other module, so it cannot importcompress/xzandcompress/zstd, as a program of its own does.
compress/xz and compress/zstd did not change, and stay at v0.6.0.
v0.6.0: the binary walker
This release adds the binary walker: the package walk finds the files that are embedded in a binary file, such as a firmware image, as binwalk 3 does, and extracts them. The package magic does not change.
The walker
The walker has the 111 signatures of binwalk 3, with the same names and descriptions, and gives the same matches, at the commit of binwalk that docs/upstream/upstreams.tsv names.
for e, err := range walk.WalkFile(ctx, "firmware.bin", walk.WithDepth(8)) {
if err != nil {
return err
}
fmt.Printf("%*s%#x %s\n", 2*e.Depth, "", e.Offset, e.Description)
}Walk,WalkFileandWalkBytesreturn an iterator of entries, depth first.Entry.Membersgives the files of a match in memory, andwalk.Extractwrites them under anos.Root. No path or link leaves the root, and no device is made.WithDepthwalks the extracted files, as binwalk -M does.WithMaxOutputlimits what one extraction writes.walk.Entropygives the entropy of each block, as binwalk -E.cmd/walkis an example program.
A program imports the formats that it needs, or all of them:
import _ "github.com/xo/magic/walk/format/all"The formats xz, lzma and zstd need the decoders of compress/xz and compress/zstd. The keys of the firmware of D-Link are in walk/format/encrypted, which a program imports to decrypt it.
Extraction
The walker extracts the formats that binwalk extracts with its own code, and gives the same files. It also extracts most of the formats that binwalk extracts with programs, and writes what those programs write. It runs no program:
- SquashFS 1.x to 4.0, both byte orders, with gzip, lzma (and the LZMA of vendors), lzo, xz, lz4 and zstd, as sasquatch.
- cramfs and ISO 9660, as 7-Zip; JFFS2, as jefferson; YAFFS, as unyaffs; UBI and UBIFS, as ubireader; FAT and ext, as tsk_recover.
- zip, tar, cpio, 7-Zip (with PPMd and the filters), cab (stored and MSZIP), lzma, zstd, lz4, lzop and compress'd.
docs/BACKLOG.md lists the formats that the walker finds and does not extract yet.
How it is tested
The test module builds binwalk 3 from its source, and three gates compare the walker with it on 1881 inputs: the matches, the extracted files, and the walk of the extracted files. A test for each file system and archive compares the files with the tool that made the image, or with the tree that made it. FuzzWalk fuzzes every format.
The decoders
compresshas a stream decoder,compress.Stream, which also gives the size of the stream.compress/xzandcompress/zstdregister one, atcompress/xz/v0.6.0andcompress/zstd/v0.6.0.
Size
The walker is in packages of its own, so a program that imports only magic does not grow. walk/format/all adds about 0.9 MB.
License
The walker translates binwalk, under the MIT license, in LICENSE.binwalk, and the crate delink of its author, under the MIT license. Two of its decoders are copies from the Go standard library, under the BSD license of Go.
The formats build only where an int has 64 bits, as binwalk does.
v0.5.1: the README links the related xo projects
This release changes no code of the package. It gives the same answers as v0.5.0.
- The README opens with a bar of links and the badges of the other xo projects: the tests, the Go reference, the latest release and Discord.
- The README has a new section, Related projects, which lists the other xo projects, and iv, an image viewer for the terminal that uses magic.
- The test module requires magic v0.5.0, as the decoder modules do.
- CI runs on the macOS runners that GitHub still has,
macos-15-intelandmacos-latest.
compress/xz and compress/zstd did not change, and stay at v0.5.0.
v0.5.0: a port of libmagic to pure Go
magic is now a port of libmagic to pure Go. It needs no cgo, no C compiler and no libmagic, and a program that imports it builds for every target of Go.
The port gives the output of libmagic 5.48, with the commits of file after the release, to bbffb78d. The rules of file are compiled into the package.
A new API
The API is new, so this is a new minor version (D39). Detect, DetectFile, DetectReader and DetectReaderAt take a context.Context and return a Result. A Result holds every form of the answer at once: Description, MIME, Charset, Extensions, Apple and Matches. Options such as WithAll, WithDecompress and WithLimit take the place of the flags of libmagic. The package has no handle to close.
The README has a table that maps each call of v0.4.1 to the port. A file that cannot be opened is now an error, where libmagic gave the text "cannot open".
Compressed data
WithDecompress decodes a format only when the program imports its decoder, as a program imports the driver of database/sql:
import (
_ "github.com/xo/magic/compress/bzip2"
_ "github.com/xo/magic/compress/xz"
_ "github.com/xo/magic/compress/zlib"
_ "github.com/xo/magic/compress/zstd"
)compress/xz and compress/zstd are modules of their own, tagged compress/xz/v0.5.0 and compress/zstd/v0.5.0. magic never runs a program.
Size and speed
A program that prints the MIME type of a file grows by about 980 KB, stripped, on linux/amd64. Most of it is the compiled rules.
The engine is about as fast as libmagic on most files, and slower on text. On linux/amd64, one walk for the MIME type took, with v0.4.1 and with the port:
| Input | v0.4.1 (cgo) | v0.5.0 |
|---|---|---|
| Word document | 5.1 µs | 4.3 µs |
| GIF image | 20 µs | 26 µs |
| XLSX file | 35 µs | 37 µs |
| random bytes | 252 µs | 334 µs |
| UTF-8 text | 334 µs | 1.09 ms |
Most of the time on text is in Go's regexp. docs/BACKLOG.md lists the work that can make it faster.
How it is tested
The test module builds libmagic from the source of file at bbffb78d and compares the port with it: 2,471 files and damaged copies of them, 14 combinations of flags, as bytes and as files. Every answer is the same, except 50 where a decoder of Go reads damaged compressed data in another way than the library of C. All 113 tests of file pass.
License
The port translates file, which is under its own BSD license, in LICENSE.file. A program that ships magic must reproduce the notices that the README lists (D29).
Removed
The cgo package, MagicDB, the static archives and the modules of the targets are gone. The tags of those modules, such as libmagic/linux_amd64/v5.48.2, stay published.
v0.4.1: fix a crash on Linux with glibc
This release fixes a crash on Linux. Upgrade if you build on Linux with gcc, which is the default.
Fixed: a crash on Linux with glibc
Every release up to v0.4.0 can crash with a segmentation fault on Linux when the program links glibc, which is what gcc does on most Linux systems. A script of 17 bytes that starts with #! /usr/bin/cmd1 is enough. Other files get wrong descriptions, such as the size of a Netpbm image.
The Linux archives were compiled against the headers of musl, where regmatch_t has 8-byte offsets, and glibc writes 4-byte ones. libmagic then read the positions of a regular expression match wrongly.
The Linux archives now carry their own regex engine, libgnurx, which the Windows archives already use, under renamed names. libmagic never calls the regex functions of the C library, so the output is the same with glibc and with musl.
The new harness of the pure Go port found the crash. It builds libmagic 5.48 from the source of file as an oracle, and compares the package with it on 1,544 inputs and 11,571 questions. They now agree when the program links glibc, and when it links musl.
Versions
- Root
v0.4.1. The API does not change. - Target modules:
v5.48.2for linux_amd64, linux_arm64 and linux_arm, which hold the new archives. The others do not change. - libmagic 5.48, zlib 1.4, bzip2 1.0.8, xz 5.8.4, zstd 1.5.7.
- libgnurx 2.5.1 is now in the Linux archives as well as the Windows ones. It is under LGPL 2.1.
Known limits
- A program for linux/arm (32-bit) cannot link the archives with glibc, because they call the names that musl gives to its 64-bit time functions. Every release has this. linux/arm64 needs glibc 2.38 or later for
strlcpy. - macOS has no CI, so the darwin archives are compiled and not run.