Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
Core Magic API

Core file type identification functionality. More...

Collaboration diagram for Core Magic API:

Topics

 Constructors
 Magic construction methods.
 Special Member Functions
 Move, copy, and destruction operations.
 Validity Checking
 Instance and database validation methods.
 Instance Control
 Instance lifecycle management.
 Flag Management
 Flag retrieval methods.
 Parameter Management
 Parameter retrieval methods.
 Static Utilities
 Static utility methods.
 File Identification
 Core file type detection methods.
 State Queries
 Instance state inspection methods.
 Database Loading
 Magic database loading methods.
 Open Reopen
 Instance initialization methods.
 Flag Modification
 Flag modification methods.
 Parameter Modification
 Parameter modification methods.
 String Conversion Functions
 Free functions for converting Magic types to human-readable strings.
 Exception Classes
 Exception hierarchy for Magic class error handling.
 Implementation Details
 Internal implementation details for the Magic class.

Classes

class  Recognition::Magic
 A modern C++23 wrapper for libmagic — the library that powers the Unix file command. More...

Enumerations

enum  Recognition::Magic::Flags : unsigned long long {
  Recognition::Magic::None = 0ULL , Recognition::Magic::Debug = 1ULL << 0 , Recognition::Magic::Symlink = 1ULL << 1 , Recognition::Magic::Compress = 1ULL << 2 ,
  Recognition::Magic::Devices = 1ULL << 3 , Recognition::Magic::MimeType = 1ULL << 4 , Recognition::Magic::ContinueSearch = 1ULL << 5 , Recognition::Magic::CheckDatabase = 1ULL << 6 ,
  Recognition::Magic::PreserveAtime = 1ULL << 7 , Recognition::Magic::Raw = 1ULL << 8 , Recognition::Magic::Error = 1ULL << 9 , Recognition::Magic::MimeEncoding = 1ULL << 10 ,
  Recognition::Magic::Mime = 1ULL << 11 , Recognition::Magic::Apple = 1ULL << 12 , Recognition::Magic::Extension = 1ULL << 13 , Recognition::Magic::CompressTransp = 1ULL << 14 ,
  Recognition::Magic::NoCompressFork = 1ULL << 15 , Recognition::Magic::Nodesc = 1ULL << 16 , Recognition::Magic::NoCheckCompress = 1ULL << 17 , Recognition::Magic::NoCheckTar = 1ULL << 18 ,
  Recognition::Magic::NoCheckSoft = 1ULL << 19 , Recognition::Magic::NoCheckApptype = 1ULL << 20 , Recognition::Magic::NoCheckElf = 1ULL << 21 , Recognition::Magic::NoCheckText = 1ULL << 22 ,
  Recognition::Magic::NoCheckCdf = 1ULL << 23 , Recognition::Magic::NoCheckCsv = 1ULL << 24 , Recognition::Magic::NoCheckTokens = 1ULL << 25 , Recognition::Magic::NoCheckEncoding = 1ULL << 26 ,
  Recognition::Magic::NoCheckJson = 1ULL << 27 , Recognition::Magic::NoCheckSimh = 1ULL << 28 , Recognition::Magic::NoCheckBuiltin = 1ULL << 29
}
 Flags for configuring Magic behavior. More...
enum class  Recognition::Magic::Parameters : std::size_t {
  Recognition::Magic::Parameters::IndirMax = 0uz , Recognition::Magic::Parameters::NameMax = 1uz , Recognition::Magic::Parameters::ElfPhnumMax = 2uz , Recognition::Magic::Parameters::ElfShnumMax = 3uz ,
  Recognition::Magic::Parameters::ElfNotesMax = 4uz , Recognition::Magic::Parameters::RegexMax = 5uz , Recognition::Magic::Parameters::BytesMax = 6uz , Recognition::Magic::Parameters::EncodingMax = 7uz ,
  Recognition::Magic::Parameters::ElfShsizeMax = 8uz , Recognition::Magic::Parameters::MagWarnMax = 9uz
}
 Parameters for tuning Magic behavior limits. More...

Detailed Description

Core file type identification functionality.

The Magic class provides the primary interface for identifying file types using magic number analysis. It wraps the underlying libmagic C library with a modern, type-safe C++23 API.

Quick Start

#include <magic.hpp>
#include <print>
using namespace Recognition;
int main() {
// Create and configure Magic instance
// Identify a file
auto type = magic.IdentifyFile("/path/to/file");
std::println("File type: {}", type);
return 0;
}
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Definition magic.hpp:216
FileTypeT IdentifyFile(const std::filesystem::path &path) const
Identify the type of a single file.
Definition magic.cpp:2530
@ Mime
Definition magic.hpp:328
Main header file for the libmagicxx library.
auto main() -> int
Main entry point for the examples program.
Definition magic_examples.cpp:402
Root namespace for the libmagicxx library.

Exception Safety

Most methods have two overloads:

  • Throwing version: Throws exceptions on error (default)
  • Noexcept version: Returns std::optional or std::expected (pass std::nothrow)
// Throwing version
try {
auto type = magic.IdentifyFile(path);
} catch (const MagicException& e) {
// Handle error
}
// Noexcept version
auto result = magic.IdentifyFile(path, std::nothrow);
if (result) {
// Use *result
} else {
// Handle error via result.error()
}
Base exception class for all Magic-related errors.
Definition magic_exception.hpp:102

Enumeration Type Documentation

◆ Flags

enum Recognition::Magic::Flags : unsigned long long

Flags for configuring Magic behavior.

The Flags enum controls how Magic identifies files and formats output. Flags can be combined using bitwise OR operations.

Common Flag Combinations

// Get MIME type only
// Get full MIME with encoding
// Follow symlinks and decompress files
// Using a container of flags
Magic() noexcept
Default constructor. Creates an unopened Magic instance.
Definition magic.cpp:2421
@ Compress
Definition magic.hpp:319
@ Symlink
Definition magic.hpp:318
@ Debug
Definition magic.hpp:317
@ MimeType
Definition magic.hpp:321
See also
SetFlags() to change flags after construction
GetFlags() to retrieve current flags
Since
10.0.0
Enumerator
None 0ULL 

No special handling. Default textual output.

Debug 1ULL << 0 

Print debugging messages to stderr. Useful for troubleshooting.

Symlink 1ULL << 1 

If the file is a symlink, follow it and identify the target.

Compress 1ULL << 2 

If the file is compressed, decompress and identify contents.

Devices 1ULL << 3 

Open block/character devices and examine their contents.

MimeType 1ULL << 4 

Return MIME type (e.g., "text/plain") instead of description.

ContinueSearch 1ULL << 5 

Return all matches, not just the first one.

CheckDatabase 1ULL << 6 

Check database consistency and print warnings to stderr.

PreserveAtime 1ULL << 7 

Preserve access time of analyzed files (if supported by OS).

Raw 1ULL << 8 

Don't convert unprintable characters to \ooo octal.

Error 1ULL << 9 

Treat OS errors as real errors instead of printing in buffer.

MimeEncoding 1ULL << 10 

Return MIME encoding (e.g., "us-ascii") instead of description.

Mime 1ULL << 11 

Shorthand for MimeType | MimeEncoding. Returns full MIME.

Apple 1ULL << 12 

Return Apple creator and type codes.

Extension 1ULL << 13 

Return slash-separated list of file extensions.

CompressTransp 1ULL << 14 

Report on uncompressed data only, hide compression layer.

NoCompressFork 1ULL << 15 

Don't use decompressors that require fork().

Nodesc 1ULL << 16 

Shorthand for Extension | Mime | Apple.

NoCheckCompress 1ULL << 17 

Skip compressed file inspection.

NoCheckTar 1ULL << 18 

Skip tar archive examination.

NoCheckSoft 1ULL << 19 

Skip magic file consultation.

NoCheckApptype 1ULL << 20 

Skip EMX application type check (EMX only).

NoCheckElf 1ULL << 21 

Skip ELF details printing.

NoCheckText 1ULL << 22 

Skip text file type detection.

NoCheckCdf 1ULL << 23 

Skip MS Compound Document inspection.

NoCheckCsv 1ULL << 24 

Skip CSV file examination.

NoCheckTokens 1ULL << 25 

Skip known token search in ASCII files.

NoCheckEncoding 1ULL << 26 

Skip text encoding detection.

NoCheckJson 1ULL << 27 

Skip JSON file examination.

NoCheckSimh 1ULL << 28 

Skip SIMH tape file examination.

NoCheckBuiltin 1ULL << 29 

Use only magic file, skip all built-in tests.

◆ Parameters

enum class Recognition::Magic::Parameters : std::size_t
strong

Parameters for tuning Magic behavior limits.

The Parameters enum provides access to various internal limits that control how deeply Magic analyzes files. Adjusting these can help balance between thoroughness and performance.

// Limit bytes scanned for faster performance
// Get current value
// Get all parameters
auto params = magic.GetParameters();
for (const auto& [param, value] : params) {
std::println("{}: {}", ToString(param), value);
}
friend std::string ToString(Flags)
Friend declaration for ToString(Flags) free function.
Definition magic.cpp:2355
@ BytesMax
Definition magic.hpp:389
ParameterValueMapT GetParameters() const
Get all parameter values.
Definition magic.cpp:2513
std::size_t GetParameter(Parameters parameter) const
Get the value of a specific parameter.
Definition magic.cpp:2500
void SetParameter(Parameters parameter, std::size_t value)
Set a single parameter value.
Definition magic.cpp:2697
See also
SetParameter() to modify a single parameter
SetParameters() to modify multiple parameters
GetParameter() to retrieve a single parameter value
GetParameters() to retrieve all parameter values
Since
10.0.0
Enumerator
IndirMax 0uz 

Maximum recursion depth for indirect magic (default: 15).

NameMax 1uz 

Maximum use count for name/use magic entries (default: 30).

ElfPhnumMax 2uz 

Maximum ELF program headers to process (default: 128).

ElfShnumMax 3uz 

Maximum ELF section headers to process (default: 32768).

ElfNotesMax 4uz 

Maximum ELF notes to process (default: 256).

RegexMax 5uz 

Maximum regex search length in bytes (default: 8192).

BytesMax 6uz 

Maximum bytes to read from file (default: 7340032 = 7MB).

EncodingMax 7uz 

Maximum bytes to scan for encoding detection (default: 1048576 = 1MB).

ElfShsizeMax 8uz 

Maximum ELF section size to process (default: 134217728 = 128MB).

MagWarnMax 9uz 

Maximum warnings to tolerate from magic file (default: 64).