Libmagicxx v8.2.1
A C++ wrapper library over the Magic Number Recognition Library.
Loading...
Searching...
No Matches
magic.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2025 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
4#ifndef MAGIC_HPP
5#define MAGIC_HPP
6
7#include <algorithm>
8#include <bitset>
9#include <expected>
10#include <map>
11#include <memory>
12#include <string_view>
13#include <vector>
14
15#include "file_concepts.hpp"
16#include "magic_exception.hpp"
17
18namespace recognition {
19
29class magic {
30public:
34 using flags_mask_t = std::bitset<30uz>;
35
39 using file_type_t = std::string;
40
44 using error_message_t = std::string;
45
49 using expected_file_type_t = std::expected<file_type_t, error_message_t>;
50
54 using types_of_files_t = std::map<std::filesystem::path, file_type_t>;
55
59 using expected_types_of_files_t = std::map<
60 std::filesystem::path,
62
68 enum flags : unsigned long long {
69 /* clang-format off */
70 none = 0ULL,
71 debug = 1ULL << 0,
72 symlink = 1ULL << 1,
73 compress = 1ULL << 2,
74 devices = 1ULL << 3,
75 mime_type = 1ULL << 4,
76 continue_search = 1ULL << 5,
77 check_database = 1ULL << 6,
78 preserve_atime = 1ULL << 7,
79 raw = 1ULL << 8,
80 error = 1ULL << 9,
81 mime_encoding = 1ULL << 10,
82 mime = 1ULL << 11,
83 apple = 1ULL << 12,
84 extension = 1ULL << 13,
85 compress_transp = 1ULL << 14,
86 no_compress_fork = 1ULL << 15,
87 nodesc = 1ULL << 16,
88 no_check_compress = 1ULL << 17,
89 no_check_tar = 1ULL << 18,
90 no_check_soft = 1ULL << 19,
91 no_check_apptype = 1ULL << 20,
92 no_check_elf = 1ULL << 21,
93 no_check_text = 1ULL << 22,
94 no_check_cdf = 1ULL << 23,
95 no_check_csv = 1ULL << 24,
96 no_check_tokens = 1ULL << 25,
97 no_check_encoding = 1ULL << 26,
98 no_check_json = 1ULL << 27,
99 no_check_simh = 1ULL << 28,
100 no_check_builtin = 1ULL << 29
101 /* clang-format on */
102 };
103
108 enum class parameters : std::size_t {
109 /* clang-format off */
110 indir_max = 0uz,
111 name_max = 1uz,
112 elf_phnum_max = 2uz,
113 elf_shnum_max = 3uz,
114 elf_notes_max = 4uz,
115 regex_max = 5uz,
116 bytes_max = 6uz,
117 encoding_max = 7uz,
118 elf_shsize_max = 8uz,
119 mag_warn_max = 9uz
120 /* clang-format on */
121 };
122
126 using flags_container_t = std::vector<flags>;
127
131 using parameter_value_map_t = std::map<parameters, std::size_t>;
132
136 static std::string_view default_database_file;
137
141 magic() noexcept;
142
155 explicit magic(
156 flags_mask_t flags_mask,
157 const std::filesystem::path& database_file = default_database_file
158 );
159
172 explicit magic(
173 const flags_container_t& flags_container,
174 const std::filesystem::path& database_file = default_database_file
175 );
176
182 magic(magic&& other) noexcept;
183
187 magic(const magic&) = delete;
188
194 magic& operator=(magic&& other) noexcept;
195
199 magic& operator=(const magic&) = delete;
200
205
211 [[nodiscard]] operator bool() const noexcept;
212
221 bool check(
222 const std::filesystem::path& database_file = default_database_file
223 ) const noexcept;
224
230 void close() noexcept;
231
243 bool compile(
244 const std::filesystem::path& database_file = default_database_file
245 ) const noexcept;
246
254 [[nodiscard]] flags_container_t get_flags() const;
255
265 [[nodiscard]] std::size_t get_parameter(parameters parameter) const;
266
274 [[nodiscard]] parameter_value_map_t get_parameters() const;
275
281 [[nodiscard]] static std::string get_version() noexcept;
282
294 [[nodiscard]] file_type_t identify_file(const std::filesystem::path& path
295 ) const;
296
305 const std::filesystem::path& path,
306 std::nothrow_t
307 ) const noexcept;
308
322 const std::filesystem::path& directory,
323 std::filesystem::directory_options option = std::filesystem::
324 directory_options::follow_directory_symlink
325 ) const
326 {
327 return identify_files_impl(
328 std::filesystem::recursive_directory_iterator{directory, option}
329 );
330 }
331
341 const std::filesystem::path& directory,
342 std::nothrow_t,
343 std::filesystem::directory_options option = std::filesystem::
344 directory_options::follow_directory_symlink
345 ) const noexcept
346 {
347 return identify_files_impl(
348 std::filesystem::recursive_directory_iterator{directory, option},
349 std::nothrow
350 );
351 }
352
365 const file_concepts::file_container auto& files
366 ) const
367 {
368 return identify_files_impl(files);
369 }
370
379 const file_concepts::file_container auto& files,
380 std::nothrow_t
381 ) const noexcept
382 {
383 return identify_files_impl(files, std::nothrow);
384 }
385
391 [[nodiscard]] bool is_open() const noexcept;
392
406 const std::filesystem::path& database_file = default_database_file
407 );
408
418 void open(flags_mask_t flags_mask);
419
429 void open(const flags_container_t& flags_container);
430
439 void set_flags(flags_mask_t flags_mask);
440
449 void set_flags(const flags_container_t& flags_container);
450
460 void set_parameter(parameters parameter, std::size_t value);
461
471
472private:
473 class magic_private;
474 std::unique_ptr<magic_private> m_impl;
475
477 const std::ranges::range auto& files
478 ) const
479 {
480 types_of_files_t types_of_files;
481 std::ranges::for_each(files, [&](const std::filesystem::path& file) {
482 types_of_files[file] = identify_file(file);
483 });
484 return types_of_files;
485 }
486
488 const std::ranges::range auto& files,
489 std::nothrow_t
490 ) const noexcept
491 {
492 expected_types_of_files_t expected_types_of_files;
493 std::ranges::for_each(files, [&](const std::filesystem::path& file) {
494 expected_types_of_files[file] = identify_file(file, std::nothrow);
495 });
496 return expected_types_of_files;
497 }
498
499 friend std::string to_string(flags);
500 friend std::string to_string(parameters);
501};
502
512[[nodiscard]] std::string to_string(
513 const magic::types_of_files_t& types_of_files,
514 const std::string& type_separator = " -> ",
515 const std::string& file_separator = "\n"
516);
517
525[[nodiscard]] std::string to_string(
526 const magic::expected_file_type_t& expected_file_type
527);
528
538[[nodiscard]] std::string to_string(
539 const magic::expected_types_of_files_t& expected_types_of_files,
540 const std::string& type_separator = " -> ",
541 const std::string& file_separator = "\n"
542);
543
551[[nodiscard]] std::string to_string(magic::flags flag);
552
561[[nodiscard]] std::string to_string(
562 const magic::flags_container_t& flags,
563 const std::string& separator = ", "
564);
565
573[[nodiscard]] std::string to_string(magic::parameters parameter);
574
584[[nodiscard]] std::string to_string(
585 const magic::parameter_value_map_t& parameters,
586 const std::string& value_separator = ": ",
587 const std::string& parameter_separator = ", "
588);
589
590} /* namespace recognition */
591
592#endif /* MAGIC_HPP */
Definition magic.cpp:17
The magic class provides a C++ wrapper over the Magic Number Recognition Library.
Definition magic.hpp:29
bool is_open() const noexcept
Used for testing whether magic is open or closed.
Definition magic.cpp:609
void set_flags(flags_mask_t flags_mask)
Set the flags of magic.
Definition magic.cpp:629
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.
Definition magic.cpp:568
void set_parameters(const parameter_value_map_t &parameters)
Set the values of the parameters of magic.
Definition magic.cpp:644
std::size_t get_parameter(parameters parameter) const
Get the value of a parameter of magic.
Definition magic.cpp:578
std::unique_ptr< magic_private > m_impl
Definition magic.hpp:474
std::string error_message_t
The error_message_t typedef.
Definition magic.hpp:44
flags_container_t get_flags() const
Get the flags of magic.
Definition magic.cpp:573
flags
The flags enums are used for configuring the flags of a magic.
Definition magic.hpp:68
@ raw
Definition magic.hpp:79
@ no_check_tokens
Definition magic.hpp:96
@ no_check_cdf
Definition magic.hpp:94
@ symlink
Definition magic.hpp:72
@ no_check_apptype
Definition magic.hpp:91
@ error
Definition magic.hpp:80
@ no_check_compress
Definition magic.hpp:88
@ no_check_tar
Definition magic.hpp:89
@ mime_type
Definition magic.hpp:75
@ mime_encoding
Definition magic.hpp:81
@ debug
Definition magic.hpp:71
@ no_check_elf
Definition magic.hpp:92
@ none
Definition magic.hpp:70
@ nodesc
Definition magic.hpp:87
@ compress_transp
Definition magic.hpp:85
@ apple
Definition magic.hpp:83
@ mime
Definition magic.hpp:82
@ compress
Definition magic.hpp:73
@ no_check_text
Definition magic.hpp:93
@ no_check_soft
Definition magic.hpp:90
@ no_compress_fork
Definition magic.hpp:86
@ preserve_atime
Definition magic.hpp:78
@ no_check_builtin
Definition magic.hpp:100
@ extension
Definition magic.hpp:84
@ devices
Definition magic.hpp:74
@ no_check_encoding
Definition magic.hpp:97
@ no_check_simh
Definition magic.hpp:99
@ no_check_csv
Definition magic.hpp:95
@ continue_search
Definition magic.hpp:76
@ check_database
Definition magic.hpp:77
@ no_check_json
Definition magic.hpp:98
magic() noexcept
Construct magic without opening it.
Definition magic.cpp:523
void open(flags_mask_t flags_mask)
Open magic using the flags.
Definition magic.cpp:619
std::map< std::filesystem::path, expected_file_type_t > expected_types_of_files_t
The expected_types_of_files_t typedef.
Definition magic.hpp:59
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.
Definition magic.cpp:558
std::bitset< 30uz > flags_mask_t
The flags_mask_t typedef.
Definition magic.hpp:34
std::vector< flags > flags_container_t
The flags_container_t typedef.
Definition magic.hpp:126
file_type_t identify_file(const std::filesystem::path &path) const
Identify the type of a file.
Definition magic.cpp:594
parameters
The parameters enums are used for displaying or modifying the parameters of a magic.
Definition magic.hpp:108
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.
Definition magic.hpp:340
void set_parameter(parameters parameter, std::size_t value)
Set the value of a parameter of magic.
Definition magic.cpp:639
types_of_files_t identify_files(const file_concepts::file_container auto &files) const
Identify the types of files.
Definition magic.hpp:364
static std::string_view default_database_file
The path of the default database file.
Definition magic.hpp:136
std::map< parameters, std::size_t > parameter_value_map_t
The parameter_value_map_t typedef.
Definition magic.hpp:131
std::expected< file_type_t, error_message_t > expected_file_type_t
The expected_file_type_t typedef.
Definition magic.hpp:49
std::string file_type_t
The file_type_t typedef.
Definition magic.hpp:39
std::map< std::filesystem::path, file_type_t > types_of_files_t
The types_of_files_t typedef.
Definition magic.hpp:54
void close() noexcept
Close magic.
Definition magic.cpp:563
friend std::string to_string(flags)
Convert the magic::flags to string.
Definition magic.cpp:470
void load_database_file(const std::filesystem::path &database_file=default_database_file)
Load a magic database file.
Definition magic.cpp:614
parameter_value_map_t get_parameters() const
Get the values ​​of all parameters of magic.
Definition magic.cpp:584
expected_types_of_files_t identify_files_impl(const std::ranges::range auto &files, std::nothrow_t) const noexcept
Definition magic.hpp:487
types_of_files_t identify_files_impl(const std::ranges::range auto &files) const
Definition magic.hpp:476
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.
Definition magic.hpp:321
static std::string get_version() noexcept
Get the version of the Magic Number Recognition Library.
Definition magic.cpp:589
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.
Definition magic.hpp:378
The file_container concept specifies the requirements of a container which can be used as a file cont...
Definition file_concepts.hpp:18
Definition magic.hpp:18
std::string to_string(const magic::types_of_files_t &types_of_files, const std::string &type_separator=" -> ", const std::string &file_separator="\n")
Convert the magic::types_of_files_t to string.
Definition magic.cpp:423