Libmagicxx v9.0.2
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 <bitset>
8#include <expected>
9#include <map>
10#include <memory>
11#include <optional>
12#include <string_view>
13#include <vector>
14
15#include "magic_exception.hpp"
16#include "progress_tracker.hpp"
17#include "utility.hpp"
18
19namespace recognition {
20
31class magic {
32public:
38 using flags_mask_t = std::bitset<30uz>;
39
45 using file_type_t = std::string;
46
52 using error_message_t = std::string;
53
59 using expected_file_type_t = std::expected<file_type_t, error_message_t>;
60
66 using types_of_files_t = std::map<std::filesystem::path, file_type_t>;
67
73 using type_of_a_file_t = types_of_files_t::value_type;
74
80 using expected_types_of_files_t = std::map<
81 std::filesystem::path,
83
89 using expected_type_of_a_file_t = expected_types_of_files_t::value_type;
90
99
105 enum flags : unsigned long long {
106 /* clang-format off */
107 none = 0ULL,
108 debug = 1ULL << 0,
109 symlink = 1ULL << 1,
110 compress = 1ULL << 2,
111 devices = 1ULL << 3,
112 mime_type = 1ULL << 4,
113 continue_search = 1ULL << 5,
114 check_database = 1ULL << 6,
115 preserve_atime = 1ULL << 7,
116 raw = 1ULL << 8,
117 error = 1ULL << 9,
118 mime_encoding = 1ULL << 10,
119 mime = 1ULL << 11,
120 apple = 1ULL << 12,
121 extension = 1ULL << 13,
122 compress_transp = 1ULL << 14,
123 no_compress_fork = 1ULL << 15,
124 nodesc = 1ULL << 16,
125 no_check_compress = 1ULL << 17,
126 no_check_tar = 1ULL << 18,
127 no_check_soft = 1ULL << 19,
128 no_check_apptype = 1ULL << 20,
129 no_check_elf = 1ULL << 21,
130 no_check_text = 1ULL << 22,
131 no_check_cdf = 1ULL << 23,
132 no_check_csv = 1ULL << 24,
133 no_check_tokens = 1ULL << 25,
134 no_check_encoding = 1ULL << 26,
135 no_check_json = 1ULL << 27,
136 no_check_simh = 1ULL << 28,
137 no_check_builtin = 1ULL << 29
138 /* clang-format on */
139 };
140
145 enum class parameters : std::size_t {
146 /* clang-format off */
147 indir_max = 0uz,
148 name_max = 1uz,
149 elf_phnum_max = 2uz,
150 elf_shnum_max = 3uz,
151 elf_notes_max = 4uz,
152 regex_max = 5uz,
153 bytes_max = 6uz,
154 encoding_max = 7uz,
155 elf_shsize_max = 8uz,
156 mag_warn_max = 9uz
157 /* clang-format on */
158 };
159
165 using flags_container_t = std::vector<flags>;
166
172 using parameter_value_map_t = std::map<parameters, std::size_t>;
173
179 using parameter_value_t = parameter_value_map_t::value_type;
180
184 static std::string_view default_database_file;
185
192 magic() noexcept;
193
208 explicit magic(
209 flags_mask_t flags_mask,
210 const std::filesystem::path& database_file = default_database_file
211 );
212
222 magic(
223 flags_mask_t flags_mask,
224 const std::nothrow_t& tag,
225 const std::filesystem::path& database_file = default_database_file
226 ) noexcept;
227
242 explicit magic(
243 const flags_container_t& flags_container,
244 const std::filesystem::path& database_file = default_database_file
245 );
246
256 magic(
257 const flags_container_t& flags_container,
258 const std::nothrow_t& tag,
259 const std::filesystem::path& database_file = default_database_file
260 ) noexcept;
261
269 magic(magic&& other) noexcept;
270
274 magic(const magic&) = delete;
275
283 magic& operator=(magic&& other) noexcept;
284
288 magic& operator=(const magic&) = delete;
289
294
303 [[nodiscard]] operator bool() const noexcept;
304
313 [[nodiscard]] static bool check(
314 const std::filesystem::path& database_file = default_database_file
315 ) noexcept;
316
323 void close() noexcept;
324
336 [[nodiscard]] static bool compile(
337 const std::filesystem::path& database_file = default_database_file
338 ) noexcept;
339
347 [[nodiscard]] flags_container_t get_flags() const;
348
356 [[nodiscard]] std::optional<flags_container_t> get_flags(
357 const std::nothrow_t& tag
358 ) const noexcept;
359
369 [[nodiscard]] std::size_t get_parameter(parameters parameter) const;
370
379 [[nodiscard]] std::optional<std::size_t> get_parameter(
380 parameters parameter,
381 const std::nothrow_t& tag
382 ) const noexcept;
383
391 [[nodiscard]] parameter_value_map_t get_parameters() const;
392
400 [[nodiscard]] std::optional<parameter_value_map_t> get_parameters(
401 const std::nothrow_t& tag
402 ) const noexcept;
403
409 [[nodiscard]] static std::string get_version() noexcept;
410
424 [[nodiscard]] file_type_t identify_file(const std::filesystem::path& path
425 ) const;
426
436 const std::filesystem::path& path,
437 const std::nothrow_t& tag
438 ) const noexcept;
439
457 const std::filesystem::path& directory,
458 std::filesystem::directory_options option = std::filesystem::
459 directory_options::follow_directory_symlink
460 ) const
461 {
462 return identify_directory_impl(directory, option);
463 }
464
484 const std::filesystem::path& directory,
485 tracker_t tracker,
486 std::filesystem::directory_options option = std::filesystem::
487 directory_options::follow_directory_symlink
488 ) const
489 {
490 return identify_directory_impl(directory, option, tracker);
491 }
492
503 const std::filesystem::path& directory,
504 const std::nothrow_t& tag,
505 std::filesystem::directory_options option = std::filesystem::
506 directory_options::follow_directory_symlink
507 ) const noexcept
508 {
509 return identify_directory_impl(directory, tag, option);
510 }
511
523 const std::filesystem::path& directory,
524 const std::nothrow_t& tag,
525 tracker_t tracker,
526 std::filesystem::directory_options option = std::filesystem::
527 directory_options::follow_directory_symlink
528 ) const noexcept
529 {
530 return identify_directory_impl(directory, tag, option, tracker);
531 }
532
547 const utility::file_container auto& files
548 ) const
549 {
551 {std::ranges::begin(files), std::ranges::end(files)}
552 );
553 }
554
571 const utility::file_container auto& files,
572 tracker_t tracker
573 ) const
574 {
576 {std::ranges::begin(files), std::ranges::end(files)},
577 tracker
578 );
579 }
580
590 const utility::file_container auto& files,
591 const std::nothrow_t& tag
592 ) const noexcept
593 {
595 {std::ranges::begin(files), std::ranges::end(files)},
596 tag
597 );
598 }
599
610 const utility::file_container auto& files,
611 const std::nothrow_t& tag,
612 tracker_t tracker
613 ) const noexcept
614 {
616 {std::ranges::begin(files), std::ranges::end(files)},
617 tag,
618 tracker
619 );
620 }
621
627 [[nodiscard]] bool is_database_loaded() const noexcept;
628
634 [[nodiscard]] bool is_open() const noexcept;
635
644 [[nodiscard]] bool is_valid() const noexcept;
645
660 const std::filesystem::path& database_file = default_database_file
661 );
662
673 [[nodiscard]] bool load_database_file(
674 const std::nothrow_t& tag,
675 const std::filesystem::path& database_file = default_database_file
676 ) noexcept;
677
688 void open(flags_mask_t flags_mask);
689
701 [[nodiscard]] bool open(
702 flags_mask_t flags_mask,
703 const std::nothrow_t& tag
704 ) noexcept;
705
716 void open(const flags_container_t& flags_container);
717
729 [[nodiscard]] bool open(
730 const flags_container_t& flags_container,
731 const std::nothrow_t& tag
732 ) noexcept;
733
742 void set_flags(flags_mask_t flags_mask);
743
752 [[nodiscard]] bool set_flags(
753 flags_mask_t flags_mask,
754 const std::nothrow_t& tag
755 ) noexcept;
756
765 void set_flags(const flags_container_t& flags_container);
766
775 [[nodiscard]] bool set_flags(
776 const flags_container_t& flags_container,
777 const std::nothrow_t& tag
778 ) noexcept;
779
789 void set_parameter(parameters parameter, std::size_t value);
790
800 [[nodiscard]] bool set_parameter(
801 parameters parameter,
802 std::size_t value,
803 const std::nothrow_t& tag
804 ) noexcept;
805
815
824 [[nodiscard]] bool set_parameters(
826 const std::nothrow_t& tag
827 ) noexcept;
828
829private:
830 class magic_private;
831 std::unique_ptr<magic_private> m_impl;
832
833 using default_file_container_t = std::vector<std::filesystem::path>;
834
836 const std::filesystem::path& directory,
837 std::filesystem::directory_options option,
838 tracker_t tracker = utility::make_shared_progress_tracker()
839 ) const;
840
842 const std::filesystem::path& directory,
843 const std::nothrow_t& tag,
844 std::filesystem::directory_options option,
845 tracker_t tracker = utility::make_shared_progress_tracker()
846 ) const noexcept;
847
849 const default_file_container_t& files,
850 tracker_t tracker = utility::make_shared_progress_tracker()
851 ) const;
852
854 const default_file_container_t& files,
855 const std::nothrow_t& tag,
856 tracker_t tracker = utility::make_shared_progress_tracker()
857 ) const noexcept;
858
859 friend std::string to_string(flags);
860 friend std::string to_string(parameters);
861};
862
871[[nodiscard]] std::string to_string(
872 const magic::type_of_a_file_t& type_of_a_file,
873 const std::string& type_separator = " -> "
874);
875
885[[nodiscard]] std::string to_string(
886 const magic::types_of_files_t& types_of_files,
887 const std::string& type_separator = " -> ",
888 const std::string& file_separator = "\n"
889);
890
898[[nodiscard]] std::string to_string(
899 const magic::expected_file_type_t& expected_file_type
900);
901
910[[nodiscard]] std::string to_string(
911 const magic::expected_type_of_a_file_t& expected_type_of_a_file,
912 const std::string& type_separator = " -> "
913);
914
924[[nodiscard]] std::string to_string(
925 const magic::expected_types_of_files_t& expected_types_of_files,
926 const std::string& type_separator = " -> ",
927 const std::string& file_separator = "\n"
928);
929
937[[nodiscard]] std::string to_string(magic::flags flag);
938
947[[nodiscard]] std::string to_string(
949 const std::string& separator = ", "
950);
951
959[[nodiscard]] std::string to_string(magic::parameters parameter);
960
969[[nodiscard]] std::string to_string(
970 const magic::parameter_value_t& parameter_value,
971 const std::string& value_separator = ": "
972);
973
983[[nodiscard]] std::string to_string(
985 const std::string& value_separator = ": ",
986 const std::string& parameter_separator = ", "
987);
988
989} /* namespace recognition */
990
991#endif /* MAGIC_HPP */
Definition magic.cpp:17
The magic class provides a C++ wrapper over the Magic Number Recognition Library.
Definition magic.hpp:31
bool is_open() const noexcept
Used for testing whether magic is open or closed.
Definition magic.cpp:1138
void set_flags(flags_mask_t flags_mask)
Set the flags of magic.
Definition magic.cpp:1184
expected_types_of_files_t identify_files(const std::filesystem::path &directory, const std::nothrow_t &tag, tracker_t tracker, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const noexcept
Identify the types of all files in a directory with progress tracking, noexcept version.
Definition magic.hpp:522
void set_parameters(const parameter_value_map_t &parameters)
Set the values of the parameters of magic.
Definition magic.cpp:1224
std::size_t get_parameter(parameters parameter) const
Get the value of a parameter of magic.
Definition magic.cpp:1013
static bool check(const std::filesystem::path &database_file=default_database_file) noexcept
Check the validity of entries in the colon separated database files passed in as database_file.
Definition magic.cpp:982
std::unique_ptr< magic_private > m_impl
Definition magic.hpp:831
std::string error_message_t
String type representing an error message from file identification.
Definition magic.hpp:52
flags_container_t get_flags() const
Get the flags of magic.
Definition magic.cpp:1001
utility::shared_progress_tracker_t tracker_t
Alias for a shared pointer to a progress tracker used for monitoring file identification progress.
Definition magic.hpp:98
bool is_database_loaded() const noexcept
Used for testing whether a magic database is loaded or not.
Definition magic.cpp:1133
flags
The flags enums are used for configuring the flags of a magic.
Definition magic.hpp:105
@ raw
Definition magic.hpp:116
@ no_check_tokens
Definition magic.hpp:133
@ no_check_cdf
Definition magic.hpp:131
@ symlink
Definition magic.hpp:109
@ no_check_apptype
Definition magic.hpp:128
@ error
Definition magic.hpp:117
@ no_check_compress
Definition magic.hpp:125
@ no_check_tar
Definition magic.hpp:126
@ mime_type
Definition magic.hpp:112
@ mime_encoding
Definition magic.hpp:118
@ debug
Definition magic.hpp:108
@ no_check_elf
Definition magic.hpp:129
@ none
Definition magic.hpp:107
@ nodesc
Definition magic.hpp:124
@ compress_transp
Definition magic.hpp:122
@ apple
Definition magic.hpp:120
@ mime
Definition magic.hpp:119
@ compress
Definition magic.hpp:110
@ no_check_text
Definition magic.hpp:130
@ no_check_soft
Definition magic.hpp:127
@ no_compress_fork
Definition magic.hpp:123
@ preserve_atime
Definition magic.hpp:115
@ no_check_builtin
Definition magic.hpp:137
@ extension
Definition magic.hpp:121
@ devices
Definition magic.hpp:111
@ no_check_encoding
Definition magic.hpp:134
@ no_check_simh
Definition magic.hpp:136
@ no_check_csv
Definition magic.hpp:132
@ continue_search
Definition magic.hpp:113
@ check_database
Definition magic.hpp:114
@ no_check_json
Definition magic.hpp:135
magic() noexcept
Construct magic without opening it.
Definition magic.cpp:931
expected_types_of_files_t::value_type expected_type_of_a_file_t
Key-value pair representing a single file and its expected file type result.
Definition magic.hpp:89
void open(flags_mask_t flags_mask)
Open magic using the flags.
Definition magic.cpp:1161
std::map< std::filesystem::path, expected_file_type_t > expected_types_of_files_t
Map from file paths to expected file type results (success or error).
Definition magic.hpp:80
std::bitset< 30uz > flags_mask_t
Bitmask type representing a set of magic::flags used to configure the magic instance.
Definition magic.hpp:38
std::vector< flags > flags_container_t
Container type holding a collection of magic::flags.
Definition magic.hpp:165
std::vector< std::filesystem::path > default_file_container_t
Definition magic.hpp:833
file_type_t identify_file(const std::filesystem::path &path) const
Identify the type of a file.
Definition magic.cpp:1043
parameters
The parameters enums are used for displaying or modifying the parameters of a magic.
Definition magic.hpp:145
parameter_value_map_t::value_type parameter_value_t
Key-value pair representing a single parameter and its value.
Definition magic.hpp:179
expected_types_of_files_t identify_files(const utility::file_container auto &files, const std::nothrow_t &tag, tracker_t tracker) const noexcept
Identify the types of files with progress tracking, noexcept version.
Definition magic.hpp:609
types_of_files_t identify_files(const std::filesystem::path &directory, tracker_t tracker, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const
Identify the types of all files in a directory with progress tracking.
Definition magic.hpp:483
types_of_files_t::value_type type_of_a_file_t
Key-value pair representing a single file and its detected type.
Definition magic.hpp:73
void set_parameter(parameters parameter, std::size_t value)
Set the value of a parameter of magic.
Definition magic.cpp:1210
bool is_valid() const noexcept
Used for testing whether magic is valid for identifying file types or not.
Definition magic.cpp:1143
static std::string_view default_database_file
The path of the default database file.
Definition magic.hpp:184
std::map< parameters, std::size_t > parameter_value_map_t
Map from magic::parameters to their corresponding values.
Definition magic.hpp:172
std::expected< file_type_t, error_message_t > expected_file_type_t
Result type for file identification, containing either a file type or an error message.
Definition magic.hpp:59
types_of_files_t identify_directory_impl(const std::filesystem::path &directory, std::filesystem::directory_options option, tracker_t tracker=utility::make_shared_progress_tracker()) const
Definition magic.cpp:1063
std::string file_type_t
String type representing the detected type of a file.
Definition magic.hpp:45
expected_types_of_files_t identify_files(const utility::file_container auto &files, const std::nothrow_t &tag) const noexcept
Identify the types of files, noexcept version.
Definition magic.hpp:589
expected_types_of_files_t identify_files(const std::filesystem::path &directory, const std::nothrow_t &tag, 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:502
std::map< std::filesystem::path, file_type_t > types_of_files_t
Map from file paths to their detected types.
Definition magic.hpp:66
void close() noexcept
Close magic.
Definition magic.cpp:989
friend std::string to_string(flags)
Convert the magic::flags to string.
Definition magic.cpp:876
void load_database_file(const std::filesystem::path &database_file=default_database_file)
Load a magic database file.
Definition magic.cpp:1148
types_of_files_t identify_container_impl(const default_file_container_t &files, tracker_t tracker=utility::make_shared_progress_tracker()) const
Definition magic.cpp:1102
types_of_files_t identify_files(const utility::file_container auto &files, tracker_t tracker) const
Identify the types of files with progress tracking.
Definition magic.hpp:570
parameter_value_map_t get_parameters() const
Get the values ​​of all parameters of magic.
Definition magic.cpp:1026
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:456
static std::string get_version() noexcept
Get the version of the Magic Number Recognition Library.
Definition magic.cpp:1038
static bool compile(const std::filesystem::path &database_file=default_database_file) noexcept
Compile the colon separated list of database files passed in as database_file.
Definition magic.cpp:994
types_of_files_t identify_files(const utility::file_container auto &files) const
Identify the types of files.
Definition magic.hpp:546
The file_container concept specifies the requirements of a container which can be used as a file cont...
Definition utility.hpp:81
std::shared_ptr< progress_tracker > shared_progress_tracker_t
Alias for a shared pointer to progress_tracker.
Definition progress_tracker.hpp:181
Definition magic.hpp:19