Libmagicxx v9.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-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 {
30class magic {
31public:
37 using flags_mask_t = std::bitset<30uz>;
38
44 using file_type_t = std::string;
45
51 using error_message_t = std::string;
52
58 using expected_file_type_t = std::expected<file_type_t, error_message_t>;
59
65 using types_of_files_t = std::map<std::filesystem::path, file_type_t>;
66
72 using type_of_a_file_t = types_of_files_t::value_type;
73
79 using expected_types_of_files_t = std::map<
80 std::filesystem::path,
82 >;
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,
152 regex_max = 5uz,
153 bytes_max = 6uz,
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(
425 const std::filesystem::path& path
426 ) const;
427
437 const std::filesystem::path& path,
438 const std::nothrow_t& tag
439 ) const noexcept;
440
458 const std::filesystem::path& directory,
459 std::filesystem::directory_options option = std::filesystem::
460 directory_options::follow_directory_symlink
461 ) const
462 {
463 return identify_directory_impl(directory, option);
464 }
465
485 const std::filesystem::path& directory,
486 tracker_t tracker,
487 std::filesystem::directory_options option = std::filesystem::
488 directory_options::follow_directory_symlink
489 ) const
490 {
491 return identify_directory_impl(directory, option, tracker);
492 }
493
504 const std::filesystem::path& directory,
505 const std::nothrow_t& tag,
506 std::filesystem::directory_options option = std::filesystem::
507 directory_options::follow_directory_symlink
508 ) const noexcept
509 {
510 return identify_directory_impl(directory, tag, option);
511 }
512
524 const std::filesystem::path& directory,
525 const std::nothrow_t& tag,
526 tracker_t tracker,
527 std::filesystem::directory_options option = std::filesystem::
528 directory_options::follow_directory_symlink
529 ) const noexcept
530 {
531 return identify_directory_impl(directory, tag, option, tracker);
532 }
533
548 const utility::file_container auto& files
549 ) const
550 {
552 {std::ranges::begin(files), std::ranges::end(files)}
553 );
554 }
555
572 const utility::file_container auto& files,
573 tracker_t tracker
574 ) const
575 {
577 {std::ranges::begin(files), std::ranges::end(files)},
578 tracker
579 );
580 }
581
591 const utility::file_container auto& files,
592 const std::nothrow_t& tag
593 ) const noexcept
594 {
596 {std::ranges::begin(files), std::ranges::end(files)},
597 tag
598 );
599 }
600
611 const utility::file_container auto& files,
612 const std::nothrow_t& tag,
613 tracker_t tracker
614 ) const noexcept
615 {
617 {std::ranges::begin(files), std::ranges::end(files)},
618 tag,
619 tracker
620 );
621 }
622
628 [[nodiscard]] bool is_database_loaded() const noexcept;
629
635 [[nodiscard]] bool is_open() const noexcept;
636
645 [[nodiscard]] bool is_valid() const noexcept;
646
661 const std::filesystem::path& database_file = default_database_file
662 );
663
674 [[nodiscard]] bool load_database_file(
675 const std::nothrow_t& tag,
676 const std::filesystem::path& database_file = default_database_file
677 ) noexcept;
678
689 void open(flags_mask_t flags_mask);
690
702 [[nodiscard]] bool open(
703 flags_mask_t flags_mask,
704 const std::nothrow_t& tag
705 ) noexcept;
706
717 void open(const flags_container_t& flags_container);
718
730 [[nodiscard]] bool open(
731 const flags_container_t& flags_container,
732 const std::nothrow_t& tag
733 ) noexcept;
734
743 void set_flags(flags_mask_t flags_mask);
744
753 [[nodiscard]] bool set_flags(
754 flags_mask_t flags_mask,
755 const std::nothrow_t& tag
756 ) noexcept;
757
766 void set_flags(const flags_container_t& flags_container);
767
776 [[nodiscard]] bool set_flags(
777 const flags_container_t& flags_container,
778 const std::nothrow_t& tag
779 ) noexcept;
780
790 void set_parameter(parameters parameter, std::size_t value);
791
801 [[nodiscard]] bool set_parameter(
802 parameters parameter,
803 std::size_t value,
804 const std::nothrow_t& tag
805 ) noexcept;
806
816
825 [[nodiscard]] bool set_parameters(
827 const std::nothrow_t& tag
828 ) noexcept;
829
830private:
831 class magic_private;
832 std::unique_ptr<magic_private> m_impl;
833
834 using default_file_container_t = std::vector<std::filesystem::path>;
835
837 const std::filesystem::path& directory,
838 std::filesystem::directory_options option,
839 tracker_t tracker = utility::make_shared_progress_tracker()
840 ) const;
841
843 const std::filesystem::path& directory,
844 const std::nothrow_t& tag,
845 std::filesystem::directory_options option,
846 tracker_t tracker = utility::make_shared_progress_tracker()
847 ) const noexcept;
848
850 const default_file_container_t& files,
851 tracker_t tracker = utility::make_shared_progress_tracker()
852 ) const;
853
855 const default_file_container_t& files,
856 const std::nothrow_t& tag,
857 tracker_t tracker = utility::make_shared_progress_tracker()
858 ) const noexcept;
859
860 friend std::string to_string(flags);
861 friend std::string to_string(parameters);
862};
863
872[[nodiscard]] std::string to_string(
873 const magic::type_of_a_file_t& type_of_a_file,
874 const std::string& type_separator = " -> "
875);
876
886[[nodiscard]] std::string to_string(
887 const magic::types_of_files_t& types_of_files,
888 const std::string& type_separator = " -> ",
889 const std::string& file_separator = "\n"
890);
891
899[[nodiscard]] std::string to_string(
900 const magic::expected_file_type_t& expected_file_type
901);
902
911[[nodiscard]] std::string to_string(
912 const magic::expected_type_of_a_file_t& expected_type_of_a_file,
913 const std::string& type_separator = " -> "
914);
915
925[[nodiscard]] std::string to_string(
926 const magic::expected_types_of_files_t& expected_types_of_files,
927 const std::string& type_separator = " -> ",
928 const std::string& file_separator = "\n"
929);
930
938[[nodiscard]] std::string to_string(magic::flags flag);
939
948[[nodiscard]] std::string to_string(
950 const std::string& separator = ", "
951);
952
960[[nodiscard]] std::string to_string(magic::parameters parameter);
961
970[[nodiscard]] std::string to_string(
971 const magic::parameter_value_t& parameter_value,
972 const std::string& value_separator = ": "
973);
974
984[[nodiscard]] std::string to_string(
986 const std::string& value_separator = ": ",
987 const std::string& parameter_separator = ", "
988);
989} /* namespace recognition */
990
991#endif /* MAGIC_HPP */
Definition magic.cpp:16
bool is_open() const noexcept
Used for testing whether magic is open or closed.
Definition magic.cpp:1155
void set_flags(flags_mask_t flags_mask)
Set the flags of magic.
Definition magic.cpp:1201
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:523
void set_parameters(const parameter_value_map_t &parameters)
Set the values of the parameters of magic.
Definition magic.cpp:1241
std::size_t get_parameter(parameters parameter) const
Get the value of a parameter of magic.
Definition magic.cpp:1027
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:996
std::unique_ptr< magic_private > m_impl
Definition magic.hpp:832
std::string error_message_t
String type representing an error message from file identification.
Definition magic.hpp:51
flags_container_t get_flags() const
Get the flags of magic.
Definition magic.cpp:1015
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:1150
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:945
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:1178
std::bitset< 30uz > flags_mask_t
Bitmask type representing a set of magic::flags used to configure the magic instance.
Definition magic.hpp:37
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:834
file_type_t identify_file(const std::filesystem::path &path) const
Identify the type of a file.
Definition magic.cpp:1057
parameters
The parameters enums are used for displaying or modifying the parameters of a magic.
Definition magic.hpp:145
@ bytes_max
Definition magic.hpp:153
@ indir_max
Definition magic.hpp:147
@ elf_shsize_max
Definition magic.hpp:155
@ regex_max
Definition magic.hpp:152
@ elf_phnum_max
Definition magic.hpp:149
@ name_max
Definition magic.hpp:148
@ elf_shnum_max
Definition magic.hpp:150
@ mag_warn_max
Definition magic.hpp:156
@ elf_notes_max
Definition magic.hpp:151
@ encoding_max
Definition magic.hpp:154
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:610
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:484
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:72
void set_parameter(parameters parameter, std::size_t value)
Set the value of a parameter of magic.
Definition magic.cpp:1227
bool is_valid() const noexcept
Used for testing whether magic is valid for identifying file types or not.
Definition magic.cpp:1160
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:58
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:1077
std::string file_type_t
String type representing the detected type of a file.
Definition magic.hpp:44
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:590
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:503
std::map< std::filesystem::path, file_type_t > types_of_files_t
Map from file paths to their detected types.
Definition magic.hpp:65
void close() noexcept
Close magic.
Definition magic.cpp:1003
friend std::string to_string(flags)
Convert the magic::flags to string.
Definition magic.cpp:889
void load_database_file(const std::filesystem::path &database_file=default_database_file)
Load a magic database file.
Definition magic.cpp:1165
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:1119
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:571
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:79
parameter_value_map_t get_parameters() const
Get the values ​​of all parameters of magic.
Definition magic.cpp:1040
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:457
static std::string get_version() noexcept
Get the version of the Magic Number Recognition Library.
Definition magic.cpp:1052
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:1008
types_of_files_t identify_files(const utility::file_container auto &files) const
Identify the types of files.
Definition magic.hpp:547
The file_container concept specifies the requirements of a container which can be used as a file cont...
Definition utility.hpp:82
Definition percentage.hpp:12
std::shared_ptr< progress_tracker > shared_progress_tracker_t
Alias for a shared pointer to progress_tracker.
Definition progress_tracker.hpp:179
Definition magic.hpp:19