Libmagicxx v5.1.1
A C++ wrapper library over the Magic Number Recognition Library.
|
The magic class provides a C++ wrapper over the Magic Number Recognition Library. The magic class is used to identify the type of a file, if the following steps have been completed;. More...
#include <magic.hpp>
Classes | |
class | magic_private |
Public Types | |
enum | flags : unsigned long long { none = 0ULL , debug = 1ULL << 0 , symlink = 1ULL << 1 , compress = 1ULL << 2 , devices = 1ULL << 3 , mime_type = 1ULL << 4 , continue_search = 1ULL << 5 , check_database = 1ULL << 6 , preserve_atime = 1ULL << 7 , raw = 1ULL << 8 , error = 1ULL << 9 , mime_encoding = 1ULL << 10 , mime = 1ULL << 11 , apple = 1ULL << 12 , extension = 1ULL << 13 , compress_transp = 1ULL << 14 , no_compress_fork = 1ULL << 15 , nodesc = 1ULL << 16 , no_check_compress = 1ULL << 17 , no_check_tar = 1ULL << 18 , no_check_soft = 1ULL << 19 , no_check_apptype = 1ULL << 20 , no_check_elf = 1ULL << 21 , no_check_text = 1ULL << 22 , no_check_cdf = 1ULL << 23 , no_check_csv = 1ULL << 24 , no_check_tokens = 1ULL << 25 , no_check_encoding = 1ULL << 26 , no_check_json = 1ULL << 27 , no_check_simh = 1ULL << 28 , no_check_builtin = 1ULL << 29 } |
The flags enums are used for configuring the flags of a magic. More... | |
enum class | parameters : std::size_t { indir_max = 0uz , name_max = 1uz , elf_phnum_max = 2uz , elf_shnum_max = 3uz , elf_notes_max = 4uz , regex_max = 5uz , bytes_max = 6uz , encoding_max = 7uz , elf_shsize_max = 8uz } |
The parameters enums are used for displaying or modifying the parameters of a magic. More... | |
using | flags_mask_t = std::bitset<30uz> |
The flags_mask_t typedef. | |
using | file_type_t = std::string |
The file_type_t typedef. | |
using | error_message_t = std::string |
The error_message_t typedef. | |
using | expected_file_type_t = std::expected<file_type_t, error_message_t> |
The expected_file_type_t typedef. | |
using | types_of_files_t = std::map<std::filesystem::path, file_type_t> |
The types_of_files_t typedef. | |
using | expected_types_of_files_t = std::map<std::filesystem::path, expected_file_type_t> |
The expected_types_of_files_t typedef. | |
using | flags_container_t = std::vector<flags> |
The flags_container_t typedef. | |
using | parameter_value_map_t = std::map<parameters, std::size_t> |
The parameter_value_map_t typedef. | |
Public Member Functions | |
magic () noexcept | |
Construct magic without opening it. | |
magic (flags_mask_t flags_mask, const std::filesystem::path &database_file=default_database_file) | |
Construct magic, open it using the flags and load the magic database file. | |
magic (const flags_container_t &flags_container, const std::filesystem::path &database_file=default_database_file) | |
Construct magic, open it using the flags and load the magic database file. | |
magic (magic &&other) noexcept | |
Move construct magic. | |
magic (const magic &)=delete | |
Deleted copy constructor. | |
magic & | operator= (magic &&other) noexcept |
Move assign to this magic. | |
magic & | operator= (const magic &)=delete |
Deleted copy assignment. | |
~magic () | |
Destruct magic. | |
operator bool () const noexcept | |
Used for testing whether magic is open or closed. | |
bool | check (const std::filesystem::path &database_file=default_database_file) const noexcept |
check the validity of entries in the colon separated database files passed in as database_file. | |
void | close () noexcept |
Close magic. | |
bool | compile (const std::filesystem::path &database_file=default_database_file) const noexcept |
Compile the colon separated list of database files passed in as database_file. | |
flags_container_t | get_flags () const |
Get the flags of magic. | |
std::size_t | get_parameter (parameters parameter) const |
Get the value of a parameter of magic. | |
parameter_value_map_t | get_parameters () const |
Get the values of all parameters of magic. | |
file_type_t | identify_file (const std::filesystem::path &path) const |
Identify the type of a file. | |
expected_file_type_t | identify_file (const std::filesystem::path &path, std::nothrow_t) const noexcept |
Identify the type of a file, noexcept version. | |
types_of_files_t | identify_files (const std::filesystem::path &directory, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const |
Identify the types of all files in a directory. | |
expected_types_of_files_t | identify_files (const std::filesystem::path &directory, std::nothrow_t, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const noexcept |
Identify the types of all files in a directory, noexcept version. | |
types_of_files_t | identify_files (const file_concepts::file_container auto &files) const |
Identify the types of files. | |
expected_types_of_files_t | identify_files (const file_concepts::file_container auto &files, std::nothrow_t) const noexcept |
Identify the types of files, noexcept version. | |
bool | is_open () const noexcept |
Used for testing whether magic is open or closed. | |
void | load_database_file (const std::filesystem::path &database_file=default_database_file) |
Load a magic database file. | |
void | open (flags_mask_t flags_mask) |
Open magic using the flags. | |
void | open (const flags_container_t &flags_container) |
Open magic using the flags. | |
void | set_flags (flags_mask_t flags_mask) |
Set the flags of magic. | |
void | set_flags (const flags_container_t &flags_container) |
Set the flags of magic. | |
void | set_parameter (parameters parameter, std::size_t value) |
Set the value of a parameter of magic. | |
void | set_parameters (const parameter_value_map_t ¶meters) |
Set the values of the parameters of magic. | |
Static Public Member Functions | |
static std::string | get_version () noexcept |
Get the version of the Magic Number Recognition Library. | |
Static Public Attributes | |
static constexpr auto | default_database_file = "/usr/share/misc/magic" |
The path of the default database file. | |
Private Member Functions | |
types_of_files_t | identify_files_impl (const std::ranges::range auto &files) const |
expected_types_of_files_t | identify_files_impl (const std::ranges::range auto &files, std::nothrow_t) const noexcept |
Private Attributes | |
std::unique_ptr< magic_private > | m_impl |
Friends | |
std::string | to_string (flags) |
Convert the magic::flags to string. | |
std::string | to_string (parameters) |
Convert the magic::parameters to string. | |
The magic class provides a C++ wrapper over the Magic Number Recognition Library. The magic class is used to identify the type of a file, if the following steps have been completed;.
using recognition::magic::error_message_t = std::string |
The error_message_t typedef.
using recognition::magic::expected_file_type_t = std::expected<file_type_t, error_message_t> |
The expected_file_type_t typedef.
using recognition::magic::expected_types_of_files_t = std::map<std::filesystem::path, expected_file_type_t> |
The expected_types_of_files_t typedef.
using recognition::magic::file_type_t = std::string |
The file_type_t typedef.
using recognition::magic::flags_container_t = std::vector<flags> |
The flags_container_t typedef.
using recognition::magic::flags_mask_t = std::bitset<30uz> |
The flags_mask_t typedef.
using recognition::magic::parameter_value_map_t = std::map<parameters, std::size_t> |
The parameter_value_map_t typedef.
using recognition::magic::types_of_files_t = std::map<std::filesystem::path, file_type_t> |
The types_of_files_t typedef.
enum recognition::magic::flags : unsigned long long |
The flags enums are used for configuring the flags of a magic.
|
strong |
The parameters enums are used for displaying or modifying the parameters of a magic.
|
noexcept |
Construct magic without opening it.
|
explicit |
Construct magic, open it using the flags and load the magic database file.
[in] | flags_mask | One of the flags enums or bitwise or of the flags enums. |
[in] | database_file | The path of magic database file, default is /usr/share/misc/magic. |
magic_open_error | if opening magic fails. |
invalid_path | if the path of the magic database file is not a file. |
magic_load_error | if loading the magic database file fails. |
|
explicit |
Construct magic, open it using the flags and load the magic database file.
[in] | flags_container | Flags. |
[in] | database_file | The path of magic database file, default is /usr/share/misc/magic. |
magic_open_error | if opening magic fails. |
invalid_path | if the path of the magic database file is not a file. |
magic_load_error | if loading the magic database file fails. |
|
noexcept |
Move construct magic.
|
delete |
Deleted copy constructor.
|
default |
Destruct magic.
|
noexcept |
check the validity of entries in the colon separated database files passed in as database_file.
[in] | database_file | The file to check, default is /usr/share/misc/magic. |
|
noexcept |
Close magic.
|
noexcept |
Compile the colon separated list of database files passed in as database_file.
[in] | database_file | The file to compile, default is /usr/share/misc/magic. |
magic::flags_container_t recognition::magic::get_flags | ( | ) | const |
std::size_t recognition::magic::get_parameter | ( | magic::parameters | parameter | ) | const |
Get the value of a parameter of magic.
[in] | parameter | One of the parameters enum. |
magic_is_closed | if magic is closed. |
magic::parameter_value_map_t recognition::magic::get_parameters | ( | ) | const |
Get the values of all parameters of magic.
magic_is_closed | if magic is closed. |
|
staticnoexcept |
Get the version of the Magic Number Recognition Library.
magic::file_type_t recognition::magic::identify_file | ( | const std::filesystem::path & | path | ) | const |
Identify the type of a file.
[in] | path | The path of the file. |
magic_is_closed | if magic is closed. |
empty_path | if the path of the file is empty. |
magic_file_error | if identifying the type of the file fails. |
|
noexcept |
Identify the type of a file, noexcept version.
[in] | path | The path of the file. |
|
inline |
Identify the types of files.
[in] | files | The container that holds the paths of the files. |
magic_is_closed | if magic is closed. |
empty_path | if the path of the file is empty. |
magic_file_error | if identifying the type of the file fails. |
|
inlinenoexcept |
Identify the types of files, noexcept version.
[in] | files | The container that holds the paths of the files. |
|
inline |
Identify the types of all files in a directory.
[in] | directory | The path of the directory. |
[in] | option | The directory iteration option, default is follow_directory_symlink. |
magic_is_closed | if magic is closed. |
empty_path | if the path of the file is empty. |
magic_file_error | if identifying the type of the file fails. |
|
inlinenoexcept |
Identify the types of all files in a directory, noexcept version.
[in] | directory | The path of the directory. |
[in] | option | The directory iteration option, default is follow_directory_symlink. |
|
inlineprivate |
|
inlineprivatenoexcept |
|
noexcept |
Used for testing whether magic is open or closed.
void recognition::magic::load_database_file | ( | const std::filesystem::path & | database_file = default_database_file | ) |
Load a magic database file.
[in] | database_file | The path of the magic database file, default is /usr/share/misc/magic. |
magic_is_closed | if magic is closed. |
empty_path | if the path of the database file is empty. |
invalid_path | if the path of the database file is not a file. |
magic_load_error | if loading the database file fails. |
void recognition::magic::open | ( | const flags_container_t & | flags_container | ) |
Open magic using the flags.
[in] | flags_container | Flags. |
magic_open_error | if opening magic fails. |
void recognition::magic::open | ( | flags_mask_t | flags_mask | ) |
Open magic using the flags.
[in] | flags_mask | One of the flags enums or bitwise or of the flags enums. |
magic_open_error | if opening magic fails. |
|
noexcept |
Used for testing whether magic is open or closed.
Move assign to this magic.
void recognition::magic::set_flags | ( | const flags_container_t & | flags_container | ) |
Set the flags of magic.
[in] | flags_container | Flags. |
magic_is_closed | if magic is closed. |
magic_set_flags_error | if setting the flags of magic fails. |
void recognition::magic::set_flags | ( | flags_mask_t | flags_mask | ) |
Set the flags of magic.
[in] | flags_mask | One of the flags enums or bitwise or of the flags enums. |
magic_is_closed | if magic is closed. |
magic_set_flags_error | if setting the flags of magic fails. |
void recognition::magic::set_parameter | ( | magic::parameters | parameter, |
std::size_t | value ) |
Set the value of a parameter of magic.
[in] | parameter | One of the parameters enum. |
[in] | value | The value of the parameter. |
magic_is_closed | if magic is closed. |
magic_set_param_error | if setting the parameter of magic fails. |
void recognition::magic::set_parameters | ( | const parameter_value_map_t & | parameters | ) |
Set the values of the parameters of magic.
[in] | parameters | Parameters with corresponding values. |
magic_is_closed | if magic is closed. |
magic_set_param_error | if setting the parameter of magic fails. |
|
friend |
|
friend |
Convert the magic::parameters to string.
[in] | parameter | The parameter. |
|
staticconstexpr |
The path of the default database file.
|
private |