Debian Codegraph @

src/openmpt/fileformat_base/magic.hpp

raw
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */
3
4
5 #pragma once
6
7 #include "openmpt/all/BuildSettings.hpp"
8
9 #include "mpt/base/macros.hpp"
10 #include "openmpt/base/Types.hpp"
11
12
13
14 OPENMPT_NAMESPACE_BEGIN
15
16
17
18 // Functions to create 4-byte and 2-byte magic byte identifiers in little-endian format
19 // Use this together with uint32le/uint16le file members.
20 MPT_CONSTEVAL uint32 MagicLE(const char (&id)[5])
21 {
22 return static_cast<uint32>((static_cast<uint8>(id[3]) << 24) | (static_cast<uint8>(id[2]) << 16) | (static_cast<uint8>(id[1]) << 8) | static_cast<uint8>(id[0]));
23 }
24 MPT_CONSTEVAL uint16 MagicLE(const char (&id)[3])
25 {
26 return static_cast<uint16>((static_cast<uint8>(id[1]) << 8) | static_cast<uint8>(id[0]));
27 }
28
29 // Functions to create 4-byte and 2-byte magic byte identifiers in big-endian format
30 // Use this together with uint32be/uint16be file members.
31 // Note: Historically, some magic bytes in MPT-specific fields are reversed (due to the use of multi-char literals).
32 // Such fields turned up reversed in files, so MagicBE is used to keep them readable in the code.
33 MPT_CONSTEVAL uint32 MagicBE(const char (&id)[5])
34 {
35 return static_cast<uint32>((static_cast<uint8>(id[0]) << 24) | (static_cast<uint8>(id[1]) << 16) | (static_cast<uint8>(id[2]) << 8) | static_cast<uint8>(id[3]));
36 }
37 MPT_CONSTEVAL uint16 MagicBE(const char (&id)[3])
38 {
39 return static_cast<uint16>((static_cast<uint8>(id[0]) << 8) | static_cast<uint8>(id[1]));
40 }
41
42
43
44 OPENMPT_NAMESPACE_END