SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
magic_header.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <array>
13#include <bit>
14#include <span>
15#include <string>
16#include <type_traits>
17#include <vector>
18
22
23namespace seqan3::detail
24{
25
28struct gz_compression
29{
31 static inline std::vector<std::string> file_extensions{{"gz"}};
32
34 static constexpr std::array<char, 3> magic_header{'\x1f', '\x8b', '\x08'};
35};
36
39struct bz2_compression
40{
42 static inline std::vector<std::string> file_extensions{{"bz2"}};
43
45 static constexpr std::array<char, 3> magic_header{'\x42', '\x5a', '\x68'};
46};
47
50struct zstd_compression
51{
53 static inline std::vector<std::string> file_extensions{{"zst"}};
54
56 static constexpr std::array<char, 4> magic_header{'\x28', '\xb5', '\x2f', '\xfd'};
57};
58
61struct bgzf_compression
62{
64 static inline std::vector<std::string> file_extensions{{"bgzf"}};
65
67 static constexpr std::array<char, 18> magic_header{
68 // ID1 ID2 CM
69 gz_compression::magic_header[0],
70 gz_compression::magic_header[1],
71 gz_compression::magic_header[2],
72 // FLG [MTIME ] XFL OS [XLEN ]
73 '\x04',
74 '\x00',
75 '\x00',
76 '\x00',
77 '\x00',
78 '\x00',
79 '\xff',
80 '\x06',
81 '\x00',
82 // B C [SLEN ] [BSIZE ]
83 '\x42',
84 '\x43',
85 '\x02',
86 '\x00',
87 '\x00',
88 '\x00'};
89
94 template <typename char_t, size_t extend>
95 static bool validate_header(std::span<char_t, extend> header)
96 {
97 static_assert(std::equality_comparable_with<char_t, char>,
98 "The given char type of the span must be comparable with char.");
99
100 static constexpr auto id1_pos = std::endian::native == std::endian::little ? 10 : 11;
101
102 return (header[0] == magic_header[0] && // GZ_ID1
103 header[1] == magic_header[1] && // GZ_ID2
104 header[2] == magic_header[2] && // GZ_CM
105 (header[3] & magic_header[3]) != 0 && // FLG_FEXTRA
106 header[id1_pos] == magic_header[10] && // BGZF_ID1
107 header[12] == magic_header[12] && // BGZF_ID2
108 header[13] == magic_header[13] && // BGZF_SLEN
109 header[id1_pos + 4] == magic_header[14]); // BGZF_XLEN
110 }
111};
112
116using compression_formats = pack_traits::drop_front<void
117#if defined(SEQAN3_HAS_ZLIB)
118 ,
119 gz_compression,
120 bgzf_compression
121#endif // defined(SEQAN3_HAS_ZLIB)
122#if defined(SEQAN3_HAS_BZIP2)
123 ,
124 bz2_compression
125#endif // defined(SEQAN3_HAS_BZIP2)
126#if SEQAN3_HAS_ZSTD
127 ,
128 zstd_compression
129#endif // SEQAN3_HAS_ZSTD
130 >;
131
132} // namespace seqan3::detail
typename decltype(detail::drop_front< pack_t... >())::type drop_front
Return a seqan3::type_list of all the types in the type pack, except the first.
Definition type_pack/traits.hpp:305
Provides type traits for working with templates.
Provides various traits for template packs.
Provides concepts that do not have equivalents in C++20.
Hide me