Libmagicxx v5.1.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-2024 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 <map>
8#include <bitset>
9#include <vector>
10#include <memory>
11#include <expected>
12
13#include <file_concepts.hpp>
14#include <magic_exception.hpp>
15
16namespace recognition {
17
26class magic {
27public:
28
32 using flags_mask_t = std::bitset<30uz>;
33
37 using file_type_t = std::string;
38
42 using error_message_t = std::string;
43
47 using expected_file_type_t = std::expected<file_type_t, error_message_t>;
48
52 using types_of_files_t = std::map<std::filesystem::path, file_type_t>;
53
57 using expected_types_of_files_t = std::map<std::filesystem::path, expected_file_type_t>;
58
64 enum flags : unsigned long long {
65 none = 0ULL,
66 debug = 1ULL << 0,
67 symlink = 1ULL << 1,
68 compress = 1ULL << 2,
69 devices = 1ULL << 3,
70 mime_type = 1ULL << 4,
71 continue_search = 1ULL << 5,
72 check_database = 1ULL << 6,
73 preserve_atime = 1ULL << 7,
74 raw = 1ULL << 8,
75 error = 1ULL << 9,
76 mime_encoding = 1ULL << 10,
77 mime = 1ULL << 11,
78 apple = 1ULL << 12,
79 extension = 1ULL << 13,
80 compress_transp = 1ULL << 14,
81 no_compress_fork = 1ULL << 15,
82 nodesc = 1ULL << 16,
83 no_check_compress = 1ULL << 17,
84 no_check_tar = 1ULL << 18,
85 no_check_soft = 1ULL << 19,
86 no_check_apptype = 1ULL << 20,
87 no_check_elf = 1ULL << 21,
88 no_check_text = 1ULL << 22,
89 no_check_cdf = 1ULL << 23,
90 no_check_csv = 1ULL << 24,
91 no_check_tokens = 1ULL << 25,
92 no_check_encoding = 1ULL << 26,
93 no_check_json = 1ULL << 27,
94 no_check_simh = 1ULL << 28,
95 no_check_builtin = 1ULL << 29
96 };
97
102 enum class parameters : std::size_t {
103 indir_max = 0uz,
104 name_max = 1uz,
105 elf_phnum_max = 2uz,
106 elf_shnum_max = 3uz,
107 elf_notes_max = 4uz,
108 regex_max = 5uz,
109 bytes_max = 6uz,
110 encoding_max = 7uz,
111 elf_shsize_max = 8uz
112 };
113
117 using flags_container_t = std::vector<flags>;
118
122 using parameter_value_map_t = std::map<parameters, std::size_t>;
123
127 static constexpr auto default_database_file = "/usr/share/misc/magic";
128
132 magic() noexcept;
133
146 explicit magic(flags_mask_t flags_mask, const std::filesystem::path& database_file = default_database_file);
147
160 explicit magic(const flags_container_t& flags_container, const std::filesystem::path& database_file = default_database_file);
161
167 magic(magic&& other) noexcept;
168
172 magic(const magic&) = delete;
173
179 magic& operator=(magic&& other) noexcept;
180
184 magic& operator=(const magic&) = delete;
185
190
196 [[nodiscard]]
197 operator bool() const noexcept;
198
207 bool check(const std::filesystem::path& database_file = default_database_file) const noexcept;
208
214 void close() noexcept;
215
227 bool compile(const std::filesystem::path& database_file = default_database_file) const noexcept;
228
236 [[nodiscard]]
238
248 [[nodiscard]]
249 std::size_t get_parameter(parameters parameter) const;
250
258 [[nodiscard]]
260
266 [[nodiscard]]
267 static std::string get_version() noexcept;
268
280 [[nodiscard]]
281 file_type_t identify_file(const std::filesystem::path& path) const;
282
290 [[nodiscard]]
292 identify_file(const std::filesystem::path& path, std::nothrow_t) const noexcept;
293
306 [[nodiscard]]
308 const std::filesystem::path& directory,
309 std::filesystem::directory_options option = std::filesystem::directory_options::follow_directory_symlink
310 ) const
311 {
312 return identify_files_impl(
313 std::filesystem::recursive_directory_iterator{directory, option}
314 );
315 }
316
325 [[nodiscard]]
327 const std::filesystem::path& directory, std::nothrow_t,
328 std::filesystem::directory_options option = std::filesystem::directory_options::follow_directory_symlink
329 ) const noexcept
330 {
331 return identify_files_impl(
332 std::filesystem::recursive_directory_iterator{directory, option}, std::nothrow
333 );
334 }
335
347 [[nodiscard]]
349 {
350 return identify_files_impl(files);
351 }
352
360 [[nodiscard]]
362 const file_concepts::file_container auto& files, std::nothrow_t
363 ) const noexcept
364 {
365 return identify_files_impl(files, std::nothrow);
366 }
367
373 [[nodiscard]]
374 bool is_open() const noexcept;
375
388 void load_database_file(const std::filesystem::path& database_file = default_database_file);
389
399 void open(flags_mask_t flags_mask);
400
410 void open(const flags_container_t& flags_container);
411
420 void set_flags(flags_mask_t flags_mask);
421
430 void set_flags(const flags_container_t& flags_container);
431
441 void set_parameter(parameters parameter, std::size_t value);
442
452
453private:
454 class magic_private;
455 std::unique_ptr<magic_private> m_impl;
456
457 [[nodiscard]]
458 types_of_files_t identify_files_impl(const std::ranges::range auto& files) const
459 {
460 types_of_files_t types_of_files;
461 std::ranges::for_each(files,
462 [&](const std::filesystem::path& file){
463 types_of_files[file] = identify_file(file);
464 }
465 );
466 return types_of_files;
467 }
468
469 [[nodiscard]]
470 expected_types_of_files_t identify_files_impl(const std::ranges::range auto& files, std::nothrow_t) const noexcept
471 {
472 expected_types_of_files_t expected_types_of_files;
473 std::ranges::for_each(files,
474 [&](const std::filesystem::path& file){
475 expected_types_of_files[file] = identify_file(file, std::nothrow);
476 }
477 );
478 return expected_types_of_files;
479 }
480
481 friend std::string to_string(flags);
482 friend std::string to_string(parameters);
483};
484
494[[nodiscard]]
495std::string to_string(
496 const magic::types_of_files_t& types_of_files,
497 const std::string& type_separator = " -> ",
498 const std::string& file_separator = "\n"
499);
500
508[[nodiscard]]
509std::string to_string(const magic::expected_file_type_t& expected_file_type);
510
520[[nodiscard]]
521std::string to_string(
522 const magic::expected_types_of_files_t& expected_types_of_files,
523 const std::string& type_separator = " -> ",
524 const std::string& file_separator = "\n"
525);
526
534[[nodiscard]]
535std::string to_string(magic::flags flag);
536
545[[nodiscard]]
546std::string to_string(const magic::flags_container_t& flags, const std::string& separator = ", ");
547
555[[nodiscard]]
556std::string to_string(magic::parameters parameter);
557
567[[nodiscard]]
568std::string to_string(
569 const magic::parameter_value_map_t& parameters,
570 const std::string& value_separator = ": ",
571 const std::string& parameter_separator = ", "
572);
573
574} /* namespace recognition */
575
576#endif /* MAGIC_HPP */
Definition magic.cpp:17
The magic class provides a C++ wrapper over the Magic Number Recognition Library. The magic class is ...
Definition magic.hpp:26
bool is_open() const noexcept
Used for testing whether magic is open or closed.
Definition magic.cpp:537
void set_flags(flags_mask_t flags_mask)
Set the flags of magic.
Definition magic.cpp:557
static constexpr auto default_database_file
The path of the default database file.
Definition magic.hpp:127
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:494
void set_parameters(const parameter_value_map_t &parameters)
Set the values of the parameters of magic.
Definition magic.cpp:572
std::size_t get_parameter(parameters parameter) const
Get the value of a parameter of magic.
Definition magic.cpp:506
std::unique_ptr< magic_private > m_impl
Definition magic.hpp:455
std::string error_message_t
The error_message_t typedef.
Definition magic.hpp:42
flags_container_t get_flags() const
Get the flags of magic.
Definition magic.cpp:500
flags
The flags enums are used for configuring the flags of a magic.
Definition magic.hpp:64
@ raw
Definition magic.hpp:74
@ no_check_tokens
Definition magic.hpp:91
@ no_check_cdf
Definition magic.hpp:89
@ symlink
Definition magic.hpp:67
@ no_check_apptype
Definition magic.hpp:86
@ error
Definition magic.hpp:75
@ no_check_compress
Definition magic.hpp:83
@ no_check_tar
Definition magic.hpp:84
@ mime_type
Definition magic.hpp:70
@ mime_encoding
Definition magic.hpp:76
@ debug
Definition magic.hpp:66
@ no_check_elf
Definition magic.hpp:87
@ none
Definition magic.hpp:65
@ nodesc
Definition magic.hpp:82
@ compress_transp
Definition magic.hpp:80
@ apple
Definition magic.hpp:78
@ mime
Definition magic.hpp:77
@ compress
Definition magic.hpp:68
@ no_check_text
Definition magic.hpp:88
@ no_check_soft
Definition magic.hpp:85
@ no_compress_fork
Definition magic.hpp:81
@ preserve_atime
Definition magic.hpp:73
@ no_check_builtin
Definition magic.hpp:95
@ extension
Definition magic.hpp:79
@ devices
Definition magic.hpp:69
@ no_check_encoding
Definition magic.hpp:92
@ no_check_simh
Definition magic.hpp:94
@ no_check_csv
Definition magic.hpp:90
@ continue_search
Definition magic.hpp:71
@ check_database
Definition magic.hpp:72
@ no_check_json
Definition magic.hpp:93
magic() noexcept
Construct magic without opening it.
Definition magic.cpp:454
friend std::string to_string(flags)
Convert the magic::flags to string.
Definition magic.cpp:413
void open(flags_mask_t flags_mask)
Open magic using the flags.
Definition magic.cpp:547
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:484
std::bitset< 30uz > flags_mask_t
The flags_mask_t typedef.
Definition magic.hpp:32
std::vector< flags > flags_container_t
The flags_container_t typedef.
Definition magic.hpp:117
file_type_t identify_file(const std::filesystem::path &path) const
Identify the type of a file.
Definition magic.cpp:524
parameters
The parameters enums are used for displaying or modifying the parameters of a magic.
Definition magic.hpp:102
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:326
void set_parameter(parameters parameter, std::size_t value)
Set the value of a parameter of magic.
Definition magic.cpp:567
types_of_files_t identify_files(const file_concepts::file_container auto &files) const
Identify the types of files.
Definition magic.hpp:348
std::map< parameters, std::size_t > parameter_value_map_t
The parameter_value_map_t typedef.
Definition magic.hpp:122
std::expected< file_type_t, error_message_t > expected_file_type_t
The expected_file_type_t typedef.
Definition magic.hpp:47
std::string file_type_t
The file_type_t typedef.
Definition magic.hpp:37
std::map< std::filesystem::path, file_type_t > types_of_files_t
The types_of_files_t typedef.
Definition magic.hpp:52
void close() noexcept
Close magic.
Definition magic.cpp:489
void load_database_file(const std::filesystem::path &database_file=default_database_file)
Load a magic database file.
Definition magic.cpp:542
parameter_value_map_t get_parameters() const
Get the values ​​of all parameters of magic.
Definition magic.cpp:512
expected_types_of_files_t identify_files_impl(const std::ranges::range auto &files, std::nothrow_t) const noexcept
Definition magic.hpp:470
types_of_files_t identify_files_impl(const std::ranges::range auto &files) const
Definition magic.hpp:458
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:307
static std::string get_version() noexcept
Get the version of the Magic Number Recognition Library.
Definition magic.cpp:518
std::map< std::filesystem::path, expected_file_type_t > expected_types_of_files_t
The expected_types_of_files_t typedef.
Definition magic.hpp:57
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:361
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:16
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:382